1 | /* |
---|
2 | * To change this license header, choose License Headers in Project Properties. |
---|
3 | * To change this template file, choose Tools | Templates |
---|
4 | * and open the template in the editor. |
---|
5 | */ |
---|
6 | |
---|
7 | package org.biohackathon.SPARQLBuilder.OWL; |
---|
8 | |
---|
9 | import com.hp.hpl.jena.query.*; |
---|
10 | import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP; |
---|
11 | import java.util.*; |
---|
12 | |
---|
13 | /** |
---|
14 | * |
---|
15 | * @author atsuko |
---|
16 | */ |
---|
17 | |
---|
18 | /// Currently, this class is not used. Maybe it should be removed... |
---|
19 | |
---|
20 | public class EndpointAccess { |
---|
21 | static public boolean checkPath(Path path, String sparqlEndpoint){ |
---|
22 | return checkPath(path.getStartClass(), path.getClassLinks(), sparqlEndpoint); |
---|
23 | } |
---|
24 | |
---|
25 | static public boolean checkPath(String startClass, List<ClassLink> classlinks, String sparqlEndpoint){ |
---|
26 | // SPARQL Query construction |
---|
27 | StringBuilder queryStr = new StringBuilder(); |
---|
28 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
29 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
30 | queryStr.append("ASK { \n"); |
---|
31 | |
---|
32 | ListIterator<ClassLink> cit = classlinks.listIterator(); |
---|
33 | queryStr.append("?r0").append(" rdf:type <").append(startClass).append("> .\n"); |
---|
34 | int i = 0; |
---|
35 | while( cit.hasNext() ){ |
---|
36 | ClassLink link = cit.next(); |
---|
37 | //String next = link.getLinkedClassURI(); |
---|
38 | String sval = "?r".concat(Integer.toString(i)); |
---|
39 | String oval = "?r".concat(Integer.toString(i+1)); |
---|
40 | queryStr.append(oval).append(" rdf:type <").append(link.getLinkedClassURI()).append("> .\n"); |
---|
41 | if( link.getDirection() == Direction.forward ){ |
---|
42 | queryStr.append(sval).append(" <").append(link.getPropertyURI()).append("> ") |
---|
43 | .append(oval).append(" .\n"); |
---|
44 | }else{ |
---|
45 | queryStr.append(oval).append(" <").append(link.getPropertyURI()).append("> ") |
---|
46 | .append(sval).append(" .\n"); |
---|
47 | } |
---|
48 | i++; |
---|
49 | } |
---|
50 | queryStr.append("} \n"); |
---|
51 | |
---|
52 | String sparqlQuery = queryStr.toString(); |
---|
53 | Query query = QueryFactory.create(sparqlQuery, Syntax.syntaxARQ); |
---|
54 | QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, query); |
---|
55 | boolean res = qexec.execAsk(); |
---|
56 | qexec.close(); |
---|
57 | return res; |
---|
58 | } |
---|
59 | |
---|
60 | static public boolean checkSimplePath(List<Integer> nodes, OWLClassGraph ocg, String sparqlEndpoint){ |
---|
61 | // SPARQL Query construction |
---|
62 | StringBuilder queryStr = new StringBuilder(); |
---|
63 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
64 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
65 | queryStr.append("ASK { \n"); |
---|
66 | |
---|
67 | ListIterator<Integer> cit = nodes.listIterator(); |
---|
68 | queryStr.append("?r0").append(" rdf:type <").append(ocg.labels.get(cit.next())).append("> .\n"); |
---|
69 | int i = 0; |
---|
70 | while( cit.hasNext() ){ |
---|
71 | String cURI = ocg.labels.get(cit.next()); |
---|
72 | //String next = link.getLinkedClassURI(); |
---|
73 | String sval = "?r".concat(Integer.toString(i)); |
---|
74 | String pval = "?p".concat(Integer.toString(i)); |
---|
75 | String oval = "?r".concat(Integer.toString(i+1)); |
---|
76 | queryStr.append(oval).append(" rdf:type <").append(cURI).append("> .\n"); |
---|
77 | queryStr.append("{{"); |
---|
78 | queryStr.append(sval).append(" ").append(pval).append(" ") |
---|
79 | .append(oval).append(" }\n UNION\n {"); |
---|
80 | queryStr.append(oval).append(" ").append(pval).append(" ") |
---|
81 | .append(sval).append(" }}\n"); |
---|
82 | i++; |
---|
83 | } |
---|
84 | queryStr.append("} \n"); |
---|
85 | |
---|
86 | String sparqlQuery = queryStr.toString(); |
---|
87 | System.out.println("Ask Simple"); |
---|
88 | System.out.println(sparqlQuery); |
---|
89 | Query query = QueryFactory.create(sparqlQuery, Syntax.syntaxARQ); |
---|
90 | /* |
---|
91 | QueryEngineHTTP httpQuery = new QueryEngineHTTP(sparqlEndpoint, query); |
---|
92 | boolean res = httpQuery.execAsk(); |
---|
93 | httpQuery.close(); |
---|
94 | */ |
---|
95 | QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, query); |
---|
96 | boolean res = qexec.execAsk(); |
---|
97 | qexec.close(); |
---|
98 | return res; |
---|
99 | } |
---|
100 | |
---|
101 | static public boolean check3SimplePath(Integer node1, Integer node2, Integer node3, OWLClassGraph ocg, String sparqlEndpoint){ |
---|
102 | // SPARQL Query construction |
---|
103 | StringBuilder queryStr = new StringBuilder(); |
---|
104 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
105 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
106 | queryStr.append("ASK { \n"); |
---|
107 | |
---|
108 | queryStr.append("?n1縲rdf:type ?c1 .\n"); |
---|
109 | queryStr.append("?n2縲rdf:type ?c2 .\n"); |
---|
110 | queryStr.append("?n3縲rdf:type ?c3 .\n"); |
---|
111 | |
---|
112 | queryStr.append("{{"); |
---|
113 | queryStr.append("?n1").append(" ").append("?p1").append(" ") |
---|
114 | .append("?n2").append(" }\n UNION\n {"); |
---|
115 | queryStr.append("?n2").append(" ").append("?p1").append(" ") |
---|
116 | .append("?n1").append(" }}\n"); |
---|
117 | |
---|
118 | queryStr.append("{{"); |
---|
119 | queryStr.append("?n2").append(" ").append("?p2").append(" ") |
---|
120 | .append("?n3").append(" }\n UNION\n {"); |
---|
121 | queryStr.append("?n3").append(" ").append("?p2").append(" ") |
---|
122 | .append("?n2").append(" }}\n"); |
---|
123 | |
---|
124 | queryStr.append("VALUES (?c1 ?c2 ?c3){ \n"); |
---|
125 | queryStr.append("<").append(ocg.labels.get(node1)).append("> \n"); |
---|
126 | queryStr.append("<").append(ocg.labels.get(node2)).append("> \n"); |
---|
127 | queryStr.append("<").append(ocg.labels.get(node3)).append("> \n"); |
---|
128 | queryStr.append("} \n"); |
---|
129 | queryStr.append("} \n"); |
---|
130 | |
---|
131 | String sparqlQuery = queryStr.toString(); |
---|
132 | System.out.println("Ask Simple3"); |
---|
133 | System.out.println(sparqlQuery); |
---|
134 | Query query = QueryFactory.create(sparqlQuery, Syntax.syntaxARQ); |
---|
135 | QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, query); |
---|
136 | boolean res = qexec.execAsk(); |
---|
137 | qexec.close(); |
---|
138 | return res; |
---|
139 | } |
---|
140 | |
---|
141 | static public boolean check3SimplePathwithJoin(Integer node1, Integer node2, Integer node3, OWLClassGraph ocg, String sparqlEndpoint){ |
---|
142 | StringBuilder queryStr = new StringBuilder(); |
---|
143 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
144 | queryStr.append("SELECT DISTINCT ?n2 { \n"); |
---|
145 | |
---|
146 | queryStr.append("?n1").append(" rdf:type <").append(ocg.labels.get(node1)).append("> .\n"); |
---|
147 | queryStr.append("?n2").append(" rdf:type <").append(ocg.labels.get(node2)).append("> .\n"); |
---|
148 | |
---|
149 | queryStr.append("{{"); |
---|
150 | queryStr.append("?n1").append(" ").append("?p1").append(" ") |
---|
151 | .append("?n2").append(" }\n UNION\n {"); |
---|
152 | queryStr.append("?n2").append(" ").append("?p1").append(" ") |
---|
153 | .append("?n1").append(" }}\n"); |
---|
154 | |
---|
155 | queryStr.append("} \n"); |
---|
156 | |
---|
157 | String sparqlQuery = queryStr.toString(); |
---|
158 | System.out.println("SELECT Simple3 by Join"); |
---|
159 | System.out.println(sparqlQuery); |
---|
160 | Query query = QueryFactory.create(sparqlQuery, Syntax.syntaxARQ); |
---|
161 | QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, query); |
---|
162 | ResultSet res1 = qexec.execSelect(); |
---|
163 | qexec.close(); |
---|
164 | |
---|
165 | queryStr = new StringBuilder(); |
---|
166 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
167 | queryStr.append("SELECT DISTINCT ?n2 { \n"); |
---|
168 | |
---|
169 | queryStr.append("?n2").append(" rdf:type <").append(ocg.labels.get(node2)).append("> .\n"); |
---|
170 | queryStr.append("?n3").append(" rdf:type <").append(ocg.labels.get(node3)).append("> .\n"); |
---|
171 | |
---|
172 | queryStr.append("{{"); |
---|
173 | queryStr.append("?n2").append(" ").append("?p2").append(" ") |
---|
174 | .append("?n3").append(" }\n UNION\n {"); |
---|
175 | queryStr.append("?n3").append(" ").append("?p2").append(" ") |
---|
176 | .append("?n2").append(" }}\n"); |
---|
177 | |
---|
178 | queryStr.append("} \n"); |
---|
179 | |
---|
180 | sparqlQuery = queryStr.toString(); |
---|
181 | System.out.println("SELECT Simple3 by Join 2"); |
---|
182 | System.out.println(sparqlQuery); |
---|
183 | query = QueryFactory.create(sparqlQuery, Syntax.syntaxARQ); |
---|
184 | qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, query); |
---|
185 | ResultSet res2 = qexec.execSelect(); |
---|
186 | qexec.close(); |
---|
187 | |
---|
188 | Set<String> res1hash = new HashSet<String>(); |
---|
189 | while(res1.hasNext()){ |
---|
190 | String resstr = res1.next().toString(); |
---|
191 | res1hash.add(resstr); |
---|
192 | } |
---|
193 | while(res2.hasNext()){ |
---|
194 | if ( res1hash.contains(res2.next().toString())){ |
---|
195 | return true; |
---|
196 | } |
---|
197 | } |
---|
198 | return false; |
---|
199 | } |
---|
200 | } |
---|