| 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.Iterator; |
|---|
| 12 | import java.util.List; |
|---|
| 13 | import java.util.logging.Level; |
|---|
| 14 | import java.util.logging.Logger; |
|---|
| 15 | import javax.servlet.ServletException; |
|---|
| 16 | import javax.servlet.annotation.WebServlet; |
|---|
| 17 | import javax.servlet.http.HttpServlet; |
|---|
| 18 | import javax.servlet.http.HttpServletRequest; |
|---|
| 19 | import javax.servlet.http.HttpServletResponse; |
|---|
| 20 | import org.biohackathon.SPARQLBuilder.OWL.*; |
|---|
| 21 | import org.json.JSONArray; |
|---|
| 22 | import org.json.JSONException; |
|---|
| 23 | import org.json.JSONObject; |
|---|
| 24 | |
|---|
| 25 | //import javax.json.Json; |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * |
|---|
| 29 | * @author atsuko |
|---|
| 30 | */ |
|---|
| 31 | @WebServlet(name = "SPServlet", urlPatterns = {"/sparql"}) |
|---|
| 32 | public class SPServlet extends HttpServlet { |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> |
|---|
| 36 | * methods. |
|---|
| 37 | * |
|---|
| 38 | * @param request servlet request |
|---|
| 39 | * @param response servlet response |
|---|
| 40 | * @throws ServletException if a servlet-specific error occurs |
|---|
| 41 | * @throws IOException if an I/O error occurs |
|---|
| 42 | */ |
|---|
| 43 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
|---|
| 44 | throws ServletException, IOException { |
|---|
| 45 | response.setContentType("text/html;charset=UTF-8"); |
|---|
| 46 | try (PrintWriter out = response.getWriter()) { |
|---|
| 47 | /* TODO output your page here. You may use following sample code. */ |
|---|
| 48 | out.println("<!DOCTYPE html>"); |
|---|
| 49 | out.println("<html>"); |
|---|
| 50 | out.println("<head>"); |
|---|
| 51 | out.println("<title>Servlet SPServlet</title>"); |
|---|
| 52 | out.println("</head>"); |
|---|
| 53 | out.println("<body>"); |
|---|
| 54 | out.println("<h1>Servlet SPServlet at " + request.getContextPath() + "</h1>"); |
|---|
| 55 | out.println("</body>"); |
|---|
| 56 | out.println("</html>"); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> |
|---|
| 61 | /** |
|---|
| 62 | * Handles the HTTP <code>GET</code> method. |
|---|
| 63 | * |
|---|
| 64 | * @param request servlet request |
|---|
| 65 | * @param response servlet response |
|---|
| 66 | * @throws ServletException if a servlet-specific error occurs |
|---|
| 67 | * @throws IOException if an I/O error occurs |
|---|
| 68 | */ |
|---|
| 69 | @Override |
|---|
| 70 | /* protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|---|
| 71 | throws ServletException, IOException { |
|---|
| 72 | processRequest(request, response); |
|---|
| 73 | }*/ |
|---|
| 74 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|---|
| 75 | throws ServletException, IOException { |
|---|
| 76 | //processRequest(request, response); |
|---|
| 77 | response.setContentType("application/json;charset=UTF-8"); |
|---|
| 78 | PrintWriter out = response.getWriter(); |
|---|
| 79 | String jpath = request.getParameter("jsonpath"); |
|---|
| 80 | Path path = null; |
|---|
| 81 | try { |
|---|
| 82 | path = convertJ2Path(jpath); |
|---|
| 83 | } catch (JSONException ex) { |
|---|
| 84 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 85 | } |
|---|
| 86 | String query = null; |
|---|
| 87 | try { |
|---|
| 88 | query = convertPath2SPARQL(path); |
|---|
| 89 | } catch (Exception ex) { |
|---|
| 90 | Logger.getLogger(SPServlet.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 91 | } |
|---|
| 92 | out.print(query); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Handles the HTTP <code>POST</code> method. |
|---|
| 99 | * |
|---|
| 100 | * @param request servlet request |
|---|
| 101 | * @param response servlet response |
|---|
| 102 | * @throws ServletException if a servlet-specific error occurs |
|---|
| 103 | * @throws IOException if an I/O error occurs |
|---|
| 104 | */ |
|---|
| 105 | @Override |
|---|
| 106 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
|---|
| 107 | throws ServletException, IOException { |
|---|
| 108 | processRequest(request, response); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | /** |
|---|
| 112 | * Returns a short description of the servlet. |
|---|
| 113 | * |
|---|
| 114 | * @return a String containing servlet description |
|---|
| 115 | */ |
|---|
| 116 | @Override |
|---|
| 117 | public String getServletInfo() { |
|---|
| 118 | return "Short description"; |
|---|
| 119 | }// </editor-fold> |
|---|
| 120 | |
|---|
| 121 | private Path convertJ2Path(String jpath) throws JSONException{ |
|---|
| 122 | |
|---|
| 123 | JSONObject object = new JSONObject(jpath); |
|---|
| 124 | String startClass = object.getJSONObject("startClassURI").toString(); |
|---|
| 125 | int width = Integer.parseInt(object.getJSONObject("width").toString()); |
|---|
| 126 | JSONArray classLinks = object.getJSONArray("classLinks"); |
|---|
| 127 | JSONObject jsonObject; |
|---|
| 128 | List <ClassLink> list = null; |
|---|
| 129 | |
|---|
| 130 | for (int i=0;i<classLinks.length();i++) |
|---|
| 131 | { |
|---|
| 132 | jsonObject = classLinks.getJSONObject(i); |
|---|
| 133 | String propertyURI = jsonObject.getJSONObject("propertyURI").toString(); |
|---|
| 134 | String linkedClassURI = jsonObject.getJSONObject("linkedClassURI").toString(); |
|---|
| 135 | String linkedLiteralDatatypeURI = jsonObject.getJSONObject("linkedLiteralDatatypeURI").toString(); |
|---|
| 136 | int numOfLinks = Integer.parseInt(jsonObject.getJSONObject("numOfLinks").toString()); |
|---|
| 137 | int numOfLinkedInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedInstances").toString()); |
|---|
| 138 | int numOfOriginInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
|---|
| 139 | int numOfOriginClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfOriginInstances").toString()); |
|---|
| 140 | int numOfLinkedClassInstances = Integer.parseInt(jsonObject.getJSONObject("numOfLinkedClassInstances").toString()); |
|---|
| 141 | |
|---|
| 142 | ClassLink classLink =new ClassLink(propertyURI, linkedClassURI, linkedLiteralDatatypeURI, null, |
|---|
| 143 | numOfLinks, numOfOriginInstances, numOfLinkedInstances, |
|---|
| 144 | numOfOriginClassInstances, numOfLinkedClassInstances, |
|---|
| 145 | false, false); |
|---|
| 146 | list.add(classLink); |
|---|
| 147 | } |
|---|
| 148 | Path path = new Path(startClass, list, width); |
|---|
| 149 | return path; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | private String convertPath2SPARQL(Path path) throws Exception{ |
|---|
| 153 | if( path == null ){ |
|---|
| 154 | throw new Exception("Path is null."); |
|---|
| 155 | } |
|---|
| 156 | String startClass = path.getStartClass(); |
|---|
| 157 | List<ClassLink> classLinks = path.getClassLinks(); |
|---|
| 158 | |
|---|
| 159 | StringBuffer queryStr = new StringBuffer(); |
|---|
| 160 | StringBuffer selStr = new StringBuffer(); |
|---|
| 161 | StringBuffer whereStr = new StringBuffer(); |
|---|
| 162 | // if(num==0){ |
|---|
| 163 | int num = classLinks.size(); |
|---|
| 164 | // } |
|---|
| 165 | |
|---|
| 166 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
|---|
| 167 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
|---|
| 168 | |
|---|
| 169 | selStr.append("SELECT "); |
|---|
| 170 | whereStr.append("WHERE { \n"); |
|---|
| 171 | |
|---|
| 172 | String properties = null; |
|---|
| 173 | String objectClasses = null; |
|---|
| 174 | String subjectClasses = null; |
|---|
| 175 | Direction direction = null; |
|---|
| 176 | int i = 0; |
|---|
| 177 | int k = 0; |
|---|
| 178 | for (ClassLink link :classLinks ) |
|---|
| 179 | { |
|---|
| 180 | properties = link.getPropertyURI(); |
|---|
| 181 | objectClasses = link.getLinkedClassURI(); |
|---|
| 182 | direction = link.getDirection(); |
|---|
| 183 | |
|---|
| 184 | if (i==0) |
|---|
| 185 | subjectClasses = startClass; |
|---|
| 186 | |
|---|
| 187 | selStr.append("?c").append(i).append(" "); |
|---|
| 188 | selStr.append("?l").append(i).append(" "); |
|---|
| 189 | |
|---|
| 190 | if(i == classLinks.size()) |
|---|
| 191 | selStr.append("\n"); |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | whereStr.append("?c").append(i). |
|---|
| 195 | append(" rdf:type "). |
|---|
| 196 | append("<"). |
|---|
| 197 | append(subjectClasses). |
|---|
| 198 | append(">"). |
|---|
| 199 | append(".\n"); |
|---|
| 200 | |
|---|
| 201 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | if(direction == Direction.forward) |
|---|
| 205 | { |
|---|
| 206 | whereStr.append("?c").append(i).append(" "); |
|---|
| 207 | whereStr.append("<").append(properties).append("> "); |
|---|
| 208 | whereStr.append("?c").append(i+1).append(".\n"); |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | { |
|---|
| 212 | whereStr.append("?c").append(i+1).append(" "); |
|---|
| 213 | whereStr.append("<").append(properties).append("> "); |
|---|
| 214 | whereStr.append("?c").append(i).append(".\n"); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | subjectClasses = objectClasses; |
|---|
| 218 | i++; |
|---|
| 219 | k++; |
|---|
| 220 | if(k>=num){ |
|---|
| 221 | break; |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | selStr.append("?c").append(i).append(" \n"); |
|---|
| 226 | selStr.append("?l").append(i).append(" \n"); |
|---|
| 227 | whereStr.append("?c").append(i).append(" rdf:type "). |
|---|
| 228 | append("<"). |
|---|
| 229 | append(subjectClasses). |
|---|
| 230 | append(">"). |
|---|
| 231 | append(".\n"); |
|---|
| 232 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | queryStr.append(selStr).append(whereStr); |
|---|
| 236 | |
|---|
| 237 | queryStr.append("}"); |
|---|
| 238 | //OPTIONAL |
|---|
| 239 | queryStr.append("LIMIT 100\n");; |
|---|
| 240 | |
|---|
| 241 | //System.out.println(queryStr); |
|---|
| 242 | return queryStr.toString(); |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | } |
|---|
| 246 | } |
|---|