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