| 1 | package org.biohackathon.SPARQLBuilder.OWL;
 | 
|---|
| 2 | 
 | 
|---|
| 3 | import java.util.ArrayList;
 | 
|---|
| 4 | import java.util.Collection;
 | 
|---|
| 5 | import java.util.HashMap;
 | 
|---|
| 6 | import java.util.HashSet;
 | 
|---|
| 7 | import java.util.Set;
 | 
|---|
| 8 | 
 | 
|---|
| 9 | import com.hp.hpl.jena.query.Query;
 | 
|---|
| 10 | import com.hp.hpl.jena.query.QueryExecution;
 | 
|---|
| 11 | import com.hp.hpl.jena.query.QueryExecutionFactory;
 | 
|---|
| 12 | import com.hp.hpl.jena.query.QueryFactory;
 | 
|---|
| 13 | import com.hp.hpl.jena.query.QuerySolution;
 | 
|---|
| 14 | import com.hp.hpl.jena.query.ResultSet;
 | 
|---|
| 15 | import com.hp.hpl.jena.rdf.model.Literal;
 | 
|---|
| 16 | import com.hp.hpl.jena.rdf.model.Resource;
 | 
|---|
| 17 | 
 | 
|---|
| 18 | /**
 | 
|---|
| 19 |  * 繧ッ繧ィ繝ェ繧堤函謌舌☆繧九◆繧√�讖溯�繧呈署萓帙☆繧区ク繧ッ繝ゥ繧ケ
 | 
|---|
| 20 |  * 
 | 
|---|
| 21 |  * @author Norio KOBAYASHI
 | 
|---|
| 22 |  * @since 28.01.2014
 | 
|---|
| 23 |  * @version 29.01.2014
 | 
|---|
| 24 |  */
 | 
|---|
| 25 | public class EndpointAnalyzer implements RDFSchemaAnalyzer {
 | 
|---|
| 26 | 
 | 
|---|
| 27 |         // private Model model = null;
 | 
|---|
| 28 |         private String endpointURI = null;
 | 
|---|
| 29 | 
 | 
|---|
| 30 |         /**
 | 
|---|
| 31 |          * 繧「繧ッ繧サ繧ケ縺吶kSPARQL endpoint縺ョURI繧呈欠螳壹☆繧区ァ区�蟄� | 
|---|
| 32 |          * 
 | 
|---|
| 33 |          * @param endpointURI
 | 
|---|
| 34 |          *            縲繧「繧ッ繧サ繧ケ縺吶kSPARQL endpoint縺ョURI
 | 
|---|
| 35 |          * @throws Exception
 | 
|---|
| 36 |          * @since 28.01.2014
 | 
|---|
| 37 |          */
 | 
|---|
| 38 |         public EndpointAnalyzer(String endpointURI) {
 | 
|---|
| 39 |                 this.endpointURI = endpointURI;
 | 
|---|
| 40 |         }
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         /**
 | 
|---|
| 43 |          * 繝�せ繝医↓菴ソ逕ィ縺吶kmain繝。繧ス繝�ラ
 | 
|---|
| 44 |          * <p>
 | 
|---|
| 45 |          * 繧ッ繧ィ繝ェ繝薙Ν繝繝シ縺ョ譛ャ逡ェ繝励Ο繧ー繝ゥ繝縺ァ縺ッ縺薙�繝。繧ス繝�ラ縺ッ菴ソ逕ィ縺励↑縺� | 
|---|
| 46 |          * </p>
 | 
|---|
| 47 |          * 
 | 
|---|
| 48 |          * @param args
 | 
|---|
| 49 |          *            菴ソ逕ィ縺励↑縺� | 
|---|
| 50 |          * @throws Exception
 | 
|---|
| 51 |          * @since 28.01.2014
 | 
|---|
| 52 |          */
 | 
|---|
| 53 |         public static void main(String[] args) throws Exception {
 | 
|---|
| 54 |                 // String sparqlEndpoint = "http://dbpedia.org/sparql";
 | 
|---|
| 55 |                 String sparqlEndpoint = "http://lsd.dbcls.jp/sparql";
 | 
|---|
| 56 |                 // String keyword = "artiste";
 | 
|---|
| 57 |                 String[] keyword = { "Frequency", "Class" };
 | 
|---|
| 58 |                 String[] graphURIs = new String[0];
 | 
|---|
| 59 | 
 | 
|---|
| 60 |                 // keyword = null;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |                 EndpointAnalyzer builder = new EndpointAnalyzer(sparqlEndpoint);
 | 
|---|
| 63 | 
 | 
|---|
| 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);
 | 
|---|
| 90 |                 /*
 | 
|---|
| 91 |                  * SClass[] clz = builder.getOWLClasses(graphURIs, keyword, false); for
 | 
|---|
| 92 |                  * (SClass cls : clz) { System.out.println(cls); } clz =
 | 
|---|
| 93 |                  * builder.countInstances(null, clz); for (SClass cls : clz) {
 | 
|---|
| 94 |                  * System.out.println(cls); }
 | 
|---|
| 95 |                  */
 | 
|---|
| 96 | 
 | 
|---|
| 97 |                 /*
 | 
|---|
| 98 |                  * System.out.println("CLS");
 | 
|---|
| 99 |                  * 
 | 
|---|
| 100 |                  * String uri =
 | 
|---|
| 101 |                  * "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry";
 | 
|---|
| 102 |                  * 
 | 
|---|
| 103 |                  * ClassLink[] cls = null; try{ cls = builder.getNextClass(null, uri,
 | 
|---|
| 104 |                  * 100, false); }catch(Exception ex) { ex.printStackTrace(); }
 | 
|---|
| 105 |                  * 
 | 
|---|
| 106 |                  * System.out.println("Solutions: "); if (cls != null) { for (ClassLink
 | 
|---|
| 107 |                  * cl : cls) { System.out.println(cl.toString()); } cls =
 | 
|---|
| 108 |                  * builder.countLinks(null, uri, cls); for (ClassLink cl : cls) {
 | 
|---|
| 109 |                  * System.out.println(cl.toString()); } }
 | 
|---|
| 110 |                  */
 | 
|---|
| 111 |                 /*
 | 
|---|
| 112 |                  * 
 | 
|---|
| 113 |                  * String uri =
 | 
|---|
| 114 |                  * "http://purl.jp/bio/10/lsd/ontology/201209#EnglishEntry";
 | 
|---|
| 115 |                  * 
 | 
|---|
| 116 |                  * ClassLink[] cls = null;
 | 
|---|
| 117 |                  * 
 | 
|---|
| 118 |                  * System.out.println("CLS-INS"); cls = null; try{ cls =
 | 
|---|
| 119 |                  * builder.getNextClassViaInstanceLink(null, uri, 100); }catch(Exception
 | 
|---|
| 120 |                  * ex){ ex.printStackTrace(); } if (cls != null) { for (ClassLink cl :
 | 
|---|
| 121 |                  * cls) { System.out.println(cl.toString()); } }
 | 
|---|
| 122 |                  */
 | 
|---|
| 123 |                 /*
 | 
|---|
| 124 |                  * System.out.println("Instances"); Instance[] ins =
 | 
|---|
| 125 |                  * builder.getInstances(null, "\"A.C. Reed\"@en"); if (ins != null) {
 | 
|---|
| 126 |                  * for (Instance in : ins) { System.out.println(in.toString()); } }
 | 
|---|
| 127 |                  */
 | 
|---|
| 128 |                 /*
 | 
|---|
| 129 |                  * System.out.println("INS-INS"); ins = builder.getInstances(null,
 | 
|---|
| 130 |                  * "\"A.C. Reed\"@en"); InstanceLink[] iLinks =
 | 
|---|
| 131 |                  * builder.getNextInstancesViaInstanceLink(null,
 | 
|---|
| 132 |                  * ins[0].getInstanceURI(), 100);
 | 
|---|
| 133 |                  * 
 | 
|---|
| 134 |                  * if (iLinks != null) { for (InstanceLink in : iLinks) {
 | 
|---|
| 135 |                  * System.out.println(in.toString()); } }
 | 
|---|
| 136 |                  */
 | 
|---|
| 137 |         }
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         /**
 | 
|---|
| 140 |          * 譏守、コ逧�↓RDF縺ァ譖ク縺九l縺ヲ縺�k繧ッ繝ゥ繧ケ繧貞叙蠕励☆繧� | 
|---|
| 141 |          * <p>
 | 
|---|
| 142 |          * 謖�ョ壹&繧後◆graphURIs縺ョ荳ュ縺九i縲√く繝シ繝ッ繝シ繝峨↓繝偵ャ繝医☆繧九Λ繝吶Ν繧呈戟縺、繧ッ繝ゥ繧ケ��dfs:Class�峨r縺吶∋縺ヲ霑斐☆ <br>
 | 
|---|
| 143 |          * </p>
 | 
|---|
| 144 |          * 
 | 
|---|
| 145 |          * @param graphURIs
 | 
|---|
| 146 |          *            縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� (null繧�聞縺�縺ョ驟榊�繧ょ庄)
 | 
|---|
| 147 |          * @param keyword
 | 
|---|
| 148 |          *            ��ull繧�ゥコ譁�ュ励�荳榊庄�� | 
|---|
| 149 |          * @return 繧ッ繝ゥ繧ケURI縺ョ驟榊�
 | 
|---|
| 150 |          * @throws Exception
 | 
|---|
| 151 |          * @since 28.01.2014
 | 
|---|
| 152 |          */
 | 
|---|
| 153 | 
 | 
|---|
| 154 | //        public List<SClass> 
 | 
|---|
| 155 |         
 | 
