Search code examples
javardfjenasemantics

How to seperate one RDF model into two models in Jena?


Now I have a RDF data, which contain two resources(I don't know whether it is correct to call the staff in rdf:description a resource), Now I want to separate the two resource into two rdf data in Jena, I do not know how to use the API to do it, The data example:

<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>

I try to work like that:

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

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

but I don't know how to quickly add the resource to another model.


Solution

  • Once you have the subject, you can use listProperties to get a StmtIterator over the triples with that subject, and then you can use Model#add(StmtIterator) to add all those triples to a new 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>