用jena库写猫头鹰文件

问题描述 投票:1回答:1

我做了一个Spring mvc项目,我尝试在owl文件中编写实例,但是我有这个例外。问题是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)

知道发生了什么错误吗?

我的代码:

 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;
        }
    }

通话方式:

jenaDAO.instanceInsertWithDataProperty("Subject1", "SubjectBR_Birads1", "SubjectID", "test"  );
            jenaDAO.anatomicalUpdateWithObjectRestriction("Subject2","SubjectBR_Birads1" , "hasBreast");
java jena owl
1个回答
0
投票

如你所说:

问题是OntClass,ObjectProperty和Resource为null。

你正在使用getClass(强调添加):

OntClass getOntClass(String uri)

回答表示此模型中的类描述节点的资源。如果模型中存在具有给定URI的资源,并且可以将其视为OntClass,则返回OntClass方面,否则返回null。

我猜测模型中没有这样的资源,你应该使用createClass:

OntClass createClass(String uri)

回答表示此模型中的类描述节点的资源。如果模型中存在具有给定URI的资源,则将重新使用该资源。如果不是,则在本体模型的可写子模型中创建新的。

© www.soinside.com 2019 - 2024. All rights reserved.