使用pizza ontology,我希望能够查找American
披萨的所有配料。
如果我在Protégé打开本体,我可以看到American
披萨有以下限制:
hasTopping some MozerellaTopping
hasTopping some TomatoTopping
如何通过Jena以编程方式获取相同的信息?
答案 0 :(得分:10)
这是我的解决方案。我刚刚打印出你要求的字符串,但希望你能从中看到如何使用Jena OntAPI遍历本体图并挑选出你感兴趣的内容。
package examples;
import java.util.Iterator;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
public class PizzaExample
{
/***********************************/
/* Constants */
/***********************************/
public static String BASE = "http://www.co-ode.org/ontologies/pizza/pizza.owl";
public static String NS = BASE + "#";
/***********************************/
/* External signature methods */
/***********************************/
public static void main( String[] args ) {
new PizzaExample().run();
}
public void run() {
OntModel m = getPizzaOntology();
OntClass american = m.getOntClass( NS + "American" );
for (Iterator<OntClass> supers = american.listSuperClasses(); supers.hasNext(); ) {
displayType( supers.next() );
}
}
/***********************************/
/* Internal implementation methods */
/***********************************/
protected OntModel getPizzaOntology() {
OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
m.read( BASE );
return m;
}
protected void displayType( OntClass sup ) {
if (sup.isRestriction()) {
displayRestriction( sup.asRestriction() );
}
}
protected void displayRestriction( Restriction sup ) {
if (sup.isAllValuesFromRestriction()) {
displayRestriction( "all", sup.getOnProperty(), sup.asAllValuesFromRestriction().getAllValuesFrom() );
}
else if (sup.isSomeValuesFromRestriction()) {
displayRestriction( "some", sup.getOnProperty(), sup.asSomeValuesFromRestriction().getSomeValuesFrom() );
}
}
protected void displayRestriction( String qualifier, OntProperty onP, Resource constraint ) {
String out = String.format( "%s %s %s",
qualifier, renderURI( onP ), renderConstraint( constraint ) );
System.out.println( "american pizza: " + out );
}
protected Object renderConstraint( Resource constraint ) {
if (constraint.canAs( UnionClass.class )) {
UnionClass uc = constraint.as( UnionClass.class );
// this would be so much easier in ruby ...
String r = "union{ ";
for (Iterator<? extends OntClass> i = uc.listOperands(); i.hasNext(); ) {
r = r + " " + renderURI( i.next() );
}
return r + "}";
}
else {
return renderURI( constraint );
}
}
protected Object renderURI( Resource onP ) {
String qName = onP.getModel().qnameFor( onP.getURI() );
return qName == null ? onP.getLocalName() : qName;
}
}
产生以下输出:
american pizza: some pizza:hasTopping pizza:MozzarellaTopping
american pizza: some pizza:hasTopping pizza:PeperoniSausageTopping
american pizza: some pizza:hasTopping pizza:TomatoTopping
american pizza: all pizza:hasTopping union{ pizza:MozzarellaTopping pizza:TomatoTopping pizza:PeperoniSausageTopping}
答案 1 :(得分:0)
现在(使用Apache Jena 3.x.x)(对于Jena2而言)仍然存在org.apache.jena.ontology.OntModel
和pizza.owl
的一个可能问题,即Jena OntAPI仅支持OWL1,而pizza是OWL2本体。
虽然上面的示例无关紧要('存在量化'限制对于OWL1和OWL2看起来都相同,并且Jena API可以处理它),但在一般情况下,您不能使用OntModel
处理本体同样容易。
作为一种选择,有一种方法可以从ONT-API调用ru.avicomp.ontapi.jena.model.OntGraphModel
。它基于与耶拿OntModel
相同的原则,但它强烈适用于OWL2,而且不是InfModel
(在撰写本文时)。也许对某人有帮助。
使用示例(仅从类的限制中获取对象 - 某些值):
String web = "https://raw.githubusercontent.com/avicomp/ont-api/master/src/test/resources/ontapi/pizza.ttl";
// create an OWL2 RDF-view (Jena Model):
OntGraphModel m = ru.avicomp.ontapi.jena.OntModelFactory.createModel();
// load pizza ontology from web
m.read(web, "ttl");
// find class and property
OntClass clazz = m.getOntClass(m.expandPrefix(":American"));
OntNOP prop = m.getObjectProperty(m.expandPrefix(":hasTopping"));
// list and print all some values from restrictions with desired property
clazz.superClasses()
.filter(c -> c.canAs(OntCE.ObjectSomeValuesFrom.class))
.map(c -> c.as(OntCE.ObjectSomeValuesFrom.class))
.filter(c -> prop.equals(c.getProperty()))
.map(x -> x.getValue())
.forEach(System.out::println);
输出:
http://www.co-ode.org/ontologies/pizza/pizza.owl#PeperoniSausageTopping(OntClass)
http://www.co-ode.org/ontologies/pizza/pizza.owl#TomatoTopping(OntClass)
http://www.co-ode.org/ontologies/pizza/pizza.owl#MozzarellaTopping(OntClass)