257 | | |
258 | | //TODO |
259 | | return null; |
| 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:domain ?cls. }\n"); |
| 280 | queryStr.append("\tUNION\n"); |
| 281 | queryStr.append("\t{ [] rdfs:range ?cls. }\n"); |
| 282 | queryStr.append("\tUNION\n"); |
| 283 | queryStr.append("\t{ ?cls rdfs:subclassOf []. }\n"); |
| 284 | queryStr.append("\t?cls rdfs:label ?pLabel.\n"); |
| 285 | if (countInstances) { |
| 286 | queryStr.append("\t?i rdf:type ?c.\n"); |
| 287 | } |
| 288 | if (countInstances) { |
| 289 | queryStr.append("} GROUP BY ?c ?pLabel"); |
| 290 | } else { |
| 291 | queryStr.append("}"); |
| 292 | } |
| 293 | System.out.println(queryStr.toString()); |
| 294 | |
| 295 | Query query = QueryFactory.create(queryStr.toString()); |
| 296 | |
| 297 | |
| 298 | QueryExecution qexec = null; |
| 299 | try{ |
| 300 | long start = System.currentTimeMillis(); |
| 301 | qexec = QueryExecutionFactory.sparqlService(endpointURI, |
| 302 | query); |
| 303 | long end = System.currentTimeMillis(); |
| 304 | System.out.println("EXEC TIME: " + (end-start)); |
| 305 | }catch(Exception ex){ |
| 306 | ex.printStackTrace(); |
| 307 | throw ex; |
| 308 | } |
| 309 | |
| 310 | ResultSet results = qexec.execSelect(); |
| 311 | HashMap<String, SClass> classMap = new HashMap<String, SClass>(); |
| 312 | for (; results.hasNext();) { |
| 313 | QuerySolution sol = results.next(); |
| 314 | Resource res = sol.getResource("c"); |
| 315 | if (res != null) { |
| 316 | String uri = res.getURI(); |
| 317 | int numOfInstances = 0; |
| 318 | if (countInstances) { |
| 319 | numOfInstances = sol.getLiteral("numOfInstances").getInt(); |
| 320 | } |
| 321 | // System.out.println(numOfInstances); |
| 322 | Literal labelLiteral = sol.getLiteral("pLabel"); |
| 323 | SClass sClass = null; |
| 324 | if (classMap.containsKey(uri)) { |
| 325 | sClass = classMap.get(uri); |
| 326 | } else { |
| 327 | sClass = new SClass(uri, null, numOfInstances); |
| 328 | classMap.put(uri, sClass); |
| 329 | } |
| 330 | if (labelLiteral != null) { |
| 331 | String label = labelLiteral.getString(); |
| 332 | String lang = labelLiteral.getLanguage(); |
| 333 | sClass.addLabel(new Label(label, lang)); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | qexec.close(); |
| 338 | return classMap.values().toArray(new SClass[0]); |