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 | SClass[] clz = builder.getOWLClasses(graphURIs, keyword, false);
|
---|
64 | for (SClass cls : clz) {
|
---|
65 | System.out.println(cls);
|
---|
66 | }
|
---|
67 | clz = builder.countInstances(null, clz);
|
---|
68 | for (SClass cls : clz) {
|
---|
69 | System.out.println(cls);
|
---|
70 | }
|
---|
71 |
|
---|
72 | System.out.println("CLS");
|
---|
73 |
|
---|
74 | ClassLink[] cls = builder.getNextClass(null, clz[0].getClassURI(), 100,
|
---|
75 | false);
|
---|
76 | if (cls != null) {
|
---|
77 | for (ClassLink cl : cls) {
|
---|
78 | System.out.println(cl.toString());
|
---|
79 | }
|
---|
80 | cls = builder.countLinks(null, clz[0].getClassURI(), cls);
|
---|
81 | for (ClassLink cl : cls) {
|
---|
82 | System.out.println(cl.toString());
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | System.out.println("CLS-INS");
|
---|
87 | cls = builder.getNextClassViaInstanceLink(null, clz[0].getClassURI(),
|
---|
88 | 100);
|
---|
89 | if (cls != null) {
|
---|
90 | for (ClassLink cl : cls) {
|
---|
91 | System.out.println(cl.toString());
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | System.out.println("Instances");
|
---|
96 | Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en");
|
---|
97 | if (ins != null) {
|
---|
98 | for (Instance in : ins) {
|
---|
99 | System.out.println(in.toString());
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * System.out.println("INS-INS"); ins = builder.getInstances(null,
|
---|
105 | * "\"A.C. Reed\"@en"); InstanceLink[] iLinks =
|
---|
106 | * builder.getNextInstancesViaInstanceLink(null,
|
---|
107 | * ins[0].getInstanceURI(), 100);
|
---|
108 | *
|
---|
109 | * if (iLinks != null) { for (InstanceLink in : iLinks) {
|
---|
110 | * System.out.println(in.toString()); } }
|
---|
111 | */
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * 譏守、コ逧�↓RDF縺ァ譖ク縺九l縺ヲ縺�k繧ッ繝ゥ繧ケ繧貞叙蠕励☆繧� |
---|
116 | * <p>
|
---|
117 | * 謖�ョ壹&繧後◆graphURIs縺ョ荳ュ縺九i縲√く繝シ繝ッ繝シ繝峨↓繝偵ャ繝医☆繧九Λ繝吶Ν繧呈戟縺、繧ッ繝ゥ繧ケ��dfs:Class�峨r縺吶∋縺ヲ霑斐☆ <br>
|
---|
118 | * </p>
|
---|
119 | *
|
---|
120 | * @param graphURIs
|
---|
121 | * 縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� (null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
122 | * @param keyword
|
---|
123 | * ��ull繧�ゥコ譁�ュ励�荳榊庄�� |
---|
124 | * @return 繧ッ繝ゥ繧ケURI縺ョ驟榊�
|
---|
125 | * @throws Exception
|
---|
126 | * @since 28.01.2014
|
---|
127 | */
|
---|
128 | public SClass[] getOWLClasses(String[] graphURIs, String keyword,
|
---|
129 | boolean countInstances) throws Exception {
|
---|
130 | StringBuffer queryStr = new StringBuffer();
|
---|
131 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
132 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
133 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
134 | if (countInstances) {
|
---|
135 | queryStr.append("SELECT DISTINCT ?c ?pLabel (COUNT(?i) AS ?numOfInstances)\n");
|
---|
136 | } else {
|
---|
137 | queryStr.append("SELECT DISTINCT ?c ?pLabel \n");
|
---|
138 | }
|
---|
139 | if (graphURIs != null) {
|
---|
140 | for (String graphURI : graphURIs) {
|
---|
141 | queryStr.append("FROM <");
|
---|
142 | queryStr.append(graphURI);
|
---|
143 | queryStr.append(">\n");
|
---|
144 | }
|
---|
145 | }
|
---|
146 | queryStr.append("WHERE{\n");
|
---|
147 |
|
---|
148 | queryStr.append(" {?c rdf:type rdfs:Class} UNION {?c rdf:type owl:Class}\n");
|
---|
149 | // queryStr.append(" ?i rdf:type ?c.\n");
|
---|
150 |
|
---|
151 |
|
---|
152 |
|
---|
153 | if (countInstances) {
|
---|
154 | queryStr.append(" ?c rdfs:label ?label.\n");
|
---|
155 | }
|
---|
156 | queryStr.append(" ?c rdfs:label ?pLabel.\n");
|
---|
157 | if (countInstances) {
|
---|
158 | queryStr.append(" ?i rdf:type ?c.\n");
|
---|
159 | }
|
---|
160 | // queryStr.append(" ?c rdfs:label ");
|
---|
161 | // queryStr.append(keyword);
|
---|
162 | // queryStr.append(".");
|
---|
163 | if (keyword != null && keyword.length() != 0) {
|
---|
164 | queryStr.append(" FILTER (\n");
|
---|
165 | queryStr.append(" REGEX( ?label , \"");
|
---|
166 | queryStr.append(keyword);
|
---|
167 | queryStr.append("\" , \"i\" )\n");
|
---|
168 | queryStr.append(" )\n");
|
---|
169 | }
|
---|
170 | if (countInstances) {
|
---|
171 | queryStr.append("} GROUP BY ?c ?pLabel");
|
---|
172 | } else {
|
---|
173 | queryStr.append("}");
|
---|
174 | }
|
---|
175 | System.out.println(queryStr.toString());
|
---|
176 |
|
---|
177 | Query query = QueryFactory.create(queryStr.toString());
|
---|
178 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
179 | query);
|
---|
180 |
|
---|
181 | ResultSet results = qexec.execSelect();
|
---|
182 | HashMap<String, SClass> classMap = new HashMap<String, SClass>();
|
---|
183 | for (; results.hasNext();) {
|
---|
184 | QuerySolution sol = results.next();
|
---|
185 | Resource res = sol.getResource("c");
|
---|
186 | if (res != null) {
|
---|
187 | String uri = res.getURI();
|
---|
188 | int numOfInstances = 0;
|
---|
189 | if (countInstances) {
|
---|
190 | numOfInstances = sol.getLiteral("numOfInstances").getInt();
|
---|
191 | }
|
---|
192 | // System.out.println(numOfInstances);
|
---|
193 | Literal labelLiteral = sol.getLiteral("pLabel");
|
---|
194 | SClass sClass = null;
|
---|
195 | if (classMap.containsKey(uri)) {
|
---|
196 | sClass = classMap.get(uri);
|
---|
197 | } else {
|
---|
198 | sClass = new SClass(uri, null, numOfInstances);
|
---|
199 | classMap.put(uri, sClass);
|
---|
200 | }
|
---|
201 | if (labelLiteral != null) {
|
---|
202 | String label = labelLiteral.getString();
|
---|
203 | String lang = labelLiteral.getLanguage();
|
---|
204 | sClass.addLabel(new Label(label, lang));
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 | qexec.close();
|
---|
209 | return classMap.values().toArray(new SClass[0]);
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * 縲繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧貞叙蠕励☆繧� |
---|
214 | * <p>
|
---|
215 | * 謖�ョ壹&繧後◆graphURIs縺ョ荳ュ縺九i縲√く繝シ繝ッ繝シ繝峨↓繝偵ャ繝医☆繧九Λ繝吶Ν繧呈戟縺、繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧偵☆縺ケ縺ヲ霑斐☆ <br>
|
---|
216 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
|
---|
217 | * </p>
|
---|
218 | *
|
---|
219 | * @param graphURIs
|
---|
220 | * 縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� (null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
221 | * @param keyword
|
---|
222 | * ��ull繧�ゥコ譁�ュ励�荳榊庄�� |
---|
223 | * @return 繧ッ繝ゥ繧ケURI縺ョ驟榊�
|
---|
224 | * @throws Exception
|
---|
225 | * @since 28.01.2014
|
---|
226 | */
|
---|
227 | public Instance[] getInstances(String[] graphURIs, String keyword)
|
---|
228 | throws Exception {
|
---|
229 | StringBuffer queryStr = new StringBuffer();
|
---|
230 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
231 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
232 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
233 | queryStr.append("SELECT DISTINCT ?ins ?c \n");
|
---|
234 | if (graphURIs != null) {
|
---|
235 | for (String graphURI : graphURIs) {
|
---|
236 | queryStr.append("FROM <");
|
---|
237 | queryStr.append(graphURI);
|
---|
238 | queryStr.append(">\n");
|
---|
239 | }
|
---|
240 | }
|
---|
241 | queryStr.append("WHERE{\n");
|
---|
242 | // queryStr.append(" {?c rdf:type rdfs:Class} UNION {?c rdf:type owl:Class}\n");
|
---|
243 |
|
---|
244 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
245 | queryStr.append(" ?ins rdfs:label ");
|
---|
246 | queryStr.append(keyword);
|
---|
247 | queryStr.append(".\n");
|
---|
248 | queryStr.append(" FILTER (?c != rdf:Property)");
|
---|
249 | queryStr.append("}");
|
---|
250 |
|
---|
251 | // System.out.println(queryStr.toString());
|
---|
252 |
|
---|
253 | Query query = QueryFactory.create(queryStr.toString());
|
---|
254 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
255 | query);
|
---|
256 |
|
---|
257 | ResultSet results = qexec.execSelect();
|
---|
258 | HashMap<String, HashSet<String>> instanceMap = new HashMap<String, HashSet<String>>();
|
---|
259 | for (; results.hasNext();) {
|
---|
260 | QuerySolution sol = results.next();
|
---|
261 | Resource cls = sol.getResource("c");
|
---|
262 | Resource ins = sol.getResource("ins");
|
---|
263 | String clsURI = cls.getURI();
|
---|
264 | String insURI = ins.getURI();
|
---|
265 | if (instanceMap.containsKey(insURI)) {
|
---|
266 | HashSet<String> classes = instanceMap.get(insURI);
|
---|
267 | classes.add(clsURI);
|
---|
268 | } else {
|
---|
269 | HashSet<String> classes = new HashSet<String>();
|
---|
270 | instanceMap.put(insURI, classes);
|
---|
271 | classes.add(clsURI);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | qexec.close();
|
---|
275 | Set<String> keySet = instanceMap.keySet();
|
---|
276 | ArrayList<Instance> instanceList = new ArrayList<Instance>();
|
---|
277 | for (String key : keySet) {
|
---|
278 | Instance ins = new Instance(key, instanceMap.get(key).toArray(
|
---|
279 | new String[0]));
|
---|
280 | instanceList.add(ins);
|
---|
281 | }
|
---|
282 | return instanceList.toArray(new Instance[0]);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * 縲謖�ョ壹&繧後◆繧ッ繝ゥ繧ケ繧定オキ轤ケ縺ィ縺励∵�遉コ逧�↓險倩ソー縺輔l縺ヲ縺�kOWL縺ョproperty蛻カ邏�r隱ソ縺ケ縲√◎縺ョproperty蛻カ邏�〒
|
---|
287 | * 髢「騾」縺・縺代i繧後※縺�k繧ッ繝ゥ繧ケ繧堤カイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
288 | * <p>
|
---|
289 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
|
---|
290 | * <br>
|
---|
291 | * </p>
|
---|
292 | *
|
---|
293 | * @param graphURIs
|
---|
294 | * 縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
295 | * @param originClass
|
---|
296 | * 襍キ轤ケ縺ィ縺ェ繧九け繝ゥ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
297 | * @param limit
|
---|
298 | * 隗」縺ィ縺励※霑斐&繧後kClassLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
299 | * @return ClassLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
300 | * @throws Exception
|
---|
301 | * @since 28.01.2014
|
---|
302 | */
|
---|
303 | public ClassLink[] getNextClass(String[] graphURIs, String originClass,
|
---|
304 | int limit, boolean countLinks) throws Exception {
|
---|
305 | StringBuffer queryStr = new StringBuffer();
|
---|
306 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
307 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
308 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
309 |
|
---|
310 | // SELECT
|
---|
311 | if (countLinks) {
|
---|
312 | queryStr.append("SELECT DISTINCT ?p ?pDirection ?c (COUNT(?s) AS ?numOfLinks) \n");
|
---|
313 | } else {
|
---|
314 | queryStr.append("SELECT DISTINCT ?p ?pDirection ?c \n");
|
---|
315 | }
|
---|
316 |
|
---|
317 | if (graphURIs != null) {
|
---|
318 | for (String graphURI : graphURIs) {
|
---|
319 | queryStr.append("FROM <");
|
---|
320 | queryStr.append(graphURI);
|
---|
321 | queryStr.append(">\n");
|
---|
322 | }
|
---|
323 | }
|
---|
324 | queryStr.append("WHERE{\n");
|
---|
325 | // queryStr.append(" { ?c rdf:type rdfs:Class. }\n UNION\n { ?c rdf:type owl:Class. }\n");
|
---|
326 | queryStr.append(" { ?p rdfs:domain <");
|
---|
327 | queryStr.append(originClass);
|
---|
328 | queryStr.append(">.\n ?p rdfs:range ?c.\n");
|
---|
329 |
|
---|
330 | if (countLinks) {
|
---|
331 | queryStr.append(" ?o ?p ?s.");
|
---|
332 | } else {
|
---|
333 | queryStr.append("filter(exists{\n");
|
---|
334 | queryStr.append(" ?o rdf:type ?c.\n");
|
---|
335 | queryStr.append(" ?s rdf:type <");
|
---|
336 | queryStr.append(originClass);
|
---|
337 | queryStr.append(">.\n");
|
---|
338 | queryStr.append(" ?s ?p ?o.})\n");
|
---|
339 | }
|
---|
340 |
|
---|
341 | queryStr.append("}\n UNION\n");
|
---|
342 | queryStr.append(" { ?p rdfs:range <");
|
---|
343 | queryStr.append(originClass);
|
---|
344 | queryStr.append(">.\n");
|
---|
345 | queryStr.append(" ?p rdfs:domain ?c.\n");
|
---|
346 |
|
---|
347 | if (countLinks) {
|
---|
348 | queryStr.append(" ?s ?p ?o.");
|
---|
349 | } else {
|
---|
350 | queryStr.append("filter(exists{\n");
|
---|
351 | queryStr.append(" ?s rdf:type ?c.\n");
|
---|
352 | queryStr.append(" ?o rdf:type <");
|
---|
353 | queryStr.append(originClass);
|
---|
354 | queryStr.append(">.\n");
|
---|
355 | queryStr.append(" ?s ?p ?o.})\n");
|
---|
356 | }
|
---|
357 |
|
---|
358 | queryStr.append("}\n");
|
---|
359 |
|
---|
360 | queryStr.append(" ?p ?pDirection ?c.\n");
|
---|
361 |
|
---|
362 | queryStr.append(" ?s rdf:type ?c.\n");
|
---|
363 |
|
---|
364 | if (countLinks) {
|
---|
365 | queryStr.append("}\nGROUP BY ?p ?pDirection ?c\n");
|
---|
366 | } else {
|
---|
367 | queryStr.append("}\n");
|
---|
368 | }
|
---|
369 | if (limit > 0) {
|
---|
370 | queryStr.append("limit ");
|
---|
371 | queryStr.append(limit);
|
---|
372 | queryStr.append("\n");
|
---|
373 | }
|
---|
374 |
|
---|
375 | System.out.println(queryStr.toString());
|
---|
376 |
|
---|
377 | Query query = QueryFactory.create(queryStr.toString());
|
---|
378 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
379 | query);
|
---|
380 |
|
---|
381 | ResultSet results = qexec.execSelect();
|
---|
382 | ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
|
---|
383 | for (; results.hasNext();) {
|
---|
384 | QuerySolution sol = results.next();
|
---|
385 | Resource pro = sol.getResource("p");
|
---|
386 | if (pro != null) {
|
---|
387 | Resource cls = sol.getResource("c");
|
---|
388 | Resource dir = sol.getResource("pDirection");
|
---|
389 | String proURI = pro.getURI();
|
---|
390 | String clsURI = cls.getURI();
|
---|
391 | String dirURI = dir.getURI();
|
---|
392 |
|
---|
393 | // System.out.println(pro.getURI() + " " + cls.getURI() + " " +
|
---|
394 | // dir.getURI());
|
---|
395 |
|
---|
396 | Direction direction = null;
|
---|
397 | if (dirURI
|
---|
398 | .equals("http://www.w3.org/2000/01/rdf-schema#domain")
|
---|
399 | || dirURI.equals("rdfs:domain")) {
|
---|
400 | direction = Direction.reverse;
|
---|
401 | }
|
---|
402 | if (dirURI.equals("http://www.w3.org/2000/01/rdf-schema#range")
|
---|
403 | || dirURI.equals("rdfs:range")) {
|
---|
404 | if (direction != null) {
|
---|
405 | direction = Direction.both;
|
---|
406 | } else {
|
---|
407 | direction = Direction.forward;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | // System.out.println(direction);
|
---|
411 | int numOfLinks = 0;
|
---|
412 | if (countLinks) {
|
---|
413 | numOfLinks = sol.getLiteral("numOfLinks").getInt();
|
---|
414 | }
|
---|
415 | ClassLink cl = new ClassLink(proURI, clsURI, direction,
|
---|
416 | numOfLinks);
|
---|
417 | solCLs.add(cl);
|
---|
418 | }
|
---|
419 | }
|
---|
420 | qexec.close();
|
---|
421 | return solCLs.toArray(new ClassLink[0]);
|
---|
422 | }
|
---|
423 |
|
---|
424 | /**
|
---|
425 | * 縲謖�ョ壹&繧後◆繧ッ繝ゥ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧ッ繝ゥ繧ケ縺ォ螻槭@縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励@縲� |
---|
426 | * 蜿門セ励@縺溘う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧堤カイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
427 | * <p>
|
---|
428 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
|
---|
429 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
|
---|
430 | * </p>
|
---|
431 | *
|
---|
432 | * @param graphURIs
|
---|
433 | * 縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
434 | * @param originClass
|
---|
435 | * 襍キ轤ケ縺ィ縺ェ繧九け繝ゥ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
436 | * @param limit
|
---|
437 | * 隗」縺ィ縺励※霑斐&繧後kClassLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
438 | * @return ClassLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
439 | * @throws Exception
|
---|
440 | * @since 28.01.2014
|
---|
441 | */
|
---|
442 | public ClassLink[] getNextClassViaInstanceLink(String[] graphURIs,
|
---|
443 | String originClass, int limit) throws Exception {
|
---|
444 | StringBuffer queryStr = new StringBuffer();
|
---|
445 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
446 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
447 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
448 | queryStr.append("SELECT DISTINCT ?pf ?pr ?c \n");
|
---|
449 | if (graphURIs != null) {
|
---|
450 | for (String graphURI : graphURIs) {
|
---|
451 | queryStr.append("FROM <");
|
---|
452 | queryStr.append(graphURI);
|
---|
453 | queryStr.append(">\n");
|
---|
454 | }
|
---|
455 | }
|
---|
456 | queryStr.append("WHERE{\n");
|
---|
457 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
458 | queryStr.append(" ?insOrg rdf:type <");
|
---|
459 | queryStr.append(originClass);
|
---|
460 | queryStr.append(">.\n");
|
---|
461 | queryStr.append(" { ?ins ?pr ?insOrg. }\n UNION { ?insOrg ?pf ?ins. }\n");
|
---|
462 | queryStr.append("}\n");
|
---|
463 | if (limit > 0) {
|
---|
464 | queryStr.append("limit ");
|
---|
465 | queryStr.append(limit);
|
---|
466 | queryStr.append("\n");
|
---|
467 | }
|
---|
468 |
|
---|
469 | // System.out.println(queryStr.toString());
|
---|
470 |
|
---|
471 | Query query = QueryFactory.create(queryStr.toString());
|
---|
472 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
473 | query);
|
---|
474 |
|
---|
475 | ResultSet results = qexec.execSelect();
|
---|
476 | ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
|
---|
477 | for (; results.hasNext();) {
|
---|
478 | QuerySolution sol = results.next();
|
---|
479 | Resource proForward = sol.getResource("pf");
|
---|
480 | Resource proReverse = sol.getResource("pr");
|
---|
481 | Resource cls = sol.getResource("c");
|
---|
482 | Direction direction = null;
|
---|
483 | String propURI = null;
|
---|
484 | if (proForward != null) {
|
---|
485 | if (proReverse != null) {
|
---|
486 | direction = Direction.both;
|
---|
487 | } else {
|
---|
488 | direction = Direction.forward;
|
---|
489 | }
|
---|
490 | propURI = proForward.getURI();
|
---|
491 | } else {
|
---|
492 | direction = Direction.reverse;
|
---|
493 | propURI = proReverse.getURI();
|
---|
494 | }
|
---|
495 | String clsURI = cls.getURI();
|
---|
496 | // System.out.println(propURI + " " + clsURI + " " + direction);
|
---|
497 |
|
---|
498 | ClassLink cl = new ClassLink(propURI, clsURI, direction, 0);
|
---|
499 | solCLs.add(cl);
|
---|
500 | }
|
---|
501 | qexec.close();
|
---|
502 | return solCLs.toArray(new ClassLink[0]);
|
---|
503 | }
|
---|
504 |
|
---|
505 | public Path[] getPaths(String startClass, String endClass, int mode,
|
---|
506 | boolean countLinks) throws Exception {
|
---|
507 | OWLClassGraph graph = new OWLClassGraph(startClass, endClass);
|
---|
508 | return graph.getPaths(this, mode, countLinks);
|
---|
509 | }
|
---|
510 |
|
---|
511 | public String createSPARQL(Path path) throws Exception {
|
---|
512 | return null;
|
---|
513 | }
|
---|
514 |
|
---|
515 | /*
|
---|
516 | * private String executeSelect(String sparqlQuery) throws Exception {
|
---|
517 | * HttpClient client = HttpClientBuilder.create().build(); HttpPost httppost
|
---|
518 | * = new HttpPost(endpointURI); httppost.setHeader("Content-Type",
|
---|
519 | * "application/x-www-form-urlencoded"); httppost.setHeader("Accept",
|
---|
520 | * "application/sparql-results+xml"); List<NameValuePair> nvpList = new
|
---|
521 | * ArrayList<NameValuePair>(); nvpList.add(new BasicNameValuePair("query",
|
---|
522 | * sparqlQuery)); // nvpList.add(new BasicNameValuePair("format", //
|
---|
523 | * outputFormat.getMime())); httppost.setEntity(new
|
---|
524 | * UrlEncodedFormEntity(nvpList, Charset .forName("UTF-8"))); HttpResponse
|
---|
525 | * response = client.execute(httppost); //
|
---|
526 | * System.out.println("[StatusLine] " + response.getStatusLine());
|
---|
527 | * HttpEntity entity = response.getEntity(); String entityString =
|
---|
528 | * EntityUtils.toString(entity, "UTF-8"); return entityString; }
|
---|
529 | */
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * 縲謖�ョ壹&繧後◆繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ォ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励☆繧九� |
---|
533 | * 縲蜿門セ励&繧後◆蜷�う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧らカイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
534 | * <p>
|
---|
535 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
|
---|
536 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
|
---|
537 | * </p>
|
---|
538 | *
|
---|
539 | * @param graphURIs
|
---|
540 | * 縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
541 | * @param originInstance
|
---|
542 | * 襍キ轤ケ縺ィ縺ェ繧九う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
543 | * @param limit
|
---|
544 | * 隗」縺ィ縺励※霑斐&繧後kInstanceLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
545 | * @return InstanceLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
546 | * @throws Exception
|
---|
547 | * @since 28.01.2014
|
---|
548 | */
|
---|
549 | public InstanceLink[] getNextInstancesViaInstanceLink(String[] graphURIs,
|
---|
550 | String originInstance, int limit) throws Exception {
|
---|
551 | StringBuffer queryStr = new StringBuffer();
|
---|
552 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
553 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
554 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
555 | queryStr.append("SELECT DISTINCT ?pf ?pr ?ins ?c \n");
|
---|
556 | if (graphURIs != null) {
|
---|
557 | for (String graphURI : graphURIs) {
|
---|
558 | queryStr.append("FROM <");
|
---|
559 | queryStr.append(graphURI);
|
---|
560 | queryStr.append(">\n");
|
---|
561 | }
|
---|
562 | }
|
---|
563 | queryStr.append("WHERE{\n");
|
---|
564 | // queryStr.append(" { ?c rdf:type rdfs:Class. }\n UNION\n { ?c rdf:type owl:Class. }\n");
|
---|
565 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
566 | queryStr.append(" { ?ins ?pr <");
|
---|
567 | queryStr.append(originInstance);
|
---|
568 | queryStr.append(">. }\n UNION { <");
|
---|
569 | queryStr.append(originInstance);
|
---|
570 | queryStr.append("> ?pf ?ins. }\n");
|
---|
571 |
|
---|
572 | queryStr.append(" FILTER (?c != rdf:Property)");
|
---|
573 |
|
---|
574 | queryStr.append("}\n");
|
---|
575 | if (limit > 0) {
|
---|
576 | queryStr.append("limit ");
|
---|
577 | queryStr.append(limit);
|
---|
578 | queryStr.append("\n");
|
---|
579 | }
|
---|
580 |
|
---|
581 | // System.out.println(queryStr.toString());
|
---|
582 |
|
---|
583 | Query query = QueryFactory.create(queryStr.toString());
|
---|
584 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
585 | query);
|
---|
586 |
|
---|
587 | ResultSet results = qexec.execSelect();
|
---|
588 | HashMap<String, InstanceLink> insLinkMap = new HashMap<String, InstanceLink>();
|
---|
589 | for (; results.hasNext();) {
|
---|
590 | QuerySolution sol = results.next();
|
---|
591 | Resource proForward = sol.getResource("pf");
|
---|
592 | Resource proReverse = sol.getResource("pr");
|
---|
593 | Resource ins = sol.getResource("ins");
|
---|
594 | Resource cls = sol.getResource("c");
|
---|
595 | Direction direction = null;
|
---|
596 | String propURI = null;
|
---|
597 | if (proForward != null) {
|
---|
598 | if (proReverse != null) {
|
---|
599 | direction = Direction.both;
|
---|
600 | } else {
|
---|
601 | direction = Direction.forward;
|
---|
602 | }
|
---|
603 | propURI = proForward.getURI();
|
---|
604 | } else {
|
---|
605 | direction = Direction.reverse;
|
---|
606 | propURI = proReverse.getURI();
|
---|
607 | }
|
---|
608 | String clsURI = cls.getURI();
|
---|
609 | String insURI = ins.getURI();
|
---|
610 | String key = propURI + "\t" + insURI + "\t" + direction;
|
---|
611 | if (insLinkMap.containsKey(key)) {
|
---|
612 | InstanceLink insLink = insLinkMap.get(key);
|
---|
613 | insLink.addLinkedClassURI(clsURI);
|
---|
614 | } else {
|
---|
615 | InstanceLink insLink = new InstanceLink(propURI, insURI,
|
---|
616 | new String[] { clsURI }, direction);
|
---|
617 | insLinkMap.put(key, insLink);
|
---|
618 | }
|
---|
619 | }
|
---|
620 | qexec.close();
|
---|
621 | Collection<InstanceLink> values = insLinkMap.values();
|
---|
622 | return values.toArray(new InstanceLink[0]);
|
---|
623 | }
|
---|
624 |
|
---|
625 | public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs,
|
---|
626 | String language) throws Exception {
|
---|
627 | if (resourceURIs == null || resourceURIs.length == 0) {
|
---|
628 | return new LabelMap[0];
|
---|
629 | }
|
---|
630 | StringBuffer queryStr = new StringBuffer();
|
---|
631 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
632 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
633 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
634 | queryStr.append("SELECT DISTINCT ?res ?label \n");
|
---|
635 | if (graphURIs != null) {
|
---|
636 | for (String graphURI : graphURIs) {
|
---|
637 | queryStr.append("FROM <");
|
---|
638 | queryStr.append(graphURI);
|
---|
639 | queryStr.append(">\n");
|
---|
640 | }
|
---|
641 | }
|
---|
642 | queryStr.append("WHERE{\n");
|
---|
643 | queryStr.append(" ?res rdfs:label ?label.\n");
|
---|
644 | queryStr.append(" FILTER(?res IN (");
|
---|
645 | boolean f = false;
|
---|
646 | for (String resourceURI : resourceURIs) {
|
---|
647 | if (f) {
|
---|
648 | queryStr.append(", ");
|
---|
649 | }
|
---|
650 | f = true;
|
---|
651 | queryStr.append("<");
|
---|
652 | queryStr.append(resourceURI);
|
---|
653 | queryStr.append(">");
|
---|
654 | }
|
---|
655 | queryStr.append("))\n");
|
---|
656 | queryStr.append("}");
|
---|
657 |
|
---|
658 | System.out.println(queryStr.toString());
|
---|
659 |
|
---|
660 | Query query = QueryFactory.create(queryStr.toString());
|
---|
661 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
662 | query);
|
---|
663 |
|
---|
664 | ResultSet results = qexec.execSelect();
|
---|
665 | HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>();
|
---|
666 | for (; results.hasNext();) {
|
---|
667 | QuerySolution sol = results.next();
|
---|
668 | String uri = sol.getResource("res").getURI();
|
---|
669 | Literal literal = sol.getLiteral("label");
|
---|
670 | if (literal != null) {
|
---|
671 | String label = literal.getString();
|
---|
672 | String lang = literal.getLanguage();
|
---|
673 | if (language != null && language.equals(lang)) {
|
---|
674 | Label lbl = new Label(label, lang);
|
---|
675 | if (lMap.containsKey(uri)) {
|
---|
676 | LabelMap lm = lMap.get(uri);
|
---|
677 | lm.addLabel(lbl);
|
---|
678 | } else {
|
---|
679 | LabelMap lm = new LabelMap(uri, new Label[] { lbl });
|
---|
680 | lMap.put(uri, lm);
|
---|
681 | }
|
---|
682 | }
|
---|
683 | }
|
---|
684 | }
|
---|
685 | return lMap.values().toArray(new LabelMap[0]);
|
---|
686 | }
|
---|
687 |
|
---|
688 | public ClassLink[] countLinks(String[] graphURIs, String startClassURI,
|
---|
689 | ClassLink[] classLinks) throws Exception {
|
---|
690 | if (classLinks == null || classLinks.length == 0) {
|
---|
691 | return new ClassLink[0];
|
---|
692 | }
|
---|
693 | for (ClassLink classLink : classLinks) {
|
---|
694 | StringBuffer queryStr = new StringBuffer();
|
---|
695 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
696 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
697 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
698 |
|
---|
699 | queryStr.append("SELECT (COUNT(?os) AS ?numOfLinks) \n");
|
---|
700 | if (graphURIs != null) {
|
---|
701 | for (String graphURI : graphURIs) {
|
---|
702 | queryStr.append("FROM <");
|
---|
703 | queryStr.append(graphURI);
|
---|
704 | queryStr.append(">\n");
|
---|
705 | }
|
---|
706 | }
|
---|
707 |
|
---|
708 | queryStr.append("WHERE{\n");
|
---|
709 | if (classLink.getDirection() == Direction.forward) {
|
---|
710 | queryStr.append("?is rdf:type <");
|
---|
711 | queryStr.append(startClassURI);
|
---|
712 | queryStr.append(">.\n");
|
---|
713 | queryStr.append("?os rdf:type <");
|
---|
714 | queryStr.append(classLink.getLinkedClassURI());
|
---|
715 | queryStr.append(">.\n");
|
---|
716 | queryStr.append("?is ?p ?os.\n");
|
---|
717 | queryStr.append("}");
|
---|
718 | } else {
|
---|
719 | if (classLink.getDirection() == Direction.forward) {
|
---|
720 | queryStr.append("?os rdf:type <");
|
---|
721 | queryStr.append(startClassURI);
|
---|
722 | queryStr.append(">.\n");
|
---|
723 | queryStr.append("?is rdf:type <");
|
---|
724 | queryStr.append(classLink.getLinkedClassURI());
|
---|
725 | queryStr.append(">.\n");
|
---|
726 | queryStr.append("?is ?p ?os.\n");
|
---|
727 | queryStr.append("}");
|
---|
728 | } else {
|
---|
729 | queryStr.append("?os rdf:type <");
|
---|
730 | queryStr.append(startClassURI);
|
---|
731 | queryStr.append(">.\n");
|
---|
732 | queryStr.append("?is rdf:type <");
|
---|
733 | queryStr.append(classLink.getLinkedClassURI());
|
---|
734 | queryStr.append(">.\n");
|
---|
735 | queryStr.append("{?is ?p ?os.}\n");
|
---|
736 | queryStr.append("UNION\n");
|
---|
737 | queryStr.append("{?os ?p ?is.}\n");
|
---|
738 | queryStr.append("}");
|
---|
739 | }
|
---|
740 | }
|
---|
741 | System.out.println(queryStr.toString());
|
---|
742 |
|
---|
743 | Query query = QueryFactory.create(queryStr.toString());
|
---|
744 | QueryExecution qexec = QueryExecutionFactory.sparqlService(
|
---|
745 | endpointURI, query);
|
---|
746 |
|
---|
747 | ResultSet results = qexec.execSelect();
|
---|
748 | if (results.hasNext()) {
|
---|
749 | QuerySolution sol = results.next();
|
---|
750 | Literal lit = sol.getLiteral("numOfLinks");
|
---|
751 | if (lit != null) {
|
---|
752 | int numOfLinks = lit.getInt();
|
---|
753 | classLink.setNumOfLinks(numOfLinks);
|
---|
754 | }
|
---|
755 | }
|
---|
756 | qexec.close();
|
---|
757 | }
|
---|
758 | return classLinks;
|
---|
759 | }
|
---|
760 |
|
---|
761 | public SClass[] countInstances(String[] graphURIs, SClass[] classes)
|
---|
762 | throws Exception {
|
---|
763 | if (classes == null || classes.length == 0) {
|
---|
764 | return new SClass[0];
|
---|
765 | }
|
---|
766 | HashMap<String, SClass> classMap = new HashMap<String, SClass>();
|
---|
767 | for (SClass sc : classes) {
|
---|
768 | classMap.put(sc.getClassURI(), sc);
|
---|
769 | }
|
---|
770 | StringBuffer queryStr = new StringBuffer();
|
---|
771 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
772 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
773 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
774 | queryStr.append("SELECT DISTINCT ?cls (COUNT(?is) AS ?numOfInstances)\n");
|
---|
775 | if (graphURIs != null) {
|
---|
776 | for (String graphURI : graphURIs) {
|
---|
777 | queryStr.append("FROM <");
|
---|
778 | queryStr.append(graphURI);
|
---|
779 | queryStr.append(">\n");
|
---|
780 | }
|
---|
781 | }
|
---|
782 | queryStr.append("WHERE{\n");
|
---|
783 | queryStr.append(" ?is rdf:type ?cls.\n");
|
---|
784 | queryStr.append(" FILTER(?cls IN (");
|
---|
785 | boolean f = false;
|
---|
786 | Set<String> clsSet = classMap.keySet();
|
---|
787 | for (String clsURI : clsSet) {
|
---|
788 | if (f) {
|
---|
789 | queryStr.append(", ");
|
---|
790 | }
|
---|
791 | f = true;
|
---|
792 | queryStr.append("<");
|
---|
793 | queryStr.append(clsURI);
|
---|
794 | queryStr.append(">");
|
---|
795 | }
|
---|
796 | queryStr.append("))\n");
|
---|
797 | queryStr.append("} GROUP BY ?cls");
|
---|
798 |
|
---|
799 | System.out.println(queryStr.toString());
|
---|
800 |
|
---|
801 | Query query = QueryFactory.create(queryStr.toString());
|
---|
802 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
803 | query);
|
---|
804 |
|
---|
805 | ResultSet results = qexec.execSelect();
|
---|
806 | for (; results.hasNext();) {
|
---|
807 | QuerySolution sol = results.next();
|
---|
808 | String uri = sol.getResource("cls").getURI();
|
---|
809 | SClass sc = classMap.get(uri);
|
---|
810 | Literal lit = sol.getLiteral("numOfInstances");
|
---|
811 | if (lit != null) {
|
---|
812 | int numOfInstances = lit.getInt();
|
---|
813 | sc.setNumOfInstances(numOfInstances);
|
---|
814 | }
|
---|
815 | }
|
---|
816 | qexec.close();
|
---|
817 |
|
---|
818 | return classes;
|
---|
819 | }
|
---|
820 |
|
---|
821 | }
|
---|