|---|
| 156 |         public SClass[] getOWLClasses(String[] graphURIs, String[] keywords,
 | 
|---|
| 157 |                         String language,
 | 
|---|
| 158 | 
 | 
|---|
| 159 |                         boolean countInstances) throws Exception {
 | 
|---|
| 160 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 161 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 162 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 163 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 164 |                 if (countInstances) {
 | 
|---|
| 165 |                         queryStr.append("SELECT DISTINCT ?c ?pLabel (COUNT(?i) AS ?numOfInstances)\n");
 | 
|---|
| 166 |                 } else {
 | 
|---|
| 167 |                         queryStr.append("SELECT DISTINCT ?c ?pLabel \n");
 | 
|---|
| 168 |                 }
 | 
|---|
| 169 |                 if (graphURIs != null) {
 | 
|---|
| 170 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 171 |                                 queryStr.append("FROM <");
 | 
|---|
| 172 |                                 queryStr.append(graphURI);
 | 
|---|
| 173 |                                 queryStr.append(">\n");
 | 
|---|
| 174 |                         }
 | 
|---|
| 175 |                 }
 | 
|---|
| 176 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 177 | 
 | 
|---|
| 178 |                 //
 | 
|---|
| 179 |                 queryStr.append(" {?c rdf:type rdfs:Class} UNION {?c rdf:type owl:Class}\n");
 | 
|---|
| 180 |                 // queryStr.append("  ?i rdf:type ?c.\n");
 | 
|---|
| 181 | 
 | 
|---|
| 182 |                 if (countInstances) {
 | 
|---|
| 183 |                         queryStr.append("  ?c rdfs:label ?label.\n");
 | 
|---|
| 184 |                 }
 | 
|---|
| 185 |                 queryStr.append("  ?c rdfs:label ?pLabel.\n");
 | 
|---|
| 186 |                 if (countInstances) {
 | 
|---|
| 187 |                         queryStr.append("      ?i rdf:type ?c.\n");
 | 
|---|
| 188 |                 }
 | 
|---|
| 189 |                 // queryStr.append("      ?c rdfs:label ");
 | 
|---|
| 190 |                 // queryStr.append(keyword);
 | 
|---|
| 191 |                 // queryStr.append(".");
 | 
|---|
| 192 | 
 | 
|---|
| 193 |                 // TODO OR relationship between keywords
 | 
|---|
| 194 |                 if (keywords != null && keywords.length != 0) {
 | 
|---|
| 195 | 
 | 
|---|
| 196 |                         queryStr.append(" ?c rdfs:label ");
 | 
|---|
| 197 |                         queryStr.append("?keywords").append(".\n");
 | 
|---|
| 198 |                         queryStr.append("  filter((LANG(?keywords) = \'").append(language);
 | 
|---|
| 199 |                         queryStr.append("\') && \n (");
 | 
|---|
| 200 | 
 | 
|---|
| 201 |                         // (LANG(?keywords) = 'en') &&
 | 
|---|
| 202 | 
 | 
|---|
| 203 |                         for (int i = 0; i < keywords.length; i++) {
 | 
|---|
| 204 |                                 if (i > 0)
 | 
|---|
| 205 |                                         queryStr.append(" || \n ");
 | 
|---|
| 206 | 
 | 
|---|
| 207 |                                 queryStr.append("regex(str(").append("?keywords")
 | 
|---|
| 208 |                                                 .append("),\"");
 | 
|---|
| 209 |                                 queryStr.append(keywords[i]);
 | 
|---|
| 210 |                                 queryStr.append("\", \"i\" )");
 | 
|---|
| 211 | 
 | 
|---|
| 212 |                         }
 | 
|---|
| 213 |                         queryStr.append("))\n");
 | 
|---|
| 214 | 
 | 
|---|
| 215 |                 }
 | 
|---|
| 216 | 
 | 
|---|
| 217 |                 if (countInstances) {
 | 
|---|
| 218 |                         queryStr.append("}  GROUP BY ?c ?pLabel");
 | 
|---|
| 219 |                 } else {
 | 
|---|
| 220 |                         queryStr.append("}");
 | 
|---|
| 221 |                 }
 | 
|---|
| 222 |                 System.out.println(queryStr.toString());
 | 
|---|
| 223 | 
 | 
|---|
| 224 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 225 | 
 | 
|---|
| 226 |                 QueryExecution qexec = null;
 | 
|---|
| 227 |                 ResultSet results = null;
 | 
|---|
| 228 |                 try {
 | 
|---|
| 229 |                         long start = System.currentTimeMillis();
 | 
|---|
| 230 |                         qexec = QueryExecutionFactory.sparqlService(endpointURI, query);
 | 
|---|
| 231 |                         results = qexec.execSelect();
 | 
|---|
| 232 |                         long end = System.currentTimeMillis();
 | 
|---|
| 233 |                         System.out.println("EXEC TIME: " + (end - start));
 | 
|---|
| 234 |                 } catch (Exception ex) {
 | 
|---|
| 235 |                         ex.printStackTrace();
 | 
|---|
| 236 |                         throw ex;
 | 
|---|
| 237 |                 }
 | 
|---|
| 238 | 
 | 
|---|
| 239 |                 HashMap<String, SClass> classMap = new HashMap<String, SClass>();
 | 
|---|
| 240 |                 for (; results.hasNext();) {
 | 
|---|
| 241 |                         QuerySolution sol = results.next();
 | 
|---|
| 242 |                         Resource res = sol.getResource("c");
 | 
|---|
| 243 |                         if (res != null) {
 | 
|---|
| 244 |                                 String uri = res.getURI();
 | 
|---|
| 245 |                                 int numOfInstances = 0;
 | 
|---|
| 246 |                                 if (countInstances) {
 | 
|---|
| 247 |                                         numOfInstances = sol.getLiteral("numOfInstances").getInt();
 | 
|---|
| 248 |                                 } //
 | 
|---|
| 249 |                                 System.out.println(numOfInstances);
 | 
|---|
| 250 |                                 Literal labelLiteral = sol.getLiteral("pLabel");
 | 
|---|
| 251 |                                 SClass sClass = null;
 | 
|---|
| 252 |                                 if (classMap.containsKey(uri)) {
 | 
|---|
| 253 |                                         sClass = classMap.get(uri);
 | 
|---|
| 254 |                                 } else {
 | 
|---|
| 255 |                                         sClass = new SClass(uri, null, numOfInstances);
 | 
|---|
| 256 |                                         classMap.put(uri, sClass);
 | 
|---|
| 257 |                                 }
 | 
|---|
| 258 |                                 if (labelLiteral != null) {
 | 
|---|
| 259 |                                         String label = labelLiteral.getString();
 | 
|---|
| 260 |                                         String lang = labelLiteral.getLanguage();
 | 
|---|
| 261 |                                         sClass.addLabel(new Label(label, lang));
 | 
|---|
| 262 |                                 }
 | 
|---|
| 263 |                         }
 | 
|---|
| 264 |                 }
 | 
|---|
| 265 |                 qexec.close();
 | 
|---|
| 266 |                 return classMap.values().toArray(new SClass[0]);
 | 
|---|
| 267 | 
 | 
|---|
| 268 |         }
 | 
|---|
| 269 | 
 | 
|---|
| 270 |         public SClass[] listClasses(String[] graphURIs, boolean countInstances)
 | 
|---|
| 271 |                         throws Exception {
 | 
|---|
| 272 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 273 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 274 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 275 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 276 |                 if (countInstances) {
 | 
|---|
| 277 |                         queryStr.append("SELECT DISTINCT ?cls ?pLabel (COUNT(?i) AS ?numOfInstances)\n");
 | 
|---|
| 278 |                 } else {
 | 
|---|
| 279 |                         queryStr.append("SELECT DISTINCT ?cls ?pLabel \n");
 | 
|---|
| 280 |                 }
 | 
|---|
| 281 | 
 | 
|---|
| 282 |                 if (graphURIs != null) {
 | 
|---|
| 283 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 284 |                                 queryStr.append("FROM <");
 | 
|---|
| 285 |                                 queryStr.append(graphURI);
 | 
|---|
| 286 |                                 queryStr.append(">\n");
 | 
|---|
| 287 |                         }
 | 
|---|
| 288 |                 }
 | 
|---|
| 289 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 290 |                 queryStr.append("\t{ ?cls rdf:type rdfs:Class. }\n");
 | 
|---|
| 291 |                 queryStr.append("\tUNION\n");
 | 
|---|
| 292 |                 queryStr.append("\t{ ?cls rdf:type owl:Class. }\n");
 | 
|---|
| 293 |                 queryStr.append("\tUNION\n");
 | 
|---|
| 294 |                 queryStr.append("\t{ [] rdfs:type ?cls. }\n");
 | 
|---|
| 295 |                 queryStr.append("\tUNION\n");
 | 
|---|
| 296 |                 queryStr.append("\t{ [] rdfs:domain ?cls. }\n");
 | 
|---|
| 297 |                 queryStr.append("\tUNION\n");
 | 
|---|
| 298 |                 queryStr.append("\t{ [] rdfs:range ?cls. }\n");
 | 
|---|
| 299 |                 queryStr.append("\tUNION\n");
 | 
|---|
| 300 |                 queryStr.append("\t{ ?cls rdfs:subclassOf []. }\n");
 | 
|---|
| 301 |                 queryStr.append("\t?cls rdfs:label ?pLabel.\n");
 | 
|---|
| 302 |                 if (countInstances) {
 | 
|---|
| 303 |                         queryStr.append("\t?i rdf:type ?c.\n");
 | 
|---|
| 304 |                 }
 | 
