257 | | StringBuffer queryStr = new StringBuffer(); |
258 | | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); |
259 | | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
260 | | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
261 | | if (countInstances) { |
262 | | queryStr.append("SELECT DISTINCT ?cls ?pLabel (COUNT(?i) AS ?numOfInstances)\n"); |
263 | | } else { |
264 | | queryStr.append("SELECT DISTINCT ?cls ?pLabel \n"); |
265 | | } |
266 | | |
267 | | if (graphURIs != null) { |
268 | | for (String graphURI : graphURIs) { |
269 | | queryStr.append("FROM <"); |
270 | | queryStr.append(graphURI); |
271 | | queryStr.append(">\n"); |
272 | | } |
273 | | } |
274 | | queryStr.append("WHERE{\n"); |
275 | | queryStr.append("\t{ ?cls rdf:type rdfs:Class. }\n"); |
276 | | queryStr.append("\tUNION\n"); |
277 | | queryStr.append("\t{ ?cls rdf:type owl:Class. }\n"); |
278 | | queryStr.append("\tUNION\n"); |
279 | | queryStr.append("\t{ [] rdfs:type ?cls. }\n"); |
280 | | queryStr.append("\tUNION\n"); |
281 | | queryStr.append("\t{ [] rdfs:domain ?cls. }\n"); |
282 | | queryStr.append("\tUNION\n"); |
283 | | queryStr.append("\t{ [] rdfs:range ?cls. }\n"); |
284 | | queryStr.append("\tUNION\n"); |
285 | | queryStr.append("\t{ ?cls rdfs:subclassOf []. }\n"); |
286 | | queryStr.append("\t?cls rdfs:label ?pLabel.\n"); |
287 | | if (countInstances) { |
288 | | queryStr.append("\t?i rdf:type ?c.\n"); |
289 | | } |
290 | | if (countInstances) { |
291 | | queryStr.append("} GROUP BY ?c ?pLabel"); |
292 | | } else { |
293 | | queryStr.append("}"); |
294 | | } |
295 | | System.out.println(queryStr.toString()); |
296 | | |
297 | | Query query = QueryFactory.create(queryStr.toString()); |
298 | | |
299 | | |
300 | | QueryExecution qexec = null; |
301 | | try{ |
302 | | long start = System.currentTimeMillis(); |
303 | | qexec = QueryExecutionFactory.sparqlService(endpointURI, |
304 | | query); |
305 | | long end = System.currentTimeMillis(); |
306 | | System.out.println("EXEC TIME: " + (end-start)); |
307 | | }catch(Exception ex){ |
308 | | ex.printStackTrace(); |
309 | | throw ex; |
310 | | } |
311 | | |
312 | | ResultSet results = qexec.execSelect(); |
313 | | HashMap<String, SClass> classMap = new HashMap<String, SClass>(); |
314 | | for (; results.hasNext();) { |
315 | | QuerySolution sol = results.next(); |
316 | | Resource res = sol.getResource("c"); |
317 | | if (res != null) { |
318 | | String uri = res.getURI(); |
319 | | int numOfInstances = 0; |
320 | | if (countInstances) { |
321 | | numOfInstances = sol.getLiteral("numOfInstances").getInt(); |
322 | | } |
323 | | // System.out.println(numOfInstances); |
324 | | Literal labelLiteral = sol.getLiteral("pLabel"); |
325 | | SClass sClass = null; |
326 | | if (classMap.containsKey(uri)) { |
327 | | sClass = classMap.get(uri); |
328 | | } else { |
329 | | sClass = new SClass(uri, null, numOfInstances); |
330 | | classMap.put(uri, sClass); |
331 | | } |
332 | | if (labelLiteral != null) { |
333 | | String label = labelLiteral.getString(); |
334 | | String lang = labelLiteral.getLanguage(); |
335 | | sClass.addLabel(new Label(label, lang)); |
336 | | } |
337 | | } |
338 | | } |
339 | | qexec.close(); |
340 | | return classMap.values().toArray(new SClass[0]); |
| 270 | |
| 271 | //TODO |
| 272 | return null; |