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