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.www; |
---|
8 | |
---|
9 | import java.io.IOException; |
---|
10 | import java.io.PrintWriter; |
---|
11 | import java.util.ArrayList; |
---|
12 | import java.util.Arrays; |
---|
13 | import java.util.Iterator; |
---|
14 | import java.util.List; |
---|
15 | import java.util.logging.Level; |
---|
16 | import java.util.logging.Logger; |
---|
17 | import javax.servlet.ServletException; |
---|
18 | import javax.servlet.annotation.WebServlet; |
---|
19 | import javax.servlet.http.HttpServlet; |
---|
20 | import javax.servlet.http.HttpServletRequest; |
---|
21 | import javax.servlet.http.HttpServletResponse; |
---|
22 | import org.biohackathon.SPARQLBuilder.OWL.*; |
---|
23 | import org.json.JSONArray; |
---|
24 | import org.json.JSONException; |
---|
25 | import org.json.JSONObject; |
---|
26 | |
---|
27 | //import javax.json.Json; |
---|
28 | |
---|
29 | /** |
---|
30 | * |
---|
31 | * @author atsuko |
---|
32 | */ |
---|
33 | @WebServlet(name = "SPServlet", urlPatterns = {"/sparql"}) |
---|
34 | public class SPServlet extends HttpServlet { |
---|
35 | |
---|
36 | /** |
---|
37 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
---|
38 | * methods. |
---|
39 | * |
---|
40 | * @param request servlet request |
---|
41 | * @param response servlet response |
---|
42 | * @throws ServletException if a servlet-specific error occurs |
---|
43 | * @throws IOException if an I/O error occurs |
---|
44 | */ |
---|
45 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
---|
46 | throws ServletException, IOException { |
---|
47 | response.setContentType("text/html;charset=UTF-8"); |
---|
48 | try (PrintWriter out = response.getWriter()) { |
---|
49 | /* TODO output your page here. You may use following sample code. */ |
---|
50 | out.println("<!DOCTYPE html>"); |
---|
51 | out.println("<html>"); |
---|
52 | out.println("<head>"); |
---|
53 | out.println("<title>Servlet SPServlet</title>"); |
---|
54 | out.println("</head>"); |
---|
55 | out.println("<body>"); |
---|
56 | out.println("<h1>Servlet SPServlet at " + request.getContextPath() + "</h1>"); |
---|
57 | out.println("</body>"); |
---|
58 | out.println("</html>"); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
---|
63 | /** |
---|
64 | * Handles the HTTP <code>GET</code> method. |
---|
65 | * |
---|
66 | * @param request servlet request |
---|
67 | * @param response servlet response |
---|
68 | * @throws ServletException if a servlet-specific error occurs |
---|
69 | * @throws IOException if an I/O error occurs |
---|
70 | */ |
---|
71 | @Override |
---|
72 | /* protected void doGet(HttpServletRequest request, HttpServletResponse response) |
---|
73 | throws ServletException, IOException { |
---|
74 | processRequest(request, response); |
---|
75 | }*/ |
---|
76 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
---|
77 | throws ServletException, IOException { |
---|
78 | //processRequest(request, response); |
---|
79 | response.setContentType("application/json;charset=UTF-8"); |
---|
80 | response.setHeader("Access-Control-Allow-Origin", "*"); |
---|
81 | response.setHeader("Access-Control-Allow-Methods", "GET"); |
---|
82 | response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers")); |
---|
83 | response.setHeader("Access-Control-Max-Age", "-1"); |
---|
84 | PrintWriter out = response.getWriter(); |
---|
85 | String jpath = request.getParameter("path"); |
---|
86 | System.out.println("Path"); |
---|
87 | System.out.println(jpath); |
---|
88 | Path path = null; |
---|
89 | try { |
---|
90 | path = convertJ2Path(jpath); |
---|
91 | } catch (JSONException ex) { |
---|
92 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
---|
93 | } |
---|
94 | String query = null; |
---|
95 | try { |
---|
96 | query = convertPath2SPARQL(path); |
---|
97 | } catch (Exception ex) { |
---|
98 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
---|
99 | } |
---|
100 | out.print(query); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * Handles the HTTP <code>POST</code> method. |
---|
107 | * |
---|
108 | * @param request servlet request |
---|
109 | * @param response servlet response |
---|
110 | * @throws ServletException if a servlet-specific error occurs |
---|
111 | * @throws IOException if an I/O error occurs |
---|
112 | */ |
---|
113 | @Override |
---|
114 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
115 | throws ServletException, IOException { |
---|
116 | processRequest(request, response); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Returns a short description of the servlet. |
---|
121 | * |
---|
122 | * @return a String containing servlet description |
---|
123 | */ |
---|
124 | @Override |
---|
125 | public String getServletInfo() { |
---|
126 | return "Short description"; |
---|
127 | }// </editor-fold> |
---|
128 | |
---|
129 | private Path convertJ2Path(String jpath) throws JSONException{ |
---|
130 | |
---|
131 | JSONObject object = new JSONObject(jpath); |
---|
132 | |
---|
133 | // int width = Integer.parseInt(object.getJSONObject("width").toString()); |
---|
134 | JSONArray classLinks = object.getJSONArray("classLinks"); |
---|
135 | JSONObject jsonObject; |
---|
136 | List <ClassLink> list = new ArrayList<ClassLink>(); |
---|
137 | |
---|
138 | for (int i=0;i<classLinks.length();i++) |
---|
139 | { |
---|
140 | jsonObject = classLinks.getJSONObject(i); |
---|
141 | |
---|
142 | String direction=jsonObject.getString("direction"); |
---|
143 | Direction myDirection = null; |
---|
144 | if (direction.equals(Direction.forward.toString())) |
---|
145 | myDirection=Direction.forward; |
---|
146 | else if (direction.equals(Direction.reverse.toString())) |
---|
147 | myDirection=Direction.reverse; |
---|
148 | else if (direction.equals(Direction.both.toString())) |
---|
149 | myDirection=Direction.both; |
---|
150 | |
---|
151 | |
---|
152 | String linkedLiteralDatatypeURI=null; |
---|
153 | linkedLiteralDatatypeURI = jsonObject.getString("linkedLiteralDatatypeURI"); |
---|
154 | String linkedClassURI = jsonObject.getString("linkedClass"); |
---|
155 | String propertyURI = jsonObject.getString("predicate"); |
---|
156 | |
---|
157 | // int numOfLinks = Integer.parseInt(jsonObject.getJSONObject("numOfLinks").toString()); |
---|
158 | // int numOfLinkedInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedInstances").toString()); |
---|
159 | // int numOfOriginInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
---|
160 | // int numOfOriginClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
---|
161 | // int numOfLinkedClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedClassInstances").toString()); |
---|
162 | |
---|
163 | // ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, null, |
---|
164 | // numOfLinks, numOfOriginInstances, numOfLinkedInstances, |
---|
165 | // numOfOriginClassInstances, numOfLinkedClassInstances, |
---|
166 | // false, false); |
---|
167 | ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, myDirection, |
---|
168 | 0, 0, 0, |
---|
169 | 0, 0, |
---|
170 | false, false); |
---|
171 | System.out.println(classLink.getDirection().toString()); |
---|
172 | list.add(classLink); |
---|
173 | } |
---|
174 | String startClass = object.getString("startClass"); |
---|
175 | Path path = new Path(startClass, list, 0); |
---|
176 | |
---|
177 | return path; |
---|
178 | } |
---|
179 | |
---|
180 | private String convertPath2SPARQL(Path path) throws Exception{ |
---|
181 | |
---|
182 | ArrayList<String> classname =new ArrayList<String>() ; |
---|
183 | |
---|
184 | if( path == null ){ |
---|
185 | throw new Exception("Path is null."); |
---|
186 | } |
---|
187 | String startClass = path.getStartClass(); |
---|
188 | List<ClassLink> classLinks = path.getClassLinks(); |
---|
189 | |
---|
190 | StringBuffer queryStr = new StringBuffer(); |
---|
191 | StringBuffer selStr = new StringBuffer(); |
---|
192 | StringBuffer whereStr = new StringBuffer(); |
---|
193 | // if(num==0){ |
---|
194 | int num = classLinks.size(); |
---|
195 | // } |
---|
196 | |
---|
197 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
198 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
199 | |
---|
200 | selStr.append("SELECT "); |
---|
201 | whereStr.append("WHERE { \n"); |
---|
202 | |
---|
203 | String properties = null; |
---|
204 | String objectClasses = null; |
---|
205 | String subjectClasses = null; |
---|
206 | Direction direction = null; |
---|
207 | int i = 0; |
---|
208 | int k = 0; |
---|
209 | for (ClassLink link :classLinks ) |
---|
210 | { |
---|
211 | properties = link.getPropertyURI(); |
---|
212 | objectClasses = link.getLinkedClassURI(); |
---|
213 | direction = link.getDirection(); |
---|
214 | |
---|
215 | if (i==0) |
---|
216 | subjectClasses = startClass; |
---|
217 | |
---|
218 | classname.add(subjectClasses); |
---|
219 | selStr.append("?c").append(i).append(" "); |
---|
220 | selStr.append("?l").append(i).append(" "); |
---|
221 | |
---|
222 | if(i == classLinks.size()) |
---|
223 | selStr.append("\n"); |
---|
224 | |
---|
225 | |
---|
226 | whereStr.append("?c").append(i). |
---|
227 | append(" rdf:type "). |
---|
228 | append("<"). |
---|
229 | append(subjectClasses). |
---|
230 | append(">"). |
---|
231 | append(".\n"); |
---|
232 | |
---|
233 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
234 | |
---|
235 | |
---|
236 | if(direction == Direction.forward) |
---|
237 | { |
---|
238 | whereStr.append("?c").append(i).append(" "); |
---|
239 | whereStr.append("<").append(properties).append("> "); |
---|
240 | whereStr.append("?c").append(i+1).append(".\n"); |
---|
241 | } |
---|
242 | else |
---|
243 | { |
---|
244 | whereStr.append("?c").append(i+1).append(" "); |
---|
245 | whereStr.append("<").append(properties).append("> "); |
---|
246 | whereStr.append("?c").append(i).append(".\n"); |
---|
247 | } |
---|
248 | |
---|
249 | subjectClasses = objectClasses; |
---|
250 | i++; |
---|
251 | k++; |
---|
252 | if(k>=num){ |
---|
253 | break; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | selStr.append("?c").append(i).append(" \n"); |
---|
258 | selStr.append("?l").append(i).append(" \n"); |
---|
259 | whereStr.append("?c").append(i).append(" rdf:type "). |
---|
260 | append("<"). |
---|
261 | append(subjectClasses). |
---|
262 | append(">"). |
---|
263 | append(".\n"); |
---|
264 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
265 | classname.add(subjectClasses); |
---|
266 | |
---|
267 | |
---|
268 | queryStr.append(selStr).append(whereStr); |
---|
269 | |
---|
270 | queryStr.append("}"); |
---|
271 | //OPTIONAL |
---|
272 | queryStr.append("LIMIT 100\n"); |
---|
273 | |
---|
274 | System.out.println(queryStr); |
---|
275 | |
---|
276 | //rewrite Sparql |
---|
277 | |
---|
278 | ArrayList<String> classname2 = new ArrayList<String>(); |
---|
279 | for(int index=0;index<classname.size();index++){ |
---|
280 | String tmp=classname.get(index); |
---|
281 | |
---|
282 | int mark; |
---|
283 | if((mark=tmp.indexOf("#"))!=-1) |
---|
284 | classname2.add(tmp.substring(mark+1)); |
---|
285 | else classname2.add(tmp.substring(tmp.indexOf("/")+1)); |
---|
286 | |
---|
287 | } |
---|
288 | String query=queryStr.toString(); |
---|
289 | for(int index=0;index<classname2.size();index++){ |
---|
290 | String original="c"+index; |
---|
291 | query= query.replaceAll(original, classname2.get(index)); |
---|
292 | } |
---|
293 | query= query.replaceAll("\\?l","\\?label"); |
---|
294 | System.out.println(query); |
---|
295 | return query; |
---|
296 | |
---|
297 | |
---|
298 | } |
---|
299 | private static String rewriteSparql(String query){ |
---|
300 | StringBuffer tmp=new StringBuffer(query); |
---|
301 | int index= tmp.indexOf("WHERE"); |
---|
302 | int begin=0,cnt=0; |
---|
303 | while(begin<index) |
---|
304 | { |
---|
305 | begin= tmp.indexOf("?c", begin); |
---|
306 | cnt++; |
---|
307 | |
---|
308 | } |
---|
309 | return null; |
---|
310 | } |
---|
311 | private static List<String> convertJ2Path2(String jpath) throws JSONException{ |
---|
312 | List <String> list = null; |
---|
313 | String temp =(String) jpath.subSequence(2, jpath.length()-2); |
---|
314 | // if (temp.contains(","")) |
---|
315 | list =Arrays.asList(temp.split("\",\"")); |
---|
316 | return list; |
---|
317 | |
---|
318 | /* JSONArray classLinks=new JSONArray(jpath); |
---|
319 | String string; |
---|
320 | |
---|
321 | for (int i=0;i<classLinks.length();i++) |
---|
322 | { |
---|
323 | string = classLinks.getJSONObject(i).toString(); |
---|
324 | if (string.contains(",")) { |
---|
325 | list =Arrays.asList(string.split(",")); |
---|
326 | |
---|
327 | } else |
---|
328 | throw new IllegalArgumentException("path error"); |
---|
329 | |
---|
330 | }*/ |
---|
331 | |
---|
332 | // return list; |
---|
333 | } |
---|
334 | |
---|
335 | private static String convertPath2SPARQL2(List<String> path) throws Exception{ |
---|
336 | if( path == null ){ |
---|
337 | throw new Exception("Path is null."); |
---|
338 | } |
---|
339 | |
---|
340 | |
---|
341 | //List<String> classLinks = path.getClassLinks(); |
---|
342 | |
---|
343 | StringBuffer queryStr = new StringBuffer(); |
---|
344 | StringBuffer selStr = new StringBuffer(); |
---|
345 | StringBuffer whereStr = new StringBuffer(); |
---|
346 | // if(num==0){ |
---|
347 | // int num = classLinks.size(); |
---|
348 | // } |
---|
349 | |
---|
350 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
351 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
352 | |
---|
353 | selStr.append("SELECT "); |
---|
354 | whereStr.append("WHERE { \n"); |
---|
355 | |
---|
356 | String properties = null; |
---|
357 | String objectClasses = null; |
---|
358 | String subjectClasses = null; |
---|
359 | Direction direction = null; |
---|
360 | int i = 0; |
---|
361 | int k = 0; |
---|
362 | |
---|
363 | String startClass = path.get(path.size()-1); |
---|
364 | List<String> classLinks = null; |
---|
365 | // ArrayList<String> array= (String[])path.toArray(); |
---|
366 | |
---|
367 | // classLinks.add(path.get(i)); |
---|
368 | |
---|
369 | int num =(path.size()-1)/2; |
---|
370 | |
---|
371 | for(int j=path.size()-2;j>0;j=j-2) |
---|
372 | { |
---|
373 | // properties = link.getPropertyURI(); |
---|
374 | // objectClasses = link.getLinkedClassURI(); |
---|
375 | // direction = link.getDirection(); |
---|
376 | properties = path.get(j); |
---|
377 | objectClasses = path.get(j-1); |
---|
378 | direction = Direction.forward; |
---|
379 | |
---|
380 | if (i==0) |
---|
381 | subjectClasses = startClass; |
---|
382 | |
---|
383 | selStr.append("?c").append(i).append(" "); |
---|
384 | selStr.append("?l").append(i).append(" "); |
---|
385 | |
---|
386 | // if(i == path.size()) |
---|
387 | // selStr.append("\n"); |
---|
388 | |
---|
389 | |
---|
390 | whereStr.append("?c").append(i). |
---|
391 | append(" rdf:type "). |
---|
392 | append("<"). |
---|
393 | append(subjectClasses). |
---|
394 | append(">"). |
---|
395 | append(".\n"); |
---|
396 | |
---|
397 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
398 | |
---|
399 | |
---|
400 | if(direction == Direction.forward) |
---|
401 | { |
---|
402 | whereStr.append("?c").append(i).append(" "); |
---|
403 | whereStr.append("<").append(properties).append("> "); |
---|
404 | whereStr.append("?c").append(i+1).append(".\n"); |
---|
405 | } |
---|
406 | else |
---|
407 | { |
---|
408 | whereStr.append("?c").append(i+1).append(" "); |
---|
409 | whereStr.append("<").append(properties).append("> "); |
---|
410 | whereStr.append("?c").append(i).append(".\n"); |
---|
411 | } |
---|
412 | |
---|
413 | subjectClasses = objectClasses; |
---|
414 | i++; |
---|
415 | k++; |
---|
416 | if(k>=num){ |
---|
417 | break; |
---|
418 | } |
---|
419 | } |
---|
420 | |
---|
421 | selStr.append("?c").append(i).append(" \n"); |
---|
422 | selStr.append("?l").append(i).append(" \n"); |
---|
423 | whereStr.append("?c").append(i).append(" rdf:type "). |
---|
424 | append("<"). |
---|
425 | append(subjectClasses). |
---|
426 | append(">"). |
---|
427 | append(".\n"); |
---|
428 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
429 | |
---|
430 | |
---|
431 | queryStr.append(selStr).append(whereStr); |
---|
432 | |
---|
433 | queryStr.append("}"); |
---|
434 | //OPTIONAL |
---|
435 | queryStr.append("LIMIT 100\n");; |
---|
436 | |
---|
437 | System.out.println(queryStr); |
---|
438 | return queryStr.toString(); |
---|
439 | |
---|
440 | |
---|
441 | } |
---|
442 | |
---|
443 | } |
---|