チェンジセット 19 : BH13SPARQLBuilder
- 更新日時:
- 2014/01/30 15:00:30 (11 年 前)
- パス:
- BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL
- ファイル:
-
- 1 追加
- 2 変更
凡例:
- 変更なし
- 追加
- 削除
-
BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/Label.java
r10 r19 5 5 private String label = null; 6 6 private String language = null; 7 8 9 public Label(String label, String language){ 7 8 public boolean equals(Object oLabel) { 9 if( oLabel == null ){ 10 return false; 11 } 12 Label loLabel = (Label) oLabel; 13 if (label != null) { 14 if (!label.equals(loLabel.getLabel())) { 15 return false; 16 } 17 } else { 18 if (loLabel.getLabel() != null) { 19 return false; 20 } 21 } 22 23 if (language != null) { 24 return language.equals(loLabel.getLanguage()); 25 } else { 26 return loLabel.getLanguage() == null; 27 } 28 } 29 30 public Label(String label, String language) { 10 31 this.label = label; 11 32 this.language = language; 12 33 } 13 14 34 15 35 public String getLabel() { … … 17 37 } 18 38 19 20 39 public void setLabel(String label) { 21 40 this.label = label; 22 41 } 23 24 42 25 43 public String getLanguage() { … … 27 45 } 28 46 29 30 47 public void setLanguage(String language) { 31 48 this.language = language; 32 49 } 33 50 34 public String toString() {51 public String toString() { 35 52 StringBuffer sb = new StringBuffer(); 36 53 sb.append("\""); 37 if ( label != null ){54 if (label != null) { 38 55 sb.append(label); 39 56 } 40 57 sb.append("\""); 41 if ( language != null ){58 if (language != null) { 42 59 sb.append("@"); 43 60 sb.append(language); -
BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/OWLQueryBuilderImpl.java
r18 r19 1 1 package org.biohackathon.SPARQLBuilder.OWL; 2 2 3 import java.nio.charset.Charset;4 3 import java.util.ArrayList; 5 4 import java.util.Collection; 6 5 import java.util.HashMap; 7 6 import java.util.HashSet; 8 import java.util.List;9 7 import java.util.Set; 10 8 11 import org.apache.http.HttpEntity;12 import org.apache.http.HttpResponse;13 import org.apache.http.NameValuePair;14 import org.apache.http.client.HttpClient;15 import org.apache.http.client.entity.UrlEncodedFormEntity;16 import org.apache.http.client.methods.HttpPost;17 //import org.apache.http.impl.client.HttpClientBuilder;18 import org.apache.http.message.BasicNameValuePair;19 import org.apache.http.util.EntityUtils;20 9 21 10 import com.hp.hpl.jena.query.Query; … … 288 277 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); 289 278 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); 290 queryStr.append("SELECT DISTINCT ?p ?pDirection ?c (COUNT(?s) AS ?numOfLinks) \n"); 279 // queryStr.append("SELECT DISTINCT ?p ?pDirection ?c (COUNT(?s) AS ?numOfLinks) \n"); 280 queryStr.append("SELECT DISTINCT ?p ?pDirection ?c \n"); 291 281 if (graphURIs != null) { 292 282 for (String graphURI : graphURIs) { … … 300 290 queryStr.append(" { ?p rdfs:domain <"); 301 291 queryStr.append(originClass); 302 queryStr.append(">.\n ?p rdfs:range ?c.\n ?o ?p ?s.}\n UNION\n"); 292 queryStr.append(">.\n ?p rdfs:range ?c.\n"); 293 // queryStr.append(" ?o ?p ?s."); 294 queryStr.append("}\n UNION\n"); 303 295 queryStr.append(" { ?p rdfs:range <"); 304 296 queryStr.append(originClass); 305 297 queryStr.append(">.\n"); 306 298 queryStr.append(" ?p rdfs:domain ?c.\n"); 307 queryStr.append(" ?s ?p ?o.}\n"); 299 // queryStr.append(" ?s ?p ?o."); 300 queryStr.append("}\n"); 308 301 309 302 queryStr.append(" ?p ?pDirection ?c.\n"); … … 311 304 312 305 queryStr.append(" ?s rdf:type ?c.\n"); 313 queryStr.append(" ?o rdf:type <"); 314 queryStr.append(originClass); 315 queryStr.append(">.\n"); 316 queryStr.append("}\nGROUP BY ?p ?pDirection ?c\n"); 306 // queryStr.append(" ?o rdf:type <"); 307 // queryStr.append(originClass); 308 // queryStr.append(">.\n"); 309 queryStr.append("}\n"); 310 // queryStr.append("GROUP BY ?p ?pDirection ?c\n"); 317 311 if (limit > 0) { 318 312 queryStr.append("limit "); … … 567 561 } 568 562 563 564 public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs, String language) throws Exception{ 565 if( resourceURIs == null || resourceURIs.length == 0 ){ 566 return new LabelMap[0]; 567 } 568 StringBuffer queryStr = new StringBuffer(); 569 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"); 570 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); 571 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); 572 queryStr.append("SELECT DISTINCT ?res ?label \n"); 573 if (graphURIs != null) { 574 for (String graphURI : graphURIs) { 575 queryStr.append("FROM <"); 576 queryStr.append(graphURI); 577 queryStr.append(">\n"); 578 } 579 } 580 queryStr.append("WHERE{\n"); 581 queryStr.append(" ?res rdfs:label ?label.\n"); 582 queryStr.append(" FILTER(?res IN ("); 583 boolean f = false; 584 for( String resourceURI: resourceURIs ){ 585 if(f){ 586 queryStr.append(", "); 587 } 588 f = true; 589 queryStr.append("<"); 590 queryStr.append(resourceURI); 591 queryStr.append(">"); 592 } 593 queryStr.append("))\n"); 594 queryStr.append("}"); 595 596 System.out.println(queryStr.toString()); 597 598 Query query = QueryFactory.create(queryStr.toString()); 599 QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI, 600 query); 601 602 ResultSet results = qexec.execSelect(); 603 HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>(); 604 HashMap<String, InstanceLink> insLinkMap = new HashMap<String, InstanceLink>(); 605 for (; results.hasNext();) { 606 QuerySolution sol = results.next(); 607 String uri = sol.getResource("res").getURI(); 608 Literal literal = sol.getLiteral("label"); 609 if( literal != null ){ 610 String label = literal.getString(); 611 String lang = literal.getLanguage(); 612 if( language != null && language.equals(lang)){ 613 Label lbl = new Label(label, lang); 614 if( lMap.containsKey(uri)){ 615 LabelMap lm = lMap.get(uri); 616 lm.addLabel(lbl); 617 }else{ 618 LabelMap lm = new LabelMap(uri, new Label[]{lbl}); 619 lMap.put(uri, lm); 620 } 621 } 622 } 623 } 624 return lMap.values().toArray(new LabelMap[0]); 625 626 627 } 628 569 629 }