| 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 | List<String> 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 List<String> convertJ2Path(String jpath) throws JSONException{ |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | JSONArray classLinks=new JSONArray(jpath); |
|---|
| 127 | |
|---|
| 128 | String string; |
|---|
| 129 | List <String> list = null; |
|---|
| 130 | for (int i=0;i<classLinks.length();i++) |
|---|
| 131 | { |
|---|
| 132 | string = classLinks.getJSONObject(i).toString(); |
|---|
| 133 | if (string.contains(",")) { |
|---|
| 134 | list =Arrays.asList(string.split(",")); |
|---|
| 135 | |
|---|
| 136 | } else |
|---|
| 137 | throw new IllegalArgumentException("path error"); |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | return list; |
|---|
| 143 | |
|---|
| 144 | /* JSONObject object = new JSONObject(jpath); |
|---|
| 145 | String startClass = object.getJSONObject("startClassURI").toString(); |
|---|
| 146 | int width = Integer.parseInt(object.getJSONObject("width").toString()); |
|---|
| 147 | JSONArray classLinks = object.getJSONArray("classLinks"); |
|---|
| 148 | JSONObject jsonObject; |
|---|
| 149 | List <ClassLink> list = null; |
|---|
| 150 | |
|---|
| 151 | for (int i=0;i<classLinks.length();i++) |
|---|
| 152 | { |
|---|
| 153 | jsonObject = classLinks.getJSONObject(i); |
|---|
| 154 | String propertyURI = jsonObject.getJSONObject("propertyURI").toString(); |
|---|
| 155 | String linkedClassURI = jsonObject.getJSONObject("linkedClassURI").toString(); |
|---|
| 156 | String linkedLiteralDatatypeURI = jsonObject.getJSONObject("linkedLiteralDatatypeURI").toString(); |
|---|
| 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 | list.add(classLink); |
|---|
| 168 | } |
|---|
| 169 | Path path = new Path(startClass, list, width); |
|---|
| 170 | return path; |
|---|
| 171 | */ |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | private String convertPath2SPARQL(List<String> path) throws Exception{ |
|---|
| 177 | if( path == null ){ |
|---|
| 178 | throw new Exception("Path is null."); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | //List<String> 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 | |
|---|
| 204 | String startClass = path.get(path.size()-1); |
|---|
| 205 | List<String> classLinks = null; |
|---|
| 206 | // ArrayList<String> array= (String[])path.toArray(); |
|---|
| 207 | |
|---|
| 208 | // classLinks.add(path.get(i)); |
|---|
| 209 | |
|---|
| 210 | int num =(path.size()-1)/2; |
|---|
| 211 | |
|---|
| 212 | for(int j=path.size()-2;j>0;j=j-2) |
|---|
| 213 | { |
|---|
| 214 | // properties = link.getPropertyURI(); |
|---|
| 215 | // objectClasses = link.getLinkedClassURI(); |
|---|
| 216 | // direction = link.getDirection(); |
|---|
| 217 | properties = path.get(j); |
|---|
| 218 | objectClasses = path.get(j-1); |
|---|
| 219 | direction = Direction.forward; |
|---|
| 220 | |
|---|
| 221 | if (i==0) |
|---|
| 222 | subjectClasses = startClass; |
|---|
| 223 | |
|---|
| 224 | selStr.append("?c").append(i).append(" "); |
|---|
| 225 | selStr.append("?l").append(i).append(" "); |
|---|
| 226 | |
|---|
| 227 | if(i == classLinks.size()) |
|---|
| 228 | selStr.append("\n"); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | whereStr.append("?c").append(i). |
|---|
| 232 | append(" rdf:type "). |
|---|
| 233 | append("<"). |
|---|
| 234 | append(subjectClasses). |
|---|
| 235 | append(">"). |
|---|
| 236 | append(".\n"); |
|---|
| 237 | |
|---|
| 238 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | if(direction == Direction.forward) |
|---|
| 242 | { |
|---|
| 243 | whereStr.append("?c").append(i).append(" "); |
|---|
| 244 | whereStr.append("<").append(properties).append("> "); |
|---|
| 245 | whereStr.append("?c").append(i+1).append(".\n"); |
|---|
| 246 | } |
|---|
| 247 | else |
|---|
| 248 | { |
|---|
| 249 | whereStr.append("?c").append(i+1).append(" "); |
|---|
| 250 | whereStr.append("<").append(properties).append("> "); |
|---|
| 251 | whereStr.append("?c").append(i).append(".\n"); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | subjectClasses = objectClasses; |
|---|
| 255 | i++; |
|---|
| 256 | k++; |
|---|
| 257 | if(k>=num){ |
|---|
| 258 | break; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | selStr.append("?c").append(i).append(" \n"); |
|---|
| 263 | selStr.append("?l").append(i).append(" \n"); |
|---|
| 264 | whereStr.append("?c").append(i).append(" rdf:type "). |
|---|
| 265 | append("<"). |
|---|
| 266 | append(subjectClasses). |
|---|
| 267 | append(">"). |
|---|
| 268 | append(".\n"); |
|---|
| 269 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | queryStr.append(selStr).append(whereStr); |
|---|
| 273 | |
|---|
| 274 | queryStr.append("}"); |
|---|
| 275 | //OPTIONAL |
|---|
| 276 | queryStr.append("LIMIT 100\n");; |
|---|
| 277 | |
|---|
| 278 | //System.out.println(queryStr); |
|---|
| 279 | return queryStr.toString(); |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | } |
|---|
| 283 | } |
|---|