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 | PrintWriter out = response.getWriter(); |
---|
81 | String jpath = request.getParameter("jsonpath"); |
---|
82 | Path path = null; |
---|
83 | try { |
---|
84 | path = convertJ2Path(jpath); |
---|
85 | } catch (JSONException ex) { |
---|
86 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
---|
87 | } |
---|
88 | String query = null; |
---|
89 | try { |
---|
90 | query = convertPath2SPARQL(path); |
---|
91 | } catch (Exception ex) { |
---|
92 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
---|
93 | } |
---|
94 | out.print(query); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | * Handles the HTTP <code>POST</code> method. |
---|
101 | * |
---|
102 | * @param request servlet request |
---|
103 | * @param response servlet response |
---|
104 | * @throws ServletException if a servlet-specific error occurs |
---|
105 | * @throws IOException if an I/O error occurs |
---|
106 | */ |
---|
107 | @Override |
---|
108 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
---|
109 | throws ServletException, IOException { |
---|
110 | processRequest(request, response); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Returns a short description of the servlet. |
---|
115 | * |
---|
116 | * @return a String containing servlet description |
---|
117 | */ |
---|
118 | @Override |
---|
119 | public String getServletInfo() { |
---|
120 | return "Short description"; |
---|
121 | }// </editor-fold> |
---|
122 | |
---|
123 | private Path convertJ2Path(String jpath) throws JSONException{ |
---|
124 | |
---|
125 | JSONObject object = new JSONObject(jpath); |
---|
126 | |
---|
127 | // int width = Integer.parseInt(object.getJSONObject("width").toString()); |
---|
128 | JSONArray classLinks = object.getJSONArray("classLinks"); |
---|
129 | JSONObject jsonObject; |
---|
130 | List <ClassLink> list = new ArrayList<ClassLink>(); |
---|
131 | |
---|
132 | for (int i=0;i<classLinks.length();i++) |
---|
133 | { |
---|
134 | jsonObject = classLinks.getJSONObject(i); |
---|
135 | |
---|
136 | String direction=jsonObject.getString("direction"); |
---|
137 | Direction myDirection = null; |
---|
138 | if (direction.equals(Direction.forward.toString())) |
---|
139 | myDirection=Direction.forward; |
---|
140 | else if (direction.equals(Direction.reverse.toString())) |
---|
141 | myDirection=Direction.reverse; |
---|
142 | else if (direction.equals(Direction.both.toString())) |
---|
143 | myDirection=Direction.both; |
---|
144 | |
---|
145 | |
---|
146 | String linkedLiteralDatatypeURI=null; |
---|
147 | linkedLiteralDatatypeURI = jsonObject.getString("linkedLiteralDatatypeURI"); |
---|
148 | String linkedClassURI = jsonObject.getString("linkedClassURI"); |
---|
149 | String propertyURI = jsonObject.getString("propertyURI"); |
---|
150 | |
---|
151 | // int numOfLinks = Integer.parseInt(jsonObject.getJSONObject("numOfLinks").toString()); |
---|
152 | // int numOfLinkedInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedInstances").toString()); |
---|
153 | // int numOfOriginInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
---|
154 | // int numOfOriginClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
---|
155 | // int numOfLinkedClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedClassInstances").toString()); |
---|
156 | |
---|
157 | // ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, null, |
---|
158 | // numOfLinks, numOfOriginInstances, numOfLinkedInstances, |
---|
159 | // numOfOriginClassInstances, numOfLinkedClassInstances, |
---|
160 | // false, false); |
---|
161 | ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, myDirection, |
---|
162 | 0, 0, 0, |
---|
163 | 0, 0, |
---|
164 | false, false); |
---|
165 | System.out.println(classLink.getDirection().toString()); |
---|
166 | list.add(classLink); |
---|
167 | } |
---|
168 | String startClass = object.getString("startClassURI"); |
---|
169 | Path path = new Path(startClass, list, 0); |
---|
170 | |
---|
171 | return path; |
---|
172 | } |
---|
173 | |
---|
174 | private String convertPath2SPARQL(Path path) throws Exception{ |
---|
175 | |
---|
176 | ArrayList<String> classname =new ArrayList<String>() ; |
---|
177 | |
---|
178 | if( path == null ){ |
---|
179 | throw new Exception("Path is null."); |
---|
180 | } |
---|
181 | String startClass = path.getStartClass(); |
---|
182 | List<ClassLink> classLinks = path.getClassLinks(); |
---|
183 | |
---|
184 | StringBuffer queryStr = new StringBuffer(); |
---|
185 | StringBuffer selStr = new StringBuffer(); |
---|
186 | StringBuffer whereStr = new StringBuffer(); |
---|
187 | // if(num==0){ |
---|
188 | int num = classLinks.size(); |
---|
189 | // } |
---|
190 | |
---|
191 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
192 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
193 | |
---|
194 | selStr.append("SELECT "); |
---|
195 | whereStr.append("WHERE { \n"); |
---|
196 | |
---|
197 | String properties = null; |
---|
198 | String objectClasses = null; |
---|
199 | String subjectClasses = null; |
---|
200 | Direction direction = null; |
---|
201 | int i = 0; |
---|
202 | int k = 0; |
---|
203 | for (ClassLink link :classLinks ) |
---|
204 | { |
---|
205 | properties = link.getPropertyURI(); |
---|
206 | objectClasses = link.getLinkedClassURI(); |
---|
207 | direction = link.getDirection(); |
---|
208 | |
---|
209 | if (i==0) |
---|
210 | subjectClasses = startClass; |
---|
211 | |
---|
212 | classname.add(subjectClasses); |
---|
213 | selStr.append("?c").append(i).append(" "); |
---|
214 | selStr.append("?l").append(i).append(" "); |
---|
215 | |
---|
216 | if(i == classLinks.size()) |
---|
217 | selStr.append("\n"); |
---|
218 | |
---|
219 | |
---|
220 | whereStr.append("?c").append(i). |
---|
221 | append(" rdf:type "). |
---|
222 | append("<"). |
---|
223 | append(subjectClasses). |
---|
224 | append(">"). |
---|
225 | append(".\n"); |
---|
226 | |
---|
227 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
228 | |
---|
229 | |
---|
230 | if(direction == Direction.forward) |
---|
231 | { |
---|
232 | whereStr.append("?c").append(i).append(" "); |
---|
233 | whereStr.append("<").append(properties).append("> "); |
---|
234 | whereStr.append("?c").append(i+1).append(".\n"); |
---|
235 | } |
---|
236 | else |
---|
237 | { |
---|
238 | whereStr.append("?c").append(i+1).append(" "); |
---|
239 | whereStr.append("<").append(properties).append("> "); |
---|
240 | whereStr.append("?c").append(i).append(".\n"); |
---|
241 | } |
---|
242 | |
---|
243 | subjectClasses = objectClasses; |
---|
244 | i++; |
---|
245 | k++; |
---|
246 | if(k>=num){ |
---|
247 | break; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | selStr.append("?c").append(i).append(" \n"); |
---|
252 | selStr.append("?l").append(i).append(" \n"); |
---|
253 | whereStr.append("?c").append(i).append(" rdf:type "). |
---|
254 | append("<"). |
---|
255 | append(subjectClasses). |
---|
256 | append(">"). |
---|
257 | append(".\n"); |
---|
258 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
259 | classname.add(subjectClasses); |
---|
260 | |
---|
261 | |
---|
262 | queryStr.append(selStr).append(whereStr); |
---|
263 | |
---|
264 | queryStr.append("}"); |
---|
265 | //OPTIONAL |
---|
266 | queryStr.append("LIMIT 100\n"); |
---|
267 | |
---|
268 | System.out.println(queryStr); |
---|
269 | |
---|
270 | //rewrite Sparql |
---|
271 | |
---|
272 | ArrayList<String> classname2 = new ArrayList<String>(); |
---|
273 | for(int index=0;index<classname.size();index++){ |
---|
274 | String tmp=classname.get(index); |
---|
275 | |
---|
276 | int mark; |
---|
277 | if((mark=tmp.indexOf("#"))!=-1) |
---|
278 | classname2.add(tmp.substring(mark+1)); |
---|
279 | else classname2.add(tmp.substring(tmp.indexOf("/")+1)); |
---|
280 | |
---|
281 | } |
---|
282 | String query=queryStr.toString(); |
---|
283 | for(int index=0;index<classname2.size();index++){ |
---|
284 | String original="c"+index; |
---|
285 | query= query.replaceAll(original, classname2.get(index)); |
---|
286 | } |
---|
287 | query= query.replaceAll("\\?l","\\?label"); |
---|
288 | System.out.println(query); |
---|
289 | return query; |
---|
290 | |
---|
291 | |
---|
292 | } |
---|
293 | private static String rewriteSparql(String query){ |
---|
294 | StringBuffer tmp=new StringBuffer(query); |
---|
295 | int index= tmp.indexOf("WHERE"); |
---|
296 | int begin=0,cnt=0; |
---|
297 | while(begin<index) |
---|
298 | { |
---|
299 | begin= tmp.indexOf("?c", begin); |
---|
300 | cnt++; |
---|
301 | |
---|
302 | } |
---|
303 | return null; |
---|
304 | } |
---|
305 | private static List<String> convertJ2Path2(String jpath) throws JSONException{ |
---|
306 | List <String> list = null; |
---|
307 | String temp =(String) jpath.subSequence(2, jpath.length()-2); |
---|
308 | // if (temp.contains(","")) |
---|
309 | list =Arrays.asList(temp.split("\",\"")); |
---|
310 | return list; |
---|
311 | |
---|
312 | /* JSONArray classLinks=new JSONArray(jpath); |
---|
313 | String string; |
---|
314 | |
---|
315 | for (int i=0;i<classLinks.length();i++) |
---|
316 | { |
---|
317 | string = classLinks.getJSONObject(i).toString(); |
---|
318 | if (string.contains(",")) { |
---|
319 | list =Arrays.asList(string.split(",")); |
---|
320 | |
---|
321 | } else |
---|
322 | throw new IllegalArgumentException("path error"); |
---|
323 | |
---|
324 | }*/ |
---|
325 | |
---|
326 | // return list; |
---|
327 | } |
---|
328 | |
---|
329 | private static String convertPath2SPARQL2(List<String> path) throws Exception{ |
---|
330 | if( path == null ){ |
---|
331 | throw new Exception("Path is null."); |
---|
332 | } |
---|
333 | |
---|
334 | |
---|
335 | //List<String> classLinks = path.getClassLinks(); |
---|
336 | |
---|
337 | StringBuffer queryStr = new StringBuffer(); |
---|
338 | StringBuffer selStr = new StringBuffer(); |
---|
339 | StringBuffer whereStr = new StringBuffer(); |
---|
340 | // if(num==0){ |
---|
341 | // int num = classLinks.size(); |
---|
342 | // } |
---|
343 | |
---|
344 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
345 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
346 | |
---|
347 | selStr.append("SELECT "); |
---|
348 | whereStr.append("WHERE { \n"); |
---|
349 | |
---|
350 | String properties = null; |
---|
351 | String objectClasses = null; |
---|
352 | String subjectClasses = null; |
---|
353 | Direction direction = null; |
---|
354 | int i = 0; |
---|
355 | int k = 0; |
---|
356 | |
---|
357 | String startClass = path.get(path.size()-1); |
---|
358 | List<String> classLinks = null; |
---|
359 | // ArrayList<String> array= (String[])path.toArray(); |
---|
360 | |
---|
361 | // classLinks.add(path.get(i)); |
---|
362 | |
---|
363 | int num =(path.size()-1)/2; |
---|
364 | |
---|
365 | for(int j=path.size()-2;j>0;j=j-2) |
---|
366 | { |
---|
367 | // properties = link.getPropertyURI(); |
---|
368 | // objectClasses = link.getLinkedClassURI(); |
---|
369 | // direction = link.getDirection(); |
---|
370 | properties = path.get(j); |
---|
371 | objectClasses = path.get(j-1); |
---|
372 | direction = Direction.forward; |
---|
373 | |
---|
374 | if (i==0) |
---|
375 | subjectClasses = startClass; |
---|
376 | |
---|
377 | selStr.append("?c").append(i).append(" "); |
---|
378 | selStr.append("?l").append(i).append(" "); |
---|
379 | |
---|
380 | // if(i == path.size()) |
---|
381 | // selStr.append("\n"); |
---|
382 | |
---|
383 | |
---|
384 | whereStr.append("?c").append(i). |
---|
385 | append(" rdf:type "). |
---|
386 | append("<"). |
---|
387 | append(subjectClasses). |
---|
388 | append(">"). |
---|
389 | append(".\n"); |
---|
390 | |
---|
391 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
392 | |
---|
393 | |
---|
394 | if(direction == Direction.forward) |
---|
395 | { |
---|
396 | whereStr.append("?c").append(i).append(" "); |
---|
397 | whereStr.append("<").append(properties).append("> "); |
---|
398 | whereStr.append("?c").append(i+1).append(".\n"); |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | whereStr.append("?c").append(i+1).append(" "); |
---|
403 | whereStr.append("<").append(properties).append("> "); |
---|
404 | whereStr.append("?c").append(i).append(".\n"); |
---|
405 | } |
---|
406 | |
---|
407 | subjectClasses = objectClasses; |
---|
408 | i++; |
---|
409 | k++; |
---|
410 | if(k>=num){ |
---|
411 | break; |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | selStr.append("?c").append(i).append(" \n"); |
---|
416 | selStr.append("?l").append(i).append(" \n"); |
---|
417 | whereStr.append("?c").append(i).append(" rdf:type "). |
---|
418 | append("<"). |
---|
419 | append(subjectClasses). |
---|
420 | append(">"). |
---|
421 | append(".\n"); |
---|
422 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
423 | |
---|
424 | |
---|
425 | queryStr.append(selStr).append(whereStr); |
---|
426 | |
---|
427 | queryStr.append("}"); |
---|
428 | //OPTIONAL |
---|
429 | queryStr.append("LIMIT 100\n");; |
---|
430 | |
---|
431 | System.out.println(queryStr); |
---|
432 | return queryStr.toString(); |
---|
433 | |
---|
434 | |
---|
435 | } |
---|
436 | |
---|
437 | } |
---|