0

I have made an Ontologu in Protege and imported in Eclipse.My ontology already 10 instances and i want to add more instances.The following piece of code adds instances to existing class (Noun) of ontology. After excecution it do not update ontology model and shows same number of instances.

  public static void main(String[] args) throws OWLException, IOException{
  OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

  File file = new File("D:\\word.owl");{  
      OntModel model=ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
       System.out.println("Model is called successfully");
    OWLOntology ont = manager.loadOntologyFromOntologyDocument(file);
    System.out.println("Loaded ontology: " + ont);

   String SOURCE = ("D:\\word.owl");
   String NS = SOURCE;
   OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
   base.read( SOURCE, "" );

OntModel inf =ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF, base );

 OntClass Noun = base.getOntClass( NS + "Noun" );
 Individual jack = base.createIndividual( NS + "Jack", Noun );
 Individual Helley = base.createIndividual( NS + "Helley", Noun );  
  manager.saveOntology(ont);

 System.out.println("Number of individuals: " + ont.getIndividualsInSignature().size());

    }
  }
 }

Output

  Model is called successfully
  Loaded ontology: 
 Number of individuals: 10
ALee
  • 53
  • 1
  • 8
  • You're mixing owlapi and Jena classes, using owlapi to save but making all changes to Jena models. – Ignazio May 13 '17 at 07:14
  • @Ignazio I am confused between.Would you like to fix my issue..please – ALee May 13 '17 at 09:21
  • Remove OWLAPI classes from your code and use Jena to save the file instead: `FileWriter out = new FileWriter( "D:\\word.owl" ); base.write( out, "Turtle" ); out.close();` – Ignazio May 13 '17 at 10:32
  • Incidentally, your code is not adding any individuals, it is only creating individual objects, but there are no statements added to either the OWLAPI or the Jena model. This question has examples on how to add data to a Jena model http://stackoverflow.com/questions/17442969/how-to-create-owl-file-using-jena – Ignazio May 13 '17 at 10:35
  • @Ignazio Thank you for your answer. Below is my updated code but gives NullPointerException..Please see it what am i doing wrong – ALee May 14 '17 at 11:53

3 Answers3

1

I dont use that API but I can see your issue.

At the start of your code you create an OWLOntology object:

OWLOntology ont = manager.loadOntologyFromOntologyDocument(file);

And here is the issue, you do not alter ont anywhere in your code, so when you call the line below, it will only show/save the same 10 individuals that you loaded from file at the beginning of your code:

manager.saveOntology(ont);

System.out.println("Number of individuals: " + ont.getIndividualsInSignature().size());

So to fix this you need to somehow modify ont to include the new Individual before using the above lines.

sorifiend
  • 5,927
  • 1
  • 28
  • 45
0

Updated Code It gives Exception in thread "main" java.lang.NullPointerException at Mgt.main(Mgt.java:29)

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
public class Mgt {
static OntModel jenaModel = null;
public static void main(String[] args) throws Exception{
    String file =("D:\\word.owl");
     jenaModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);

            InputStream in = FileManager.get().open(file);
            try 
            {
                jenaModel.read(in, null);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            System.out.println("Ontology " + file + " loaded.");
            OntClass Noun = jenaModel.getOntClass( "http://www.semanticweb.org/Word#Noun" );
            Individual Organization = Noun.createIndividual(file + "Organization");
            FileWriter out = null;
            try {
              // XML format - long and verbose
              out = new FileWriter( file);
              jenaModel.write( out, "RDF/XML" );

            }
            finally {
              if (out != null) {
                try {out.close();
                } catch (IOException ignore) {}
              }
            }
          }
         }
