のメソッド シグネチャ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> .