使用Jena获取OWL对类的限制 [英] get OWL restrictions on classes using Jena

查看:98
本文介绍了使用Jena获取OWL对类的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用披萨本体,我想成为能够查找 <$ c的所有浇头$ c>美国 披萨。
如果我在Protégé打开本体,我可以看到美国披萨有以下限制:

Using the pizza ontology, I want to be able to look up all the toppings for American pizza. If I open the ontology in Protégé, I can see that American pizza has the following restrictions:

hasTopping some MozerellaTopping
hasTopping some TomatoTopping

如何通过Jena以编程方式获取相同的信息?

How can I get the same information programatically through Jena?

推荐答案

这是我的解决方案。我刚刚打印出你要求的字符串,但希望你能从中看到如何使用Jena OntAPI遍历本体图并挑选出你感兴趣的东西。

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.

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}

这篇关于使用Jena获取OWL对类的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