Mercurial > repos > mikel-egana-aranguren > sparql_galaxy
annotate src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java @ 0:137f9a4a6337 draft
First version to init the repo, still README etc to add but it works
author | mikel-egana-aranguren |
---|---|
date | Thu, 25 Oct 2012 12:17:40 -0400 |
parents | |
children | b8bf1af83841 |
rev | line source |
---|---|
0
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
1 package es.cbgp.galaxy.sparql.jena; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
2 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
3 import java.io.BufferedReader; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
4 import java.io.FileReader; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
5 import java.util.Iterator; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
6 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
7 import com.hp.hpl.jena.ontology.OntModel; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
8 import com.hp.hpl.jena.query.*; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
9 import com.hp.hpl.jena.shared.Lock; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
10 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
11 public class SPARQLQueryEngine { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
12 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
13 private OntModel queryModel; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
14 private String sparqlFile; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
15 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
16 public SPARQLQueryEngine(OntModel om) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
17 this.queryModel = om; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
18 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
19 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
20 public String executeQuery() throws Exception { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
21 String finalQuery = loadQueryFromFile(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
22 // System.out.println("Final query: " + finalQuery); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
23 Query query = null; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
24 QueryExecution qexec = null; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
25 try { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
26 queryModel.enterCriticalSection(Lock.READ); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
27 query = QueryFactory.create(finalQuery); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
28 qexec = QueryExecutionFactory.create(query, queryModel); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
29 ResultSet results = qexec.execSelect(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
30 String res = ""; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
31 while (results.hasNext()) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
32 QuerySolution qs = results.next(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
33 Iterator<String> vars = qs.varNames(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
34 while (vars.hasNext()) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
35 String var = vars.next(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
36 if (!qs.getResource(var).isAnon()) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
37 res += "?" + var + "\t" + qs.getResource(var) + "\n"; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
38 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
39 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
40 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
41 return res; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
42 } catch (Exception e) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
43 e.printStackTrace(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
44 } finally { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
45 queryModel.leaveCriticalSection(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
46 if (qexec != null) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
47 qexec.close(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
48 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
49 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
50 return null; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
51 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
52 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
53 private String loadQueryFromFile() throws Exception { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
54 String query = ""; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
55 BufferedReader bL = new BufferedReader(new FileReader(this.sparqlFile)); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
56 while (bL.ready()) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
57 query += bL.readLine(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
58 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
59 bL.close(); |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
60 return query; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
61 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
62 |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
63 public void setQueryFile(String sparqlFile) { |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
64 this.sparqlFile = sparqlFile; |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
65 } |
137f9a4a6337
First version to init the repo, still README etc to add but it works
mikel-egana-aranguren
parents:
diff
changeset
|
66 } |