|---|
| 305 |                 if (countInstances) {
 | 
|---|
| 306 |                         queryStr.append("}  GROUP BY ?c ?pLabel");
 | 
|---|
| 307 |                 } else {
 | 
|---|
| 308 |                         queryStr.append("}");
 | 
|---|
| 309 |                 }
 | 
|---|
| 310 |                 System.out.println(queryStr.toString());
 | 
|---|
| 311 | 
 | 
|---|
| 312 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 313 | 
 | 
|---|
| 314 |                 QueryExecution qexec = null;
 | 
|---|
| 315 |                 ResultSet results = null;
 | 
|---|
| 316 |                 try {
 | 
|---|
| 317 |                         long start = System.currentTimeMillis();
 | 
|---|
| 318 |                         qexec = QueryExecutionFactory.sparqlService(endpointURI, query);
 | 
|---|
| 319 |                         results = qexec.execSelect();
 | 
|---|
| 320 |                         long end = System.currentTimeMillis();
 | 
|---|
| 321 |                         System.out.println("EXEC TIME: " + (end - start));
 | 
|---|
| 322 |                 } catch (Exception ex) {
 | 
|---|
| 323 |                         ex.printStackTrace();
 | 
|---|
| 324 |                         throw ex;
 | 
|---|
| 325 |                 }
 | 
|---|
| 326 | 
 | 
|---|
| 327 |                 HashMap<String, SClass> classMap = new HashMap<String, SClass>();
 | 
|---|
| 328 |                 for (; results.hasNext();) {
 | 
|---|
| 329 |                         QuerySolution sol = results.next();
 | 
|---|
| 330 |                         Resource res = sol.getResource("cls");
 | 
|---|
| 331 |                         if (res != null) {
 | 
|---|
| 332 |                                 String uri = res.getURI();
 | 
|---|
| 333 |                                 int numOfInstances = 0;
 | 
|---|
| 334 |                                 if (countInstances) {
 | 
|---|
| 335 |                                         numOfInstances = sol.getLiteral("numOfInstances").getInt();
 | 
|---|
| 336 |                                 }
 | 
|---|
| 337 |                                 // System.out.println(numOfInstances);
 | 
|---|
| 338 |                                 Literal labelLiteral = sol.getLiteral("pLabel");
 | 
|---|
| 339 |                                 SClass sClass = null;
 | 
|---|
| 340 |                                 if (classMap.containsKey(uri)) {
 | 
|---|
| 341 |                                         sClass = classMap.get(uri);
 | 
|---|
| 342 |                                 } else {
 | 
|---|
| 343 |                                         sClass = new SClass(uri, null, numOfInstances);
 | 
|---|
| 344 |                                         classMap.put(uri, sClass);
 | 
|---|
| 345 |                                 }
 | 
|---|
| 346 |                                 if (labelLiteral != null) {
 | 
|---|
| 347 |                                         String label = labelLiteral.getString();
 | 
|---|
| 348 |                                         String lang = labelLiteral.getLanguage();
 | 
|---|
| 349 |                                         sClass.addLabel(new Label(label, lang));
 | 
|---|
| 350 |                                 }
 | 
|---|
| 351 |                         }
 | 
|---|
| 352 |                 }
 | 
|---|
| 353 |                 qexec.close();
 | 
|---|
| 354 |                 return classMap.values().toArray(new SClass[0]);
 | 
|---|
| 355 | 
 | 
|---|
| 356 |         }
 | 
|---|
| 357 | 
 | 
|---|
| 358 |         /**
 | 
|---|
| 359 |          * 縲繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧貞叙蠕励☆繧� | 
|---|
| 360 |          * <p>
 | 
|---|
| 361 |          * 謖�ョ壹&繧後◆graphURIs縺ョ荳ュ縺九i縲√く繝シ繝ッ繝シ繝峨↓繝偵ャ繝医☆繧九Λ繝吶Ν繧呈戟縺、繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧偵☆縺ケ縺ヲ霑斐☆ <br>
 | 
|---|
| 362 |          * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
 | 
|---|
| 363 |          * </p>
 | 
|---|
| 364 |          * 
 | 
|---|
| 365 |          * @param graphURIs
 | 
|---|
| 366 |          *            縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� (null繧�聞縺�縺ョ驟榊�繧ょ庄)
 | 
|---|
| 367 |          * @param keyword
 | 
|---|
| 368 |          *            ��ull繧�ゥコ譁�ュ励�荳榊庄�� | 
|---|
| 369 |          * @return 繧ッ繝ゥ繧ケURI縺ョ驟榊�
 | 
|---|
| 370 |          * @throws Exception
 | 
|---|
| 371 |          * @since 28.01.2014
 | 
|---|
| 372 |          */
 | 
|---|
| 373 |         public Instance[] getInstances(String[] graphURIs, String keyword)
 | 
|---|
| 374 |                         throws Exception {
 | 
|---|
| 375 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 376 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 377 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 378 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 379 |                 queryStr.append("SELECT DISTINCT ?ins ?c \n");
 | 
|---|
| 380 |                 if (graphURIs != null) {
 | 
|---|
| 381 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 382 |                                 queryStr.append("FROM <");
 | 
|---|
| 383 |                                 queryStr.append(graphURI);
 | 
|---|
| 384 |                                 queryStr.append(">\n");
 | 
|---|
| 385 |                         }
 | 
|---|
| 386 |                 }
 | 
|---|
| 387 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 388 |                 // queryStr.append(" {?c rdf:type rdfs:Class} UNION {?c rdf:type owl:Class}\n");
 | 
|---|
| 389 | 
 | 
|---|
| 390 |                 queryStr.append("      ?ins rdf:type ?c.\n");
 | 
|---|
| 391 |                 queryStr.append("      ?ins rdfs:label ");
 | 
|---|
| 392 |                 queryStr.append(keyword);
 | 
|---|
| 393 |                 queryStr.append(".\n");
 | 
|---|
| 394 |                 queryStr.append(" FILTER (?c != rdf:Property)");
 | 
|---|
| 395 |                 queryStr.append("}");
 | 
|---|
| 396 | 
 | 
|---|
| 397 |                 // System.out.println(queryStr.toString());
 | 
|---|
| 398 | 
 | 
|---|
| 399 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 400 |                 QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
 | 
|---|
| 401 |                                 query);
 | 
|---|
| 402 | 
 | 
|---|
| 403 |                 ResultSet results = qexec.execSelect();
 | 
|---|
| 404 |                 HashMap<String, HashSet<String>> instanceMap = new HashMap<String, HashSet<String>>();
 | 
|---|
| 405 |                 for (; results.hasNext();) {
 | 
|---|
| 406 |                         QuerySolution sol = results.next();
 | 
|---|
| 407 |                         Resource cls = sol.getResource("c");
 | 
|---|
| 408 |                         Resource ins = sol.getResource("ins");
 | 
|---|
| 409 |                         String clsURI = cls.getURI();
 | 
|---|
| 410 |                         String insURI = ins.getURI();
 | 
|---|
| 411 |                         if (instanceMap.containsKey(insURI)) {
 | 
|---|
| 412 |                                 HashSet<String> classes = instanceMap.get(insURI);
 | 
|---|
| 413 |                                 classes.add(clsURI);
 | 
|---|
| 414 |                         } else {
 | 
|---|
| 415 |                                 HashSet<String> classes = new HashSet<String>();
 | 
|---|
| 416 |                                 instanceMap.put(insURI, classes);
 | 
|---|
| 417 |                                 classes.add(clsURI);
 | 
|---|
| 418 |                         }
 | 
|---|
| 419 |                 }
 | 
|---|
| 420 |                 qexec.close();
 | 
|---|
| 421 |                 Set<String> keySet = instanceMap.keySet();
 | 
|---|
| 422 |                 ArrayList<Instance> instanceList = new ArrayList<Instance>();
 | 
|---|
| 423 |                 for (String key : keySet) {
 | 
|---|
| 424 |                         Instance ins = new Instance(key, instanceMap.get(key).toArray(
 | 
|---|
| 425 |                                         new String[0]));
 | 
|---|
| 426 |                         instanceList.add(ins);
 | 
|---|
| 427 |                 }
 | 
|---|
| 428 |                 return instanceList.toArray(new Instance[0]);
 | 
|---|
| 429 |         }
 | 
|---|
| 430 | 
 | 
|---|
| 431 |         /**
 | 
|---|
| 432 |          * 縲謖�ョ壹&繧後◆繧ッ繝ゥ繧ケ繧定オキ轤ケ縺ィ縺励∵�遉コ逧�↓險倩ソー縺輔l縺ヲ縺�kOWL縺ョproperty蛻カ邏�r隱ソ縺ケ縲√◎縺ョproperty蛻カ邏�〒
 | 
|---|
| 433 |          * 髢「騾」縺・縺代i繧後※縺�k繧ッ繝ゥ繧ケ繧堤カイ鄒�噪縺ォ蜿門セ励☆繧� | 
|---|
| 434 |          * <p>
 | 
|---|
| 435 |          * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
 | 
|---|
| 436 |          * <br>
 | 
|---|
| 437 |          * </p>
 | 
|---|
| 438 |          * 
 | 
|---|
| 439 |          * @param graphURIs
 | 
|---|
| 440 |          *            縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
 | 
|---|
| 441 |          * @param originClass
 | 
|---|
| 442 |          *            襍キ轤ケ縺ィ縺ェ繧九け繝ゥ繧ケ縺ョURI縲��ull荳榊庄�� | 
|---|
| 443 |          * @param limit
 | 
|---|
| 444 |          *            隗」縺ィ縺励※霑斐&繧後kClassLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
 | 
|---|
| 445 |          * @return ClassLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� | 
|---|
| 446 |          * @throws Exception
 | 
|---|
| 447 |          * @since 28.01.2014
 | 
|---|
| 448 |          */
 | 
