- 更新日時:
- 2014/03/13 16:02:20 (11 年 前)
- パス:
- BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL
- ファイル:
-
- 2 変更
凡例:
- 変更なし
- 追加
- 削除
-
BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/OWLQueryBuilder.java
r55 r59 25 25 * @since 28.01.2014 26 26 */ 27 public SClass[] getOWLClasses(String[] graphURIs, String[] keyword, boolean countInstances) throws Exception;27 public SClass[] getOWLClasses(String[] graphURIs, String[] keyword, String language, boolean countInstances) throws Exception; 28 28 29 29 public SClass[] listClasses(String[] graphURIs, boolean countInstances) throws Exception; -
BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/OWLQueryBuilderImpl.java
r58 r59 55 55 String sparqlEndpoint = "http://lsd.dbcls.jp/sparql"; 56 56 // String keyword = "artiste"; 57 String[] keyword = { "Player","Soccer"};57 String[] keyword = { "Player", "Soccer" }; 58 58 String[] graphURIs = new String[0]; 59 59 60 // keyword = null;60 // keyword = null; 61 61 62 62 OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint); 63 64 builder.getOWLClasses(null,keyword,false); 65 /* 66 SClass[] clz = builder.getOWLClasses(graphURIs, keyword, false); 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 /* System.out.println("CLS"); 78 79 String uri = "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry"; 80 81 ClassLink[] cls = null; 82 try{ 83 cls = builder.getNextClass(null, uri, 100, false); 84 }catch(Exception ex) { 85 ex.printStackTrace(); 86 } 87 88 System.out.println("Solutions: "); 89 if (cls != null) { 90 for (ClassLink cl : cls) { 91 System.out.println(cl.toString()); 92 } 93 cls = builder.countLinks(null, uri, cls); 94 for (ClassLink cl : cls) { 95 System.out.println(cl.toString()); 96 } 97 } 98 */ 99 /* 100 101 String uri = "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry"; 102 103 ClassLink[] cls = null; 104 105 System.out.println("CLS-INS"); 106 cls = null; 107 try{ 108 cls = builder.getNextClassViaInstanceLink(null, uri, 100); 109 }catch(Exception ex){ 110 ex.printStackTrace(); 111 } 112 if (cls != null) { 113 for (ClassLink cl : cls) { 114 System.out.println(cl.toString()); 115 } 116 } 117 */ 63 64 builder.getOWLClasses(null, keyword, false); 118 65 /* 119 System.out.println("Instances"); 120 Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en"); 121 if (ins != null) { 122 for (Instance in : ins) { 123 System.out.println(in.toString()); 124 } 125 } 126 */ 66 * SClass[] clz = builder.getOWLClasses(graphURIs, keyword, false); for 67 * (SClass cls : clz) { System.out.println(cls); } clz = 68 * builder.countInstances(null, clz); for (SClass cls : clz) { 69 * System.out.println(cls); } 70 */ 71 72 /* 73 * System.out.println("CLS"); 74 * 75 * String uri = 76 * "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry"; 77 * 78 * ClassLink[] cls = null; try{ cls = builder.getNextClass(null, uri, 79 * 100, false); }catch(Exception ex) { ex.printStackTrace(); } 80 * 81 * System.out.println("Solutions: "); if (cls != null) { for (ClassLink 82 * cl : cls) { System.out.println(cl.toString()); } cls = 83 * builder.countLinks(null, uri, cls); for (ClassLink cl : cls) { 84 * System.out.println(cl.toString()); } } 85 */ 86 /* 87 * 88 * String uri = 89 * "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry"; 90 * 91 * ClassLink[] cls = null; 92 * 93 * System.out.println("CLS-INS"); cls = null; try{ cls = 94 * builder.getNextClassViaInstanceLink(null, uri, 100); }catch(Exception 95 * ex){ ex.printStackTrace(); } if (cls != null) { for (ClassLink cl : 96 * cls) { System.out.println(cl.toString()); } } 97 */ 98 /* 99 * System.out.println("Instances"); Instance[] ins = 100 * builder.getInstances(null, "\"A.C. Reed\"@en"); if (ins != null) { 101 * for (Instance in : ins) { System.out.println(in.toString()); } } 102 */ 127 103 /* 128 104 * System.out.println("INS-INS"); ins = builder.getInstances(null, … … 136 112 } 137 113 138 139 140 141 114 /** 142 115 * 譏守、コ逧�↓RDF縺ァ譖ク縺九l縺ヲ縺�k繧ッ繝ゥ繧ケ繧貞叙蠕励☆繧� … … 153 126 * @since 28.01.2014 154 127 */ 155 public SClass[] getOWLClasses(String[] graphURIs, String[] keywords, 128 public SClass[] getOWLClasses(String[] graphURIs, String[] keywords,縲String language, 156 129 boolean countInstances) throws Exception { 157 130 StringBuffer queryStr = new StringBuffer(); … … 173 146 queryStr.append("WHERE{\n"); 174 147 175 // 148 // 176 149 queryStr.append(" {?c rdf:type rdfs:Class} UNION {?c rdf:type owl:Class}\n"); 177 // queryStr.append(" ?i rdf:type ?c.\n"); 178 179 150 // queryStr.append(" ?i rdf:type ?c.\n"); 151 180 152 if (countInstances) { 181 153 queryStr.append(" ?c rdfs:label ?label.\n"); … … 188 160 // queryStr.append(keyword); 189 161 // queryStr.append("."); 190 191 // TODO 162 163 // TODO OR relationship between keywords 192 164 if (keywords != null && keywords.length != 0) { 193 194 165 queryStr.append(" ?c rdfs:label "); 195 queryStr.append("?keywords") 196 .append(".\n"); 197 queryStr.append(" filter("); 198 199 for (int i=0;i<keywords.length;i++) 200 { 201 if (i>0) queryStr.append(" or \n "); 202 203 queryStr.append("regex(str(") 204 .append("?keywords") 205 .append("),\""); 206 queryStr.append(keywords[i]); 207 queryStr.append("\", \"i\" )"); 208 209 } 210 queryStr.append(")\n"); 211 212 } 213 166 queryStr.append("?keywords").append(".\n"); 167 queryStr.append(" filter("); 168 169 for (int i = 0; i < keywords.length; i++) { 170 if (i > 0) 171 queryStr.append(" or \n "); 172 173 queryStr.append("regex(str(").append("?keywords") 174 .append("),\""); 175 queryStr.append(keywords[i]); 176 queryStr.append("\", \"i\" )"); 177 } 178 queryStr.append(")\n"); 179 180 } 181 214 182 if (countInstances) { 215 183 queryStr.append("} GROUP BY ?c ?pLabel"); … … 218 186 } 219 187 System.out.println(queryStr.toString()); 220 221 222 /* Query query = QueryFactory.create(queryStr.toString()); 223 224 225 QueryExecution qexec = null; 226 try{ 227 long start = System.currentTimeMillis(); 228 qexec = QueryExecutionFactory.sparqlService(endpointURI, 229 query); 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 ResultSet results = qexec.execSelect(); 238 HashMap<String, SClass> classMap = new HashMap<String, SClass>(); 239 for (; results.hasNext();) { 240 QuerySolution sol = results.next(); 241 Resource res = sol.getResource("c"); 242 if (res != null) { 243 String uri = res.getURI(); 244 int numOfInstances = 0; 245 if (countInstances) { 246 numOfInstances = sol.getLiteral("numOfInstances").getInt(); 247 } 248 // System.out.println(numOfInstances); 249 Literal labelLiteral = sol.getLiteral("pLabel"); 250 SClass sClass = null; 251 if (classMap.containsKey(uri)) { 252 sClass = classMap.get(uri); 253 } else { 254 sClass = new SClass(uri, null, numOfInstances); 255 classMap.put(uri, sClass); 256 } 257 if (labelLiteral != null) { 258 String label = labelLiteral.getString(); 259 String lang = labelLiteral.getLanguage(); 260 sClass.addLabel(new Label(label, lang)); 261 } 262 } 263 } 264 qexec.close(); 265 return classMap.values().toArray(new SClass[0]);*/ 188 189 /* 190 * Query query = QueryFactory.create(queryStr.toString()); 191 * 192 * 193 * QueryExecution qexec = null; try{ long start = 194 * System.currentTimeMillis(); qexec = 195 * QueryExecutionFactory.sparqlService(endpointURI, query); long end = 196 * System.currentTimeMillis(); System.out.println("EXEC TIME: " + 197 * (end-start)); }catch(Exception ex){ ex.printStackTrace(); throw ex; } 198 * 199 * ResultSet results = qexec.execSelect(); HashMap<String, SClass> 200 * classMap = new HashMap<String, SClass>(); for (; results.hasNext();) 201 * { QuerySolution sol = results.next(); Resource res = 202 * sol.getResource("c"); if (res != null) { String uri = res.getURI(); 203 * int numOfInstances = 0; if (countInstances) { numOfInstances = 204 * sol.getLiteral("numOfInstances").getInt(); } // 205 * System.out.println(numOfInstances); Literal labelLiteral = 206 * sol.getLiteral("pLabel"); SClass sClass = null; if 207 * (classMap.containsKey(uri)) { sClass = classMap.get(uri); } else { 208 * sClass = new SClass(uri, null, numOfInstances); classMap.put(uri, 209 * sClass); } if (labelLiteral != null) { String label = 210 * labelLiteral.getString(); String lang = labelLiteral.getLanguage(); 211 * sClass.addLabel(new Label(label, lang)); } } } qexec.close(); return 212 * classMap.values().toArray(new SClass[0]); 213 */ 266 214 return null; 267 215 } 268 216 269 public SClass[] listClasses(String[] graphURIs, boolean countInstances) throws Exception{ 270 271 //TODO 217 public SClass[] listClasses(String[] graphURIs, boolean countInstances) 218 throws Exception { 219 220 // TODO 272 221 return null; 273 222 } 274 223 275 276 277 224 /** 278 225 * 縲繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧貞叙蠕励☆繧� … … 368 315 public ClassLink[] getNextClass(String[] graphURIs, String originClass, 369 316 int limit, boolean countLinks) throws Exception { 370 ClassLink[] cLinks = getNextClassSub(graphURIs, originClass, limit, false); 371 if( countLinks ){ 317 ClassLink[] cLinks = getNextClassSub(graphURIs, originClass, limit, 318 false); 319 if (countLinks) { 372 320 cLinks = countLinks(graphURIs, originClass, cLinks); 373 321 } … … 375 323 } 376 324 377 private ClassLink[] getNextClassSub(String[] graphURIs, String originClass, 325 private ClassLink[] getNextClassSub(String[] graphURIs, String originClass, 378 326 int limit, boolean countLinks) throws Exception { 379 327 StringBuffer queryStr = new StringBuffer(); 380 328 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); 381 329 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); … … 434 382 queryStr.append(" ?p ?pDirection ?c.\n"); 435 383 436 //queryStr.append(" ?s rdf:type ?c.\n");384 // queryStr.append(" ?s rdf:type ?c.\n"); 437 385 438 386 if (countLinks) { … … 452 400 Query query = QueryFactory.create(queryStr.toString()); 453 401 QueryExecution qexec = null; 454 try{ 455 qexec = QueryExecutionFactory.sparqlService(endpointURI, 456 query); 457 }catch(Exception ex){ 402 try { 403 qexec = QueryExecutionFactory.sparqlService(endpointURI, query); 404 } catch (Exception ex) { 458 405 ex.printStackTrace(); 459 406 throw ex; 460 407 } 461 408 462 409 ResultSet results = null; 463 try {410 try { 464 411 long start = System.currentTimeMillis(); 465 412 results = qexec.execSelect(); 466 413 long end = System.currentTimeMillis(); 467 System.out.println("EXEC TIME: " + (end -start));468 } catch(Exception ex){414 System.out.println("EXEC TIME: " + (end - start)); 415 } catch (Exception ex) { 469 416 ex.printStackTrace(); 470 417 throw ex; 471 418 } 472 419 473 420 ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>(); 474 421 for (; results.hasNext();) { … … 505 452 } 506 453 ClassLink cl = new ClassLink(proURI, clsURI, direction, 507 numOfLinks, 0, 0, 0, 0 454 numOfLinks, 0, 0, 0, 0); 508 455 solCLs.add(cl); 509 456 } … … 559 506 } 560 507 561 System.out.println(queryStr.toString());508 System.out.println(queryStr.toString()); 562 509 563 510 Query query = QueryFactory.create(queryStr.toString()); … … 566 513 567 514 ResultSet results = null; 568 try {515 try { 569 516 long start = System.currentTimeMillis(); 570 517 results = qexec.execSelect(); 571 518 long end = System.currentTimeMillis(); 572 System.out.println("EXEC TIME: " + (end -start));573 } catch(Exception ex){519 System.out.println("EXEC TIME: " + (end - start)); 520 } catch (Exception ex) { 574 521 ex.printStackTrace(); 575 522 throw ex; … … 591 538 numOfLinks = sol.getLiteral("numOfForwardLinks").getInt(); 592 539 numOfLinks += sol.getLiteral("numOfReverseLinks").getInt(); 593 numOfLinkedInstances = sol.getLiteral("numOfLinkedInstances").getInt(); 594 numOfOriginInstances = sol.getLiteral("numOfOriginInstances").getInt(); 540 numOfLinkedInstances = sol.getLiteral( 541 "numOfLinkedInstances").getInt(); 542 numOfOriginInstances = sol.getLiteral( 543 "numOfOriginInstances").getInt(); 595 544 direction = Direction.both; 596 545 } else { 597 numOfLinkedInstances = sol.getLiteral("numOfLinkedInstances").getInt(); 598 numOfOriginInstances = sol.getLiteral("numOfOriginInstances").getInt(); 546 numOfLinkedInstances = sol.getLiteral( 547 "numOfLinkedInstances").getInt(); 548 numOfOriginInstances = sol.getLiteral( 549 "numOfOriginInstances").getInt(); 599 550 numOfLinks = sol.getLiteral("numOfForwardLinks").getInt(); 600 551 direction = Direction.forward; … … 604 555 direction = Direction.reverse; 605 556 propURI = proReverse.getURI(); 606 numOfLinkedInstances = sol.getLiteral("numOfOriginInstances").getInt(); 607 numOfOriginInstances = sol.getLiteral("numOfLinkedInstances").getInt(); 557 numOfLinkedInstances = sol.getLiteral("numOfOriginInstances") 558 .getInt(); 559 numOfOriginInstances = sol.getLiteral("numOfLinkedInstances") 560 .getInt(); 608 561 numOfLinks = sol.getLiteral("numOfReverseLinks").getInt(); 609 562 } … … 611 564 // System.out.println(propURI + " " + clsURI + " " + direction); 612 565 613 ClassLink cl = new ClassLink(propURI, clsURI, direction, numOfLinks, numOfOriginInstances, numOfLinkedInstances, 0, 0); 566 ClassLink cl = new ClassLink(propURI, clsURI, direction, 567 numOfLinks, numOfOriginInstances, numOfLinkedInstances, 0, 568 0); 614 569 solCLs.add(cl); 615 570 } … … 889 844 if (lit != null) { 890 845 int numOfOriginClassInstances = lit.getInt(); 891 classLink.setNumOfOriginClassInstances(numOfOriginClassInstances); 846 classLink 847 .setNumOfOriginClassInstances(numOfOriginClassInstances); 892 848 } 893 849 lit = sol.getLiteral("numOfLinkedClassInstances"); 894 850 if (lit != null) { 895 851 int numOfLinkedClassInstances = lit.getInt(); 896 classLink.setNumOfLinkedClassInstances(numOfLinkedClassInstances); 852 classLink 853 .setNumOfLinkedClassInstances(numOfLinkedClassInstances); 897 854 } 898 855 } … … 933 890 } 934 891 } 935 892 936 893 System.out.println(queryStr.toString()); 937 894 938 895 query = QueryFactory.create(queryStr.toString()); 939 qexec = QueryExecutionFactory.sparqlService( 940 endpointURI, query); 896 qexec = QueryExecutionFactory.sparqlService(endpointURI, query); 941 897 942 898 results = qexec.execSelect(); … … 947 903 if (lit != null) { 948 904 int numOfOriginClassInstances = lit.getInt(); 949 classLink.setNumOfOriginClassInstances(numOfOriginClassInstances); 905 classLink 906 .setNumOfOriginClassInstances(numOfOriginClassInstances); 950 907 } 951 908 } … … 986 943 } 987 944 } 988 945 989 946 System.out.println(queryStr.toString()); 990 947 991 948 query = QueryFactory.create(queryStr.toString()); 992 qexec = QueryExecutionFactory.sparqlService( 993 endpointURI, query); 949 qexec = QueryExecutionFactory.sparqlService(endpointURI, query); 994 950 995 951 results = qexec.execSelect(); … … 1000 956 if (lit != null) { 1001 957 int numOfLinkedClassInstances = lit.getInt(); 1002 classLink.setNumOfLinkedClassInstances(numOfLinkedClassInstances); 958 classLink 959 .setNumOfLinkedClassInstances(numOfLinkedClassInstances); 1003 960 } 1004 961 } 1005 962 qexec.close(); 1006 963 1007 964 } 1008 965 return classLinks; 1009 966 } 1010 967 1011 1012 968 public SClass[] countInstances(String[] graphURIs, SClass[] classes) 1013 969 throws Exception {