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