|---|
| 449 |         public ClassLink[] getNextClass(String[] graphURIs, String originClass,
 | 
|---|
| 450 |                         int limit, boolean countLinks) throws Exception {
 | 
|---|
| 451 |                 ClassLink[] cLinks = getNextClassSub(graphURIs, originClass, limit,
 | 
|---|
| 452 |                                 false);
 | 
|---|
| 453 |                 if (countLinks) {
 | 
|---|
| 454 |                         cLinks = countLinks(graphURIs, originClass, cLinks);
 | 
|---|
| 455 |                 }
 | 
|---|
| 456 |                 return cLinks;
 | 
|---|
| 457 |         }
 | 
|---|
| 458 | 
 | 
|---|
| 459 |         private ClassLink[] getNextClassSub(String[] graphURIs, String originClass,
 | 
|---|
| 460 |                         int limit, boolean countLinks) throws Exception {
 | 
|---|
| 461 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 462 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 463 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 464 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 465 | 
 | 
|---|
| 466 |                 // SELECT
 | 
|---|
| 467 |                 if (countLinks) {
 | 
|---|
| 468 |                         queryStr.append("SELECT DISTINCT ?p ?pDirection ?c (COUNT(?oci) AS ?numOfOriginalClassInstances) (COUNT(?lci) AS ?numOfLinkedClassInstances) (COUNT(?s) AS ?numOfLinks) (COUNT(DISTINCT(?s)) AS ?numOfLinkedInstances) \n");
 | 
|---|
| 469 |                 } else {
 | 
|---|
| 470 |                         queryStr.append("SELECT DISTINCT ?p ?pDirection ?c \n");
 | 
|---|
| 471 |                 }
 | 
|---|
| 472 | 
 | 
|---|
| 473 |                 if (graphURIs != null) {
 | 
|---|
| 474 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 475 |                                 queryStr.append("FROM <");
 | 
|---|
| 476 |                                 queryStr.append(graphURI);
 | 
|---|
| 477 |                                 queryStr.append(">\n");
 | 
|---|
| 478 |                         }
 | 
|---|
| 479 |                 }
 | 
|---|
| 480 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 481 |                 // queryStr.append("  { ?c rdf:type rdfs:Class. }\n  UNION\n  { ?c rdf:type owl:Class. }\n");
 | 
|---|
| 482 |                 queryStr.append("  { ?p rdfs:domain <");
 | 
|---|
| 483 |                 queryStr.append(originClass);
 | 
|---|
| 484 |                 queryStr.append(">.\n    ?p rdfs:range ?c.\n");
 | 
|---|
| 485 | 
 | 
|---|
| 486 |                 if (countLinks) {
 | 
|---|
| 487 |                         queryStr.append("    ?o ?p ?s.");
 | 
|---|
| 488 |                 } else {
 | 
|---|
| 489 |                         queryStr.append("filter(exists{\n");
 | 
|---|
| 490 |                         queryStr.append("  ?o rdf:type ?c.\n");
 | 
|---|
| 491 |                         queryStr.append(" ?s rdf:type <");
 | 
|---|
| 492 |                         queryStr.append(originClass);
 | 
|---|
| 493 |                         queryStr.append(">.\n");
 | 
|---|
| 494 |                         queryStr.append("       ?s ?p ?o.})\n");
 | 
|---|
| 495 |                 }
 | 
|---|
| 496 | 
 | 
|---|
| 497 |                 queryStr.append("}\n  UNION\n");
 | 
|---|
| 498 |                 queryStr.append("  { ?p rdfs:range <");
 | 
|---|
| 499 |                 queryStr.append(originClass);
 | 
|---|
| 500 |                 queryStr.append(">.\n");
 | 
|---|
| 501 |                 queryStr.append("    ?p rdfs:domain ?c.\n");
 | 
|---|
| 502 | 
 | 
|---|
| 503 |                 if (countLinks) {
 | 
|---|
| 504 |                         queryStr.append("    ?s ?p ?o.");
 | 
|---|
| 505 |                 } else {
 | 
|---|
| 506 |                         queryStr.append("filter(exists{\n");
 | 
|---|
| 507 |                         queryStr.append("  ?s rdf:type ?c.\n");
 | 
|---|
| 508 |                         queryStr.append(" ?o rdf:type <");
 | 
|---|
| 509 |                         queryStr.append(originClass);
 | 
|---|
| 510 |                         queryStr.append(">.\n");
 | 
|---|
| 511 |                         queryStr.append("       ?s ?p ?o.})\n");
 | 
|---|
| 512 |                 }
 | 
|---|
| 513 | 
 | 
|---|
| 514 |                 queryStr.append("}\n");
 | 
|---|
| 515 | 
 | 
|---|
| 516 |                 queryStr.append("    ?p ?pDirection ?c.\n");
 | 
|---|
| 517 | 
 | 
|---|
| 518 |                 // queryStr.append("  ?s rdf:type ?c.\n");
 | 
|---|
| 519 | 
 | 
|---|
| 520 |                 if (countLinks) {
 | 
|---|
| 521 |                         queryStr.append("}\nGROUP BY ?p ?pDirection ?c\n");
 | 
|---|
| 522 |                 } else {
 | 
|---|
| 523 |                         queryStr.append("}\n");
 | 
|---|
| 524 |                 }
 | 
|---|
| 525 |                 if (limit > 0) {
 | 
|---|
| 526 |                         queryStr.append("limit ");
 | 
|---|
| 527 |                         queryStr.append(limit);
 | 
|---|
| 528 |                         queryStr.append("\n");
 | 
|---|
| 529 |                 }
 | 
|---|
| 530 | 
 | 
|---|
| 531 |                 System.out.println("getNextClasses SPARQL Query: ");
 | 
|---|
| 532 |                 System.out.println(queryStr.toString());
 | 
|---|
| 533 | 
 | 
|---|
| 534 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 535 |                 QueryExecution qexec = null;
 | 
|---|
| 536 |                 try {
 | 
|---|
| 537 |                         qexec = QueryExecutionFactory.sparqlService(endpointURI, query);
 | 
|---|
| 538 |                 } catch (Exception ex) {
 | 
|---|
| 539 |                         ex.printStackTrace();
 | 
|---|
| 540 |                         throw ex;
 | 
|---|
| 541 |                 }
 | 
|---|
| 542 | 
 | 
|---|
| 543 |                 ResultSet results = null;
 | 
|---|
| 544 |                 try {
 | 
|---|
| 545 |                         long start = System.currentTimeMillis();
 | 
|---|
| 546 |                         results = qexec.execSelect();
 | 
|---|
| 547 |                         long end = System.currentTimeMillis();
 | 
|---|
| 548 |                         System.out.println("EXEC TIME: " + (end - start));
 | 
|---|
| 549 |                 } catch (Exception ex) {
 | 
|---|
| 550 |                         ex.printStackTrace();
 | 
|---|
| 551 |                         throw ex;
 | 
|---|
| 552 |                 }
 | 
|---|
| 553 | 
 | 
|---|
| 554 |                 ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
 | 
|---|
| 555 |                 for (; results.hasNext();) {
 | 
|---|
| 556 |                         QuerySolution sol = results.next();
 | 
|---|
| 557 |                         Resource pro = sol.getResource("p");
 | 
|---|
| 558 |                         if (pro != null) {
 | 
|---|
| 559 |                                 Resource cls = sol.getResource("c");
 | 
|---|
| 560 |                                 Resource dir = sol.getResource("pDirection");
 | 
|---|
| 561 |                                 String proURI = pro.getURI();
 | 
|---|
| 562 |                                 String clsURI = cls.getURI();
 | 
|---|
| 563 |                                 String dirURI = dir.getURI();
 | 
|---|
| 564 | 
 | 
|---|
| 565 |                                 // System.out.println(pro.getURI() + " " + cls.getURI() + " " +
 | 
|---|
| 566 |                                 // dir.getURI());
 | 
|---|
| 567 | 
 | 
|---|
| 568 |                                 Direction direction = null;
 | 
|---|
| 569 |                                 if (dirURI
 | 
|---|
| 570 |                                                 .equals("http://www.w3.org/2000/01/rdf-schema#domain")
 | 
|---|
| 571 |                                                 || dirURI.equals("rdfs:domain")) {
 | 
|---|
| 572 |                                         direction = Direction.reverse;
 | 
|---|
| 573 |                                 }
 | 
|---|
| 574 |                                 if (dirURI.equals("http://www.w3.org/2000/01/rdf-schema#range")
 | 
|---|
| 575 |                                                 || dirURI.equals("rdfs:range")) {
 | 
|---|
| 576 |                                         if (direction != null) {
 | 
|---|
| 577 |                                                 direction = Direction.both;
 | 
|---|
| 578 |                                         } else {
 | 
|---|
| 579 |                                                 direction = Direction.forward;
 | 
|---|
| 580 |                                         }
 | 
|---|
| 581 |                                 }
 | 
|---|
| 582 |                                 // System.out.println(direction);
 | 
|---|
| 583 |                                 int numOfLinks = 0;
 | 
|---|
| 584 |                                 if (countLinks) {
 | 
|---|
| 585 |                                         numOfLinks = sol.getLiteral("numOfLinks").getInt();
 | 
|---|
| 586 |                                 }
 | 
|---|
| 587 |                                 ClassLink cl = new ClassLink(proURI, clsURI, null,direction,
 | 
|---|
| 588 |                                                 numOfLinks, 0, 0, 0, 0, true, true);
 | 
|---|
| 589 |                                 solCLs.add(cl);
 | 
|---|
| 590 |                         }
 | 
|---|
| 591 |                 }
 | 
|---|
| 592 |                 qexec.close();
 | 
