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 static List<String> convertJ2Path(String jpath) throws JSONException{ |
---|
124 | List <String> list = null; |
---|
125 | String temp =(String) jpath.subSequence(2, jpath.length()-2); |
---|
126 | // if (temp.contains(","")) |
---|
127 | list =Arrays.asList(temp.split("\",\"")); |
---|
128 | return list; |
---|
129 | |
---|
130 | /* JSONArray classLinks=new JSONArray(jpath); |
---|
131 | String string; |
---|
132 | |
---|
133 | for (int i=0;i<classLinks.length();i++) |
---|
134 | { |
---|
135 | string = classLinks.getJSONObject(i).toString(); |
---|
136 | if (string.contains(",")) { |
---|
137 | list =Arrays.asList(string.split(",")); |
---|
138 | |
---|
139 | } else |
---|
140 | throw new IllegalArgumentException("path error"); |
---|
141 | |
---|
142 | }*/ |
---|
143 | |
---|
144 | // return list; |
---|
145 | } |
---|
146 | |
---|
147 | private static String convertPath2SPARQL(List<String> path) throws Exception{ |
---|
148 | if( path == null ){ |
---|
149 | throw new Exception("Path is null."); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | //List<String> classLinks = path.getClassLinks(); |
---|
154 | |
---|
155 | StringBuffer queryStr = new StringBuffer(); |
---|
156 | StringBuffer selStr = new StringBuffer(); |
---|
157 | StringBuffer whereStr = new StringBuffer(); |
---|
158 | // if(num==0){ |
---|
159 | // int num = classLinks.size(); |
---|
160 | // } |
---|
161 | |
---|
162 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
---|
163 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
---|
164 | |
---|
165 | selStr.append("SELECT "); |
---|
166 | whereStr.append("WHERE { \n"); |
---|
167 | |
---|
168 | String properties = null; |
---|
169 | String objectClasses = null; |
---|
170 | String subjectClasses = null; |
---|
171 | Direction direction = null; |
---|
172 | int i = 0; |
---|
173 | int k = 0; |
---|
174 | |
---|
175 | String startClass = path.get(path.size()-1); |
---|
176 | List<String> classLinks = null; |
---|
177 | // ArrayList<String> array= (String[])path.toArray(); |
---|
178 | |
---|
179 | // classLinks.add(path.get(i)); |
---|
180 | |
---|
181 | int num =(path.size()-1)/2; |
---|
182 | |
---|
183 | for(int j=path.size()-2;j>0;j=j-2) |
---|
184 | { |
---|
185 | // properties = link.getPropertyURI(); |
---|
186 | // objectClasses = link.getLinkedClassURI(); |
---|
187 | // direction = link.getDirection(); |
---|
188 | properties = path.get(j); |
---|
189 | objectClasses = path.get(j-1); |
---|
190 | direction = Direction.forward; |
---|
191 | |
---|
192 | if (i==0) |
---|
193 | subjectClasses = startClass; |
---|
194 | |
---|
195 | selStr.append("?c").append(i).append(" "); |
---|
196 | selStr.append("?l").append(i).append(" "); |
---|
197 | |
---|
198 | // if(i == path.size()) |
---|
199 | // selStr.append("\n"); |
---|
200 | |
---|
201 | |
---|
202 | whereStr.append("?c").append(i). |
---|
203 | append(" rdf:type "). |
---|
204 | append("<"). |
---|
205 | append(subjectClasses). |
---|
206 | append(">"). |
---|
207 | append(".\n"); |
---|
208 | |
---|
209 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
210 | |
---|
211 | |
---|
212 | if(direction == Direction.forward) |
---|
213 | { |
---|
214 | whereStr.append("?c").append(i).append(" "); |
---|
215 | whereStr.append("<").append(properties).append("> "); |
---|
216 | whereStr.append("?c").append(i+1).append(".\n"); |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | whereStr.append("?c").append(i+1).append(" "); |
---|
221 | whereStr.append("<").append(properties).append("> "); |
---|
222 | whereStr.append("?c").append(i).append(".\n"); |
---|
223 | } |
---|
224 | |
---|
225 | subjectClasses = objectClasses; |
---|
226 | i++; |
---|
227 | k++; |
---|
228 | if(k>=num){ |
---|
229 | break; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | selStr.append("?c").append(i).append(" \n"); |
---|
234 | selStr.append("?l").append(i).append(" \n"); |
---|
235 | whereStr.append("?c").append(i).append(" rdf:type "). |
---|
236 | append("<"). |
---|
237 | append(subjectClasses). |
---|
238 | append(">"). |
---|
239 | append(".\n"); |
---|
240 | whereStr.append("OPTIONAL{\n?c"+i+" rdfs:label ?l"+i+".}\n"); |
---|
241 | |
---|
242 | |
---|
243 | queryStr.append(selStr).append(whereStr); |
---|
244 | |
---|
245 | queryStr.append("}"); |
---|
246 | //OPTIONAL |
---|
247 | queryStr.append("LIMIT 100\n");; |
---|
248 | |
---|
249 | System.out.println(queryStr); |
---|
250 | return queryStr.toString(); |
---|
251 | |
---|
252 | |
---|
253 | } |
---|
254 | |
---|
255 | } |
---|
256 | |
---|