- 更新日時:
- 2014/01/30 11:47:45 (11 年 前)
- ファイル:
-
- 1 変更
凡例:
- 変更なし
- 追加
- 削除
-
BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/OWLQueryBuilderImpl.java
r5 r10 25 25 import com.hp.hpl.jena.query.QuerySolution; 26 26 import com.hp.hpl.jena.query.ResultSet; 27 import com.hp.hpl.jena.rdf.model.Literal; 27 28 import com.hp.hpl.jena.rdf.model.Resource; 28 29 … … 65 66 66 67 OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint); 67 S tring[] clsURIs= builder.getOWLClasses(graphURIs, keyword);68 for(S tring cls: clsURIs){68 SClass[] clz = builder.getOWLClasses(graphURIs, keyword); 69 for(SClass cls: clz){ 69 70 System.out.println(cls); 70 71 } 71 72 72 73 System.out.println("CLS"); 73 ClassLink[] cls = builder.getNextClass(null, cl sURIs[0], 0);74 ClassLink[] cls = builder.getNextClass(null, clz[0].getClassURI(), 0); 74 75 if( cls != null ){ 75 76 for( ClassLink cl: cls){ … … 79 80 80 81 System.out.println("CLS-INS"); 81 cls = builder.getNextClassViaInstanceLink(null, cl sURIs[0], 100);82 cls = builder.getNextClassViaInstanceLink(null, clz[0].getClassURI(), 100); 82 83 if( cls != null ){ 83 84 for( ClassLink cl: cls){ … … 124 125 * @since 28.01.2014 125 126 */ 126 public S tring[] getOWLClasses(String[] graphURIs, String keyword)127 public SClass[] getOWLClasses(String[] graphURIs, String keyword) 127 128 throws Exception { 128 129 StringBuffer queryStr = new StringBuffer(); … … 130 131 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"); 131 132 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"); 132 queryStr.append("SELECT DISTINCT ?c \n");133 queryStr.append("SELECT DISTINCT ?c ?pLabel (COUNT(?i) AS ?numOfInstances)\n"); 133 134 if (graphURIs != null) { 134 135 for (String graphURI : graphURIs) { … … 143 144 144 145 queryStr.append(" ?c rdfs:label ?label.\n"); 146 queryStr.append(" ?c rdfs:label ?pLabel.\n"); 147 queryStr.append(" ?i rdf:type ?c.\n"); 145 148 146 149 // queryStr.append(" ?c rdfs:label "); … … 153 156 queryStr.append("\" , \"i\" )\n"); 154 157 queryStr.append(" )"); 155 queryStr.append("\n} ");158 queryStr.append("\n} GROUP BY ?c ?pLabel"); 156 159 157 160 System.out.println(queryStr.toString()); … … 162 165 163 166 ResultSet results = qexec.execSelect(); 164 ArrayList<String> solStrings = new ArrayList<String>();167 HashMap<String,SClass> classMap = new HashMap<String,SClass>(); 165 168 for (; results.hasNext();) { 166 169 QuerySolution sol = results.next(); 167 170 Resource res = sol.getResource("c"); 168 171 String uri = res.getURI(); 169 solStrings.add(uri); 172 int numOfInstances = sol.getLiteral("numOfInstances").getInt(); 173 //System.out.println(numOfInstances); 174 Literal labelLiteral = sol.getLiteral("pLabel"); 175 SClass sClass = null; 176 if( classMap.containsKey(uri)){ 177 sClass = classMap.get(uri); 178 } 179 else{ 180 sClass = new SClass(uri, null, numOfInstances); 181 classMap.put(uri, sClass); 182 } 183 if( labelLiteral != null ){ 184 String label = labelLiteral.getString(); 185 String lang = labelLiteral.getLanguage(); 186 sClass.addLabel(new Label(label, lang)); 187 } 170 188 } 171 189 qexec.close(); 172 return solStrings.toArray(new String[0]);190 return classMap.values().toArray(new SClass[0]); 173 191 } 174 192