|---|
| 593 |                 return solCLs.toArray(new ClassLink[0]);
 | 
|---|
| 594 |         }
 | 
|---|
| 595 | 
 | 
|---|
| 596 |         /**
 | 
|---|
| 597 |          * 縲謖�ョ壹&繧後◆繧ッ繝ゥ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧ッ繝ゥ繧ケ縺ォ螻槭@縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励@縲� | 
|---|
| 598 |          * 蜿門セ励@縺溘う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧堤カイ鄒�噪縺ォ蜿門セ励☆繧� | 
|---|
| 599 |          * <p>
 | 
|---|
| 600 |          * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
 | 
|---|
| 601 |          * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
 | 
|---|
| 602 |          * </p>
 | 
|---|
| 603 |          * 
 | 
|---|
| 604 |          * @param graphURIs
 | 
|---|
| 605 |          *            縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
 | 
|---|
| 606 |          * @param originClass
 | 
|---|
| 607 |          *            襍キ轤ケ縺ィ縺ェ繧九け繝ゥ繧ケ縺ョURI縲��ull荳榊庄�� | 
|---|
| 608 |          * @param limit
 | 
|---|
| 609 |          *            隗」縺ィ縺励※霑斐&繧後kClassLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
 | 
|---|
| 610 |          * @return ClassLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� | 
|---|
| 611 |          * @throws Exception
 | 
|---|
| 612 |          * @since 28.01.2014
 | 
|---|
| 613 |          */
 | 
|---|
| 614 |         public ClassLink[] getNextClassViaInstanceLink(String[] graphURIs,
 | 
|---|
| 615 |                         String originClass, int limit) throws Exception {
 | 
|---|
| 616 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 617 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 618 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 619 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 620 |                 queryStr.append("SELECT DISTINCT ?pf ?pr (COUNT(?pf) AS ?numOfForwardLinks) (COUNT(?pr) AS ?numOfReverseLinks) (COUNT(DISTINCT(?insOrg)) AS ?numOfOriginInstances) (COUNT(DISTINCT(?ins)) AS ?numOfLinkedInstances)  ?c \n");
 | 
|---|
| 621 |                 if (graphURIs != null) {
 | 
|---|
| 622 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 623 |                                 queryStr.append("FROM <");
 | 
|---|
| 624 |                                 queryStr.append(graphURI);
 | 
|---|
| 625 |                                 queryStr.append(">\n");
 | 
|---|
| 626 |                         }
 | 
|---|
| 627 |                 }
 | 
|---|
| 628 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 629 |                 queryStr.append("  ?ins rdf:type ?c.\n");
 | 
|---|
| 630 |                 queryStr.append("  ?insOrg rdf:type <");
 | 
|---|
| 631 |                 queryStr.append(originClass);
 | 
|---|
| 632 |                 queryStr.append(">.\n");
 | 
|---|
| 633 |                 queryStr.append("  { ?ins ?pr ?insOrg. }\n  UNION { ?insOrg ?pf ?ins. }\n");
 | 
|---|
| 634 |                 queryStr.append("}\n");
 | 
|---|
| 635 |                 queryStr.append("GROUP BY ?pf ?pr ?c\n");
 | 
|---|
| 636 |                 if (limit > 0) {
 | 
|---|
| 637 |                         queryStr.append("limit ");
 | 
|---|
| 638 |                         queryStr.append(limit);
 | 
|---|
| 639 |                         queryStr.append("\n");
 | 
|---|
| 640 |                 }
 | 
|---|
| 641 | 
 | 
|---|
| 642 |                 System.out.println(queryStr.toString());
 | 
|---|
| 643 | 
 | 
|---|
| 644 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 645 |                 QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
 | 
|---|
| 646 |                                 query);
 | 
|---|
| 647 | 
 | 
|---|
| 648 |                 ResultSet results = null;
 | 
|---|
| 649 |                 try {
 | 
|---|
| 650 |                         long start = System.currentTimeMillis();
 | 
|---|
| 651 |                         results = qexec.execSelect();
 | 
|---|
| 652 |                         long end = System.currentTimeMillis();
 | 
|---|
| 653 |                         System.out.println("EXEC TIME: " + (end - start));
 | 
|---|
| 654 |                 } catch (Exception ex) {
 | 
|---|
| 655 |                         ex.printStackTrace();
 | 
|---|
| 656 |                         throw ex;
 | 
|---|
| 657 |                 }
 | 
|---|
| 658 | 
 | 
|---|
| 659 |                 ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
 | 
|---|
| 660 |                 for (; results.hasNext();) {
 | 
|---|
| 661 |                         QuerySolution sol = results.next();
 | 
|---|
| 662 |                         Resource proForward = sol.getResource("pf");
 | 
|---|
| 663 |                         Resource proReverse = sol.getResource("pr");
 | 
|---|
| 664 |                         Resource cls = sol.getResource("c");
 | 
|---|
| 665 |                         Direction direction = null;
 | 
|---|
| 666 |                         String propURI = null;
 | 
|---|
| 667 |                         int numOfLinks = 0;
 | 
|---|
| 668 |                         int numOfLinkedInstances = 0;
 | 
|---|
| 669 |                         int numOfOriginInstances = 0;
 | 
|---|
| 670 |                         if (proForward != null) {
 | 
|---|
| 671 |                                 if (proReverse != null) {
 | 
|---|
| 672 |                                         numOfLinks = sol.getLiteral("numOfForwardLinks").getInt();
 | 
|---|
| 673 |                                         numOfLinks += sol.getLiteral("numOfReverseLinks").getInt();
 | 
|---|
| 674 |                                         numOfLinkedInstances = sol.getLiteral(
 | 
|---|
| 675 |                                                         "numOfLinkedInstances").getInt();
 | 
|---|
| 676 |                                         numOfOriginInstances = sol.getLiteral(
 | 
|---|
| 677 |                                                         "numOfOriginInstances").getInt();
 | 
|---|
| 678 |                                         direction = Direction.both;
 | 
|---|
| 679 |                                 } else {
 | 
|---|
| 680 |                                         numOfLinkedInstances = sol.getLiteral(
 | 
|---|
| 681 |                                                         "numOfLinkedInstances").getInt();
 | 
|---|
| 682 |                                         numOfOriginInstances = sol.getLiteral(
 | 
|---|
| 683 |                                                         "numOfOriginInstances").getInt();
 | 
|---|
| 684 |                                         numOfLinks = sol.getLiteral("numOfForwardLinks").getInt();
 | 
|---|
| 685 |                                         direction = Direction.forward;
 | 
|---|
| 686 |                                 }
 | 
|---|
| 687 |                                 propURI = proForward.getURI();
 | 
|---|
| 688 |                         } else {
 | 
|---|
| 689 |                                 direction = Direction.reverse;
 | 
|---|
| 690 |                                 propURI = proReverse.getURI();
 | 
|---|
| 691 |                                 numOfLinkedInstances = sol.getLiteral("numOfOriginInstances")
 | 
|---|
| 692 |                                                 .getInt();
 | 
|---|
| 693 |                                 numOfOriginInstances = sol.getLiteral("numOfLinkedInstances")
 | 
|---|
| 694 |                                                 .getInt();
 | 
|---|
| 695 |                                 numOfLinks = sol.getLiteral("numOfReverseLinks").getInt();
 | 
|---|
| 696 |                         }
 | 
|---|
| 697 |                         String clsURI = cls.getURI();
 | 
|---|
| 698 |                         // System.out.println(propURI + " " + clsURI + " " + direction);
 | 
|---|
| 699 | 
 | 
|---|
| 700 |                         ClassLink cl = new ClassLink(propURI, clsURI, null, direction,
 | 
|---|
| 701 |                                         numOfLinks, numOfOriginInstances, numOfLinkedInstances, 0,
 | 
|---|
| 702 |                                         0, true, true);
 | 
|---|
| 703 |                         solCLs.add(cl);
 | 
|---|
| 704 |                 }
 | 
|---|
| 705 |                 qexec.close();
 | 
|---|
| 706 |                 return solCLs.toArray(new ClassLink[0]);
 | 
|---|
| 707 |         }
 | 
|---|
| 708 | 
 | 
|---|
| 709 | /*
 | 
|---|
| 710 |         public Path[] getPaths(String startClass, String endClass, int mode,
 | 
|---|
| 711 |                         boolean countLinks) throws Exception {
 | 
|---|
| 712 |                 OWLClassGraph graph = new OWLClassGraph(startClass, endClass);
 | 
|---|
| 713 |                 // mode = 2 & countLinks = true are recommended
 | 
|---|
| 714 |                 return graph.getPaths(this, mode, countLinks);
 | 
|---|
| 715 |         }
 | 
|---|
| 716 | 
 | 
|---|
| 717 |         public String createSPARQL(Path path) throws Exception {
 | 
|---|
| 718 |                 return null;
 | 
|---|
| 719 |         }
 | 
|---|
| 720 | */
 | 
|---|
| 721 | 
 | 
|---|
| 722 |         /*
 | 
|---|
| 723 |          * private String executeSelect(String sparqlQuery) throws Exception {
 | 
|---|
| 724 |          * HttpClient client = HttpClientBuilder.create().build(); HttpPost httppost
 | 
|---|
| 725 |          * = new HttpPost(endpointURI); httppost.setHeader("Content-Type",
 | 
|---|
| 726 |          * "application/x-www-form-urlencoded"); httppost.setHeader("Accept",
 | 
|---|
| 727 |          * "application/sparql-results+xml"); List<NameValuePair> nvpList = new
 | 
|---|
| 728 |          * ArrayList<NameValuePair>(); nvpList.add(new BasicNameValuePair("query",
 | 
|---|
| 729 |          * sparqlQuery)); // nvpList.add(new BasicNameValuePair("format", //
 | 
|---|
| 730 |          * outputFormat.getMime())); httppost.setEntity(new
 | 
|---|
| 731 |          * UrlEncodedFormEntity(nvpList, Charset .forName("UTF-8"))); HttpResponse
 | 
|---|
| 732 |          * response = client.execute(httppost); //
 | 
|---|
| 733 |          * System.out.println("[StatusLine] " + response.getStatusLine());
 | 
|---|
| 734 |          * HttpEntity entity = response.getEntity(); String entityString =
 | 
|---|
| 735 |          * EntityUtils.toString(entity, "UTF-8"); return entityString; }
 | 
|---|
| 736 |          */
 | 
