2013-01-24 19 views
0

我基本上想用耶拿做到这一点:如何使用Jena模式创建传递属性?

我)架构

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 

二)数据

使用上述架构和数据,我要回答下列各种需求resourcecat: 获取resourcecat的PrimeFeatures:我应该同时获得Cat的PrimeFeatures和它们的值的属性名称。 所以,我应该得到如下: -

  PrimeFeatures(catresource): 
      speed=100 
      nature=violent 

而且,我应该能够修改使用动物类架构动物类的主要特点。

请帮我小急。

基本上我想控制我使用模式获取的数据。 大部分使用OntClass,资源和属性

我知道这一定是可能的使用耶拿。

谢谢

回答

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 
+0

嗨伊恩由于一吨。我唯一需要更多的是如何准确地获得primefeatures。你能帮我创建一个像showInstancePrimeFeatureProperties而不是showInstanceProperties的函数吗?一个函数,列出所有属性的名称以及它们的值? – user1374179