在模型中没有该语句的模型中声明的声明?

时间:2013-10-22 19:06:11

标签: jena reification

更具体地说,这是代表诸如“约翰说汽车是蓝色的”的情况,而不必在当前的本体中具有“汽车是蓝色的”。我的代码之后必须检查它。我可能知道如何使用OWL2 Annotation Axiom。但是,我对如何在Jena中使用RDF语句感到困惑。

1 个答案:

答案 0 :(得分:1)

语句的具体化是具有主语,属性和对象的资源,并且具有类型语句。您可以使用Model创建Statement(使用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>