0

我基本上想用 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、Resource 和 Properties。我知道使用 Jena 一定是可能的。

4

1 回答 1

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
于 2013-01-25T09:18:40.287 回答