|---|
| 737 | 
 | 
|---|
| 738 |         /**
 | 
|---|
| 739 |          * 縲謖�ョ壹&繧後◆繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ォ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励☆繧九� | 
|---|
| 740 |          * 縲蜿門セ励&繧後◆蜷�う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧らカイ鄒�噪縺ォ蜿門セ励☆繧� | 
|---|
| 741 |          * <p>
 | 
|---|
| 742 |          * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
 | 
|---|
| 743 |          * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
 | 
|---|
| 744 |          * </p>
 | 
|---|
| 745 |          * 
 | 
|---|
| 746 |          * @param graphURIs
 | 
|---|
| 747 |          *            縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
 | 
|---|
| 748 |          * @param originInstance
 | 
|---|
| 749 |          *            襍キ轤ケ縺ィ縺ェ繧九う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョURI縲��ull荳榊庄�� | 
|---|
| 750 |          * @param limit
 | 
|---|
| 751 |          *            隗」縺ィ縺励※霑斐&繧後kInstanceLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
 | 
|---|
| 752 |          * @return InstanceLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� | 
|---|
| 753 |          * @throws Exception
 | 
|---|
| 754 |          * @since 28.01.2014
 | 
|---|
| 755 |          */
 | 
|---|
| 756 |         public InstanceLink[] getNextInstancesViaInstanceLink(String[] graphURIs,
 | 
|---|
| 757 |                         String originInstance, int limit) throws Exception {
 | 
|---|
| 758 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 759 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 760 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 761 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 762 |                 queryStr.append("SELECT DISTINCT ?pf ?pr ?ins ?c \n");
 | 
|---|
| 763 |                 if (graphURIs != null) {
 | 
|---|
| 764 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 765 |                                 queryStr.append("FROM <");
 | 
|---|
| 766 |                                 queryStr.append(graphURI);
 | 
|---|
| 767 |                                 queryStr.append(">\n");
 | 
|---|
| 768 |                         }
 | 
|---|
| 769 |                 }
 | 
|---|
| 770 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 771 |                 // queryStr.append("  { ?c rdf:type rdfs:Class. }\n  UNION\n  { ?c rdf:type owl:Class. }\n");
 | 
|---|
| 772 |                 queryStr.append("  ?ins rdf:type ?c.\n");
 | 
|---|
| 773 |                 queryStr.append("  { ?ins ?pr <");
 | 
|---|
| 774 |                 queryStr.append(originInstance);
 | 
|---|
| 775 |                 queryStr.append(">. }\n  UNION { <");
 | 
|---|
| 776 |                 queryStr.append(originInstance);
 | 
|---|
| 777 |                 queryStr.append("> ?pf ?ins. }\n");
 | 
|---|
| 778 | 
 | 
|---|
| 779 |                 queryStr.append(" FILTER (?c != rdf:Property)");
 | 
|---|
| 780 | 
 | 
|---|
| 781 |                 queryStr.append("}\n");
 | 
|---|
| 782 |                 if (limit > 0) {
 | 
|---|
| 783 |                         queryStr.append("limit ");
 | 
|---|
| 784 |                         queryStr.append(limit);
 | 
|---|
| 785 |                         queryStr.append("\n");
 | 
|---|
| 786 |                 }
 | 
|---|
| 787 | 
 | 
|---|
| 788 |                 // System.out.println(queryStr.toString());
 | 
|---|
| 789 | 
 | 
|---|
| 790 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 791 |                 QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
 | 
|---|
| 792 |                                 query);
 | 
|---|
| 793 | 
 | 
|---|
| 794 |                 ResultSet results = qexec.execSelect();
 | 
|---|
| 795 |                 HashMap<String, InstanceLink> insLinkMap = new HashMap<String, InstanceLink>();
 | 
|---|
| 796 |                 for (; results.hasNext();) {
 | 
|---|
| 797 |                         QuerySolution sol = results.next();
 | 
|---|
| 798 |                         Resource proForward = sol.getResource("pf");
 | 
|---|
| 799 |                         Resource proReverse = sol.getResource("pr");
 | 
|---|
| 800 |                         Resource ins = sol.getResource("ins");
 | 
|---|
| 801 |                         Resource cls = sol.getResource("c");
 | 
|---|
| 802 |                         Direction direction = null;
 | 
|---|
| 803 |                         String propURI = null;
 | 
|---|
| 804 |                         if (proForward != null) {
 | 
|---|
| 805 |                                 if (proReverse != null) {
 | 
|---|
| 806 |                                         direction = Direction.both;
 | 
|---|
| 807 |                                 } else {
 | 
|---|
| 808 |                                         direction = Direction.forward;
 | 
|---|
| 809 |                                 }
 | 
|---|
| 810 |                                 propURI = proForward.getURI();
 | 
|---|
| 811 |                         } else {
 | 
|---|
| 812 |                                 direction = Direction.reverse;
 | 
|---|
| 813 |                                 propURI = proReverse.getURI();
 | 
|---|
| 814 |                         }
 | 
|---|
| 815 |                         String clsURI = cls.getURI();
 | 
|---|
| 816 |                         String insURI = ins.getURI();
 | 
|---|
| 817 |                         String key = propURI + "\t" + insURI + "\t" + direction;
 | 
|---|
| 818 |                         if (insLinkMap.containsKey(key)) {
 | 
|---|
| 819 |                                 InstanceLink insLink = insLinkMap.get(key);
 | 
|---|
| 820 |                                 insLink.addLinkedClassURI(clsURI);
 | 
|---|
| 821 |                         } else {
 | 
|---|
| 822 |                                 InstanceLink insLink = new InstanceLink(propURI, insURI,
 | 
|---|
| 823 |                                                 new String[] { clsURI }, direction);
 | 
|---|
| 824 |                                 insLinkMap.put(key, insLink);
 | 
|---|
| 825 |                         }
 | 
|---|
| 826 |                 }
 | 
|---|
| 827 |                 qexec.close();
 | 
|---|
| 828 |                 Collection<InstanceLink> values = insLinkMap.values();
 | 
|---|
| 829 |                 return values.toArray(new InstanceLink[0]);
 | 
|---|
| 830 |         }
 | 
|---|
| 831 | 
 | 
|---|
| 832 |         public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs,
 | 
|---|
| 833 |                         String language) throws Exception {
 | 
|---|
| 834 |                 if (resourceURIs == null || resourceURIs.length == 0) {
 | 
|---|
| 835 |                         return new LabelMap[0];
 | 
|---|
| 836 |                 }
 | 
|---|
| 837 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 838 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 839 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 840 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 841 |                 queryStr.append("SELECT DISTINCT ?res ?label \n");
 | 
|---|
| 842 |                 if (graphURIs != null) {
 | 
|---|
| 843 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 844 |                                 queryStr.append("FROM <");
 | 
|---|
| 845 |                                 queryStr.append(graphURI);
 | 
|---|
| 846 |                                 queryStr.append(">\n");
 | 
|---|
| 847 |                         }
 | 
|---|
| 848 |                 }
 | 
|---|
| 849 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 850 |                 queryStr.append("  ?res rdfs:label ?label.\n");
 | 
|---|
| 851 |                 queryStr.append("  FILTER(?res IN (");
 | 
|---|
| 852 |                 boolean f = false;
 | 
|---|
| 853 |                 for (String resourceURI : resourceURIs) {
 | 
|---|
| 854 |                         if (f) {
 | 
|---|
| 855 |                                 queryStr.append(", ");
 | 
|---|
| 856 |                         }
 | 
|---|
| 857 |                         f = true;
 | 
|---|
| 858 |                         queryStr.append("<");
 | 
|---|
| 859 |                         queryStr.append(resourceURI);
 | 
|---|
| 860 |                         queryStr.append(">");
 | 
|---|
| 861 |                 }
 | 
|---|
| 862 |                 queryStr.append("))\n");
 | 
|---|
| 863 |                 queryStr.append("}");
 | 
|---|
| 864 | 
 | 
|---|
| 865 |                 System.out.println(queryStr.toString());
 | 
|---|
| 866 | 
 | 
|---|
| 867 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 868 |                 QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
 | 
|---|
| 869 |                                 query);
 | 
|---|
| 870 | 
 | 
|---|
| 871 |                 ResultSet results = qexec.execSelect();
 | 
|---|
| 872 |                 HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>();
 | 
|---|
| 873 |                 for (; results.hasNext();) {
 | 
|---|
| 874 |                         QuerySolution sol = results.next();
 | 
|---|
| 875 |                         String uri = sol.getResource("res").getURI();
 | 
|---|
| 876 |                         Literal literal = sol.getLiteral("label");
 | 
|---|
| 877 |                         if (literal != null) {
 | 
|---|
| 878 |                                 String label = literal.getString();
 | 
|---|
| 879 |                                 String lang = literal.getLanguage();
 | 
|---|
| 880 |                                 if (language != null && language.equals(lang)) {
 | 
|---|
| 881 |                                         Label lbl = new Label(label, lang);
 | 
|---|
| 882 |                                         if (lMap.containsKey(uri)) {
 | 
|---|
| 883 |                                                 LabelMap lm = lMap.get(uri);
 | 
|---|
| 884 |                                                 lm.addLabel(lbl);
 | 
|---|
| 885 |                                         } else {
 | 
|---|
| 886 |                                                 LabelMap lm = new LabelMap(uri, new Label[] { lbl });
 | 
|---|
| 887 |                                                 lMap.put(uri, lm);
 | 
|---|
| 888 |                                         }
 | 
|---|
| 889 |                                 }
 | 
|---|
| 890 |                         }
 | 
|---|
| 891 |                 }
 | 
