64 | | |
65 | | builder.getOWLClasses(null,keyword,"en",false); |
66 | | /* |
67 | | SClass[] clz = builder.getOWLClasses(graphURIs, keyword, false); |
68 | | for (SClass cls : clz) { |
69 | | System.out.println(cls); |
70 | | } |
71 | | clz = builder.countInstances(null, clz); |
72 | | for (SClass cls : clz) { |
73 | | System.out.println(cls); |
74 | | } |
75 | | */ |
76 | | |
77 | | |
78 | | builder.getOWLClasses(null, keyword, "en", false); |
| 64 | SClass[] clz = builder.getOWLClasses(null, keyword, "en", false); |
| 65 | if (clz != null) { |
| 66 | |
| 67 | for (SClass cls : clz) { |
| 68 | System.out.println(cls); |
| 69 | } |
| 70 | clz = builder.countInstances(null, clz); |
| 71 | for (SClass cls : clz) { |
| 72 | System.out.println(cls); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * clz = builder.listClasses(null, false); |
| 78 | * System.out.println(clz.length); for (SClass cls : clz) { |
| 79 | * System.out.println(cls); } |
| 80 | */ |
| 81 | |
| 82 | /* |
| 83 | * SClass[] clz = builder.getOWLClasses(graphURIs, keyword, false); for |
| 84 | * (SClass cls : clz) { System.out.println(cls); } clz = |
| 85 | * builder.countInstances(null, clz); for (SClass cls : clz) { |
| 86 | * System.out.println(cls); } |
| 87 | */ |
| 88 | |
| 89 | // builder.getOWLClasses(null, keyword, "en", false); |
188 | | |
189 | | // (LANG(?keywords) = 'en') && |
190 | | |
191 | | for (int i=0;i<keywords.length;i++) |
192 | | { |
193 | | if (i>0) queryStr.append(" or \n "); |
194 | | |
195 | | |
196 | | queryStr.append("regex(str(") |
197 | | .append("?keywords") |
198 | | .append("),\""); |
199 | | queryStr.append(keywords[i]); |
200 | | queryStr.append("\", \"i\" )"); |
201 | | |
202 | | } |
203 | | queryStr.append("))\n"); |
204 | | |
| 198 | |
| 199 | // (LANG(?keywords) = 'en') && |
| 200 | |
| 201 | for (int i = 0; i < keywords.length; i++) { |
| 202 | if (i > 0) |
| 203 | queryStr.append(" || \n "); |
| 204 | |
| 205 | queryStr.append("regex(str(").append("?keywords") |
| 206 | .append("),\""); |
| 207 | queryStr.append(keywords[i]); |
| 208 | queryStr.append("\", \"i\" )"); |
| 209 | |
| 210 | } |
| 211 | queryStr.append("))\n"); |
| 212 | |
214 | | /* |
215 | | * Query query = QueryFactory.create(queryStr.toString()); |
216 | | * |
217 | | * |
218 | | * QueryExecution qexec = null; try{ long start = |
219 | | * System.currentTimeMillis(); qexec = |
220 | | * QueryExecutionFactory.sparqlService(endpointURI, query); long end = |
221 | | * System.currentTimeMillis(); System.out.println("EXEC TIME: " + |
222 | | * (end-start)); }catch(Exception ex){ ex.printStackTrace(); throw ex; } |
223 | | * |
224 | | * ResultSet results = qexec.execSelect(); HashMap<String, SClass> |
225 | | * classMap = new HashMap<String, SClass>(); for (; results.hasNext();) |
226 | | * { QuerySolution sol = results.next(); Resource res = |
227 | | * sol.getResource("c"); if (res != null) { String uri = res.getURI(); |
228 | | * int numOfInstances = 0; if (countInstances) { numOfInstances = |
229 | | * sol.getLiteral("numOfInstances").getInt(); } // |
230 | | * System.out.println(numOfInstances); Literal labelLiteral = |
231 | | * sol.getLiteral("pLabel"); SClass sClass = null; if |
232 | | * (classMap.containsKey(uri)) { sClass = classMap.get(uri); } else { |
233 | | * sClass = new SClass(uri, null, numOfInstances); classMap.put(uri, |
234 | | * sClass); } if (labelLiteral != null) { String label = |
235 | | * labelLiteral.getString(); String lang = labelLiteral.getLanguage(); |
236 | | * sClass.addLabel(new Label(label, lang)); } } } qexec.close(); return |
237 | | * classMap.values().toArray(new SClass[0]); |
238 | | */ |
239 | | return null; |
| 222 | Query query = QueryFactory.create(queryStr.toString()); |
| 223 | |
| 224 | QueryExecution qexec = null; |
| 225 | ResultSet results = null; |
| 226 | try { |
| 227 | long start = System.currentTimeMillis(); |
| 228 | qexec = QueryExecutionFactory.sparqlService(endpointURI, query); |
| 229 | results = qexec.execSelect(); |
| 230 | long end = System.currentTimeMillis(); |
| 231 | System.out.println("EXEC TIME: " + (end - start)); |
| 232 | } catch (Exception ex) { |
| 233 | ex.printStackTrace(); |
| 234 | throw ex; |
| 235 | } |
| 236 | |
| 237 | HashMap<String, SClass> classMap = new HashMap<String, SClass>(); |
| 238 | for (; results.hasNext();) { |
| 239 | QuerySolution sol = results.next(); |
| 240 | Resource res = sol.getResource("c"); |
| 241 | if (res != null) { |
| 242 | String uri = res.getURI(); |
| 243 | int numOfInstances = 0; |
| 244 | if (countInstances) { |
| 245 | numOfInstances = sol.getLiteral("numOfInstances").getInt(); |
| 246 | } // |
| 247 | System.out.println(numOfInstances); |
| 248 | Literal labelLiteral = sol.getLiteral("pLabel"); |
| 249 | SClass sClass = null; |
| 250 | if (classMap.containsKey(uri)) { |
| 251 | sClass = classMap.get(uri); |
| 252 | } else { |
| 253 | sClass = new SClass(uri, null, numOfInstances); |
| 254 | classMap.put(uri, sClass); |
| 255 | } |
| 256 | if (labelLiteral != null) { |
| 257 | String label = labelLiteral.getString(); |
| 258 | String lang = labelLiteral.getLanguage(); |
| 259 | sClass.addLabel(new Label(label, lang)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | qexec.close(); |
| 264 | return classMap.values().toArray(new SClass[0]); |
| 265 | |
244 | | |
245 | | // TODO |
246 | | return null; |
| 270 | StringBuffer queryStr = new StringBuffer(); |
| 271 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); |
| 272 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); |
| 273 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); |
| 274 | if (countInstances) { |
| 275 | queryStr.append("SELECT DISTINCT ?cls ?pLabel (COUNT(?i) AS ?numOfInstances)\n"); |
| 276 | } else { |
| 277 | queryStr.append("SELECT DISTINCT ?cls ?pLabel \n"); |
| 278 | } |
| 279 | |
| 280 | if (graphURIs != null) { |
| 281 | for (String graphURI : graphURIs) { |
| 282 | queryStr.append("FROM <"); |
| 283 | queryStr.append(graphURI); |
| 284 | queryStr.append(">\n"); |
| 285 | } |
| 286 | } |
| 287 | queryStr.append("WHERE{\n"); |
| 288 | queryStr.append("\t{ ?cls rdf:type rdfs:Class. }\n"); |
| 289 | queryStr.append("\tUNION\n"); |
| 290 | queryStr.append("\t{ ?cls rdf:type owl:Class. }\n"); |
| 291 | queryStr.append("\tUNION\n"); |
| 292 | queryStr.append("\t{ [] rdfs:type ?cls. }\n"); |
| 293 | queryStr.append("\tUNION\n"); |
| 294 | queryStr.append("\t{ [] rdfs:domain ?cls. }\n"); |
| 295 | queryStr.append("\tUNION\n"); |
| 296 | queryStr.append("\t{ [] rdfs:range ?cls. }\n"); |
| 297 | queryStr.append("\tUNION\n"); |
| 298 | queryStr.append("\t{ ?cls rdfs:subclassOf []. }\n"); |
| 299 | queryStr.append("\t?cls rdfs:label ?pLabel.\n"); |
| 300 | if (countInstances) { |
| 301 | queryStr.append("\t?i rdf:type ?c.\n"); |
| 302 | } |
| 303 | if (countInstances) { |
| 304 | queryStr.append("} GROUP BY ?c ?pLabel"); |
| 305 | } else { |
| 306 | queryStr.append("}"); |
| 307 | } |
| 308 | System.out.println(queryStr.toString()); |
| 309 | |
| 310 | Query query = QueryFactory.create(queryStr.toString()); |
| 311 | |
| 312 | QueryExecution qexec = null; |
| 313 | ResultSet results = null; |
| 314 | try { |
| 315 | long start = System.currentTimeMillis(); |
| 316 | qexec = QueryExecutionFactory.sparqlService(endpointURI, query); |
| 317 | results = qexec.execSelect(); |
| 318 | long end = System.currentTimeMillis(); |
| 319 | System.out.println("EXEC TIME: " + (end - start)); |
| 320 | } catch (Exception ex) { |
| 321 | ex.printStackTrace(); |
| 322 | throw ex; |
| 323 | } |
| 324 | |
| 325 | HashMap<String, SClass> classMap = new HashMap<String, SClass>(); |
| 326 | for (; results.hasNext();) { |
| 327 | QuerySolution sol = results.next(); |
| 328 | Resource res = sol.getResource("cls"); |
| 329 | if (res != null) { |
| 330 | String uri = res.getURI(); |
| 331 | int numOfInstances = 0; |
| 332 | if (countInstances) { |
| 333 | numOfInstances = sol.getLiteral("numOfInstances").getInt(); |
| 334 | } |
| 335 | // System.out.println(numOfInstances); |
| 336 | Literal labelLiteral = sol.getLiteral("pLabel"); |
| 337 | SClass sClass = null; |
| 338 | if (classMap.containsKey(uri)) { |
| 339 | sClass = classMap.get(uri); |
| 340 | } else { |
| 341 | sClass = new SClass(uri, null, numOfInstances); |
| 342 | classMap.put(uri, sClass); |
| 343 | } |
| 344 | if (labelLiteral != null) { |
| 345 | String label = labelLiteral.getString(); |
| 346 | String lang = labelLiteral.getLanguage(); |
| 347 | sClass.addLabel(new Label(label, lang)); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | qexec.close(); |
| 352 | return classMap.values().toArray(new SClass[0]); |
| 353 | |