0
votes

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é.

The Output is throwing me this error :

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)

I have tried to save as indicated in the OWL API examples so I don't know where my error is.

This is the code i'm using to save the ontology :

onto.saveOntology(onto.getFormat(), man.getOntologyDocumentIRI(onto));

And the code using to add dataProperties :

public ArrayList<AddAxiom> addDataProperties(ArrayList<Info> data, OWLIndividual patient) {
    ArrayList<AddAxiom> axioms = new ArrayList<>();
    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;
}

[EDIT]

There is the function that uses the method above, and the differents way i used to save the ontology.

Thank You again

public void addPatientIndividual(ArrayList<Info> 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-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-");
    }

} 

[EDIT] Code for SearchPatient

public Patient searchPatient(String id, OWLReasoner reasoner) {
    Patient pat = new Patient();
    onto.individualsInSignature().forEach(i -> onto.dataPropertiesInSignature().forEach(p -> {
        if (i.getIRI().getRemainder().get().equals(id)) {
            pat.setId(id);

            Set<OWLLiteral> prop = reasoner.getDataPropertyValues(i, p);
            Set<OWLLiteral> 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;
}

And the code for getPatient

public ArrayList<Patient> getPatientInOntology(OWLReasoner reasoner, String individualName) {
    ArrayList<Patient> pats = new ArrayList<>();
    //ArrayList<String> liste = new ArrayList<>();
    onto.classesInSignature().forEach(c -> {
        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;
}
1
OK, addDataProperties fait partie de Ontology.Java, mais je rajoute un peu plus de code si il manque des informations. - LexR
Please, add the complete stacktrace, here your code show no usage of type 'Optional' while it is the center of your problem. Have you define the format for ontology before trying to get it in 'onto.getFormat()' ? - Galigator
ontology.getOWLOntologyManager().setOntologyFormat(ontology, new RDFXMLDocumentFormatFactory().get()) - Galigator
Seems not doing anything and not modifying the ontology. I also tried to look where glassfish put files but nothing has changed. - LexR
Can you add the code for searchPatient()? the Optional that's empty is accessed in a lambda in that method. It would provide context as to what's going wrong. - Ignazio

1 Answers

1
votes

I Know from where was the error in my case.

In fact the problem was the method i used. With the new owl API there is .getRemainder().get() that replace .getFragment() which is deprecated.

In my code the line :

pats.add(searchPatient(cls.getIRI().getRemainder().get(), reasoner));

was the one that threw the error. So I replaced it by the deprecated .getFragment() as follow :

pats.add(searchPatient(cls.getIRI().getFragment(), reasoner));

If someone could explain me what is the diffence between those two methods i would be interested to use a nicer code.

Thank you for your help and time.