Search code examples
javajenaowl

writing owl file with jena library


I have make an Spring mvc project and I try to write instances in a owl file but I have this exception. The problem is OntClass,ObjectProperty and Resource is null.

java.lang.IllegalArgumentException: Cannot create someValuesFromRestriction with a null property or class
    at com.hp.hpl.jena.ontology.impl.OntModelImpl.createSomeValuesFromRestriction(OntModelImpl.java:1539) 
    at com.company.app.service.JenaDAO.anatomicalUpdateWithObjectRestriction(JenaDAO.java:168)
    at com.company.app.controllers.HomeController.saveInstance(HomeController.java:91)

Any idea what wrong happen?

My Code:

 public class JenaDAO implements JenaDAOImpl {    
        private static final Logger LOG = LoggerFactory.getLogger(JenaDAO.class);    
        static final String ns = "http://www.semanticweb.org/marilenatarouse/ontologies/2014/6/mammoOntology#";
        static final String inputFileName = "C:\\Users\\Dimitris\\Desktop\\temp\\finalMammo.owl";
        static final String outFile = "C:\\Users\\Dimitris\\Desktop\\temp\\finalMammo.owl";

        public OntModel getOntologyModel()
    {   
         ...
    }

        public OntModel ontologyImport() {
           ...
        }
     public int anatomicalUpdateWithObjectRestriction(String name, String klasi, String objectProperty) {
            OntModel ontology = ontologyImport();
            OntClass amPatient = ontology.getOntClass(ns + klasi);
            Individual newName = ontology.createIndividual(ns + name, amPatient);
            ObjectProperty prop = ontology.getObjectProperty(ns + objectProperty);

            Resource res = ontology.createResource(ns + klasi);
            Restriction rest = ontology.createSomeValuesFromRestriction(null, prop, res);
            newName.addRDFType(rest);
            FileWriter out = null;

            try {
                out = new FileWriter(inputFileName);
                ontology.write(out, "RDF/XML");
                out.close();

            } catch (IOException e) {

                e.printStackTrace();
            }

            return 1;

        }
    ublic int instanceInsertWithDataProperty(String name, String patientType, String dataProperty, String lvl) {
            System.out.println("test123: "+patientType);
            OntModel ontology = ontologyImport();
            OntClass amPatient = ontology.getOntClass(ns + patientType);
          //  System.out.println("test123: "+amPatient.getURI());
            if (!name.isEmpty()) {
                Individual newName = ontology.createIndividual(ns + name, amPatient);
                if (!dataProperty.isEmpty()) {
                    OntProperty property = ontology.getDatatypeProperty(ns + dataProperty);
                    newName.addProperty(property, lvl);
                }
                FileWriter out = null;

                try {
                    out = new FileWriter(inputFileName);
                    ontology.write(out, "RDF/XML");
                    out.close();

                } catch (IOException e) {

                    e.printStackTrace();
                }
            } else {
                System.out.print("Invalid name");
                return 0;
            }
            LOG.info("Ontology updated successfully");
            return 0;
        }
    }

call methods:

jenaDAO.instanceInsertWithDataProperty("Subject1", "SubjectBR_Birads1", "SubjectID", "test"  );
            jenaDAO.anatomicalUpdateWithObjectRestriction("Subject2","SubjectBR_Birads1" , "hasBreast");

Solution

  • As you said:

    The problem is OntClass,ObjectProperty and Resource is null.

    You're using getClass (emphasis added):

    OntClass getOntClass(String uri)

    Answer a resource that represents a class description node in this model. If a resource with the given URI exists in the model, and can be viewed as an OntClass, return the OntClass facet, otherwise return null.

    I'd guess that there isn't such a resource in the model, and that you should use createClass instead:

    OntClass createClass(String uri)

    Answer a resource that represents a class description node in this model. If a resource with the given URI exists in the model, it will be re-used. If not, a new one is created in the writable sub-model of the ontology model.