title: owl API error after saving ontology
body: <p>I'm working on a java project where i need to use an ontology.
  I have an ontology on which I can read what is inside through the owl api, but once I want to add a new instance and add dataProperties to it, I'm no more able to read this ontology with my app. And what I find strange is that it's fine with protégé.</p>
  
  <p>The Output is throwing me this error :</p>
  
  <pre><code>Avertissement:   StandardWrapperValve[SearchPatient]: Servlet.service() for servlet SearchPatient threw exception
  java.util.NoSuchElementException: No value present
  at java.util.Optional.get(Optional.java:135)
  at model.Ontology.lambda$null$6(Ontology.java:450)
  at java.util.Iterator.forEachRemaining(Iterator.java:116)
  at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
  at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
  at model.Ontology.lambda$searchPatient$7(Ontology.java:449)
  at java.util.Iterator.forEachRemaining(Iterator.java:116)
  at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
  at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
  at model.Ontology.searchPatient(Ontology.java:449)
  at model.Ontology.getPatientInOntology(Ontology.java:144)
  at com.mycompany.mavenwebapphadbpm.SearchPatient.processRequest(SearchPatient.java:57)
  at com.mycompany.mavenwebapphadbpm.SearchPatient.doGet(SearchPatient.java:91)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
  at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
  at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
  at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
  at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
  at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
  at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
  at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
  at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>I have tried to save as indicated in the OWL API examples so I don't know where my error is.</p>
  
  <p>This is the code i'm using to save the ontology :</p>
  
  <pre><code>onto.saveOntology(onto.getFormat(), man.getOntologyDocumentIRI(onto));
  </code></pre>
  
  <p>And the code using to add dataProperties :</p>
  
  <pre><code>public ArrayList&lt;AddAxiom&gt; addDataProperties(ArrayList&lt;Info&gt; data, OWLIndividual patient) {
      ArrayList&lt;AddAxiom&gt; axioms = new ArrayList&lt;&gt;();
      OWLDataFactory df = man.getOWLDataFactory();
  
      for (Info dp : data) {
          OWLDataProperty hasProp = df.getOWLDataProperty(IRI.create(owlIRI + "#" + dp.getRelation()));
          OWLDataPropertyAssertionAxiom axiom = null;
          System.out.println("Relation : " + dp.getRelation() + " valeur : " + dp.getValue());
          switch (dp.getType()) {
              case "String":
                  axiom = df.getOWLDataPropertyAssertionAxiom(hasProp, patient, dp.getValue());
                  break;
              case "int":
                  axiom = df.getOWLDataPropertyAssertionAxiom(hasProp, patient, Integer.parseInt(dp.getValue()));
                  break;
              case "float":
                  axiom = df.getOWLDataPropertyAssertionAxiom(hasProp, patient, Float.parseFloat(dp.getValue()));
                  break;
              case "boolean":
                  if (dp.getValue() == null) {
                      axiom = df.getOWLDataPropertyAssertionAxiom(hasProp, patient, false);
                  } else {
                      axiom = df.getOWLDataPropertyAssertionAxiom(hasProp, patient, true);
                  }
                  break;
          }
          AddAxiom addAxiom = new AddAxiom(onto, axiom);
          axioms.add(addAxiom);
  
      }
  
      return axioms;
  }
  </code></pre>
  
  <p>[EDIT]</p>
  
  <p>There is the function that uses the method above, and the differents way i used to save the ontology.</p>
  
  <p>Thank You again</p>
  
  <pre><code>public void addPatientIndividual(ArrayList&lt;Info&gt; data, String name) {
      OWLDataFactory df = man.getOWLDataFactory();
  
      // Actor's IRI
      OWLClass actorIRI = df.getOWLClass(IRI.create(owlIRI + "#Patient"));
  
      // Patient individual
      OWLIndividual patient = df.getOWLNamedIndividual(IRI.create(owlIRI + "#" + name));
  
      // Create a link between the class patient and the individual
      OWLClassAssertionAxiom type = df.getOWLClassAssertionAxiom(actorIRI, patient);
      // Create the axiom corresponding to the link between the patient and the
      // individual
      AddAxiom axiomType = new AddAxiom(onto, type);
      // Add the former link to the ontology
      man.applyChange(axiomType);
  
      // Disease individual
      OWLIndividual disease = df.getOWLNamedIndividual(owlIRI + "#" + ((Info) data.get(0)).getValue());
      // Create the property which is already in the owl the name of the patient to
      // the individual
      OWLObjectProperty hasDisease = df.getOWLObjectProperty(owlIRI + "#hasDisease");
      // Link the disease to the patient
      OWLObjectPropertyAssertionAxiom axiomHasDisease = df.getOWLObjectPropertyAssertionAxiom(hasDisease, patient,
              disease);
      // Create the axiom
      AddAxiom addAxiomHasDisease = new AddAxiom(onto, axiomHasDisease);
      // Apply the axiom to the ontology
      man.applyChange(addAxiomHasDisease);
  
      // Delete the axiom of the disease
      data.remove(data.get(0));
  
      for (AddAxiom axiom : addDataProperties(data, patient)) {
          man.applyChange(axiom);
      }
  
      // Save the ontology
      try {
          //System.out.println("coucou");
          //this.onto.saveOntology(); // PLENTE
          //this.getOntology().saveOntology(); // PLENTE
          //this.getOntology().saveOntology(this.getOntology().getFormat()); // PLENTE
          //manager.saveOntology(onto); // NE FAIS RIENT
          //onto.getOWLOntologyManager().saveOntology(onto); // Enregistre puis lecture impossible
          //onto.saveOntology(onto.getFormat(), manager.getOntologyDocumentIRI(onto));
          //System.out.println(manager.getOntologyDocumentIRI(this.getOntology()));
          //System.out.println(owlIRI);
          //System.out.println(onto.getOntologyID().getVersionIRI());
          //man.saveOntology(onto, onto.getFormat().asPrefixOWLDocumentFormat()); // MARCHE TOUJOURS PAS
          //man.saveOntology(onto);
          //this.getOntology().saveOntology(); // MARCHE PAS
          onto.saveOntology(onto.getFormat(), man.getOntologyDocumentIRI(onto));
          System.out.print("Sauvegarde ?????");
      } catch (Exception e) {
          e.printStackTrace();
          System.out.println("-/-/-/-/-/-/-/-/-/-/-/-PLENTE-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-");
      }
  
  } 
  </code></pre>
  
  <p>[EDIT]
  Code for SearchPatient</p>
  
  <pre><code>public Patient searchPatient(String id, OWLReasoner reasoner) {
      Patient pat = new Patient();
      onto.individualsInSignature().forEach(i -&gt; onto.dataPropertiesInSignature().forEach(p -&gt; {
          if (i.getIRI().getRemainder().get().equals(id)) {
              pat.setId(id);
  
              Set&lt;OWLLiteral&gt; prop = reasoner.getDataPropertyValues(i, p);
              Set&lt;OWLLiteral&gt; values = asUnorderedSet(prop.parallelStream());
  
              for (OWLLiteral v : values) {
                  if (p.getIRI().getRemainder().get().equals("hasFirstName")) {
                      pat.setHasFirstName(v.getLiteral());
                  }
                  if (p.getIRI().getRemainder().get().equals("hasName")) {
                      pat.setHasName(v.getLiteral());
                  }
              }
  
          }
  
      }));
      return pat;
  }
  </code></pre>
  
  <p>And the code for getPatient</p>
  
  <pre><code>public ArrayList&lt;Patient&gt; getPatientInOntology(OWLReasoner reasoner, String individualName) {
      ArrayList&lt;Patient&gt; pats = new ArrayList&lt;&gt;();
      //ArrayList&lt;String&gt; liste = new ArrayList&lt;&gt;();
      onto.classesInSignature().forEach(c -&gt; {
          if (c.getIRI().getFragment().equals(individualName)) {
              patient = c;
          }
      });
  
      // Display all the individual
      for (OWLNamedIndividual cls : reasoner.getInstances(patient).getFlattened()) {
          //liste.add(cls.getIRI().getRemainder().get());
          //pats.add()
  
          //System.out.println("cls : " + cls.getIRI().getRemainder().get());
          pats.add(searchPatient(cls.getIRI().getRemainder().get(), reasoner));
      }
  
      return pats;
  }
  </code></pre>