我正在用jena在Eclipse中实现一个本体。我正试图从整个本体中获取一个人的同义词。
我将如何从整个本体获得特定个体的所有同义词,而不论其是什么类。
static final String inputFileName = "http://word.owl";
OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
InputStream in = FileManager.get().open(inputFileName);
inf.read(in, "");
ExtendedIterator classes = inf.listClasses();
while(classes.hasNext())
{
OntClass obj = (OntClass) classes.next();
String className = obj.getLocalName().toString();
System.out.println("Class Name : "+className);
}
ExtendedIterator instances = inf.listIndividuals();
while(instances.hasNext())
{
Individual ind = (Individual) instances.next();
String indName = ind.getLocalName().toString();
System.out.println("Individual Name : "+indName);
}
ExtendedIterator property = inf.listDatatypeProperties();
while(property.hasNext())
{
DatatypeProperty prop = (DatatypeProperty) property.next();
String propName = prop.getLocalName().toString();
System.out.println("Data Propties Name : "+propName);
}
ExtendedIterator property1 = inf.listObjectProperties();
while(property1.hasNext())
{
ObjectProperty prop = (ObjectProperty) property1.next();
String propName = prop.getLocalName().toString();
System.out.println("Object Propties Name : "+propName);
}
ObjectProperty isSynonymOf = inf.getObjectProperty("http://www.../Word#isSynonymOf");
System.out.println("Individuals having isSynonymOf Proterty:");
ExtendedIterator individuals1 = inf.listIndividuals();
while(individuals1.hasNext())
{
Individual ind = (Individual) individuals1.next();
if(ind.getProperty(isSynonymOf) != null)
{
String indName = ind.getLocalName().toString();
System.out.println(indName);
}
}
}
问题:当我输入个人时,例如软件
它应该给我所有的同义词
- 软件
- 软件架构
- 软件设计
- 计划
这是我的OWL文件
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/ontologies/Word#"
xml:base="http://www.semanticweb.org/ontologies/Word"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.semanticweb.org/ontologies/Word"/>
答案 0 :(得分:2)
默认OntModel
并不考虑应用OWL推理,因此,必须启用推理以明确考虑对称属性:
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
有关详细信息,请参阅documentation。
只是旁注,Jena有很多便利方法,例如:您的代码可以通过使用
缩短model.listSubjectsWithProperty(isSynonymOf)