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 |
|
---|
302 |
|
---|
303 | if (countLinks) {
|
---|
304 | queryStr.append(" ?o ?p ?s.");
|
---|
305 | }else{
|
---|
306 | queryStr.append("filter(exists{\n");
|
---|
307 | queryStr.append(" ?o rdf:type ?c.\n");
|
---|
308 | queryStr.append(" ?s rdf:type <");
|
---|
309 | queryStr.append(originClass);
|
---|
310 | queryStr.append(">.\n");
|
---|
311 | queryStr.append(" ?s ?p ?o.})\n");
|
---|
312 | }
|
---|
313 |
|
---|
314 | queryStr.append("}\n UNION\n");
|
---|
315 | queryStr.append(" { ?p rdfs:range <");
|
---|
316 | queryStr.append(originClass);
|
---|
317 | queryStr.append(">.\n");
|
---|
318 | queryStr.append(" ?p rdfs:domain ?c.\n");
|
---|
319 |
|
---|
320 | if (countLinks) {
|
---|
321 | queryStr.append(" ?s ?p ?o.");
|
---|
322 | }else{
|
---|
323 | queryStr.append("filter(exists{\n");
|
---|
324 | queryStr.append(" ?s rdf:type ?c.\n");
|
---|
325 | queryStr.append(" ?o rdf:type <");
|
---|
326 | queryStr.append(originClass);
|
---|
327 | queryStr.append(">.\n");
|
---|
328 | queryStr.append(" ?s ?p ?o.})\n");
|
---|
329 | }
|
---|
330 |
|
---|
331 | queryStr.append("}\n");
|
---|
332 |
|
---|
333 | queryStr.append(" ?p ?pDirection ?c.\n");
|
---|
334 |
|
---|
335 | queryStr.append(" ?s rdf:type ?c.\n");
|
---|
336 |
|
---|
337 | if (countLinks) {
|
---|
338 | queryStr.append("}\nGROUP BY ?p ?pDirection ?c\n");
|
---|
339 | } else {
|
---|
340 | queryStr.append("}\n");
|
---|
341 | }
|
---|
342 | if (limit > 0) {
|
---|
343 | queryStr.append("limit ");
|
---|
344 | queryStr.append(limit);
|
---|
345 | queryStr.append("\n");
|
---|
346 | }
|
---|
347 |
|
---|
348 | System.out.println(queryStr.toString());
|
---|
349 |
|
---|
350 | Query query = QueryFactory.create(queryStr.toString());
|
---|
351 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
352 | query);
|
---|
353 |
|
---|
354 | ResultSet results = qexec.execSelect();
|
---|
355 | ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
|
---|
356 | for (; results.hasNext();) {
|
---|
357 | QuerySolution sol = results.next();
|
---|
358 | Resource pro = sol.getResource("p");
|
---|
359 | if (pro != null) {
|
---|
360 | Resource cls = sol.getResource("c");
|
---|
361 | Resource dir = sol.getResource("pDirection");
|
---|
362 | String proURI = pro.getURI();
|
---|
363 | String clsURI = cls.getURI();
|
---|
364 | String dirURI = dir.getURI();
|
---|
365 |
|
---|
366 | // System.out.println(pro.getURI() + " " + cls.getURI() + " " +
|
---|
367 | // dir.getURI());
|
---|
368 |
|
---|
369 | Direction direction = null;
|
---|
370 | if (dirURI
|
---|
371 | .equals("http://www.w3.org/2000/01/rdf-schema#domain")
|
---|
372 | || dirURI.equals("rdfs:domain")) {
|
---|
373 | direction = Direction.reverse;
|
---|
374 | }
|
---|
375 | if (dirURI.equals("http://www.w3.org/2000/01/rdf-schema#range")
|
---|
376 | || dirURI.equals("rdfs:range")) {
|
---|
377 | if (direction != null) {
|
---|
378 | direction = Direction.both;
|
---|
379 | } else {
|
---|
380 | direction = Direction.forward;
|
---|
381 | }
|
---|
382 | }
|
---|
383 | // System.out.println(direction);
|
---|
384 | int numOfLinks = 0;
|
---|
385 | if (countLinks) {
|
---|
386 | numOfLinks = sol.getLiteral("numOfLinks").getInt();
|
---|
387 | }
|
---|
388 | ClassLink cl = new ClassLink(proURI, clsURI, direction,
|
---|
389 | numOfLinks);
|
---|
390 | solCLs.add(cl);
|
---|
391 | }
|
---|
392 | }
|
---|
393 | qexec.close();
|
---|
394 | return solCLs.toArray(new ClassLink[0]);
|
---|
395 | }
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * 縲謖�ョ壹&繧後◆繧ッ繝ゥ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧ッ繝ゥ繧ケ縺ォ螻槭@縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励@縲� |
---|
399 | * 蜿門セ励@縺溘う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧堤カイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
400 | * <p>
|
---|
401 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
|
---|
402 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
|
---|
403 | * </p>
|
---|
404 | *
|
---|
405 | * @param graphURIs
|
---|
406 | * 縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
407 | * @param originClass
|
---|
408 | * 襍キ轤ケ縺ィ縺ェ繧九け繝ゥ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
409 | * @param limit
|
---|
410 | * 隗」縺ィ縺励※霑斐&繧後kClassLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
411 | * @return ClassLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
412 | * @throws Exception
|
---|
413 | * @since 28.01.2014
|
---|
414 | */
|
---|
415 | public ClassLink[] getNextClassViaInstanceLink(String[] graphURIs,
|
---|
416 | String originClass, int limit) throws Exception {
|
---|
417 | StringBuffer queryStr = new StringBuffer();
|
---|
418 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
419 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
420 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
421 | queryStr.append("SELECT DISTINCT ?pf ?pr ?c \n");
|
---|
422 | if (graphURIs != null) {
|
---|
423 | for (String graphURI : graphURIs) {
|
---|
424 | queryStr.append("FROM <");
|
---|
425 | queryStr.append(graphURI);
|
---|
426 | queryStr.append(">\n");
|
---|
427 | }
|
---|
428 | }
|
---|
429 | queryStr.append("WHERE{\n");
|
---|
430 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
431 | queryStr.append(" ?insOrg rdf:type <");
|
---|
432 | queryStr.append(originClass);
|
---|
433 | queryStr.append(">.\n");
|
---|
434 | queryStr.append(" { ?ins ?pr ?insOrg. }\n UNION { ?insOrg ?pf ?ins. }\n");
|
---|
435 | queryStr.append("}\n");
|
---|
436 | if (limit > 0) {
|
---|
437 | queryStr.append("limit ");
|
---|
438 | queryStr.append(limit);
|
---|
439 | queryStr.append("\n");
|
---|
440 | }
|
---|
441 |
|
---|
442 | // System.out.println(queryStr.toString());
|
---|
443 |
|
---|
444 | Query query = QueryFactory.create(queryStr.toString());
|
---|
445 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
446 | query);
|
---|
447 |
|
---|
448 | ResultSet results = qexec.execSelect();
|
---|
449 | ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
|
---|
450 | for (; results.hasNext();) {
|
---|
451 | QuerySolution sol = results.next();
|
---|
452 | Resource proForward = sol.getResource("pf");
|
---|
453 | Resource proReverse = sol.getResource("pr");
|
---|
454 | Resource cls = sol.getResource("c");
|
---|
455 | Direction direction = null;
|
---|
456 | String propURI = null;
|
---|
457 | if (proForward != null) {
|
---|
458 | if (proReverse != null) {
|
---|
459 | direction = Direction.both;
|
---|
460 | } else {
|
---|
461 | direction = Direction.forward;
|
---|
462 | }
|
---|
463 | propURI = proForward.getURI();
|
---|
464 | } else {
|
---|
465 | direction = Direction.reverse;
|
---|
466 | propURI = proReverse.getURI();
|
---|
467 | }
|
---|
468 | String clsURI = cls.getURI();
|
---|
469 | // System.out.println(propURI + " " + clsURI + " " + direction);
|
---|
470 |
|
---|
471 | ClassLink cl = new ClassLink(propURI, clsURI, direction, 0);
|
---|
472 | solCLs.add(cl);
|
---|
473 | }
|
---|
474 | qexec.close();
|
---|
475 | return solCLs.toArray(new ClassLink[0]);
|
---|
476 | }
|
---|
477 |
|
---|
478 | public Path[] getPaths(String startClass, String endClass) throws Exception {
|
---|
479 | OWLClassGraph graph = new OWLClassGraph(startClass, endClass);
|
---|
480 | return graph.getPaths(this, 0);
|
---|
481 | }
|
---|
482 |
|
---|
483 | public String createSPARQL(Path path) throws Exception {
|
---|
484 | return null;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /*
|
---|
488 | * private String executeSelect(String sparqlQuery) throws Exception {
|
---|
489 | * HttpClient client = HttpClientBuilder.create().build(); HttpPost httppost
|
---|
490 | * = new HttpPost(endpointURI); httppost.setHeader("Content-Type",
|
---|
491 | * "application/x-www-form-urlencoded"); httppost.setHeader("Accept",
|
---|
492 | * "application/sparql-results+xml"); List<NameValuePair> nvpList = new
|
---|
493 | * ArrayList<NameValuePair>(); nvpList.add(new BasicNameValuePair("query",
|
---|
494 | * sparqlQuery)); // nvpList.add(new BasicNameValuePair("format", //
|
---|
495 | * outputFormat.getMime())); httppost.setEntity(new
|
---|
496 | * UrlEncodedFormEntity(nvpList, Charset .forName("UTF-8"))); HttpResponse
|
---|
497 | * response = client.execute(httppost); //
|
---|
498 | * System.out.println("[StatusLine] " + response.getStatusLine());
|
---|
499 | * HttpEntity entity = response.getEntity(); String entityString =
|
---|
500 | * EntityUtils.toString(entity, "UTF-8"); return entityString; }
|
---|
501 | */
|
---|
502 |
|
---|
503 | /**
|
---|
504 | * 縲謖�ョ壹&繧後◆繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ォ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励☆繧九� |
---|
505 | * 縲蜿門セ励&繧後◆蜷�う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧らカイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
506 | * <p>
|
---|
507 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺�<br>
|
---|
508 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k <br>
|
---|
509 | * </p>
|
---|
510 | *
|
---|
511 | * @param graphURIs
|
---|
512 | * 縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
513 | * @param originInstance
|
---|
514 | * 襍キ轤ケ縺ィ縺ェ繧九う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
515 | * @param limit
|
---|
516 | * 隗」縺ィ縺励※霑斐&繧後kInstanceLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
517 | * @return InstanceLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
518 | * @throws Exception
|
---|
519 | * @since 28.01.2014
|
---|
520 | */
|
---|
521 | public InstanceLink[] getNextInstancesViaInstanceLink(String[] graphURIs,
|
---|
522 | String originInstance, int limit) throws Exception {
|
---|
523 | StringBuffer queryStr = new StringBuffer();
|
---|
524 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
525 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
526 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
527 | queryStr.append("SELECT DISTINCT ?pf ?pr ?ins ?c \n");
|
---|
528 | if (graphURIs != null) {
|
---|
529 | for (String graphURI : graphURIs) {
|
---|
530 | queryStr.append("FROM <");
|
---|
531 | queryStr.append(graphURI);
|
---|
532 | queryStr.append(">\n");
|
---|
533 | }
|
---|
534 | }
|
---|
535 | queryStr.append("WHERE{\n");
|
---|
536 | // queryStr.append(" { ?c rdf:type rdfs:Class. }\n UNION\n { ?c rdf:type owl:Class. }\n");
|
---|
537 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
538 | queryStr.append(" { ?ins ?pr <");
|
---|
539 | queryStr.append(originInstance);
|
---|
540 | queryStr.append(">. }\n UNION { <");
|
---|
541 | queryStr.append(originInstance);
|
---|
542 | queryStr.append("> ?pf ?ins. }\n");
|
---|
543 |
|
---|
544 | queryStr.append(" FILTER (?c != rdf:Property)");
|
---|
545 |
|
---|
546 | queryStr.append("}\n");
|
---|
547 | if (limit > 0) {
|
---|
548 | queryStr.append("limit ");
|
---|
549 | queryStr.append(limit);
|
---|
550 | queryStr.append("\n");
|
---|
551 | }
|
---|
552 |
|
---|
553 | // System.out.println(queryStr.toString());
|
---|
554 |
|
---|
555 | Query query = QueryFactory.create(queryStr.toString());
|
---|
556 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
557 | query);
|
---|
558 |
|
---|
559 | ResultSet results = qexec.execSelect();
|
---|
560 | HashMap<String, InstanceLink> insLinkMap = new HashMap<String, InstanceLink>();
|
---|
561 | for (; results.hasNext();) {
|
---|
562 | QuerySolution sol = results.next();
|
---|
563 | Resource proForward = sol.getResource("pf");
|
---|
564 | Resource proReverse = sol.getResource("pr");
|
---|
565 | Resource ins = sol.getResource("ins");
|
---|
566 | Resource cls = sol.getResource("c");
|
---|
567 | Direction direction = null;
|
---|
568 | String propURI = null;
|
---|
569 | if (proForward != null) {
|
---|
570 | if (proReverse != null) {
|
---|
571 | direction = Direction.both;
|
---|
572 | } else {
|
---|
573 | direction = Direction.forward;
|
---|
574 | }
|
---|
575 | propURI = proForward.getURI();
|
---|
576 | } else {
|
---|
577 | direction = Direction.reverse;
|
---|
578 | propURI = proReverse.getURI();
|
---|
579 | }
|
---|
580 | String clsURI = cls.getURI();
|
---|
581 | String insURI = ins.getURI();
|
---|
582 | String key = propURI + "\t" + insURI + "\t" + direction;
|
---|
583 | if (insLinkMap.containsKey(key)) {
|
---|
584 | InstanceLink insLink = insLinkMap.get(key);
|
---|
585 | insLink.addLinkedClassURI(clsURI);
|
---|
586 | } else {
|
---|
587 | InstanceLink insLink = new InstanceLink(propURI, insURI,
|
---|
588 | new String[] { clsURI }, direction);
|
---|
589 | insLinkMap.put(key, insLink);
|
---|
590 | }
|
---|
591 | }
|
---|
592 | qexec.close();
|
---|
593 | Collection<InstanceLink> values = insLinkMap.values();
|
---|
594 | return values.toArray(new InstanceLink[0]);
|
---|
595 | }
|
---|
596 |
|
---|
597 | public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs,
|
---|
598 | String language) throws Exception {
|
---|
599 | if (resourceURIs == null || resourceURIs.length == 0) {
|
---|
600 | return new LabelMap[0];
|
---|
601 | }
|
---|
602 | StringBuffer queryStr = new StringBuffer();
|
---|
603 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
604 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
605 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
606 | queryStr.append("SELECT DISTINCT ?res ?label \n");
|
---|
607 | if (graphURIs != null) {
|
---|
608 | for (String graphURI : graphURIs) {
|
---|
609 | queryStr.append("FROM <");
|
---|
610 | queryStr.append(graphURI);
|
---|
611 | queryStr.append(">\n");
|
---|
612 | }
|
---|
613 | }
|
---|
614 | queryStr.append("WHERE{\n");
|
---|
615 | queryStr.append(" ?res rdfs:label ?label.\n");
|
---|
616 | queryStr.append(" FILTER(?res IN (");
|
---|
617 | boolean f = false;
|
---|
618 | for (String resourceURI : resourceURIs) {
|
---|
619 | if (f) {
|
---|
620 | queryStr.append(", ");
|
---|
621 | }
|
---|
622 | f = true;
|
---|
623 | queryStr.append("<");
|
---|
624 | queryStr.append(resourceURI);
|
---|
625 | queryStr.append(">");
|
---|
626 | }
|
---|
627 | queryStr.append("))\n");
|
---|
628 | queryStr.append("}");
|
---|
629 |
|
---|
630 | System.out.println(queryStr.toString());
|
---|
631 |
|
---|
632 | Query query = QueryFactory.create(queryStr.toString());
|
---|
633 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
634 | query);
|
---|
635 |
|
---|
636 | ResultSet results = qexec.execSelect();
|
---|
637 | HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>();
|
---|
638 | for (; results.hasNext();) {
|
---|
639 | QuerySolution sol = results.next();
|
---|
640 | String uri = sol.getResource("res").getURI();
|
---|
641 | Literal literal = sol.getLiteral("label");
|
---|
642 | if (literal != null) {
|
---|
643 | String label = literal.getString();
|
---|
644 | String lang = literal.getLanguage();
|
---|
645 | if (language != null && language.equals(lang)) {
|
---|
646 | Label lbl = new Label(label, lang);
|
---|
647 | if (lMap.containsKey(uri)) {
|
---|
648 | LabelMap lm = lMap.get(uri);
|
---|
649 | lm.addLabel(lbl);
|
---|
650 | } else {
|
---|
651 | LabelMap lm = new LabelMap(uri, new Label[] { lbl });
|
---|
652 | lMap.put(uri, lm);
|
---|
653 | }
|
---|
654 | }
|
---|
655 | }
|
---|
656 | }
|
---|
657 | return lMap.values().toArray(new LabelMap[0]);
|
---|
658 | }
|
---|
659 |
|
---|
660 | public ClassLink[] countLinks(String[] graphURIs, String startClassURI,
|
---|
661 | ClassLink[] classLinks) throws Exception {
|
---|
662 | if (classLinks == null || classLinks.length == 0) {
|
---|
663 | return new ClassLink[0];
|
---|
664 | }
|
---|
665 | for (ClassLink classLink : classLinks) {
|
---|
666 | StringBuffer queryStr = new StringBuffer();
|
---|
667 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
668 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
669 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
670 |
|
---|
671 | queryStr.append("SELECT (COUNT(?os) AS ?numOfLinks) \n");
|
---|
672 | if (graphURIs != null) {
|
---|
673 | for (String graphURI : graphURIs) {
|
---|
674 | queryStr.append("FROM <");
|
---|
675 | queryStr.append(graphURI);
|
---|
676 | queryStr.append(">\n");
|
---|
677 | }
|
---|
678 | }
|
---|
679 |
|
---|
680 | queryStr.append("WHERE{\n");
|
---|
681 | if (classLink.getDirection() == Direction.forward) {
|
---|
682 | queryStr.append("?is rdf:type <");
|
---|
683 | queryStr.append(startClassURI);
|
---|
684 | queryStr.append(">.\n");
|
---|
685 | queryStr.append("?os rdf:type <");
|
---|
686 | queryStr.append(classLink.getLinkedClassURI());
|
---|
687 | queryStr.append(">.\n");
|
---|
688 | queryStr.append("?is ?p ?os.\n");
|
---|
689 | queryStr.append("}");
|
---|
690 | } else {
|
---|
691 | if (classLink.getDirection() == Direction.forward) {
|
---|
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("}");
|
---|
700 | } else {
|
---|
701 | queryStr.append("?os rdf:type <");
|
---|
702 | queryStr.append(startClassURI);
|
---|
703 | queryStr.append(">.\n");
|
---|
704 | queryStr.append("?is rdf:type <");
|
---|
705 | queryStr.append(classLink.getLinkedClassURI());
|
---|
706 | queryStr.append(">.\n");
|
---|
707 | queryStr.append("{?is ?p ?os.}\n");
|
---|
708 | queryStr.append("UNION\n");
|
---|
709 | queryStr.append("{?os ?p ?is.}\n");
|
---|
710 | queryStr.append("}");
|
---|
711 | }
|
---|
712 | }
|
---|
713 | System.out.println(queryStr.toString());
|
---|
714 |
|
---|
715 | Query query = QueryFactory.create(queryStr.toString());
|
---|
716 | QueryExecution qexec = QueryExecutionFactory.sparqlService(
|
---|
717 | endpointURI, query);
|
---|
718 |
|
---|
719 | ResultSet results = qexec.execSelect();
|
---|
720 | if (results.hasNext()) {
|
---|
721 | QuerySolution sol = results.next();
|
---|
722 | Literal lit = sol.getLiteral("numOfLinks");
|
---|
723 | if (lit != null) {
|
---|
724 | int numOfLinks = lit.getInt();
|
---|
725 | classLink.setNumOfLinks(numOfLinks);
|
---|
726 | }
|
---|
727 | }
|
---|
728 | qexec.close();
|
---|
729 | }
|
---|
730 | return classLinks;
|
---|
731 | }
|
---|
732 |
|
---|
733 | }
|
---|