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