/** * This sample shows the results of union, intersection, and * difference operations. */ //Import every class and interface by name. import java.io.PrintWriter; import java.io.OutputStream; import java.io.FileOutputStream; //The Dublin Core import com.hp.hpl.mesa.rdf.jena.vocabulary.DC; //Use the memory model implementation import com.hp.hpl.mesa.rdf.jena.mem.ModelMem; //Model package imports import com.hp.hpl.mesa.rdf.jena.model.Model; import com.hp.hpl.mesa.rdf.jena.model.Resource; import com.hp.hpl.mesa.rdf.jena.model.Property; import com.hp.hpl.mesa.rdf.jena.model.Statement; import com.hp.hpl.mesa.rdf.jena.model.StmtIterator; import com.hp.hpl.mesa.rdf.jena.model.RDFNode; import com.hp.hpl.mesa.rdf.jena.model.RDFException; public class DublinCoreEx3{ String space=" "; Model model1=null,model2=null; Model mod_union=null,mod_diff=null, mod_inter=null; public DublinCoreEx3() throws RDFException { } public Model createSimpleDC(String subjectUri) throws RDFException { Model model=new ModelMem(); Resource publication=model.createResource(subjectUri); return model; } /** * Add the creator property to the provided subject. */ public void addCreator(Resource subject, String creatorUri) throws RDFException{ subject.addProperty(DC.creator,creatorUri); } public Model createUnion(Model mod1,Model mod2) throws RDFException { return mod1.union(mod2); } /** * The order of the arguments should not be important. */ public Model createIntersection(Model mod1, Model mod2) throws RDFException { return mod1.intersection(mod2); } /** * Note that the order of arguments is important. This measures * the difference of the second model from the first. So if the * first model is a superset of the second, there is no differnece. */ public Model createDifference(Model mod1, Model mod2) throws RDFException { return mod1.difference(mod2); } /** * Prints the model out as RDF XML */ public void printRDFXML(Model model, OutputStream out) throws RDFException { model.write(new PrintWriter(out),"RDF/XML-ABBREV"); } public static void main(String[] args) throws Exception{ String creatorUri1="http://cgl.indiana.edu/people/GCF"; String creatorUri2="http://cgl.indiana.edu/people/MEP"; String creatorUri3="http://cgl.indiana.edu/people/SHK"; String subjectUri1="http://cgl.indiana.edu/publications/Paper1"; String subjectUri2="http://cgl.indiana.edu/publications/Paper2"; DublinCoreEx3 dc=new DublinCoreEx3(); Model mod1=dc.createSimpleDC(subjectUri1); dc.addCreator(mod1.getResource(subjectUri1),creatorUri1); dc.addCreator(mod1.getResource(subjectUri1),creatorUri2); dc.printRDFXML(mod1,System.out); Model mod2=dc.createSimpleDC(subjectUri2); dc.addCreator(mod2.getResource(subjectUri2),creatorUri1); dc.addCreator(mod2.getResource(subjectUri2),creatorUri3); dc.printRDFXML(mod2,System.out); Model union=dc.createUnion(mod1,mod2); System.out.println("Union:"); dc.printRDFXML(union,System.out); //Mod2 intersects the union Model inter1=dc.createIntersection(union,mod2); System.out.println("Intersection:1"); dc.printRDFXML(inter1,System.out); //Mod1 and mod2 have no intersection although they //do have a property in common. Model inter2=dc.createIntersection(mod1,mod2); System.out.println("Intersection2:"); dc.printRDFXML(inter2,System.out); //The intersection operation works for either order Model inter3=dc.createIntersection(mod2,union); System.out.println("Intersection3:"); dc.printRDFXML(inter3,System.out); //Here, publication 2 is not in mod1, so that is the //difference. Model diff1=dc.createDifference(union,mod1); System.out.println("Difference1"); dc.printRDFXML(diff1,System.out); //Union is a superset of mod1, so to mod1, there is no //difference. Model diff2=dc.createDifference(mod1,union); System.out.println("Difference2"); dc.printRDFXML(diff2,System.out); Model diff3=dc.createDifference(mod1,mod2); System.out.println("Difference3"); dc.printRDFXML(diff3,System.out); } }