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 | ArrayList<String> classname =new ArrayList<String>() ; |
---|
182 | if( path == null ){ |
---|
183 | throw new Exception("Path is null."); |
---|
184 | } |
---|
185 | String startClass = path.getStartClass(); |
---|
186 | List<ClassLink> classLinks = path.getClassLinks(); |
---|
187 | |
---|
188 | StringBuffer queryStr = new StringBuffer(); |
---|
189 | StringBuffer selStr = new StringBuffer(); |
---|
190 | StringBuffer whereStr = new StringBuffer(); |
---|
191 | // if(num==0){ |
---|
192 | int num = classLinks.size(); |
---|
193 | // } |
---|
194 | |
---|
195 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
196 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
197 | |
---|
198 | selStr.append("SELECT "); |
---|
199 | whereStr.append("WHERE { \n"); |
---|
200 | |
---|
201 | String properties = null; |
---|
202 | String objectClasses = null; |
---|
203 | String subjectClasses = null; |
---|
204 | Direction direction = null; |
---|
205 | int i = 0; |
---|
206 | int k = 0; |
---|
207 | for (ClassLink link :classLinks ) |
---|
208 | { |
---|
209 | properties = link.getPropertyURI(); |
---|
210 | objectClasses = link.getLinkedClassURI(); |
---|
211 | direction = link.getDirection(); |
---|
212 | |
---|
213 | if (i==0) |
---|
214 | subjectClasses = startClass; |
---|
215 | |
---|
216 | classname.add(subjectClasses); |
---|
217 | selStr.append("?c").append(i).append(" "); |
---|
218 | selStr.append("?l").append(i).append(" "); |
---|
219 | |
---|
220 | if(i == classLinks.size()) |
---|
221 | selStr.append("\n"); |
---|
222 | |
---|
223 | |
---|
224 | whereStr.append("?c").append(i). |
---|
225 | append(" rdf:type "). |
---|
226 | append("<"). |
---|
227 | append(subjectClasses). |
---|
228 | append(">"). |
---|
229 | append(".\n"); |
---|
230 | |
---|
231 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
232 | |
---|
233 | |
---|
234 | if(direction == Direction.forward) |
---|
235 | { |
---|
236 | whereStr.append("?c").append(i).append(" "); |
---|
237 | whereStr.append("<").append(properties).append("> "); |
---|
238 | whereStr.append("?c").append(i+1).append(".\n"); |
---|
239 | } |
---|
240 | else |
---|
241 | { |
---|
242 | whereStr.append("?c").append(i+1).append(" "); |
---|
243 | whereStr.append("<").append(properties).append("> "); |
---|
244 | whereStr.append("?c").append(i).append(".\n"); |
---|
245 | } |
---|
246 | |
---|
247 | subjectClasses = objectClasses; |
---|
248 | i++; |
---|
249 | k++; |
---|
250 | if(k>=num){ |
---|
251 | break; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | selStr.append("?c").append(i).append(" \n"); |
---|
256 | selStr.append("?l").append(i).append(" \n"); |
---|
257 | whereStr.append("?c").append(i).append(" rdf:type "). |
---|
258 | append("<"). |
---|
259 | append(subjectClasses). |
---|
260 | append(">"). |
---|
261 | append(".\n"); |
---|
262 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
263 | classname.add(subjectClasses); |
---|
264 | |
---|
265 | |
---|
266 | queryStr.append(selStr).append(whereStr); |
---|
267 | |
---|
268 | queryStr.append("}"); |
---|
269 | //OPTIONAL |
---|
270 | queryStr.append("LIMIT 100\n"); |
---|
271 | |
---|
272 | System.out.println(queryStr); |
---|
273 | |
---|
274 | //rewrite Sparql |
---|
275 | |
---|
276 | ArrayList<String> classname2 = new ArrayList<String>(); |
---|
277 | for(int index=0;index<classname.size();index++){ |
---|
278 | String tmp=classname.get(index); |
---|
279 | /* |
---|
280 | int mark; |
---|
281 | if((mark=tmp.indexOf("#"))!=-1) |
---|
282 | classname2.add(tmp.substring(mark+1)); |
---|
283 | else classname2.add(tmp.substring(tmp.indexOf("/")+1)); |
---|
284 | */ |
---|
285 | // changed by Atsuko |
---|
286 | String[] sname1 = tmp.split("#"); |
---|
287 | String[] sname2 = sname1[sname1.length -1].split("/"); |
---|
288 | String[] sname3 = sname2[sname2.length -1].split(":"); |
---|
289 | String cname = sname3[sname3.length -1].replaceAll("-", ""); |
---|
290 | classname2.add(cname); |
---|
291 | } |
---|
292 | String query=queryStr.toString(); |
---|
293 | for(int index=0;index<classname2.size();index++){ |
---|
294 | String original="c"+index; |
---|
295 | query= query.replaceAll(original, classname2.get(index)); |
---|
296 | } |
---|
297 | query= query.replaceAll("\\?l","\\?label"); |
---|
298 | System.out.println(query); |
---|
299 | return query; |
---|
300 | } |
---|
301 | |
---|
302 | private static String rewriteSparql(String query){ |
---|
303 | StringBuffer tmp=new StringBuffer(query); |
---|
304 | int index= tmp.indexOf("WHERE"); |
---|
305 | int begin=0,cnt=0; |
---|
306 | while(begin<index) |
---|
307 | { |
---|
308 | begin= tmp.indexOf("?c", begin); |
---|
309 | cnt++; |
---|
310 | |
---|
311 | } |
---|
312 | return null; |
---|
313 | } |
---|
314 | private static List<String> convertJ2Path2(String jpath) throws JSONException{ |
---|
315 | List <String> list = null; |
---|
316 | String temp =(String) jpath.subSequence(2, jpath.length()-2); |
---|
317 | // if (temp.contains(","")) |
---|
318 | list =Arrays.asList(temp.split("\",\"")); |
---|
319 | return list; |
---|
320 | |
---|
321 | /* JSONArray classLinks=new JSONArray(jpath); |
---|
322 | String string; |
---|
323 | |
---|
324 | for (int i=0;i<classLinks.length();i++) |
---|
325 | { |
---|
326 | string = classLinks.getJSONObject(i).toString(); |
---|
327 | if (string.contains(",")) { |
---|
328 | list =Arrays.asList(string.split(",")); |
---|
329 | |
---|
330 | } else |
---|
331 | throw new IllegalArgumentException("path error"); |
---|
332 | |
---|
333 | }*/ |
---|
334 | |
---|
335 | // return list; |
---|
336 | } |
---|
337 | |
---|
338 | private static String convertPath2SPARQL2(List<String> path) throws Exception{ |
---|
339 | if( path == null ){ |
---|
340 | throw new Exception("Path is null."); |
---|
341 | } |
---|
342 | |
---|
343 | |
---|
344 | //List<String> classLinks = path.getClassLinks(); |
---|
345 | |
---|
346 | StringBuffer queryStr = new StringBuffer(); |
---|
347 | StringBuffer selStr = new StringBuffer(); |
---|
348 | StringBuffer whereStr = new StringBuffer(); |
---|
349 | // if(num==0){ |
---|
350 | // int num = classLinks.size(); |
---|
351 | // } |
---|
352 | |
---|
353 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
354 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
355 | |
---|
356 | selStr.append("SELECT "); |
---|
357 | whereStr.append("WHERE { \n"); |
---|
358 | |
---|
359 | String properties = null; |
---|
360 | String objectClasses = null; |
---|
361 | String subjectClasses = null; |
---|
362 | Direction direction = null; |
---|
363 | int i = 0; |
---|
364 | int k = 0; |
---|
365 | |
---|
366 | String startClass = path.get(path.size()-1); |
---|
367 | List<String> classLinks = null; |
---|
368 | // ArrayList<String> array= (String[])path.toArray(); |
---|
369 | |
---|
370 | // classLinks.add(path.get(i)); |
---|
371 | |
---|
372 | int num =(path.size()-1)/2; |
---|
373 | |
---|
374 | for(int j=path.size()-2;j>0;j=j-2) |
---|
375 | { |
---|
376 | // properties = link.getPropertyURI(); |
---|
377 | // objectClasses = link.getLinkedClassURI(); |
---|
378 | // direction = link.getDirection(); |
---|
379 | properties = path.get(j); |
---|
380 | objectClasses = path.get(j-1); |
---|
381 | direction = Direction.forward; |
---|
382 | |
---|
383 | if (i==0) |
---|
384 | subjectClasses = startClass; |
---|
385 | |
---|
386 | selStr.append("?c").append(i).append(" "); |
---|
387 | selStr.append("?l").append(i).append(" "); |
---|
388 | |
---|
389 | // if(i == path.size()) |
---|
390 | // selStr.append("\n"); |
---|
391 | |
---|
392 | |
---|
393 | whereStr.append("?c").append(i). |
---|
394 | append(" rdf:type "). |
---|
395 | append("<"). |
---|
396 | append(subjectClasses). |
---|
397 | append(">"). |
---|
398 | append(".\n"); |
---|
399 | |
---|
400 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
401 | |
---|
402 | |
---|
403 | if(direction == Direction.forward) |
---|
404 | { |
---|
405 | whereStr.append("?c").append(i).append(" "); |
---|
406 | whereStr.append("<").append(properties).append("> "); |
---|
407 | whereStr.append("?c").append(i+1).append(".\n"); |
---|
408 | } |
---|
409 | else |
---|
410 | { |
---|
411 | whereStr.append("?c").append(i+1).append(" "); |
---|
412 | whereStr.append("<").append(properties).append("> "); |
---|
413 | whereStr.append("?c").append(i).append(".\n"); |
---|
414 | } |
---|
415 | |
---|
416 | subjectClasses = objectClasses; |
---|
417 | i++; |
---|
418 | k++; |
---|
419 | if(k>=num){ |
---|
420 | break; |
---|
421 | } |
---|
422 | } |
---|
423 | |
---|
424 | selStr.append("?c").append(i).append(" \n"); |
---|
425 | selStr.append("?l").append(i).append(" \n"); |
---|
426 | whereStr.append("?c").append(i).append(" rdf:type "). |
---|
427 | append("<"). |
---|
428 | append(subjectClasses). |
---|
429 | append(">"). |
---|
430 | append(".\n"); |
---|
431 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
432 | |
---|
433 | |
---|
434 | queryStr.append(selStr).append(whereStr); |
---|
435 | |
---|
436 | queryStr.append("}"); |
---|
437 | //OPTIONAL |
---|
438 | queryStr.append("LIMIT 100\n");; |
---|
439 | |
---|
440 | System.out.println(queryStr); |
---|
441 | return queryStr.toString(); |
---|
442 | |
---|
443 | |
---|
444 | } |
---|
445 | |
---|
446 | } |
---|