如何使用Jena API将资源与其他资源关联 [英] how to associate a resource to an other using Jena API

查看:78
本文介绍了如何使用Jena API将资源与其他资源关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含两个类的本体,第一个类名为Father,第二个类是名为Son的子类.我想为使用Jena的班级父亲设置以下条件

I have created an ontology containing two classes, the first one is named Father and the second is a subclass named Son. I would like to set the following conditions to the class father using Jena

只有一个儿子.
有一个儿子.

Has only son.
Has some son.

然后我将对类Son做同样的事情:

Then I would do the same for class Son:

正好有个父亲.

has exactly some father.

我的第二个问题是,我不知道如何同时使用Jena将类Son的实例与类Father关联.我知道可以使用Protégé操作我的课程,但是我想探索Jena.

My second issue is that I don't know how to associate the instance of class Son to the class Father using also Jena. I know it's possible to manipulate my classes using Protégé, but I want to explore Jena.

推荐答案

在Jena中,您要尝试执行的操作并不困难,但是听起来好像对OWL感到有些困惑.给定两个 B C 类,如果 B C 的子类,则意味着每个都是 B 也是 C .所以这是真的

The things that you're trying to do are not very difficult in Jena, but it sounds like there is some confusion about OWL. Given two classes B and C, if B is subclass of C, it means that every is B is also a C. So while it's true that

(1)父子类

(1) Father subClassOf Son

由于每个父亲也是别人的儿子,所以这不是真的

since every father is also someone else's son, it's not true that

(2)父亲的子子类

(2) Son subClassOf Father

因为不是每个儿子也是父亲.您所描述的限制也没有多大意义.基数限制可用于断言某个类的每个实例通过某个特定属性与(至少,或至少)至多数量的另一个类的实例完全相关.因此,您可能会说

since not every son is also a father. The restrictions that you've described also don't make a lot of sense. A cardinality restriction can be used to assert that each of instance of a class is related to exactly (or at least, or at most) some number of instances of another class by some particular property. Thus, you would probably say that

(3)儿子subClassOf(父亲恰好是1个父亲)

(3) Son subClassOf (hasFather exactly 1 Father)

断言,儿子的每个实例都与父亲的一个实例完全相关.您还可以说,每个父亲必须至少有一个孩子(并且我会在这里使用孩子而不是儿子,因为孩子也可以是女儿),