|---|
| 892 |                 return lMap.values().toArray(new LabelMap[0]);
 | 
|---|
| 893 |         }
 | 
|---|
| 894 | 
 | 
|---|
| 895 |         public ClassLink[] countLinks(String[] graphURIs, String startClassURI,
 | 
|---|
| 896 |                         ClassLink[] classLinks) throws Exception {
 | 
|---|
| 897 |                 if (classLinks == null || classLinks.length == 0) {
 | 
|---|
| 898 |                         return new ClassLink[0];
 | 
|---|
| 899 |                 }
 | 
|---|
| 900 |                 for (ClassLink classLink : classLinks) {
 | 
|---|
| 901 |                         StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 902 |                         queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 903 |                         queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 904 |                         queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 905 | 
 | 
|---|
| 906 |                         queryStr.append("SELECT (COUNT(?os) AS ?numOfLinks) (COUNT(DISTINCT(?is)) AS ?numOfOriginInstances) (COUNT(DISTINCT(?os)) AS ?numOfLinkedInstances) \n");
 | 
|---|
| 907 |                         if (graphURIs != null) {
 | 
|---|
| 908 |                                 for (String graphURI : graphURIs) {
 | 
|---|
| 909 |                                         queryStr.append("FROM <");
 | 
|---|
| 910 |                                         queryStr.append(graphURI);
 | 
|---|
| 911 |                                         queryStr.append(">\n");
 | 
|---|
| 912 |                                 }
 | 
|---|
| 913 |                         }
 | 
|---|
| 914 | 
 | 
|---|
| 915 |                         queryStr.append("WHERE{\n");
 | 
|---|
| 916 |                         if (classLink.getDirection() == Direction.forward) {
 | 
|---|
| 917 |                                 queryStr.append("?is rdf:type  <");
 | 
|---|
| 918 |                                 queryStr.append(startClassURI);
 | 
|---|
| 919 |                                 queryStr.append(">.\n");
 | 
|---|
| 920 |                                 queryStr.append("?os rdf:type  <");
 | 
|---|
| 921 |                                 queryStr.append(classLink.getLinkedClassURI());
 | 
|---|
| 922 |                                 queryStr.append(">.\n");
 | 
|---|
| 923 |                                 queryStr.append("?is <");
 | 
|---|
| 924 |                                 queryStr.append(classLink.getPropertyURI());
 | 
|---|
| 925 |                                 queryStr.append("> ?os.\n");
 | 
|---|
| 926 |                                 queryStr.append("}");
 | 
|---|
| 927 |                         } else {
 | 
|---|
| 928 |                                 if (classLink.getDirection() == Direction.forward) {
 | 
|---|
| 929 |                                         queryStr.append("?os rdf:type  <");
 | 
|---|
| 930 |                                         queryStr.append(startClassURI);
 | 
|---|
| 931 |                                         queryStr.append(">.\n");
 | 
|---|
| 932 |                                         queryStr.append("?is rdf:type  <");
 | 
|---|
| 933 |                                         queryStr.append(classLink.getLinkedClassURI());
 | 
|---|
| 934 |                                         queryStr.append(">.\n");
 | 
|---|
| 935 |                                         queryStr.append("?is <");
 | 
|---|
| 936 |                                         queryStr.append(classLink.getPropertyURI());
 | 
|---|
| 937 |                                         queryStr.append("> ?os.\n");
 | 
|---|
| 938 |                                         queryStr.append("}");
 | 
|---|
| 939 |                                 } else {
 | 
|---|
| 940 |                                         queryStr.append("?os rdf:type  <");
 | 
|---|
| 941 |                                         queryStr.append(startClassURI);
 | 
|---|
| 942 |                                         queryStr.append(">.\n");
 | 
|---|
| 943 |                                         queryStr.append("?is rdf:type  <");
 | 
|---|
| 944 |                                         queryStr.append(classLink.getLinkedClassURI());
 | 
|---|
| 945 |                                         queryStr.append(">.\n");
 | 
|---|
| 946 |                                         queryStr.append("{?is <");
 | 
|---|
| 947 |                                         queryStr.append(classLink.getPropertyURI());
 | 
|---|
| 948 |                                         queryStr.append("> ?os.}\n");
 | 
|---|
| 949 |                                         queryStr.append("UNION\n");
 | 
|---|
| 950 |                                         queryStr.append("{?os <");
 | 
|---|
| 951 |                                         queryStr.append(classLink.getPropertyURI());
 | 
|---|
| 952 |                                         queryStr.append("> ?is.}\n");
 | 
|---|
| 953 |                                         queryStr.append("}");
 | 
|---|
| 954 |                                 }
 | 
|---|
| 955 |                         }
 | 
|---|
| 956 |                         System.out.println(queryStr.toString());
 | 
|---|
| 957 | 
 | 
|---|
| 958 |                         Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 959 |                         QueryExecution qexec = QueryExecutionFactory.sparqlService(
 | 
|---|
| 960 |                                         endpointURI, query);
 | 
|---|
| 961 | 
 | 
|---|
| 962 |                         ResultSet results = qexec.execSelect();
 | 
|---|
| 963 |                         if (results.hasNext()) {
 | 
|---|
| 964 |                                 QuerySolution sol = results.next();
 | 
|---|
| 965 |                                 Literal lit = sol.getLiteral("numOfLinks");
 | 
|---|
| 966 |                                 if (lit != null) {
 | 
|---|
| 967 |                                         int numOfLinks = lit.getInt();
 | 
|---|
| 968 |                                         classLink.setNumOfLinks(numOfLinks);
 | 
|---|
| 969 |                                 }
 | 
|---|
| 970 |                                 lit = sol.getLiteral("numOfLinkedInstances");
 | 
|---|
| 971 |                                 if (lit != null) {
 | 
|---|
| 972 |                                         int numOfLinkedInstances = lit.getInt();
 | 
|---|
| 973 |                                         classLink.setNumOfLinkedInstances(numOfLinkedInstances);
 | 
|---|
| 974 |                                 }
 | 
|---|
| 975 |                                 lit = sol.getLiteral("numOfOriginInstances");
 | 
|---|
| 976 |                                 if (lit != null) {
 | 
|---|
| 977 |                                         int numOfOriginInstances = lit.getInt();
 | 
|---|
| 978 |                                         classLink.setNumOfOriginInstances(numOfOriginInstances);
 | 
|---|
| 979 |                                 }
 | 
|---|
| 980 |                                 lit = sol.getLiteral("numOfOriginClassInstances");
 | 
|---|
| 981 |                                 if (lit != null) {
 | 
|---|
| 982 |                                         int numOfOriginClassInstances = lit.getInt();
 | 
|---|
| 983 |                                         classLink
 | 
|---|
| 984 |                                                         .setNumOfOriginClassInstances(numOfOriginClassInstances);
 | 
|---|
| 985 |                                 }
 | 
|---|
| 986 |                                 lit = sol.getLiteral("numOfLinkedClassInstances");
 | 
|---|
| 987 |                                 if (lit != null) {
 | 
|---|
| 988 |                                         int numOfLinkedClassInstances = lit.getInt();
 | 
|---|
| 989 |                                         classLink
 | 
|---|
| 990 |                                                         .setNumOfLinkedClassInstances(numOfLinkedClassInstances);
 | 
|---|
| 991 |                                 }
 | 
|---|
| 992 |                         }
 | 
|---|
| 993 |                         qexec.close();
 | 
|---|
| 994 | 
 | 
|---|
| 995 |                         // count instances
 | 
|---|
| 996 |                         queryStr = new StringBuffer();
 | 
|---|
| 997 |                         queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 998 |                         queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 999 |                         queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 1000 | 
 | 
|---|
| 1001 |                         queryStr.append("SELECT (COUNT(?ics) AS ?numOfOriginClassInstances) \n");
 | 
|---|
| 1002 |                         if (graphURIs != null) {
 | 
|---|
| 1003 |                                 for (String graphURI : graphURIs) {
 | 
|---|
| 1004 |                                         queryStr.append("FROM <");
 | 
|---|
| 1005 |                                         queryStr.append(graphURI);
 | 
|---|
| 1006 |                                         queryStr.append(">\n");
 | 
|---|
| 1007 |                                 }
 | 
|---|
| 1008 |                         }
 | 
|---|
| 1009 | 
 | 
|---|
| 1010 |                         queryStr.append("WHERE{\n");
 | 
|---|
| 1011 |                         if (classLink.getDirection() == Direction.forward) {
 | 
|---|
| 1012 |                                 queryStr.append("?ics rdf:type  <");
 | 
|---|
| 1013 |                                 queryStr.append(startClassURI);
 | 
|---|
| 1014 |                                 queryStr.append(">.\n");
 | 
|---|
| 1015 |                                 queryStr.append("}");
 | 
|---|
| 1016 |                         } else {
 | 
|---|
| 1017 |                                 if (classLink.getDirection() == Direction.forward) {
 | 
|---|
| 1018 |                                         queryStr.append("?ics rdf:type  <");
 | 
|---|
| 1019 |                                         queryStr.append(classLink.getLinkedClassURI());
 | 
|---|
| 1020 |                                         queryStr.append(">.\n");
 | 
|---|
| 1021 |                                         queryStr.append("}");
 | 
|---|
| 1022 |                                 } else {
 | 
|---|
| 1023 |                                         queryStr.append("?ics rdf:type  <");
 | 
|---|
| 1024 |                                         queryStr.append(classLink.getLinkedClassURI());
 | 
|---|
| 1025 |                                         queryStr.append(">.\n");
 | 
|---|
| 1026 |                                         queryStr.append("}");
 | 
|---|
| 1027 |                                 }
 | 
|---|
| 1028 |                         }
 | 
|---|
| 1029 | 
 | 
|---|
| 1030 |                         System.out.println(queryStr.toString());
 | 
|---|
| 1031 | 
 | 
|---|
| 1032 |                         query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 1033 |                         qexec = QueryExecutionFactory.sparqlService(endpointURI, query);
 | 
|---|
| 1034 | 
 | 
|---|
| 1035 |                         results = qexec.execSelect();
 | 
|---|
| 1036 |                         if (results.hasNext()) {
 | 
|---|
| 1037 |                                 QuerySolution sol = results.next();
 | 
|---|
| 1038 |                                 Literal lit = null;
 | 
|---|
| 1039 |                                 lit = sol.getLiteral("numOfOriginClassInstances");
 | 
|---|
| 1040 |                                 if (lit != null) {
 | 
|---|
| 1041 |                                         int numOfOriginClassInstances = lit.getInt();
 | 
|---|
| 1042 |                                         classLink
 | 
|---|
| 1043 |                                                         .setNumOfOriginClassInstances(numOfOriginClassInstances);
 | 
|---|
| 1044 |                                 }
 | 
|---|
| 1045 |                         }
 | 
|---|
| 1046 |                         qexec.close();
 | 
|---|
| 1047 | 
 | 
|---|
| 1048 |                         // count instances
 | 
|---|
| 1049 |                         queryStr = new StringBuffer();
 | 
|---|
| 1050 |                         queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 1051 |                         queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 1052 |                         queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 1053 | 
 | 
|---|
| 1054 |                         queryStr.append("SELECT (COUNT(?ocs) AS ?numOfLinkedClassInstances) \n");
 | 
|---|
| 1055 |                         if (graphURIs != null) {
 | 
|---|
| 1056 |                                 for (String graphURI : graphURIs) {
 | 
|---|
| 1057 |                                         queryStr.append("FROM <");
 | 
|---|
| 1058 |                                         queryStr.append(graphURI);
 | 
|---|
| 1059 |                                         queryStr.append(">\n");
 | 
|---|
| 1060 |                                 }
 | 
|---|
| 1061 |                         }
 | 
|---|
| 1062 | 
 | 
|---|
| 1063 |                         queryStr.append("WHERE{\n");
 | 
|---|
| 1064 |                         if (classLink.getDirection() == Direction.forward) {
 | 
|---|
| 1065 |                                 queryStr.append("?ocs rdf:type  <");
 | 
|---|
| 1066 |                                 queryStr.append(classLink.getLinkedClassURI());
 | 
|---|
| 1067 |                                 queryStr.append(">.\n");
 | 
|---|
| 1068 |                                 queryStr.append("}");
 | 
|---|
| 1069 |                         } else {
 | 
|---|
| 1070 |                                 if (classLink.getDirection() == Direction.forward) {
 | 
|---|
| 1071 |                                         queryStr.append("?ocs rdf:type  <");
 | 
|---|
| 1072 |                                         queryStr.append(startClassURI);
 | 
|---|
| 1073 |                                         queryStr.append(">.\n");
 | 
|---|
| 1074 |                                         queryStr.append("}");
 | 
|---|
| 1075 |                                 } else {
 | 
|---|
| 1076 |                                         queryStr.append("?ocs rdf:type  <");
 | 
|---|
| 1077 |                                         queryStr.append(startClassURI);
 | 
|---|
| 1078 |                                         queryStr.append(">.\n");
 | 
|---|
| 1079 |                                         queryStr.append("}");
 | 
|---|
| 1080 |                                 }
 | 
|---|
| 1081 |                         }
 | 
|---|
| 1082 | 
 | 
|---|
| 1083 |                         System.out.println(queryStr.toString());
 | 
|---|
| 1084 | 
 | 
|---|
| 1085 |                         query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 1086 |                         qexec = QueryExecutionFactory.sparqlService(endpointURI, query);
 | 
|---|
| 1087 | 
 | 
|---|
| 1088 |                         results = qexec.execSelect();
 | 
|---|
| 1089 |                         if (results.hasNext()) {
 | 
|---|
| 1090 |                                 QuerySolution sol = results.next();
 | 
|---|
| 1091 |                                 Literal lit = null;
 | 
|---|
| 1092 |                                 lit = sol.getLiteral("numOfLinkedClassInstances");
 | 
|---|
| 1093 |                                 if (lit != null) {
 | 
|---|
| 1094 |                                         int numOfLinkedClassInstances = lit.getInt();
 | 
|---|
| 1095 |                                         classLink
 | 
|---|
| 1096 |                                                         .setNumOfLinkedClassInstances(numOfLinkedClassInstances);
 | 
|---|
| 1097 |                                 }
 | 
|---|
| 1098 |                         }
 | 
|---|
| 1099 |                         qexec.close();
 | 
|---|
| 1100 | 
 | 
|---|
| 1101 |                 }
 | 
|---|
| 1102 |                 return classLinks;
 | 
|---|
| 1103 |         }
 | 
