用jena库写猫头鹰文件

时间:2016-05-18 20:49:31

标签: java jena owl

我已经创建了一个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");

1 个答案:

答案 0 :(得分:0)

正如你所说:

  

问题是OntClass,ObjectProperty和Resource为空。

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

  

OntClass getOntClass(String uri)

     

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

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

  

OntClass createClass(String uri)

     

在此处回答表示类描述节点的资源   模型。如果模型中存在具有给定URI的资源,它将会   被重复使用。如果没有,则在可写子模型中创建一个新的   本体模型。