Vous êtes sur la page 1sur 17

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

SPARQLWorkingwithJena
HowtoCheckforWellformed(syntacticallycorrect)N3.

package javajenatest; import import import import import import com.hp.hpl.jena.rdf.model.InfModel; com.hp.hpl.jena.rdf.model.Model; com.hp.hpl.jena.rdf.model.ModelFactory; com.hp.hpl.jena.reasoner.ValidityReport; com.hp.hpl.jena.util.FileManager; java.util.Iterator;

/** * * @author Frank Coyle * Illustrates loading N3 and using a try-catch block to report errors * and cleanly terminate your program */ public class JenaSyntaxCheck { public static void main(String [] args) {

Model myRawModel = null; // define outside try-catch String urlString = "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3test/wilson.n3"; boolean isBadParse = false; //. assume it's ok, prove otherwise try { // Here we load from a public URL myRawModel = FileManager.get().loadModel(urlString, "N3"); } catch (Exception e) { System.out.println("EXCEPTION in parsing the N3.\n" + e.getMessage() + "\n at URL: " + urlString); isBadParse = true; } if (isBadParse) { System.out.println("Terminating program"); return; } // write the model to System.out (or any output stream) // if no 2nd parameter, the output is RDF/XML myRawModel.write(System.out, "N3"); // it's sometimews helpful to add a final print line just to see that // our processing has completed System.out.println("< -- Jena Done -->"); } }

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

OUTPUT: ASK result is true ---------------

SELECT
package jenasemweb;

import import import import import import import import import import import import import import

com.hp.hpl.jena.query.QueryExecution; com.hp.hpl.jena.query.QueryExecutionFactory; com.hp.hpl.jena.query.QueryFactory; com.hp.hpl.jena.query.QuerySolution; com.hp.hpl.jena.query.ResultSet; com.hp.hpl.jena.rdf.model.InfModel; com.hp.hpl.jena.rdf.model.Model; com.hp.hpl.jena.rdf.model.ModelFactory; com.hp.hpl.jena.rdf.model.Property; com.hp.hpl.jena.rdf.model.Resource; com.hp.hpl.jena.rdf.model.Statement; com.hp.hpl.jena.rdf.model.StmtIterator; com.hp.hpl.jena.util.FileManager; com.hp.hpl.jena.util.PrintUtil;

/** * * @author Frank Coyle * This program illustrates how one can use the SPARQL CONSTRUCT feature * as a rule system to create new triples. The new triples become a new model * that we add to our existing model which lets us execute queries that we * would not be able to execute without the new triple. */ public class SparqlQuery03 { public static void main(String [] args) { // LOAD Raw Model from URL Model myRawModel = FileManager.get().loadModel( "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/Houghland.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/cruz.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/abraham.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/akundi.v2.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/bennett.n3", "N3"); // READ another N3 into the Model

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/hanna.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/liew_hw2.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/ramani.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/rawal.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/sison.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/tara.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/turney.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/wilson.n3", "N3"); // Create an RDFS inference model from the Raw Model InfModel infmodel = ModelFactory.createRDFSModel(myRawModel); // Create a new SPARQL query String queryString = "PREFIX drc: <http://www.codesupreme.com/onto/cse7392/#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "SELECT DISTINCT ?lname ?fname " + // space after last ?var "WHERE {" + " ?who foaf:lastName ?lname." + " ?who foaf:firstName ?fname." + " }" + "ORDER BY ?lname"; // create a Jena query from the queryString com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString); // create a Jena QueryExecution object that knows the query // and the N3 over which the query will be run QueryExecution qe = QueryExecutionFactory.create(query, infmodel);

// execute the query - get back a ResultSet ResultSet results = qe.execSelect(); // iterate over the result set while(results.hasNext()) { QuerySolution sol = results.next(); System.out.println("Solution:" + sol.toString() ); }

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

} } Output: Solution:( ?lname = "Abraham" ) ( ?fname = "Sherwin" ) Solution:( ?lname = "Bennett" ) ( ?fname = "Steve" ) Solution:( ?lname = "Bennett" ) ( ?fname = "Steven" ) Solution:( ?lname = "Collins"^^<http://www.w3.org/2001/XMLSchemastring> ) ( ?fname = "Melanie"^^<http://www.w3.org/2001/XMLSchemastring> ) Solution:( ?lname = "Connery" ) ( ?fname = "Sean" ) Solution:( ?lname = "Cook" ) ( ?fname = "Norman" ) Solution:( ?lname = "Cook" ) ( ?fname = "Quentin" ) Solution:( ?lname = "Coyle" ) ( ?fname = "Frank" ) Solution:( ?lname = "Coyle"^^<http://www.w3.org/2001/XMLSchemastring> ) ( ?fname = "Frank"^^<http://www.w3.org/2001/XMLSchemastring> ) Solution:( ?lname = "Cruz" ) ( ?fname = "Tegan" ) Solution:( ?lname = "Cruz" ) ( ?fname = "Roger" ) Solution:( ?lname = "De Niro" ) ( ?fname = "Robert" ) Solution:( ?lname = "De Niro" ) ( ?fname = "Robert"^^xsd:String ) Solution:( ?lname = "DiCaprio" ) ( ?fname = "Leonardo" ) Solution:( ?lname = "Hanna"^^<http://www.w3.org/2001/XMLSchemastring> ) ( ?fname = "Michael" ) Solution:( ?lname = "Hanna"^^<http://www.w3.org/2001/XMLSchemastring> ) ( ?fname = "Mike"^^<http://www.w3.org/2001/XMLSchemastring> ) Solution:( ?lname = "Hougland" ) ( ?fname = "Jack" ) Solution:( ?lname = "Keaton" ) ( ?fname = "Michael" ) Solution:( ?lname = "Kilmer" ) ( ?fname = "Val" ) Solution:( ?lname = "Liew" ) ( ?fname = "Kar" ) Solution:( ?lname = "Lord"^^<http://www.w3.org/2001/XMLSchemastring> ) ( ?fname = "Jack"^^<http://www.w3.org/2001/XMLSchemastring> ) Solution:( ?lname = "Meyers" ) ( ?fname = "Michael" ) Solution:( ?lname = "Meyers" ) ( ?fname = "Mike" ) Solution:( ?lname = "Pfeiffer" ) ( ?fname = "Michelle" ) Solution:( ?lname = "Presley" ) ( ?fname = "Elvis" ) Solution:( ?lname = "Rawal" ) ( ?fname = "Ruchi"^^xsd:String ) Solution:( ?lname = "Roquebrune" ) ( ?fname = "Micheline" ) Solution:( ?lname = "Sison" ) ( ?fname = "Raymond" ) Solution:( ?lname = "Turney" ) ( ?fname = "Kyle" ) Solution:( ?lname = "Walken" ) ( ?fname = "Christopher" ) Solution:( ?lname = "Walken" ) ( ?fname = "Chris" ) Solution:( ?lname = "Walken" ) ( ?fname = "Georgianne" ) Solution:( ?lname = "Walken"^^<http://www.w3.org/2001/XMLSchemastring> ) ( ?fname = "Georgianne"^^<http://www.w3.org/2001/XMLSchemastring> ) Solution:( ?lname = "Walken"^^<http://www.w3.org/2001/XMLSchemastring> ) ( ?fname = "Christopher"^^<http://www.w3.org/2001/XMLSchemastring> ) Solution:( ?lname = "Wilson" ) ( ?fname = "Justin" ) Solution:( ?lname = "Wood" ) ( ?fname = "Natalie" )

---------------------

HieuTran'sDirectoryReadSolution
package jenasemweb; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.InfModel; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.util.FileManager; import com.hp.hpl.jena.util.PrintUtil; import java.net.*; import java.io.*; import java.util.*; /** * * @author Hieu Tran */ public class SparqlQueryFromMultiN3 { public static void main(String [] args) { String urlPath = "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/"; // create List of all the filenames in directory List<String> fileNames = createListOfFilesFromDirectory(urlPath); if(!fileNames.isEmpty()) { // LOAD Raw Model from URL Model myRawModel = FileManager.get().loadModel(urlPath + fileNames.get(0),"N3"); for (int i=1; i< fileNames.size(); i++) { // READ another N3 into the Model try { FileManager.get().readModel(myRawModel, urlPath + fileNames.get(i), "N3"); } catch(Exception e) { System.out.println("Error read model:" + e.getMessage() + " "+ fileNames.get(i)); } } // Create an RDFS inference model from the Raw Model InfModel infmodel = ModelFactory.createRDFSModel(myRawModel); // Create a new SPARQL query String queryString = "PREFIX "PREFIX "prefix "prefix "prefix : <http://www.codesupreme.com/onto/cse7392/#> " + foaf: <http://xmlns.com/foaf/0.1/> " + rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + foaf: <http://xmlns.com/foaf/0.1/> " +

"prefix xsd: "prefix dc:

<http://www.w3.org/2001/XMLSchema#> " + <http://purl.org/dc/elements/1.1/#> " + "WHERE {" +

"SELECT distinct ?firstName ?lastName ?age " + " ?person foaf:firstName ?firstName;" + " foaf:lastName ?lastName; " + " foaf:age ?age;" + " foaf:knows ?y. " + " ?y foaf:lastName ?ylastname ." + " FILTER (?age > 30 && ?ylastname = \"Walken\") ." + " } Order by (?firstname)";

// create a Jena query from the queryString com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString); // create a Jena QueryExecution object that knows the query // and the N3 over which the query will be run QueryExecution qe = QueryExecutionFactory.create(query, infmodel); // execute the query - get back a ResultSet ResultSet results = qe.execSelect(); // iterate over the result set while(results.hasNext()) { QuerySolution sol = (QuerySolution)results.next(); System.out.println("Solution:" + sol.toString() ); } } } /* Read the information from URL line by line, extract the file name from it, and store the file name into the list */ public static List<String> createListOfFilesFromDirectory(String urlPath) { List<String> fileNames =new ArrayList<String>(); try { URL url = new URL(urlPath); URLConnection connection = url.openConnection(); connection.setDoInput(true); InputStream inStream = connection.getInputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(inStream)); String line = ""; while ((line = input.readLine()) != null) String str = getFileName(line); if (!str.equals("")) { fileNames.add(str); } } } catch (Exception e) { System.out.println(e.toString()); } return fileNames; } /* Extract the n3 file name from the line. */ private static String getFileName(String str) { int i = str.indexOf(".n3");

if (i > 0) { str = str.substring(0, i+3); str = str.substring(str.lastIndexOf('\"')+1,str.length()); } else { str = ""; } return str; } } -------------- output run: Solution:( ?firstName Solution:( ?firstName Solution:( ?firstName Solution:( ?firstName ----------= = = = "Georgianne" ) ( ?lastName = "Walken" ) ( ?age = 71 ) "Norman" ) ( ?lastName = "Cook" ) ( ?age = 46 ) "Quentin" ) ( ?lastName = "Cook" ) ( ?age = 46 ) "Sean" ) ( ?lastName = "Connery" ) ( ?age = 79 )

DESCRIBE
package jenasemweb;

import import import import import import import import import import import import import import

com.hp.hpl.jena.query.QueryExecution; com.hp.hpl.jena.query.QueryExecutionFactory; com.hp.hpl.jena.query.QueryFactory; com.hp.hpl.jena.query.QuerySolution; com.hp.hpl.jena.query.ResultSet; com.hp.hpl.jena.rdf.model.InfModel; com.hp.hpl.jena.rdf.model.Model; com.hp.hpl.jena.rdf.model.ModelFactory; com.hp.hpl.jena.rdf.model.Property; com.hp.hpl.jena.rdf.model.Resource; com.hp.hpl.jena.rdf.model.Statement; com.hp.hpl.jena.rdf.model.StmtIterator; com.hp.hpl.jena.util.FileManager; com.hp.hpl.jena.util.PrintUtil;

/** * * @author Frank Coyle * This program illustrates how one can use the SPARQL CONSTRUCT feature * as a rule system to create new triples. The new triples become a new model * that we add to our existing model which lets us execute queries that we * would not be able to execute without the new triple. */ public class SparqlDescribe01 { public static void main(String [] args) { // LOAD Raw Model from URL Model myRawModel = FileManager.get().loadModel( "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/Houghland.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/cruz.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/abraham.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/akundi.v2.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/bennett.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/hanna.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/liew_hw2.n3", "N3");

// READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/ramani.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/rawal.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/sison.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/tara.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/turney.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/wilson.n3", "N3"); // Create an RDFS inference model from the Raw Model InfModel infmodel = ModelFactory.createRDFSModel(myRawModel); // // // // Use SPARQL CONSTRUCT to create a new Model using a reverse rule format i.e. IF drc likes someone AND that someone likes guitar THEN that someone is a musicfriend of drc String queryString = "PREFIX drc: <http://www.codesupreme.com/onto/cse7392/#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "DESCRIBE ?x " + "WHERE {" + "{?x foaf:lastName \"Walken\" }" + " UNION {?x foaf:lastName \"walken\"} " + " }" + ""; // create a Jena CONSTRUCT query from the queryString com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString); // create a Jena QueryExecution object that knows the query // and the N3 over which the query will be run QueryExecution qe = QueryExecutionFactory.create(query, infmodel); // executing the CONSTRUCT gives us a new mdoel with triples // that satisfy the WHERE clause Model newRawModel = qe.execDescribe(); // take a look at the new model newRawModel.write(System.out, "N3");

} }

------------ output -----------------run: @prefix @prefix @prefix @prefix @prefix @prefix @prefix @prefix _:b1

: dc: rdfs: foaf: dct: xsd: rdf: drc:

<http://www.codesupreme.com/onto/cse7392/#> . <http://purl.org/dc/elements/1.1/> . <http://www.w3.org/2000/01/rdf-schema#> . <http://xmlns.com/foaf/0.1/> . <http://purl.org/dc/dcmitype/> . <http://www.w3.org/2001/XMLSchema#> . <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . <http://www.codesupreme.com/onto/cse7392/#> .

a drc:Song , drc:Art , drc:Product , drc:Video , rdfs:Resource ; drc:appelation "Weapon of Choice" ; drc:availableAt <http://www.youtube.com/watch?v=K7Ky5R-vxns> . a foaf:Person , foaf:person , rdfs:Resource ; drc:IsMarriedTo _:b3 ; foaf:firstName "Georgianne" ; foaf:knows _:b3 ; foaf:lastName "Walken" . a foaf:Person , foaf:person , rdfs:Resource ; drc:coStar drc:NatalieWood ; foaf:firstName "Christopher" ; foaf:interest drc:acting ; foaf:knows drc:NatalieWood ; foaf:lastName "Walken" ; foaf:nick "Chris" .

_:b2

[]

_:b4 a foaf:Person , drc:JobTitle , foaf:person , rdfs:Resource , drc:Artist , drc:Occupation , drc:Actor ; drc:actedIn drc:TheDeparted , drc:CatchMeIfYouCan , drc:ShutterIsland , drc:TheAviator ; drc:actedWith _:b5 ; drc:numOscarsNominated 3 ; drc:numOscarsWon 0 ; foaf:age "35" ; foaf:currentProject drc:LowDweller ; foaf:firstName "Leonardo" ; foaf:gender "male" ; foaf:img <http://www.imdb.com/rg/action-boxname/headshot/media/rm1025018624/nm0000138> ; foaf:knows _:b5 ; foaf:lastName "DiCaprio" . [] a foaf:Person , drc:JobTitle , foaf:person , rdfs:Resource , drc:Artist , drc:Occupation , drc:Actor ; drc:appearedIn _:b1 ; drc:hasChildren "false"^^xsd:boolean ; drc:produces _:b1 ; foaf:familyName "Walken" ; foaf:firstName "Christopher" , "Chris" ; foaf:gender "male" ; foaf:givenName "Christopher" , "Chris" ; foaf:lastName "Walken" ; foaf:nick "Chris" ; foaf:surname "Walken" . _:b6 a foaf:Person , foaf:person , rdfs:Resource ; drc:marriedTo _:b7 ; foaf:firstName "Christopher" ;

foaf:knows _:b7 ; foaf:lastName "Walken" ; foaf:nick "Chris" . _:b5 a foaf:Person , drc:JobTitle , foaf:person , rdfs:Resource , drc:Artist , drc:Occupation , drc:Actor ; drc:actedIn drc:CatchMeIfYouCan , drc:TheDeerHunter ; drc:actedWith _:b4 ; drc:numOscarsNominated 2 ; drc:numOscarsWon 1 ; drc:oscarNominee drc:CatchMeIfYouCan ; drc:oscarWinner drc:TheDeerHunter ; foaf:age "67" ; foaf:currentProject drc:GirlAndAGun ; foaf:firstName "Christopher" ; foaf:gender "male" ; foaf:img <http://en.wikipedia.org/wiki/File:ChristopherWalkenFeb08.jpg> ; foaf:interest drc:Elvis ; foaf:knows _:b4 ; foaf:lastName "Walken" ; foaf:nick "Chris" . _:b7 a foaf:Person , foaf:person , rdfs:Resource ; drc:marriedTo _:b6 ; foaf:age 71 ; foaf:based_near "New York, NY" ; foaf:birthday "1939" ; foaf:firstName "Georgianne" ; foaf:gender "female" ; foaf:givenName "Thon" ; foaf:image <http://mccevents.net/awardchristopherandgeorgiannewalken.jpg> ; foaf:interest "acting" ; foaf:knows _:b6 ; foaf:lastName "Walken" ; foaf:title "Mrs" . a foaf:Person , foaf:person , rdfs:Resource ; drc:ActedIn drc:TheAndersonTapes , drc:CatchMeIfYouCan , drc:SaturdayNightLive ; drc:IsMarriedTo _:b2 ; drc:NumberOfAcademyAwards 1 ; foaf:firstName "Christopher" ; foaf:knows _:b2 , _:b8 ; foaf:lastName "Walken" ; foaf:nick "Chris" . a foaf:Person , foaf:person , rdfs:Resource ; drc:marriedTo _:b10 ; foaf:firstName "Georgianne" ; foaf:gender "female" ; foaf:knows _:b10 ; foaf:lastName "Walken" . a foaf:Person , foaf:person , rdfs:Resource ; drc:ActedIn drc:TheAndersonTapes , drc:YouOnlyLiveTwice , drc:NeverSayNeverAgain drc:IsMarriedTo [ a foaf:Person , foaf:person , rdfs:Resource ; drc:IsMarriedTo _:b8 ; foaf:firstName "Micheline" ; foaf:knows _:b8 ; foaf:lastName "Roquebrune"

_:b3

_:b9

_:b8 ;

] ; drc:NumberOfAcademyAwards 1 ; foaf:age 79 ; foaf:currentProject drc:SirBilli ; foaf:firstName "Sean" ; foaf:gender drc:Male ; foaf:homepage <http://www.seanconnery.com/> ; foaf:img <http://upload.wikimedia.org/wikipedia/commons/c/c5/SeanConnery88.jpg> ; foaf:interest drc:Golf , drc:Producing , drc:Acting ; foaf:knows _:b3 ; foaf:lastName "Connery" ; foaf:member drc:ScottishNationalParty ; foaf:pastProject drc:JamesBond , drc:TheRock ; foaf:status "Awesome" ; foaf:title "Sir" . _:b10 a foaf:Person , foaf:person , rdfs:Resource ; drc:currentOccupation drc:Actor ; drc:marriedTo _:b9 ; foaf:firstName "Christopher" , "Chris" ; foaf:gender "male" ; foaf:givenName "Ronald" ; foaf:knows _:b9 ; foaf:lastName "Walken" ; foaf:nick "Chris" ; foaf:pastProject <http://www.imdb.com/title/tt0103776/> . _:b11 a foaf:Person , foaf:person , rdfs:Resource ; foaf:firstName "Michael" ; foaf:lastName "Keaton" ; foaf:previousProject drc:BatmanReturns , drc:Batman . _:b12 a foaf:Person , foaf:person , rdfs:Resource ; foaf:firstName "Christopher" ; foaf:knows _:b13 ; foaf:lastName "Walken" ; foaf:nick "Chris" . _:b14 a foaf:Person , foaf:person , rdfs:Resource ; foaf:age "47" ; foaf:currentProject <http://lyle.smu.edu/stevenb/#MarvintheMartian> ; foaf:familyName "Meyers" ; foaf:firstName "Michael" , "Mike" ; foaf:gender "male" ; foaf:img <http://www.thecinemasource.com/moviesdb/images/news/mike-myersinglorious-bastards.jpg> ; foaf:interest <http://lyle.smu.edu/stevenb/#acting> ; foaf:knows _:b15 ; foaf:lastName "Meyers" ; foaf:pastProject <http://lyle.smu.edu/stevenb/#WaynesWorld2> . _:b15 a foaf:Person , foaf:person , rdfs:Resource ; <http://lyle.smu.edu/stevenb/#actor>

<http://lyle.smu.edu/stevenb/#TheDeerHunter> , <http://lyle.smu.edu/stevenb/#WaynesWorld2> , <http://lyle.smu.edu/stevenb/#PulpFiction> ; foaf:age "67" ; foaf:currentProject <http://lyle.smu.edu/stevenb/#GirlAndAGun> ; foaf:firstName "Christopher" ; foaf:gender "male" ; foaf:img <http://alertageral.files.wordpress.com/2008/12/walken_speaks.jpg> ; foaf:knows _:b14 ; foaf:lastName "Walken" ; foaf:nick "Chris" . _:b16 a foaf:Person , foaf:person , rdfs:Resource ; foaf:firstName "Christopher" ; foaf:knows _:b17 ; foaf:lastName "Walken" ; foaf:nick "Chris" ; foaf:previousProject drc:TheIrishman . _:b17 a foaf:Person , foaf:person , rdfs:Resource ; foaf:birthday "1959-12-31"^^xsd:date ; foaf:currentProject drc:Riddle ; foaf:faimlyName "Kilmer" ; foaf:firstName "Val" ; foaf:knows _:b11 , _:b16 ; foaf:lastName "Kilmer" ; foaf:previousProject drc:TheIrishman ; foaf:topic_interest drc:LedZepplin . _:b13 a foaf:Person , foaf:person , rdfs:Resource ; foaf:age "45" ; foaf:birthday "07-22" ; foaf:familyName "Spade" ; foaf:firstName "David" ; foaf:gender "male" ; foaf:homepage <http://www.davidspade.com> ; foaf:img <http://stupidcelebrities.net/wp-content/david_spade1.jpg> ; foaf:interest "tv" , "charity" , "movies" ; foaf:knows _:b12 ; foaf:schoolHomepage "http://www.asu.edu/" ; foaf:title "Mr" . BUILD SUCCESSFUL (total time: 2 seconds)

ASK
package jenasemweb;

import import import import import import import import import import import import import import

com.hp.hpl.jena.query.QueryExecution; com.hp.hpl.jena.query.QueryExecutionFactory; com.hp.hpl.jena.query.QueryFactory; com.hp.hpl.jena.query.QuerySolution; com.hp.hpl.jena.query.ResultSet; com.hp.hpl.jena.rdf.model.InfModel; com.hp.hpl.jena.rdf.model.Model; com.hp.hpl.jena.rdf.model.ModelFactory; com.hp.hpl.jena.rdf.model.Property; com.hp.hpl.jena.rdf.model.Resource; com.hp.hpl.jena.rdf.model.Statement; com.hp.hpl.jena.rdf.model.StmtIterator; com.hp.hpl.jena.util.FileManager; com.hp.hpl.jena.util.PrintUtil;

/** * * @author Frank Coyle * This program illustrates how one can use the SPARQL CONSTRUCT feature * as a rule system to create new triples. The new triples become a new model * that we add to our existing model which lets us execute queries that we * would not be able to execute without the new triple. */ public class SparqlAsk01 { public static void main(String [] args) { // LOAD Raw Model from URL Model myRawModel = FileManager.get().loadModel( "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/Houghland.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/cruz.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/abraham.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/akundi.v2.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/bennett.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/hanna.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/liew_hw2.n3", "N3");

// READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/ramani.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/rawal.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/sison.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/tara.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/turney.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/onto/a3/wilson.n3", "N3"); // Create an RDFS inference model from the Raw Model InfModel infmodel = ModelFactory.createRDFSModel(myRawModel); // // // // Use SPARQL CONSTRUCT to create a new Model using a reverse rule format i.e. IF drc likes someone AND that someone likes guitar THEN that someone is a musicfriend of drc String queryString = "PREFIX drc: <http://www.codesupreme.com/onto/cse7392/#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "ASK {" + "{?x foaf:lastName \"Walken\" }" + " UNION {?x foaf:lastName \"walken\"} " + " }" + ""; // create a Jena CONSTRUCT query from the queryString com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString); // create a Jena QueryExecution object that knows the query // and the N3 over which the query will be run QueryExecution qe = QueryExecutionFactory.create(query, infmodel); // executing the CONSTRUCT gives us a new mdoel with triples // that satisfy the WHERE clause boolean isFound = qe.execAsk(); System.out.println("ASK result is " + isFound);

} } --------- output -------ASK result is true

CONSTRUCT
package jenasemweb;

import import import import import import import import import import import import import import

com.hp.hpl.jena.query.QueryExecution; com.hp.hpl.jena.query.QueryExecutionFactory; com.hp.hpl.jena.query.QueryFactory; com.hp.hpl.jena.query.QuerySolution; com.hp.hpl.jena.query.ResultSet; com.hp.hpl.jena.rdf.model.InfModel; com.hp.hpl.jena.rdf.model.Model; com.hp.hpl.jena.rdf.model.ModelFactory; com.hp.hpl.jena.rdf.model.Property; com.hp.hpl.jena.rdf.model.Resource; com.hp.hpl.jena.rdf.model.Statement; com.hp.hpl.jena.rdf.model.StmtIterator; com.hp.hpl.jena.util.FileManager; com.hp.hpl.jena.util.PrintUtil;

/** * * @author Frank Coyle * This program illustrates how one can use the SPARQL CONSTRUCT feature * as a rule system to create new triples. The new triples become a new model * that we add to our existing model which lets us execute queries that we * would not be able to execute without the new triple. */ public class SparqlConstruct01 { public static void main(String [] args) { // LOAD Raw Model from URL Model myRawModel = FileManager.get().loadModel( "http://lyle.smu.edu/~coyle/cse7392.semweb/code/rollo.n3", "N3"); // READ another N3 into the Model FileManager.get().readModel(myRawModel, "http://lyle.smu.edu/~coyle/cse7392.semweb/code/marla.n3", "N3"); // Create an RDFS inference model from the Raw Model InfModel infmodel = ModelFactory.createRDFSModel(myRawModel); // // // // Use SPARQL CONSTRUCT to create a new Model using a reverse rule format i.e. IF drc likes someone AND that someone likes guitar THEN that someone is a musicfriend of drc String queryString = "PREFIX drc: <http://www.codesupreme.com/onto/cse7392/#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "CONSTRUCT { drc:drc drc:musicfriend ?y }" + "WHERE {" + " drc:drc drc:likes ?y ." + " ?y drc:likes drc:guitar ." + " }"; // create a Jena CONSTRUCT query from the queryString com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);

// create a Jena QueryExecution object that knows the query // and the N3 over which the query will be run QueryExecution qe = QueryExecutionFactory.create(query, infmodel); // executing the CONSTRUCT gives us a new mdoel with triples // that satisfy the WHERE clause Model newRawModel = qe.execConstruct(); // take a look at the new model newRawModel.write(System.out, "N3"); // NOW, add the statements generated by the CONSTRUCT // to the original model - expanding it with new statements infmodel.add(newRawModel); // look at the new expanded model infmodel.write(System.out, "N3"); // Create a new SPARQL query queryString = "PREFIX drc: <http://www.codesupreme.com/onto/cse7392/#> " + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "SELECT ?y " + // gotcha: MUST be a space after last ?var "WHERE {" + " drc:drc drc:musicfriend ?y ." + " }"; // create a Jena query from the queryString query = QueryFactory.create(queryString); // create a Jena QueryExecution object that knows the query // and the N3 over which the query will be run qe = QueryExecutionFactory.create(query, infmodel);

// execute the query - get back a ResultSet ResultSet results = qe.execSelect(); // iterate over the result set while(results.hasNext()) { QuerySolution sol = results.next(); System.out.println("Solution:" + sol.toString() ); }

} }