【问题标题】:How to seperate one RDF model into two models in Jena?如何在耶拿将一个 RDF 模型分成两个模型?
【发布时间】:2016-08-14 19:24:13
【问题描述】:

现在我有一个RDF数据,里面包含两个资源(不知道在rdf:description中叫staff是不是正确的一个资源),现在我想把这两个资源分成Jena中的两个rdf数据,不知道怎么用API来做,数据示例:

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:obs="http://localhost/SensorSchema/ontology#" > 
<rdf:Description rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5">
    <obs:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">65.00999166666666</obs:hasLatitude>
    <obs:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1365156000000</obs:hasDate>
    <obs:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int">212</obs:hasDirection>
    <obs:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double">28.0</obs:hasVelocity>
    <obs:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasAcceleration>
    <obs:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">25.46780833333333</obs:hasLongitude>
    <obs:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int">38</obs:hasArea>
    <obs:hasDateTime>2013-04-05T13:00:00</obs:hasDateTime>
    <obs:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int">51709293</obs:hasSender>
    <rdf:type rdf:resource="http://localhost/SensorSchema/ontology#Observation"/>
    <obs:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</obs:hasID>
    <obs:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasDistance>
</rdf:Description>
<rdf:Description rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666">
    <obs:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">65.00999166666666</obs:hasLatitude>
    <obs:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1365156000000</obs:hasDate>
    <obs:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int">500</obs:hasDirection>
    <obs:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double">28.0</obs:hasVelocity>
    <obs:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasAcceleration>
    <obs:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">25.46780833333333</obs:hasLongitude>
    <obs:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int">38</obs:hasArea>
    <obs:hasDateTime>2013-04-05T13:00:00</obs:hasDateTime>
    <obs:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int">51709293</obs:hasSender>
    <rdf:type rdf:resource="http://localhost/SensorSchema/ontology#Observation"/>
    <obs:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</obs:hasID>
    <obs:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasDistance>
</rdf:Description>
</rdf:RDF>

我尝试这样工作:

ResIterator iter= OriginalModel.listSubjects();
int i=0;
    while(iter.hasNext()) {

        Resource subject = iter.next();
        Model[i].?? // add the whole resource
        i++;
}

但我不知道如何快速将资源添加到另一个模型。

【问题讨论】:

    标签: java rdf jena semantics


    【解决方案1】:

    一旦你有了主题,你就可以使用 listProperties 来获得一个 StmtIterator 覆盖具有该主题的三元组,然后你可以使用 Model#add( StmtIterator) 将所有这些三元组添加到新的 Model

    public void splitModels() throws IOException {
        // First, create a model and read the content 
        // into it.  You probably already have this part, 
        // but we need it for a working example.
        Model model = ModelFactory.createDefaultModel();
        try (InputStream in = SplitModelExample.class.getResourceAsStream("/example.rdf")) {
            RDFDataMgr.read(model, in, Lang.RDFXML);
        }
    
        // List the subjects in the model.
        ResIterator subjects = model.listSubjects();
    
        // For each subject, create another empty model that will 
        // contain the triples of which the subject is the subject.
        // The #listProperties() method returns a StmtIterator over
        // those triples, and Model#add(StmtIterator) adds all the
        // triples to a model.  Then we'll print out each submodel
        // to make sure we're getting what we expect.
        while (subjects.hasNext()) {
            Resource subject = subjects.next();
            Model subModel = ModelFactory.createDefaultModel();
            subModel.add(subject.listProperties());
    
            System.out.println("\n<!-- Submodel for "+subject+". -->");
            RDFDataMgr.write(System.out, subModel, Lang.RDFXML);
        }
    }
    
    <!-- Submodel for http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666. -->
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:j.0="http://localhost/SensorSchema/ontology#">
      <j.0:Observation rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666">
        <j.0:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >25.46780833333333</j.0:hasLongitude>
        <j.0:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >65.00999166666666</j.0:hasLatitude>
        <j.0:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >28.0</j.0:hasVelocity>
        <j.0:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >1</j.0:hasID>
        <j.0:hasDateTime>2013-04-05T13:00:00</j.0:hasDateTime>
        <j.0:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
        >1365156000000</j.0:hasDate>
        <j.0:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >0.0</j.0:hasDistance>
        <j.0:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >51709293</j.0:hasSender>
        <j.0:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >0.0</j.0:hasAcceleration>
        <j.0:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >38</j.0:hasArea>
        <j.0:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >500</j.0:hasDirection>
      </j.0:Observation>
    </rdf:RDF>
    
    
    <!-- Submodel for http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5. -->
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:j.0="http://localhost/SensorSchema/ontology#">
      <j.0:Observation rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5">
        <j.0:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >65.00999166666666</j.0:hasLatitude>
        <j.0:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
        >1365156000000</j.0:hasDate>
        <j.0:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >212</j.0:hasDirection>
        <j.0:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >28.0</j.0:hasVelocity>
        <j.0:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >0.0</j.0:hasAcceleration>
        <j.0:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >25.46780833333333</j.0:hasLongitude>
        <j.0:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >38</j.0:hasArea>
        <j.0:hasDateTime>2013-04-05T13:00:00</j.0:hasDateTime>
        <j.0:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >51709293</j.0:hasSender>
        <j.0:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
        >1</j.0:hasID>
        <j.0:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
        >0.0</j.0:hasDistance>
      </j.0:Observation>
    </rdf:RDF>
    

    【讨论】:

    • 抱歉回复晚了,这个答案很好用!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多