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