gpt4 book ai didi

java - 使用 Jena 库编写 owl 文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:11 27 4
gpt4 key购买 nike

我创建了一个 Spring mvc 项目,并尝试在 owl 文件中写入实例,但有这个异常。问题是 OntClass、ObjectProperty 和 Resource 为空。

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");

最佳答案

正如你所说:

The problem is OntClass,ObjectProperty and Resource is null.

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

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.

我猜模型中没有这样的资源,您应该使用 createClass 来代替:

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.

关于java - 使用 Jena 库编写 owl 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37309868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com