ALee
  • 53
  • 1
  • 8
  • 2
    Don't add answers to add information to the question, edit your question instead. – Ignazio May 14 '17 at 15:37
  • We can't run this code, so you have to tell us which line throws the null pointer exception. Line 29 without knowing the imports does not help. – Ignazio May 14 '17 at 15:38
  • @Ignazio i have updated the code .Please have a look – ALee May 15 '17 at 01:34
  • @Ignazio When i use ' Individual Organization = jenaModel.createIndividual(file+"Organization",Noun);" ' then it do not gives Nullpointer error but do not add anything. – ALee May 15 '17 at 03:49
  • Your code does not have that line. However it has `Individual Organization = Noun.createIndividual(file + "Organization");` - this will return an individual named `D:\\word.owlNoun`, if one exists - but that's likely not the IRI of your actual individual. Ensure you get the actual IRI. – Ignazio May 15 '17 at 06:10
  • @Ignazio How will i get actual IRI? Above is my Ontology class hierarchy.I stucked here for a week. Your guidence will be higly appreciated sir. – ALee May 15 '17 at 06:36
  • Open the file in a text editor, you'll see the full IRI as a comment above the individual declaration. – Ignazio May 15 '17 at 07:29
  • To add data to a model see this question http://stackoverflow.com/questions/3975425/how-can-i-add-some-triple-to-my-ontology-by-jena – Ignazio May 15 '17 at 07:39
  • @Ignazio I have attached the code with exact exception:line 29. Please have a look.It looks like my code can not access the "Noun" class.First it should access the Noun class then add the "Organization" instance – ALee May 15 '17 at 07:57
  • As I said earlier, you're not using the right string to find the Noun class. This means your Noun variable is null. – Ignazio May 15 '17 at 09:47
  • @Ignazio That is issue..i did not find the correct way. I am stuck here – ALee May 15 '17 at 10:11
  • You need to share the owl file, or I cannot help you to find the right string – Ignazio May 15 '17 at 10:12
  • @Ignazio i have attached my owl file above..Pls have a look – ALee May 15 '17 at 10:37
  • The IRI is http://www.semanticweb.org/nazakatali/ontologies/Word#Noun. Your code should be: `OntClass Noun = jenaModel.getOntClass("http://www.semanticweb.org/nazakatali/ontologies/Word#Noun");` – Ignazio May 15 '17 at 18:24
  • @Ignazio Thank you. It works but it does not create correct IRI for newly added instances. for example the IRI of one of my instance is "http://www.semanticweb.org/nazakatali/ontologies/Word#Software" but the newly added instance "organization " has IRI like "D:\word.owlOrganization". – ALee May 16 '17 at 03:26
  • 1
    Please be aware of the code you've written - you're creating individuals as `Individual Organization = Noun.createIndividual(file + "Organization");`, where `file` is "D:\\word.owl". If that's not what you want, you should change it. The OntClass does not contribute namespaces to individuals. – Ignazio May 16 '17 at 06:57
  • @Ignazio Thank you sir.when I add new individual from protégé and save the file ,it appears like " " But when I add using above above code then it appears like this" " . Both are much different. I want same IRI's as when I save from protégé.I use RDF/XML format to save the file. – ALee May 16 '17 at 08:41
  • 1
    You need to change the value of the string variable named 'file' - or use the string you just pasted here. – Ignazio May 16 '17 at 10:18
  • @Ignazio Thank you sir for your kind help. But the only problem is, when I write the model "out = new FileWriter("D:\\word.owl"); then after after that the word.owl becomes empty instead of updating the word.owl file – ALee May 16 '17 at 12:36
0

This works:

import java.io.FileWriter;
import java.io.InputStream;

import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
import org.apache.jena.vocabulary.RDF;

public class Mgt {

    public static void main(String[] args) throws Exception {
        String namespace = "http://www.semanticweb.org/Word#";
        String file = "word.owl";
        OntModel jenaModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);

        InputStream in = FileManager.get().open(file);
        jenaModel.read(in, null);
        OntClass Noun = jenaModel.getOntClass(namespace + "Noun");
        Individual Organization = Noun.createIndividual(namespace + "Organization");
        jenaModel.add(Organization, RDF.type, Noun);
        FileWriter out = new FileWriter("word.out.owl");
        jenaModel.getWriter("RDF/XML-ABBREV").write(jenaModel, out, namespace);
        out.close();
    }
}

Note that namespace is not related to the file name.

ALee
  • 53
  • 1
  • 8
Ignazio
  • 10,504
  • 1
  • 14
  • 25