更具体地说,这是为了表示诸如“约翰说汽车是蓝色的”这样的情况,而在当前本体中不一定有“汽车是蓝色的”。我的代码必须在之后检查它。我可能对如何使用 OWL2 Annotation Axiom 有所了解。但是,我对如何使用 RDF 语句在 Jena 中执行此操作感到困惑。
问问题
556 次
1 回答
1
语句的具体化是具有主体、属性和对象的资源,并且具有类型语句。您可以使用模型来创建语句(使用createStatement),而无需将该语句添加到模型中。然后,您可以根据该语句获取ReifiedStatement(使用createReifiedStatement )。这是创建必要资源的 Jena 代码,然后创建语句
car hasColor blue
不将其添加到模型中,创建该x
语句的具体化(确实将三元组添加到模型中),最后添加该语句
john says x
到模型。
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ReifiedStatement;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
public class ReificationExample {
public static void main(String[] args) {
final String NS = "http://stackoverflow.com/questions/19526223/";
final Model model = ModelFactory.createDefaultModel();
final Resource john = model.createResource( NS+"john" );
final Resource car = model.createResource( NS+"car" );
final Property hasColor = model.createProperty( NS+"hasColor" );
final Property says = model.createProperty( NS+"says" );
final Resource blue = model.createResource( NS+"blue" );
// creating a statement doesn't add it to the model
final Statement stmt = model.createStatement( car, hasColor, blue );
// creating a reified statement does add some triples to the model, but
// not the triple [car hasColor blue].
final ReifiedStatement rstmt = model.createReifiedStatement( stmt );
// john says rstmt
model.add( john, says, rstmt );
model.write( System.out, "TTL", null ); // or "RDF/XML", etc.
}
}
乌龟输出:
<http://stackoverflow.com/questions/19526223/john>
<http://stackoverflow.com/questions/19526223/says>
[ a <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#object>
<http://stackoverflow.com/questions/19526223/blue> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>
<http://stackoverflow.com/questions/19526223/hasColor> ;
<http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>
<http://stackoverflow.com/questions/19526223/car>
] .
RDF/XML 输出:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://stackoverflow.com/questions/19526223/">
<rdf:Description rdf:about="http://stackoverflow.com/questions/19526223/john">
<j.0:says>
<rdf:Statement>
<rdf:object rdf:resource="http://stackoverflow.com/questions/19526223/blue"/>
<rdf:predicate rdf:resource="http://stackoverflow.com/questions/19526223/hasColor"/>
<rdf:subject rdf:resource="http://stackoverflow.com/questions/19526223/car"/>
</rdf:Statement>
</j.0:says>
</rdf:Description>
</rdf:RDF>
于 2013-10-22T21:46:13.100 回答