Intersection two graphs using Jena

Submitted by admin on Tue, 10/14/2014 - 22:39

<code>
package fr.student.team.example;

import java.io.InputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.util.FileManager;

public class Operations {

static final String inputFileName = "/Users/student/Documents/workspace/testing_jena/src/testing_jena/file.ttl";
static final String inputFileName2 = "/Users/student/Documents/workspace/testing_jena/src/testing_jena/file2.ttl";

public static void main(String[] args) {
// TODO Auto-generated method stub
Model model = ModelFactory.createDefaultModel();
InputStream in = FileManager.get().open(inputFileName);
Model results = model.read(in, "", "TTL");

Model model2 = ModelFactory.createDefaultModel();
InputStream in2 = FileManager.get().open(inputFileName2);
Model results2 = model2.read(in2, "", "TTL");

Model Intersection = results.intersection(results2);

Intersection.write(System.out, "TTL");

}

}

</code>

<code>
file.ttl
</code>
<code>
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;.
@prefix contact: <http://www.w3.org/2000/10/swap/pim/contact#&gt;.

<http://www.w3.org/People/EM/contact#me&gt;
rdf:type contact:Person;
contact:fullName "Eric Miller";
contact:mailbox <mailto:em@w3.org&gt;;
contact:personalTitle "Dr.".
</code>

Etiquetas