|---|
| 1104 | 
 | 
|---|
| 1105 |         public SClass[] countInstances(String[] graphURIs, SClass[] classes)
 | 
|---|
| 1106 |                         throws Exception {
 | 
|---|
| 1107 |                 if (classes == null || classes.length == 0) {
 | 
|---|
| 1108 |                         return new SClass[0];
 | 
|---|
| 1109 |                 }
 | 
|---|
| 1110 |                 HashMap<String, SClass> classMap = new HashMap<String, SClass>();
 | 
|---|
| 1111 |                 for (SClass sc : classes) {
 | 
|---|
| 1112 |                         classMap.put(sc.getClassURI(), sc);
 | 
|---|
| 1113 |                 }
 | 
|---|
| 1114 |                 StringBuffer queryStr = new StringBuffer();
 | 
|---|
| 1115 |                 queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
 | 
|---|
| 1116 |                 queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
 | 
|---|
| 1117 |                 queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
 | 
|---|
| 1118 |                 queryStr.append("SELECT DISTINCT ?cls  (COUNT(?is) AS ?numOfInstances)\n");
 | 
|---|
| 1119 |                 if (graphURIs != null) {
 | 
|---|
| 1120 |                         for (String graphURI : graphURIs) {
 | 
|---|
| 1121 |                                 queryStr.append("FROM <");
 | 
|---|
| 1122 |                                 queryStr.append(graphURI);
 | 
|---|
| 1123 |                                 queryStr.append(">\n");
 | 
|---|
| 1124 |                         }
 | 
|---|
| 1125 |                 }
 | 
|---|
| 1126 |                 queryStr.append("WHERE{\n");
 | 
|---|
| 1127 |                 queryStr.append("  ?is rdf:type ?cls.\n");
 | 
|---|
| 1128 |                 queryStr.append("  FILTER(?cls IN (");
 | 
|---|
| 1129 |                 boolean f = false;
 | 
|---|
| 1130 |                 Set<String> clsSet = classMap.keySet();
 | 
|---|
| 1131 |                 for (String clsURI : clsSet) {
 | 
|---|
| 1132 |                         if (f) {
 | 
|---|
| 1133 |                                 queryStr.append(", ");
 | 
|---|
| 1134 |                         }
 | 
|---|
| 1135 |                         f = true;
 | 
|---|
| 1136 |                         queryStr.append("<");
 | 
|---|
| 1137 |                         queryStr.append(clsURI);
 | 
|---|
| 1138 |                         queryStr.append(">");
 | 
|---|
| 1139 |                 }
 | 
|---|
| 1140 |                 queryStr.append("))\n");
 | 
|---|
| 1141 |                 queryStr.append("} GROUP BY ?cls");
 | 
|---|
| 1142 | 
 | 
|---|
| 1143 |                 System.out.println(queryStr.toString());
 | 
|---|
| 1144 | 
 | 
|---|
| 1145 |                 Query query = QueryFactory.create(queryStr.toString());
 | 
|---|
| 1146 |                 QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
 | 
|---|
| 1147 |                                 query);
 | 
|---|
| 1148 | 
 | 
|---|
| 1149 |                 ResultSet results = qexec.execSelect();
 | 
|---|
| 1150 |                 for (; results.hasNext();) {
 | 
|---|
| 1151 |                         QuerySolution sol = results.next();
 | 
|---|
| 1152 |                         String uri = sol.getResource("cls").getURI();
 | 
|---|
| 1153 |                         SClass sc = classMap.get(uri);
 | 
|---|
| 1154 |                         Literal lit = sol.getLiteral("numOfInstances");
 | 
|---|
| 1155 |                         if (lit != null) {
 | 
|---|
| 1156 |                                 int numOfInstances = lit.getInt();
 | 
|---|
| 1157 |                                 sc.setNumOfInstances(numOfInstances);
 | 
|---|
| 1158 |                         }
 | 
|---|
| 1159 |                 }
 | 
|---|
| 1160 |                 qexec.close();
 | 
|---|
| 1161 | 
 | 
|---|
| 1162 |                 return classes;
 | 
|---|
| 1163 |         }
 | 
|---|
| 1164 | 
 | 
|---|
| 1165 | }
 | 
|---|