3

私はinstance1class1instance2持っていclass2ます。HasName(object property)また、オントロジーで定義しました。さて、どうすればトリプル ( instance1 HasName instance2) を jena のオントロジーに追加できますか?

4

2 に答える 2

12

これは、中間を扱わない方法Statementsです。

// RDF Nodes -- you can make these immutable in your own vocabulary if you want -- see Jena's RDFS, RDF, OWL, etc vocabularies
Resource class1 = ResourceFactory.createResource(yourNamespace + "class1");
Resource class2 = ResourceFactory.createResource(yourNamespace + "class1");
Property hasName = ResourceFactory.createProperty(yourNamespace, "hasName"); // hasName property

// The RDF Model
Model model = ... // Use your preferred method to get an OntModel, InfModel, or just regular Model

Resource instance1 = model.createResource(instance1Uri);
Resource instance2 = model.createResource(instance2Uri);

// Create statements
instance1.addProperty(RDF.type, class1); // Classification of instance1
instance2.addProperty(RDF.type, class2); // Classification of instance2
instance1.addProperty(hasName, instance2); // Edge between instance1 and instance2

これらの呼び出しのいくつかをビルダー風のパターンで連鎖させることもできます。

Resource instance2 = model.createResource(instance2Uri).addProperty(RDF.type, class2);
model.createResource(instance1Uri).addProperty(RDF.type, class1).addProperty(hasName, instance2);
于 2010-11-17T06:12:55.237 に答える
2

Jena では、これはStatementのインスタンス(トリプルまたはクワッド) を作成し、そのステートメントをModelのインスタンスにコミットすることで実行できます。

たとえば、次のことを考慮してください。

OntModel model = ModelFactory.createOntologyModel(); // an ont model instance
...
Statement s = ResourceFactory.createStatement(subject, predicate, object);
model.add(s); // add the statement (triple) to the model

ここsubjectで、predicateとは、 ResourceFactory.createStatement()objectのインターフェースに準拠する型を持つトリプルのインスタンス要素です。

于 2010-11-17T05:54:00.377 に答える