to assert that each instance of Son is related to exactly one instance of Father. You could also say that each Father must have at least at least one Child (and I'd use Child here, rather than Son, since a child could also be a Daughter) by

(4)父亲subClassOf(有一个孩子,有个孩子)

(4) Father subClassOf (hasChild some Child)

但是,如果您仅模拟男性,我想您可以说

but if you're only modeling male persons, I guess you could say

(5)父亲subClassOf(有一个儿子)

(5) Father subClassOf (hasSon some Son)

(6)父亲subClassOf(拥有至少1个儿子的儿子)

(6) Father subClassOf (hasSon min 1 Son)

让我们创建一个包含Son和Father类以及公理(1),(3)和(5)的OntModel.然后,我们将添加父亲的一个实例AbrahamLincoln和儿子的一个实例RobertToddLincoln,以及它们之间的适当关系:

Let's create an OntModel that includes the classes Son and Father and the axioms (1), (3), and (5). Then we'll add an instance of Father, AbrahamLincoln, and an instance of Son, RobertToddLincoln, and the appropriate relationships between them:

(7)罗伯特·托德·林肯(RobertToddLincoln)的父亲亚伯拉罕·林肯(AbrahamLincoln)
(8)亚伯拉罕·林肯有儿子罗伯特·托德·林肯

(7) RobertToddLincoln hasFather AbrahamLincoln
(8) AbrahamLincoln hasSon RobertToddLincoln

现在,我们实际上将立即遇到一个问题:量化基数限制是OWL2的一部分,但不是OWL1的一部分,并且Jena尚不支持OWL2.幸运的是,在这种情况下,我们可以解决此问题,但要说儿子只有父亲作为父亲,而儿子恰好有一个父亲:

Now, we'll actually run into one problem right away: quantified cardinality restrictions are a part of OWL2, but not of OWL1, and Jena doesn't support OWL2 yet. Fortunately, in this case we can work around this, but saying that Sons only have Fathers as fathers, and that Sons have exactly one father:

(3'a)儿子subClassOf(只有父亲是父亲)
(3'b)儿子subClassOf(父亲精确为1)

(3'a) Son subClassOf (hasFather only Father)
(3'b) Son subClassOf (hasFather exactly 1)

您最终得到的代码是:

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class FatherSonOntology {
    public static void main(String[] args) {
        final String ns = "http://example.org/";

        final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
        model.setNsPrefix( "ex", ns );

        final OntClass son = model.createClass( ns+"Son" );
        final OntClass father = model.createClass( ns+"Father" );

        final OntProperty hasFather = model.createObjectProperty( ns+"hasFather" );
        final OntProperty hasSon = model.createObjectProperty( ns+"hasSon" );

        // (1) Father subClassOf Son
        son.addSubClass( father );

        // (3'a) Son subClassOf (hasFather only Father)
        son.addSubClass( model.createAllValuesFromRestriction( null, hasFather, father ));

        // (3'b) Son subClassOf (hasFather exactly 1)
        son.addSubClass( model.createCardinalityRestriction( null, hasFather, 1 ));

        // (5) Father subClassOf (hasSon min 1 Son)
        father.addSubClass( model.createSomeValuesFromRestriction( null, hasSon, son ));

        // You can create individuals of a given type using Individual#createIndividual, 
        // or with Model#createIndividual.
        final Individual abe = father.createIndividual( ns+"AbrahamLincoln" );
        final Individual rob = model.createIndividual( ns+"RobertToddLincoln", son );

        // You can add properties to individuals using Individual#addProperty, or you can 
        // use the various Model#add methods to add statements to the model.
        rob.addProperty( hasFather, abe ); // (7) 
        model.add( abe, hasSon, rob );     // (8)

        model.write( System.out, "RDF/XML-ABBREV" );
    }
}

最后打印出本体(您可以保存并在Protégé中打开以检查所有内容是否按预期显示):

which prints the ontology at the end (which you could save and open in Protégé to check that everything appears as expected):

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:ex="http://example.org/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <owl:Class rdf:about="http://example.org/Father">
    <rdfs:subClassOf>
      <owl:Class rdf:about="http://example.org/Son"/>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:ObjectProperty rdf:about="http://example.org/hasSon"/>
  <owl:ObjectProperty rdf:about="http://example.org/hasFather"/>
  <owl:Restriction>
    <rdfs:subClassOf rdf:resource="http://example.org/Son"/>
    <owl:allValuesFrom rdf:resource="http://example.org/Father"/>
    <owl:onProperty rdf:resource="http://example.org/hasFather"/>
  </owl:Restriction>
  <owl:Restriction>
    <rdfs:subClassOf rdf:resource="http://example.org/Father"/>
    <owl:someValuesFrom rdf:resource="http://example.org/Son"/>
    <owl:onProperty rdf:resource="http://example.org/hasSon"/>
  </owl:Restriction>
  <owl:Restriction>
    <rdfs:subClassOf rdf:resource="http://example.org/Son"/>
    <owl:cardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
    >1</owl:cardinality>
    <owl:onProperty rdf:resource="http://example.org/hasFather"/>
  </owl:Restriction>
  <ex:Son rdf:about="http://example.org/RobertToddLincoln">
    <ex:hasFather>
      <ex:Father rdf:about="http://example.org/AbrahamLincoln">
        <ex:hasSon rdf:resource="http://example.org/RobertToddLincoln"/>
      </ex:Father>
    </ex:hasFather>
  </ex:Son>
</rdf:RDF>

这篇关于如何使用Jena API将资源与其他资源关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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