简体   繁体   English

使用 Jena 获取 OWL 对类的限制

[英]get OWL restrictions on classes using Jena

Using the pizza ontology , I want to be able to look up all the toppings for American pizza.使用披萨本体,我希望能够查找American披萨的所有配料。 If I open the ontology in Protégé, I can see that American pizza has the following restrictions:如果在Protégé中打开本体,可以看到American披萨有以下限制:

hasTopping some MozerellaTopping
hasTopping some TomatoTopping

How can I get the same information programatically through Jena?如何通过 Jena 以编程方式获取相同的信息?

Here's my solution.这是我的解决方案。 I've just printed out the strings you ask for, but hopefully you can see from this how to use the Jena OntAPI to traverse an ontology graph and pick out the things you're interested in.我刚刚打印了您要求的字符串,但希望您能从中看到如何使用 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;
    }
}

Which produces the following output:产生以下输出:

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}

For Apache Jena 3.xx (and also for 2.x) there is one potential problem related to the org.apache.jena.ontology.OntModel and pizza.owl : Jena supports only OWL1, but pizza is OWL2 ontology.对于 Apache Jena 3.xx(以及 2.x),存在一个与org.apache.jena.ontology.OntModelpizza.owl相关的潜在问题:Jena 仅支持OWL1,但pizza 是OWL2 本体。

Ans, although, for the example above it doesn't matter ('Existential Quantification' restrictions looks identically both for OWL1 and OWL2, and Jena API can process it), in general case you can't use org.apache.jena.ontology.OntModel for processing ontology just as easily. Ans,尽管对于上面的示例,这并不重要(OWL1 和 OWL2 的“存在​​量化”限制看起来相同,并且 Jena API 可以处理它),但在一般情况下,您不能使用org.apache.jena.ontology.OntModel用于处理本体同样容易。

As an option there is an alternative named com.github.owlcs.ontapi.jena.model.OntModel from ONT-API .作为一种选择, ONT-API 中有一个名为com.github.owlcs.ontapi.jena.model.OntModel的替代方案。 This model is based on the same principles as a Jena OntModel , but it is strongly for OWL2 (and, also, it is not InfModel - at time of writing).该模型基于与 Jena OntModel相同的原则,但它强烈适用于 OWL2(而且,在撰写本文时,它不是InfModel )。

Consider an example of usage (object-some-values-from restrictions for a class):考虑一个使用示例(object-some-values-from 类的限制):

    String web = "https://raw.githubusercontent.com/owlcs/ont-api/master/src/test/resources/ontapi/pizza.ttl";
    // create an OWL2 RDF-view (Jena Model):
    OntModel m = com.github.owlcs.ontapi.jena.OntModelFactory.createModel();
    // load pizza ontology from web
    m.read(web, "ttl");
    // find class and property
    OntClass clazz = m.getOntClass(m.expandPrefix(":American"));
    OntObjectProperty prop = m.getObjectProperty(m.expandPrefix(":hasTopping"));
    // list and print all some values from restrictions with desired property
    clazz.superClasses()
            .filter(c -> c.canAs(OntClass.ObjectSomeValuesFrom.class))
            .map(c -> c.as(OntClass.ObjectSomeValuesFrom.class))
            .filter(c -> prop.equals(c.getProperty()))
            .map(x -> x.getValue())
            .forEach(System.out::println);

The output:输出:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM