0

他のOWLクラス(A1の範囲はOWLクラスB、A2の範囲はOWL)と同じ範囲で、多くのObjectProperty(A1、A2など)を持つOWLクラス(クラスA)の個体を追加する際に問題に直面していますclass C ...) したがって、クラス B、C には、String、float、double などの特定のデータ型を持つ DatatypeProperty または ObjectProperty があります。私の知る限り、次のような個人を追加できます。

Individual i = A.createIndividual(NS+"nameOfIndividual");

次に、次のようにプロパティを追加します。

i.addProperty(Property prop, "value");

ただし、この方法では、すべての値が文字列リテラルとして追加され、そのプロパティに定義された範囲への参照はありません。さらに、クラス レベルの階層が 2 レベルを超える場合、ベース データ型プロパティにドリルダウンするのは難しいと思います。

4

1 に答える 1

1

のメソッド シグネチャResource.addPropertyがオーバーロードされています。Javadocを見ると、次のポリモーフィック バリアントがあることがわかります。

  • addProperty(Property p, RDFNode o)
  • addProperty(Property p, String o)
  • addProperty(Property p, String lexicalForm, RDFDatatype datatype)
  • addProperty(Property p, String o, String l)

文字列を取る 2 番目のバリアントは、リテラル値を追加するのに便利です。最初のバリアントはあなたが望むものです。RDFNodeは 、Resourceしたがって 、IndividualなどのスーパータイプですOntClass。したがって、必要なことは、すでに RDFNode である値を渡すことだけです。

これを具体的にするために、2 人の個人間の関係を主張する方法を次に示します。

Individual ai = A.createIndividual(NS+"nameOfIndividualInA");
Individual bi = B.createIndividual(NS+"nameOfIndividualInB");

Property p = A.createObjectProperty( NS + "p" );

// either
ai.addProperty( p, bi );

// or
model.add( ai, p, bi );

アップデート

コメントに応えて、これらの API メソッドを呼び出すさまざまな方法を示す実際の実行可能なコードを次に示します。

package examples;

import java.util.Calendar;

import com.hp.hpl.jena.datatypes.TypeMapper;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.*;

public class AddPropertiesExample
{
    public static final String NS = "http://example.com/test#";

    private Property p;
    private Resource r, s;

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

    public void run() {
        Model m = createModel();

        // add property values using the Model.add( Resource, Property, RDFNode) signature
        m.add( r, p, s );
        m.add( r, p, ResourceFactory.createPlainLiteral( "foo" ) );
        m.add( r, p, ResourceFactory.createLangLiteral( "le foo", "fr" ) );
        m.add( r, p, ResourceFactory.createTypedLiteral( 42 ) );
        m.add( r, p, ResourceFactory.createTypedLiteral( Calendar.getInstance() ));

        // ditto using the Model.add( Resource, Property, String ) signature
        m.add( r, p, "This is a plain literal" );

        // ditto using the Model.add( Resource, Property, String, String ) signature
        m.add( r, p, "Das foo", "de" );

        // ditto using the Model.add( Resource, Property, String, RDFDatatype ) signature
        m.add( r, p, "42.42", XSDDatatype.XSDfloat );
        m.add( r, p, "2000-01-01",
               TypeMapper.getInstance().getTypeByName( "http://www.w3.org/2001/XMLSchema#date" ) );

        m.write( System.out, "Turtle" );
    }

    private Model createModel() {
        Model m = ModelFactory.createOntologyModel();

        p = m.createProperty( NS + "p" );
        r = m.createResource( NS + "r" );
        s = m.createResource( NS + "s" );

        return m;
    }
}

出力:

@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://example.com/test#r>
    <http://example.com/test#p>  "2000-01-01"^^xsd:date , 
         "42.42"^^xsd:float , 
         "Das foo"@de , 
         "This is a plain literal" , 
         "2013-10-28T11:46:35.596Z"^^xsd:dateTime , 
         "42"^^xsd:int , 
         "le foo"@fr , 
         "foo" ;
    <http://example.com/test#p>  <http://example.com/test#s> .
于 2013-10-18T07:57:39.743 に答える