comparison src/es/cbgp/galaxy/sparql/jena/SPARQLQueryEngine.java @ 2:b8bf1af83841 draft

Paste query added, query engine improved, examples added
author Mikel Egana Aranguren <mikel-egana-aranguren@toolshed.g2.bx.psu.edu>
date Wed, 04 Dec 2013 08:17:00 +0100
parents 137f9a4a6337
children
comparison
equal deleted inserted replaced
1:117a4b4c002d 2:b8bf1af83841
2 2
3 import java.io.BufferedReader; 3 import java.io.BufferedReader;
4 import java.io.FileReader; 4 import java.io.FileReader;
5 import java.util.Iterator; 5 import java.util.Iterator;
6 6
7 import com.hp.hpl.jena.ontology.OntModel;
8 import com.hp.hpl.jena.query.*; 7 import com.hp.hpl.jena.query.*;
8 import com.hp.hpl.jena.rdf.model.Model;
9 import com.hp.hpl.jena.rdf.model.RDFNode;
9 import com.hp.hpl.jena.shared.Lock; 10 import com.hp.hpl.jena.shared.Lock;
10 11
11 public class SPARQLQueryEngine { 12 public class SPARQLQueryEngine {
12 13
13 private OntModel queryModel; 14 private Model queryModel;
14 private String sparqlFile; 15 private String sparqlFile;
15 16
16 public SPARQLQueryEngine(OntModel om) { 17 public SPARQLQueryEngine(Model om) {
17 this.queryModel = om; 18 this.queryModel = om;
18 } 19 }
19 20
20 public String executeQuery() throws Exception { 21 public String executeQuery() throws Exception {
21 String finalQuery = loadQueryFromFile(); 22 String finalQuery = loadQueryFromFile();
31 while (results.hasNext()) { 32 while (results.hasNext()) {
32 QuerySolution qs = results.next(); 33 QuerySolution qs = results.next();
33 Iterator<String> vars = qs.varNames(); 34 Iterator<String> vars = qs.varNames();
34 while (vars.hasNext()) { 35 while (vars.hasNext()) {
35 String var = vars.next(); 36 String var = vars.next();
36 if (!qs.getResource(var).isAnon()) { 37 res += "?" + var + "\t" + getValue(qs, var) + "\r\n";
37 res += "?" + var + "\t" + qs.getResource(var) + "\n"; 38
38 }
39 } 39 }
40 } 40 }
41 return res; 41 return res;
42 } catch (Exception e) { 42 } catch (Exception e) {
43 e.printStackTrace(); 43 e.printStackTrace();
46 if (qexec != null) { 46 if (qexec != null) {
47 qexec.close(); 47 qexec.close();
48 } 48 }
49 } 49 }
50 return null; 50 return null;
51 }
52
53 private String getValue(QuerySolution qs, String var) {
54 RDFNode n = qs.get(var);
55 return n.toString();
56
57 // System.out.println("RDFNode (" + var + "): " + n);
58 // try {
59 // if (!qs.getResource(var).isAnon()) {
60 // if (qs.getResource(var).isResource()) {
61 // return qs.getResource(var).toString();
62 // }
63 // if (qs.getResource(var).isLiteral()) {
64 // return qs.getLiteral(var).toString();
65 // }
66 // }
67 // } catch (Exception e) {
68 //
69 // }
70 // try {
71 // if (!qs.getLiteral(var).isAnon()) {
72 // if (qs.getResource(var).isResource()) {
73 // return qs.getResource(var).toString();
74 // }
75 // if (qs.getResource(var).isLiteral()) {
76 // return qs.getLiteral(var).toString();
77 // }
78 // }
79 // } catch (Exception e) {
80 //
81 // }
82 // return "Error";
51 } 83 }
52 84
53 private String loadQueryFromFile() throws Exception { 85 private String loadQueryFromFile() throws Exception {
54 String query = ""; 86 String query = "";
55 BufferedReader bL = new BufferedReader(new FileReader(this.sparqlFile)); 87 BufferedReader bL = new BufferedReader(new FileReader(this.sparqlFile));