2010-10-20 17 views
2

我有instance1class1instance2class2。另外我在本體中定義了HasName(object property)。現在,我怎樣才能通過jena將三元組(instance1 HasName instance2)添加到我的本體中?我怎樣才能通過Jena爲我的本體添加一些三元組?

+0

根據記錄,顯然是「耶拿」是耶拿語義框架,看到http://jena.sourceforge.net/ontology/ – 2010-10-20 07:18:41

回答

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 

subjectpredicateobject與符合接口ResourceFactory.createStatement()類型的三重的實例中的元素。

8

這是一種不涉及中間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 

你也可以在builder-ish模式中鏈接這些調用中的一部分。

Resource instance2 = model.createResource(instance2Uri).addProperty(RDF.type, class2); 
model.createResource(instance1Uri).addProperty(RDF.type, class1).addProperty(hasName, instance2); 
+0

+1:我喜歡的建設者模式作風建設,我沒有看到之前! :) – sharky 2010-11-17 22:16:43

+0

嗨。答案很了不起,但我確實有疑問。使用Statement創建三元組並使用資源作爲上述解決方案有什麼區別?請爲我澄清這一點。謝謝 – 2014-07-28 09:39:29