简体   繁体   English

用jena库写猫头鹰文件

[英]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. 我做了一个Spring mvc项目,我尝试在owl文件中编写实例,但是我有这个例外。 The problem is OntClass,ObjectProperty and Resource is null. 问题是OntClass,ObjectProperty和Resource为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");

As you said: 如你所说:

The problem is OntClass,ObjectProperty and Resource is null. 问题是OntClass,ObjectProperty和Resource为null。

You're using getClass (emphasis added): 你正在使用getClass (强调添加):

OntClass getOntClass(String uri) 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. 如果模型中存在具有给定URI的资源,并且可以将其视为OntClass,则返回OntClass方面, 否则返回null。

I'd guess that there isn't such a resource in the model, and that you should use createClass instead: 我猜测模型中没有这样的资源,你应该使用createClass

OntClass createClass(String uri) 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. 如果模型中存在具有给定URI的资源,则将重新使用该资源。 If not, a new one is created in the writable sub-model of the ontology model. 如果不是, 在本体模型的可写子模型中创建新的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM