Hi Ian,
 From your answer above it looks like Jena does allow to create
individuals, delete/add classes, statements and properties to the loaded
owl ontology model
directly from the java file. I guess the question is how?  I would like to
focus our exchange back to a real case to illustrate part of my problem
using the excerpt below. I've loaded the wine ontology (.owl) in an empty
 model and would like to write code to enrich/modify that ontology (see the
various methods listed). Do you or anyone have example code to help fill in
the gaps?

TestWine.java

package demo;


import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Iterator;

import java.util.List;


import com.hp.hpl.jena.rdf.model.InfModel;

import com.hp.hpl.jena.rdf.model.Literal;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.rdf.model.ModelFactory;

import com.hp.hpl.jena.rdf.model.Property;

import com.hp.hpl.jena.rdf.model.RDFNode;

import com.hp.hpl.jena.rdf.model.ResIterator;

import com.hp.hpl.jena.rdf.model.Resource;

import com.hp.hpl.jena.rdf.model.Statement;

import com.hp.hpl.jena.rdf.model.StmtIterator;

import com.hp.hpl.jena.reasoner.Reasoner;

import com.hp.hpl.jena.reasoner.ReasonerRegistry;

import com.hp.hpl.jena.reasoner.ValidityReport;

import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;

import com.hp.hpl.jena.reasoner.rulesys.Rule;

import com.hp.hpl.jena.shared.DoesNotExistException;

import com.hp.hpl.jena.util.FileManager;

import com.hp.hpl.jena.util.PrintUtil;

import com.hp.hpl.jena.vocabulary.RDF;

import com.hp.hpl.jena.vocabulary.VCARD;


// Create an instance of the OWL reasoner, specialized to the demo schema

// and then apply that to the demo data to obtain an inference model.


public class TestWineOntologyOWL {

   private Model model;

   String ontURL = "src/demo/ont/wine.owl";



   // constructor

   public TestWineOntologyOWL() {

      model = FileManager.get().loadModel( ontURL );

   }



   // ============================================================

   // Create and add individuals ...

   // ============================================================

   public void addIndividuals() {



   //????

   }


   // ============================================================

   // Create and add properties ...

   // ============================================================

   public void addProperties() {



   //????

   }



   // ============================================================

   // Create and add Resources ...

   // ============================================================

   public void addResource() {



   //????

   }



   // ============================================================

   // Create and add Statements ...

   // ============================================================

   public void addStatements() {



   //????

   }





   // ============================================================

   // Remove or replace individuals, property, resource, Statements,
etc.....

   // ============================================================

   public void remove() {



   //????

   }





   // ============================================================

   // Retrieve and print the RDF statements ....

   // ============================================================


   public void printStatements( String sTitle ) {

      int statementNo = 1;


      System.out.printf("List of statements: %s\n", sTitle );

      System.out.printf(
"=============================================================\n");



      StmtIterator iter = model.listStatements();


      while (iter.hasNext()) {

         Statement stmt      = iter.next();         // get next statement

         Resource  subject   = stmt.getSubject();   // get the subject

         Property  predicate = stmt.getPredicate(); // get the predicate

         RDFNode   object    = stmt.getObject();    // get the object


         // Objects can be either Resources or Literals ....


         System.out.printf("  Statement[%4d]\n", statementNo );

         System.out.printf("  Subject  : %s \n", subject.toString());

         System.out.printf("  Predicate: %s \n", predicate.toString());


         if (object instanceof Resource) {

            System.out.printf("  Object   : %s \n", object.toString());

         }


         if (object instanceof Literal) {

            System.out.printf("  Object   : \"%s\" \n", object.toString());

         }


         statementNo = statementNo + 1;

      }


      System.out.println(
"=============================================================");

  }







   public static void main(String[] args) {


      System.out.println("Step 1: Load the Wine Ontology ....
  ");

      System.out.println(
"===================================================");


      TestWineOntologyOWL wine = new TestWineOntologyOWL();


      System.out.printf("Step 2: Print size of the Wine Ontology ...\n");

      System.out.println(
"===================================================");

      System.out.println("");


      System.out.println("Wine ontology model size   = " + wine.model
.size());


      System.out.println("");

      System.out.printf("Step 3: Print details of the Wine Ontology ...\n");

      System.out.println("===============================================");

      System.out.println("");


      wine.model.write(System.out, "RDF/XML" );


      System.out.println("");

System.out.println("");

      System.out.println("===============================================");





      System.out.printf("Step 4: extend/modify the Wine Ontology ...\n");

      System.out.println("===============================================");

      System.out.println("");


      wine.addIndividuals( );

      wine.addProperties();

      wine.addResources();


      System.out.println("");

      System.out.println("===============================================");







      System.out.printf("Step 5: Print statements in the Wine Ontology
...\n");

      System.out.println("===============================================");

      System.out.println("");


      wine.printStatements( "Wine Ontology" );


      System.out.println("");



      System.out.println(
"===============================================");

}



Thank,

Leonard







2013/4/24 Ian Dickinson <[email protected]>

> On 24/04/13 04:24, Léonard PETNGA wrote:
>
>> Hi Ian!
>> Thank you for your prompt response to my email. I didn't mention it but,
>> as
>> far as my objectives are concerned, my 2 questions are not related. These
>> are some clarifications to my initial post.
>> Q1: I just wanted to  know whether it's possible to create the the owl
>> file
>>   from the java version of the ontology since I was programatically
>>   creating it in jAVA.  Thank you for letting me know about Jena's
>> schemagen
>> tool. I'll keep that in mind.
>>
> OK. To clarify: schemagen takes an OWL file and creates a corresponding
> Java file with constants that correspond to the classes, properties and
> individuals in the ontology. It does not go from Java to OWL, but vice
> versa.
>
>
>  Q2: I have  decided to encode the ontologies in Java because I wasn't able
>> to programatically access and modify the owl file  after being loaded.
>>
> Um, I'm not sure I understand this. Programatically accessing and modify
> RDF and OWL is what Jena does.
>
>
>  Specifically, i wanted to be able to create individuals, delete/add
>> classes, statements and properties to the loaded owl ontology model
>> directly from the java file.
>>
> Yep, Jena does that.
>
>
>  Jena and Eclipse were unable to recognize
>> ontology's entities and wanted them to be declared as local variable,
>>
> Having Java constants that correspond to OWL or RDFS resources is exactly
> the problem that schemagen solves.
>
>
>  thus
>> the duplicate elements i mentioned previously.
>>
> But now I'm lost.
>
>
>  Being able to merge those
>> ontologies from the Java encoding directly would also save me a lot of
>> time
>> and energy as I've already put a lot of effort in coding the ontology in
>> java. So there is no way to merge my java encoding ontologies as I
>> mentioned in my initial post?
>>
> Sorry, but you're doing something that no-one else that I know of is
> doing. You're on your own, I'm afraid! I suppose you *could* download a
> Java parser, parse your .java files and generate a new file with the merged
> definitions in. Personally I wouldn't go there, but it's your call.
>
>
>  Note: I have developed several built-ins that are being used by my
>> reasoner
>> for inferencing applied to the base ontology created in java. The java
>> encoding allows me to test those built-ins separately before integrating
>> them in with rule engine something I can't do if the original ontologies
>> are in owl (I mean I don't know how to do it - any idea will be welcome
>> though).
>>
> I don't understand enough of what you're trying to do to offer any useful
> comments. If you wanted to create a *minimal* complete example of what
> you're trying to do, we could maybe offer more advice.
>
> Ian
>
>

Reply via email to