| 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("jsonpath"); |
|---|
| 86 | Path path = null; |
|---|
| 87 | try { |
|---|
| 88 | path = convertJ2Path(jpath); |
|---|
| 89 | } catch (JSONException ex) { |
|---|
| 90 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 91 | } |
|---|
| 92 | String query = null; |
|---|
| 93 | try { |
|---|
| 94 | query = convertPath2SPARQL(path); |
|---|
| 95 | } catch (Exception ex) { |
|---|
| 96 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 97 | } |
|---|
| 98 | out.print(query); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Handles the HTTP <code>POST</code> method. |
|---|
| 105 | * |
|---|
| 106 | * @param request servlet request |
|---|
| 107 | * @param response servlet response |
|---|
| 108 | * @throws ServletException if a servlet-specific error occurs |
|---|
| 109 | * @throws IOException if an I/O error occurs |
|---|
| 110 | */ |
|---|
| 111 | @Override |
|---|
| 112 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
|---|
| 113 | throws ServletException, IOException { |
|---|
| 114 | processRequest(request, response); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * Returns a short description of the servlet. |
|---|
| 119 | * |
|---|
| 120 | * @return a String containing servlet description |
|---|
| 121 | */ |
|---|
| 122 | @Override |
|---|
| 123 | public String getServletInfo() { |
|---|
| 124 | return "Short description"; |
|---|
| 125 | }// </editor-fold> |
|---|
| 126 | |
|---|
| 127 | private Path convertJ2Path(String jpath) throws JSONException{ |
|---|
| 128 | |
|---|
| 129 | JSONObject object = new JSONObject(jpath); |
|---|
| 130 | |
|---|
| 131 | // int width = Integer.parseInt(object.getJSONObject("width").toString()); |
|---|
| 132 | JSONArray classLinks = object.getJSONArray("classLinks"); |
|---|
| 133 | JSONObject jsonObject; |
|---|
| 134 | List <ClassLink> list = new ArrayList<ClassLink>(); |
|---|
| 135 | |
|---|
| 136 | for (int i=0;i<classLinks.length();i++) |
|---|
| 137 | { |
|---|
| 138 | jsonObject = classLinks.getJSONObject(i); |
|---|
| 139 | |
|---|
| 140 | String direction=jsonObject.getString("direction"); |
|---|
| 141 | Direction myDirection = null; |
|---|
| 142 | if (direction.equals(Direction.forward.toString())) |
|---|
| 143 | myDirection=Direction.forward; |
|---|
| 144 | else if (direction.equals(Direction.reverse.toString())) |
|---|
| 145 | myDirection=Direction.reverse; |
|---|
| 146 | else if (direction.equals(Direction.both.toString())) |
|---|
| 147 | myDirection=Direction.both; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | String linkedLiteralDatatypeURI=null; |
|---|
| 151 | linkedLiteralDatatypeURI = jsonObject.getString("linkedLiteralDatatypeURI"); |
|---|
| 152 | String linkedClassURI = jsonObject.getString("linkedClassURI"); |
|---|
| 153 | String propertyURI = jsonObject.getString("propertyURI"); |
|---|
| 154 | |
|---|
| 155 | // int numOfLinks = Integer.parseInt(jsonObject.getJSONObject("numOfLinks").toString()); |
|---|
| 156 | // int numOfLinkedInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedInstances").toString()); |
|---|
| 157 | // int numOfOriginInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
|---|
| 158 | // int numOfOriginClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
|---|
| 159 | // int numOfLinkedClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedClassInstances").toString()); |
|---|
| 160 | |
|---|
| 161 | // ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, null, |
|---|
| 162 | // numOfLinks, numOfOriginInstances, numOfLinkedInstances, |
|---|
| 163 | // numOfOriginClassInstances, numOfLinkedClassInstances, |
|---|
| 164 | // false, false); |
|---|
| 165 | ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, myDirection, |
|---|
| 166 | 0, 0, 0, |
|---|
| 167 | 0, 0, |
|---|
| 168 | false, false); |
|---|
| 169 | System.out.println(classLink.getDirection().toString()); |
|---|
| 170 | list.add(classLink); |
|---|
| 171 | } |
|---|
| 172 | String startClass = object.getString("startClassURI"); |
|---|
| 173 | Path path = new Path(startClass, list, 0); |
|---|
| 174 | |
|---|
| 175 | return path; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | private String convertPath2SPARQL(Path path) throws Exception{ |
|---|
| 179 | |
|---|
| 180 | ArrayList<String> classname =new ArrayList<String>() ; |
|---|
| 181 | |
|---|
| 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 | } |
|---|
| 286 | String query=queryStr.toString(); |
|---|
| 287 | for(int index=0;index<classname2.size();index++){ |
|---|
| 288 | String original="c"+index; |
|---|
| 289 | query= query.replaceAll(original, classname2.get(index)); |
|---|
| 290 | } |
|---|
| 291 | query= query.replaceAll("\\?l","\\?label"); |
|---|
| 292 | System.out.println(query); |
|---|
| 293 | return query; |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | } |
|---|
| 297 | private static String rewriteSparql(String query){ |
|---|
| 298 | StringBuffer tmp=new StringBuffer(query); |
|---|
| 299 | int index= tmp.indexOf("WHERE"); |
|---|
| 300 | int begin=0,cnt=0; |
|---|
| 301 | while(begin<index) |
|---|
| 302 | { |
|---|
| 303 | begin= tmp.indexOf("?c", begin); |
|---|
| 304 | cnt++; |
|---|
| 305 | |
|---|
| 306 | } |
|---|
| 307 | return null; |
|---|
| 308 | } |
|---|
| 309 | private static List<String> convertJ2Path2(String jpath) throws JSONException{ |
|---|
| 310 | List <String> list = null; |
|---|
| 311 | String temp =(String) jpath.subSequence(2, jpath.length()-2); |
|---|
| 312 | // if (temp.contains(","")) |
|---|
| 313 | list =Arrays.asList(temp.split("\",\"")); |
|---|
| 314 | return list; |
|---|
| 315 | |
|---|
| 316 | /* JSONArray classLinks=new JSONArray(jpath); |
|---|
| 317 | String string; |
|---|
| 318 | |
|---|
| 319 | for (int i=0;i<classLinks.length();i++) |
|---|
| 320 | { |
|---|
| 321 | string = classLinks.getJSONObject(i).toString(); |
|---|
| 322 | if (string.contains(",")) { |
|---|
| 323 | list =Arrays.asList(string.split(",")); |
|---|
| 324 | |
|---|
| 325 | } else |
|---|
| 326 | throw new IllegalArgumentException("path error"); |
|---|
| 327 | |
|---|
| 328 | }*/ |
|---|
| 329 | |
|---|
| 330 | // return list; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | private static String convertPath2SPARQL2(List<String> path) throws Exception{ |
|---|
| 334 | if( path == null ){ |
|---|
| 335 | throw new Exception("Path is null."); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | //List<String> classLinks = path.getClassLinks(); |
|---|
| 340 | |
|---|
| 341 | StringBuffer queryStr = new StringBuffer(); |
|---|
| 342 | StringBuffer selStr = new StringBuffer(); |
|---|
| 343 | StringBuffer whereStr = new StringBuffer(); |
|---|
| 344 | // if(num==0){ |
|---|
| 345 | // int num = classLinks.size(); |
|---|
| 346 | // } |
|---|
| 347 | |
|---|
| 348 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
|---|
| 349 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
|---|
| 350 | |
|---|
| 351 | selStr.append("SELECT "); |
|---|
| 352 | whereStr.append("WHERE { \n"); |
|---|
| 353 | |
|---|
| 354 | String properties = null; |
|---|
| 355 | String objectClasses = null; |
|---|
| 356 | String subjectClasses = null; |
|---|
| 357 | Direction direction = null; |
|---|
| 358 | int i = 0; |
|---|
| 359 | int k = 0; |
|---|
| 360 | |
|---|
| 361 | String startClass = path.get(path.size()-1); |
|---|
| 362 | List<String> classLinks = null; |
|---|
| 363 | // ArrayList<String> array= (String[])path.toArray(); |
|---|
| 364 | |
|---|
| 365 | // classLinks.add(path.get(i)); |
|---|
| 366 | |
|---|
| 367 | int num =(path.size()-1)/2; |
|---|
| 368 | |
|---|
| 369 | for(int j=path.size()-2;j>0;j=j-2) |
|---|
| 370 | { |
|---|
| 371 | // properties = link.getPropertyURI(); |
|---|
| 372 | // objectClasses = link.getLinkedClassURI(); |
|---|
| 373 | // direction = link.getDirection(); |
|---|
| 374 | properties = path.get(j); |
|---|
| 375 | objectClasses = path.get(j-1); |
|---|
| 376 | direction = Direction.forward; |
|---|
| 377 | |
|---|
| 378 | if (i==0) |
|---|
| 379 | subjectClasses = startClass; |
|---|
| 380 | |
|---|
| 381 | selStr.append("?c").append(i).append(" "); |
|---|
| 382 | selStr.append("?l").append(i).append(" "); |
|---|
| 383 | |
|---|
| 384 | // if(i == path.size()) |
|---|
| 385 | // selStr.append("\n"); |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | whereStr.append("?c").append(i). |
|---|
| 389 | append(" rdf:type "). |
|---|
| 390 | append("<"). |
|---|
| 391 | append(subjectClasses). |
|---|
| 392 | append(">"). |
|---|
| 393 | append(".\n"); |
|---|
| 394 | |
|---|
| 395 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | if(direction == Direction.forward) |
|---|
| 399 | { |
|---|
| 400 | whereStr.append("?c").append(i).append(" "); |
|---|
| 401 | whereStr.append("<").append(properties).append("> "); |
|---|
| 402 | whereStr.append("?c").append(i+1).append(".\n"); |
|---|
| 403 | } |
|---|
| 404 | else |
|---|
| 405 | { |
|---|
| 406 | whereStr.append("?c").append(i+1).append(" "); |
|---|
| 407 | whereStr.append("<").append(properties).append("> "); |
|---|
| 408 | whereStr.append("?c").append(i).append(".\n"); |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | subjectClasses = objectClasses; |
|---|
| 412 | i++; |
|---|
| 413 | k++; |
|---|
| 414 | if(k>=num){ |
|---|
| 415 | break; |
|---|
| 416 | } |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | selStr.append("?c").append(i).append(" \n"); |
|---|
| 420 | selStr.append("?l").append(i).append(" \n"); |
|---|
| 421 | whereStr.append("?c").append(i).append(" rdf:type "). |
|---|
| 422 | append("<"). |
|---|
| 423 | append(subjectClasses). |
|---|
| 424 | append(">"). |
|---|
| 425 | append(".\n"); |
|---|
| 426 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
|---|
| 427 | |
|---|
| 428 | |
|---|
| 429 | queryStr.append(selStr).append(whereStr); |
|---|
| 430 | |
|---|
| 431 | queryStr.append("}"); |
|---|
| 432 | //OPTIONAL |
|---|
| 433 | queryStr.append("LIMIT 100\n");; |
|---|
| 434 | |
|---|
| 435 | System.out.println(queryStr); |
|---|
| 436 | return queryStr.toString(); |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | } |
|---|