如何使用Jena架构创建传递属性?

时间:2013-01-24 15:23:25

标签: properties jena

我基本上想用Jena做这件事:

i)架构

class:LivingBeing type owl:Class
prop:PrimeFeatures

class:Animal type owl:Class
prop:Speed typeOf PrimeFeatures
prop:Nature typeOf PrimeFeatures

class:HumanBeing type owl:Class
prop:Intelligence typeOf PrimeFeatures

ii)数据

Individual:Cat typeOf Animal resource="someuri:Cat"
prop:Speed = 100
prop:Nature = violent
prop:Veg = No

Individual:John typeOf HumanBeing resource="someuri:John"
prop:Intelligence = Average



Resource resourcecat = model.getResource("someuri:Cat") ;

使用上面的架构和数据,我想回答对resourcecat的以下需求: 获取resourcecat的PrimeFeatures:我应该得到属性的名称,这些属性是Cat的PrimeFeatures及其值。 所以,我应该遵循: -

           PrimeFeatures(catresource):
           speed=100
           nature=violent

此外,我应该能够使用Animal类架构修改Animal类的主要功能。

请帮我一点紧急。

基本上我想控制使用架构获取的数据。 主要使用OntClass,资源和属性

我知道必须使用耶拿。

谢谢

1 个答案:

答案 0 :(得分:0)

下面显示了一种可能的解决方案。但是,我要提醒你,你并没有真正按照设计的方式使用OWL和RDF的特性。如果您真的想要使用Jena的功能,您应该阅读更多有关语义Web技术的信息,并使用它来指导您的设计。否则,您也可以使用Java为您的应用程序设计自定义解决方案,而不是尝试将您的设计强制适合不适合的表示。

package test;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class User1374179_Test
{
    public static String ns = "http://example.org/test#";

    private OntClass livingBeing;
    private OntClass animal;
    private OntProperty primeFeatures;
    private OntProperty speed;
    private OntProperty nature;

    public static void main( String[] args ) {
        new User1374179_Test().run();
    }

    public void run() {
        OntModel m = createSchema();
        showClassProperties( livingBeing );
        showClassProperties( animal );

        Individual cat1 = createInstance( m, animal, "cat1" );
        cat1.addProperty( speed, m.createTypedLiteral( 100 ));
        cat1.addProperty( nature, "violent" );
        showInstanceProperties( cat1, animal );
    }

    private OntModel createSchema() {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );


        livingBeing = m.createClass( ns + "LivingBeing" );
        primeFeatures = m.createOntProperty( ns + "primeFeatures" );
        primeFeatures.addDomain( livingBeing );

        animal = m.createClass( ns + "Animal" );
        animal.addSuperClass( livingBeing );

        speed = m.createOntProperty( ns + "speed" );
        speed.addSuperProperty( primeFeatures );
        speed.addDomain( animal );

        nature = m.createOntProperty( ns + "nature" );
        nature.addSuperProperty( primeFeatures );
        nature.addDomain( animal );

        return m;
    }

    private Individual createInstance( OntModel m, OntClass cls, String name ) {
        return m.createIndividual( ns + name, cls );
    }

    private void showClassProperties( OntClass c ) {
        System.out.println( "Class " + c.getURI() );
        for (ExtendedIterator<OntProperty> i = c.listDeclaredProperties(); i.hasNext(); ) {
            System.out.println( " .. has property " + i.next() );
        }
    }

    private void showInstanceProperties( OntResource r, OntClass c ) {
        System.out.println( "Instance " + r.getURI() + " has properties: " );
        for (ExtendedIterator<OntProperty> i = c.listDeclaredProperties(true); i.hasNext(); ) {
            OntProperty p = i.next();
            System.out.println( p.getURI() + " ==> " + r.getPropertyValue( p ) );
        }
    }
}

输出:

Class http://example.org/test#LivingBeing
 .. has property http://example.org/test#primeFeatures
Class http://example.org/test#Animal
 .. has property http://example.org/test#speed
 .. has property http://example.org/test#primeFeatures
 .. has property http://example.org/test#nature
Instance http://example.org/test#cat1 has properties: 
http://example.org/test#speed ==> 100^^http://www.w3.org/2001/XMLSchema#int
http://example.org/test#nature ==> violent