2

在我的本体中,我有一个拥有这个数据属性的人

hasName "somaName"^^string,

但是,当我构建一个类表达式并发送给推理器以获取实例时,我得到一个带有以下查询的空集,

OWLClassExpression x = schema.getFactory().getOWLDataHasValue(schema.getDataProperty("hasName"), schema.getFactory().getOWLLiteral("somaName"));
System.out.println(reasoner.getInstances(x, true));

getDataProperty 只是一个小方法:

public OWLDataProperty getDataProperty(String dataProperty){
        return factory.getOWLDataProperty("#"+dataProperty,pm);
    }
4

1 回答 1

3

以下代码片段有效,将其与您的代码进行比较,看看有什么不同。您应该使用支持这种类型构造的推理器(Hermit 确实如此)。

//Initiate everything
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
String base = "http://www.example.org/";
OWLOntology ontology = manager.createOntology(IRI.create(base + "ontology.owl"));
OWLDataFactory factory = manager.getOWLDataFactory();
//Add the stuff to the ontology
OWLDataProperty hasName = factory.getOWLDataProperty(IRI.create(base + "hasName"));
OWLNamedIndividual john = factory.getOWLNamedIndividual(IRI.create(base + "john"));
OWLLiteral lit = factory.getOWLLiteral("John");
OWLDataPropertyAssertionAxiom ax = 
                  factory.getOWLDataPropertyAssertionAxiom(hasName, john, lit);
AddAxiom addAx = new AddAxiom(ontology, ax);
manager.applyChange(addAx);

//Init of the reasoner
//I use Hermit because it supports the construct of interest
OWLReasonerFactory reasonerFactory = new Reasoner.ReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
reasoner.precomputeInferences();

//Prepare the expression for the query
OWLDataProperty p = factory.getOWLDataProperty(IRI.create(base + "hasName"));
OWLClassExpression ex = 
                factory.getOWLDataHasValue(p, factory.getOWLLiteral("John"));

//Print out the results, John is inside
Set<OWLNamedIndividual> result = reasoner.getInstances(ex, true).getFlattened();        
for (OWLNamedIndividual owlNamedIndividual : result) {
    System.out.println(owlNamedIndividual);
}
于 2013-04-17T14:46:37.380 回答