title: How to update an OWL ontology file in order to add a new individual with some properties?
body: <p>I have an ontology created in protege 4.3.0 and stored in an OWL file. I need to add some individuals to this ontology and update this file. Since these individuals are very similar to each other, I would like to take advantage of OWL API in order to add them faster than manual use of the program.</p>
  
  <p>My aim is as follows:</p>
  
  <ul>
  <li>add a new individual <code>#individualSimpleSubType</code></li>
  <li>add the type <code>#SimpleSubType</code> to this individual (<code>#SimpleSubType</code> is a subclass of <code>#SimpleType</code>)</li>
  <li>add the following object property assertion to this individual: <code>#hasProperty1</code> associated to <code>#PropertyValue1</code></li>
  <li>add the following object property assertion to this individual: <code>#hasProperty2</code> associated to <code>#PropertyValue2</code></li>
  </ul>
  
  <p>The following information is already present in the ontology:</p>
  
  <ul>
  <li>the class <code>#SimpleType</code> and its subclass <code>#SimpleSubType</code></li>
  <li>the object properties <code>#hasProperty1</code> and <code>#hasProperty2</code></li>
  <li>the individuals <code>#PropertyValue1</code> and <code>#PropertyValue2</code></li>
  </ul>
  
  <p>The following is the code I used to try to achieve that aim:</p>
  
  <pre><code>OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
  
  try {
      OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File("ontology.owl"));
  
      IRI ontologyIRI = ontology.getOntologyID().getOntologyIRI();
      PrefixManager prefixManager = new DefaultPrefixManager(ontologyIRI.toString().concat("#"));
  
      OWLDataFactory dataFactory = manager.getOWLDataFactory();
  
      OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
      OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
      reasoner.precomputeInferences();
  
      // Get all subclasses of SimpleType stored within the loaded ontology.
      OWLClassNodeSet clsSimpleSubTypes = new OWLClassNodeSet();
      OWLClass simpleTypeClass = dataFactory.getOWLClass(":SimpleType", prefixManager);
      clsSimpleSubTypes.addDifferentEntities(reasoner.getSubClasses(simpleTypeClass, true).getFlattened());
  
      // Get two object properties stored within the loaded ontology.
      OWLObjectPropertyExpression objProperty1 = dataFactory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasProperty1"));
      OWLObjectPropertyExpression objProperty2 = dataFactory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasProperty2"));
  
      // Get two property values stored within the loaded ontology.
      OWLNamedIndividual propertyValue1 = dataFactory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#PropertyValue1"));
      OWLNamedIndividual propertyValue2 = dataFactory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#PropertyValue2"));
  
      for (OWLClass cls : clsSimpleSubTypes.getFlattened())
      {
          if (cls.getIRI().toString().endsWith("#SimpleSubType")) {
              // Create the new individual
              OWLNamedIndividual po = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#individualSimpleSubType"));
  
              // individualSimpleSubType is of type SimpleSubType
              OWLClassAssertionAxiom assertion = dataFactory.getOWLClassAssertionAxiom(cls, po);
              manager.addAxiom(ontology, assertion);
  
              // individualSimpleSubType has object property PropertyValue1
              OWLObjectPropertyAssertionAxiom objProperty1Axiom = dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty1, po, propertyValue1);
              manager.addAxiom(ontology, objProperty1Axiom);
  
              // individualSimpleSubType has object property PropertyValue2
              OWLObjectPropertyAssertionAxiom objProperty2Axiom = dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty2, po, propertyValue2);
              manager.addAxiom(ontology, objProperty2Axiom);
  
              break;
          }
      }
  
      //File destinationFile = new File("ontology-new-data.owl");
      //OWLOntologyFormat format = manager.getOntologyFormat(ontology);
      //manager.saveOntology(ontology, format, IRI.create(destinationFile.toURI()));
      manager.saveOntology(ontology);
  } catch (OWLOntologyCreationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (OWLOntologyStorageException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  </code></pre>
  
  <p>However, using the above code, I get a different result than the result I get using Protegé.</p>
  
  <p>If I use Protegé in order to add a new individual as explained above, the OWL file is updated with the addition the following XML lines:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;!-- http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#individualSimpleSubType --&gt;
  
  &lt;owl:NamedIndividual rdf:about="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#individualSimpleSubType"&gt;
      &lt;rdf:type rdf:resource="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#SimpleSubType"/&gt;
      &lt;hasProperty1 rdf:resource="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#PropertyValue1"/&gt;
      &lt;hasProperty2 rdf:resource="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#PropertyValue2"/&gt;
  &lt;/owl:NamedIndividual&gt;
  </code></pre>
  
  <p>However, if I use the above Java code, saving ontology also involves the modification of other lines: the same XML lines as above are added to the file, but also other lines are modified, as follows.</p>
  
  <p>The folliwing lines are written at the beginning of the OWL file saved by the above Java code (but these lines are not written when I save the ontology using Protégé after performing the same modifies).</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;!DOCTYPE rdf:RDF [
      &lt;!ENTITY terms "http://purl.org/dc/terms/" &gt;
      &lt;!ENTITY owl "http://www.w3.org/2002/07/owl#" &gt;
      &lt;!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" &gt;
      &lt;!ENTITY skos "http://www.w3.org/2004/02/skos/core#" &gt;
      &lt;!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" &gt;
      &lt;!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" &gt;
  ]&gt;
  </code></pre>
  
  <p>Then, in the attributes of some tags are added certain prefixes, so:</p>
  
  <ol>
  <li>the attribute <code>rdf:resource="&amp;rdf;List"</code> of the original OWL file is replaced by <code>rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"</code></li>
  <li>the attribute <code>rdf:resource="&amp;owl;TransitiveProperty"</code> of the original OWL file is replaced by <code>rdf:resource="http://www.w3.org/2002/07/owl#TransitiveProperty"</code></li>
  <li>the attribute <code>rdf:about="&amp;terms;description"</code> of the original OWL file is replaced by <code>rdf:about="http://purl.org/dc/terms/description"</code></li>
  <li>the attribute <code>rdf:resource="&amp;rdfs;Literal"</code> of the original OWL file is replaced by <code>rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"</code></li>
  <li>the attribute <code>rdf:resource="&amp;xsd;hexBinary"</code> of the original OWL file is replaced by <code>rdf:resource="http://www.w3.org/2001/XMLSchema#hexBinary"</code></li>
  <li>the attribute <code>rdf:resource="&amp;xsd;string"</code> of the original OWL file is replaced by <code>rdf:resource="http://www.w3.org/2001/XMLSchema#string"</code></li>
  <li>the attribute <code>rdf:resource="&amp;skos;Concept"</code> of the original OWL file is replaced by <code>rdf:resource="http://www.w3.org/2004/02/skos/core#Concept"</code></li>
  <li>the attribute <code>rdf:resource="&amp;skos;ConceptScheme"</code> of the original OWL file is replaced by <code>rdf:resource="http://www.w3.org/2004/02/skos/core#ConceptScheme"</code></li>
  </ol>
  
  <p>What is the reason for this strange behavior?</p>
  
  <p>In my Eclipse project I have imported the jar file related to OWL API 3.4.2, that is the same version integrated in Protegé 4.3.0.</p>