1 | package org.biohackathon.SPARQLBuilder.OWL;
|
---|
2 |
|
---|
3 | import java.nio.charset.Charset;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.HashSet;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import org.apache.http.HttpEntity;
|
---|
12 | import org.apache.http.HttpResponse;
|
---|
13 | import org.apache.http.NameValuePair;
|
---|
14 | import org.apache.http.client.HttpClient;
|
---|
15 | import org.apache.http.client.entity.UrlEncodedFormEntity;
|
---|
16 | import org.apache.http.client.methods.HttpPost;
|
---|
17 | //import org.apache.http.impl.client.HttpClientBuilder;
|
---|
18 | import org.apache.http.message.BasicNameValuePair;
|
---|
19 | import org.apache.http.util.EntityUtils;
|
---|
20 |
|
---|
21 | import com.hp.hpl.jena.query.Query;
|
---|
22 | import com.hp.hpl.jena.query.QueryExecution;
|
---|
23 | import com.hp.hpl.jena.query.QueryExecutionFactory;
|
---|
24 | import com.hp.hpl.jena.query.QueryFactory;
|
---|
25 | import com.hp.hpl.jena.query.QuerySolution;
|
---|
26 | import com.hp.hpl.jena.query.ResultSet;
|
---|
27 | import com.hp.hpl.jena.rdf.model.Resource;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * 繧ッ繧ィ繝ェ繧堤函謌舌☆繧九◆繧√�讖溯�繧呈署萓帙☆繧区ク繧ッ繝ゥ繧ケ
|
---|
31 | * @author Norio KOBAYASHI
|
---|
32 | * @since 28.01.2014
|
---|
33 | * @version 29.01.2014
|
---|
34 | */
|
---|
35 | public class OWLQueryBuilderImpl implements OWLQueryBuilder {
|
---|
36 |
|
---|
37 | // private Model model = null;
|
---|
38 | private String endpointURI = null;
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * 繧「繧ッ繧サ繧ケ縺吶kSPARQL endpoint縺ョURI繧呈欠螳壹☆繧区ァ区�蟄� |
---|
43 | *
|
---|
44 | * @param endpointURI縲繧「繧ッ繧サ繧ケ縺吶kSPARQL endpoint縺ョURI
|
---|
45 | * @throws Exception
|
---|
46 | * @since 28.01.2014
|
---|
47 | */
|
---|
48 | public OWLQueryBuilderImpl(String endpointURI) {
|
---|
49 | this.endpointURI = endpointURI;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * 繝�せ繝医↓菴ソ逕ィ縺吶kmain繝。繧ス繝�ラ
|
---|
54 | * <p>
|
---|
55 | * 繧ッ繧ィ繝ェ繝薙Ν繝繝シ縺ョ譛ャ逡ェ繝励Ο繧ー繝ゥ繝縺ァ縺ッ縺薙�繝。繧ス繝�ラ縺ッ菴ソ逕ィ縺励↑縺� |
---|
56 | * </p>
|
---|
57 | * @param args 菴ソ逕ィ縺励↑縺� |
---|
58 | * @throws Exception
|
---|
59 | * @since 28.01.2014
|
---|
60 | */
|
---|
61 | public static void main(String[] args) throws Exception {
|
---|
62 | String sparqlEndpoint = "http://dbpedia.org/sparql";
|
---|
63 | String keyword = "artiste";
|
---|
64 | String[] graphURIs = new String[0];
|
---|
65 |
|
---|
66 | OWLQueryBuilder builder = new OWLQueryBuilderImpl(sparqlEndpoint);
|
---|
67 | String[] clsURIs = builder.getOWLClasses(graphURIs, keyword);
|
---|
68 | for(String cls: clsURIs){
|
---|
69 | System.out.println(cls);
|
---|
70 | }
|
---|
71 |
|
---|
72 | System.out.println("CLS");
|
---|
73 | ClassLink[] cls = builder.getNextClass(null, clsURIs[0], 0);
|
---|
74 | if( cls != null ){
|
---|
75 | for( ClassLink cl: cls){
|
---|
76 | System.out.println(cl.toString());
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | System.out.println("CLS-INS");
|
---|
81 | cls = builder.getNextClassViaInstanceLink(null, clsURIs[0], 100);
|
---|
82 | if( cls != null ){
|
---|
83 | for( ClassLink cl: cls){
|
---|
84 | System.out.println(cl.toString());
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | System.out.println("Instances");
|
---|
89 | Instance[] ins = builder.getInstances(null, "\"A.C. Reed\"@en");
|
---|
90 | if( ins != null ){
|
---|
91 | for( Instance in: ins){
|
---|
92 | System.out.println(in.toString());
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | System.out.println("INS-INS");
|
---|
98 | ins = builder.getInstances(null, "\"A.C. Reed\"@en");
|
---|
99 | InstanceLink[] iLinks = builder.getNextInstancesViaInstanceLink(null, ins[0].getInstanceURI(), 100);
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | if( iLinks != null ){
|
---|
104 | for( InstanceLink in: iLinks){
|
---|
105 | System.out.println(in.toString());
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * 譏守、コ逧�↓RDF縺ァ譖ク縺九l縺ヲ縺�k繧ッ繝ゥ繧ケ繧貞叙蠕励☆繧� |
---|
115 | * <p>
|
---|
116 | * 謖�ョ壹&繧後◆graphURIs縺ョ荳ュ縺九i縲√く繝シ繝ッ繝シ繝峨↓繝偵ャ繝医☆繧九Λ繝吶Ν繧呈戟縺、繧ッ繝ゥ繧ケ��dfs:Class�峨r縺吶∋縺ヲ霑斐☆
|
---|
117 | * <br>
|
---|
118 | * </p>
|
---|
119 | *
|
---|
120 | * @param graphURIs縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� (null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
121 | * @param keyword ��ull繧�ゥコ譁�ュ励�荳榊庄�� |
---|
122 | * @return 繧ッ繝ゥ繧ケURI縺ョ驟榊�
|
---|
123 | * @throws Exception
|
---|
124 | * @since 28.01.2014
|
---|
125 | */
|
---|
126 | public String[] getOWLClasses(String[] graphURIs, String keyword)
|
---|
127 | throws Exception {
|
---|
128 | StringBuffer queryStr = new StringBuffer();
|
---|
129 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
130 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
131 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
132 | queryStr.append("SELECT DISTINCT ?c \n");
|
---|
133 | if (graphURIs != null) {
|
---|
134 | for (String graphURI : graphURIs) {
|
---|
135 | queryStr.append("FROM <");
|
---|
136 | queryStr.append(graphURI);
|
---|
137 | queryStr.append(">\n");
|
---|
138 | }
|
---|
139 | }
|
---|
140 | queryStr.append("WHERE{\n");
|
---|
141 |
|
---|
142 | queryStr.append(" {?c rdf:type rdfs:Class} UNION {?c rdf:type owl:Class}\n");
|
---|
143 |
|
---|
144 | queryStr.append(" ?c rdfs:label ?label.\n");
|
---|
145 |
|
---|
146 | // queryStr.append(" ?c rdfs:label ");
|
---|
147 | // queryStr.append(keyword);
|
---|
148 | // queryStr.append(".");
|
---|
149 |
|
---|
150 | queryStr.append(" FILTER (\n");
|
---|
151 | queryStr.append(" REGEX( ?label , \"");
|
---|
152 | queryStr.append(keyword);
|
---|
153 | queryStr.append("\" , \"i\" )\n");
|
---|
154 | queryStr.append(" )");
|
---|
155 | queryStr.append("\n}");
|
---|
156 |
|
---|
157 | System.out.println(queryStr.toString());
|
---|
158 |
|
---|
159 | Query query = QueryFactory.create(queryStr.toString());
|
---|
160 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
161 | query);
|
---|
162 |
|
---|
163 | ResultSet results = qexec.execSelect();
|
---|
164 | ArrayList<String> solStrings = new ArrayList<String>();
|
---|
165 | for (; results.hasNext();) {
|
---|
166 | QuerySolution sol = results.next();
|
---|
167 | Resource res = sol.getResource("c");
|
---|
168 | String uri = res.getURI();
|
---|
169 | solStrings.add(uri);
|
---|
170 | }
|
---|
171 | qexec.close();
|
---|
172 | return solStrings.toArray(new String[0]);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | /**
|
---|
177 | *縲繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧貞叙蠕励☆繧� |
---|
178 | * <p>
|
---|
179 | * 謖�ョ壹&繧後◆graphURIs縺ョ荳ュ縺九i縲√く繝シ繝ッ繝シ繝峨↓繝偵ャ繝医☆繧九Λ繝吶Ν繧呈戟縺、繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧偵☆縺ケ縺ヲ霑斐☆
|
---|
180 | * <br>
|
---|
181 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺� |
---|
182 | * <br>
|
---|
183 | * </p>
|
---|
184 | *
|
---|
185 | * @param graphURIs縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� (null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
186 | * @param keyword ��ull繧�ゥコ譁�ュ励�荳榊庄�� |
---|
187 | * @return 繧ッ繝ゥ繧ケURI縺ョ驟榊�
|
---|
188 | * @throws Exception
|
---|
189 | * @since 28.01.2014
|
---|
190 | */
|
---|
191 | public Instance[] getInstances(String[] graphURIs, String keyword)
|
---|
192 | throws Exception {
|
---|
193 | StringBuffer queryStr = new StringBuffer();
|
---|
194 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
195 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
196 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
197 | queryStr.append("SELECT DISTINCT ?ins ?c \n");
|
---|
198 | if (graphURIs != null) {
|
---|
199 | for (String graphURI : graphURIs) {
|
---|
200 | queryStr.append("FROM <");
|
---|
201 | queryStr.append(graphURI);
|
---|
202 | queryStr.append(">\n");
|
---|
203 | }
|
---|
204 | }
|
---|
205 | queryStr.append("WHERE{\n");
|
---|
206 | // queryStr.append(" {?c rdf:type rdfs:Class} UNION {?c rdf:type owl:Class}\n");
|
---|
207 |
|
---|
208 |
|
---|
209 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
210 | queryStr.append(" ?ins rdfs:label ");
|
---|
211 | queryStr.append(keyword);
|
---|
212 | queryStr.append(".\n");
|
---|
213 | queryStr.append(" FILTER (?c != rdf:Property)");
|
---|
214 | queryStr.append("}");
|
---|
215 |
|
---|
216 | // System.out.println(queryStr.toString());
|
---|
217 |
|
---|
218 | Query query = QueryFactory.create(queryStr.toString());
|
---|
219 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
220 | query);
|
---|
221 |
|
---|
222 | ResultSet results = qexec.execSelect();
|
---|
223 | HashMap<String, HashSet<String>> instanceMap = new HashMap<String, HashSet<String>>();
|
---|
224 | for (; results.hasNext();) {
|
---|
225 | QuerySolution sol = results.next();
|
---|
226 | Resource cls = sol.getResource("c");
|
---|
227 | Resource ins = sol.getResource("ins");
|
---|
228 | String clsURI = cls.getURI();
|
---|
229 | String insURI = ins.getURI();
|
---|
230 | if( instanceMap.containsKey(insURI)){
|
---|
231 | HashSet<String> classes = instanceMap.get(insURI);
|
---|
232 | classes.add(clsURI);
|
---|
233 | }else{
|
---|
234 | HashSet<String> classes = new HashSet<String>();
|
---|
235 | instanceMap.put(insURI, classes);
|
---|
236 | classes.add(clsURI);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | qexec.close();
|
---|
240 | Set<String> keySet = instanceMap.keySet();
|
---|
241 | ArrayList<Instance> instanceList = new ArrayList<Instance>();
|
---|
242 | for(String key: keySet){
|
---|
243 | Instance ins = new Instance(key, instanceMap.get(key).toArray(new String[0]));
|
---|
244 | instanceList.add(ins);
|
---|
245 | }
|
---|
246 | return instanceList.toArray(new Instance[0]);
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | *縲謖�ョ壹&繧後◆繧ッ繝ゥ繧ケ繧定オキ轤ケ縺ィ縺励∵�遉コ逧�↓險倩ソー縺輔l縺ヲ縺�kOWL縺ョproperty蛻カ邏�r隱ソ縺ケ縲√◎縺ョproperty蛻カ邏�〒
|
---|
252 | * 髢「騾」縺・縺代i繧後※縺�k繧ッ繝ゥ繧ケ繧堤カイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
253 | * <p>
|
---|
254 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k
|
---|
255 | * <br>
|
---|
256 | * <br>
|
---|
257 | * </p>
|
---|
258 | *
|
---|
259 | * @param graphURIs縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
260 | * @param originClass 襍キ轤ケ縺ィ縺ェ繧九け繝ゥ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
261 | * @param limit 隗」縺ィ縺励※霑斐&繧後kClassLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
262 | * @return ClassLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
263 | * @throws Exception
|
---|
264 | * @since 28.01.2014
|
---|
265 | */
|
---|
266 | public ClassLink[] getNextClass(String[] graphURIs, String originClass,
|
---|
267 | int limit) throws Exception {
|
---|
268 | StringBuffer queryStr = new StringBuffer();
|
---|
269 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
270 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
271 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
272 | queryStr.append("SELECT DISTINCT ?p ?pDirection ?c (COUNT(?s) AS ?numOfLinks) \n");
|
---|
273 | if (graphURIs != null) {
|
---|
274 | for (String graphURI : graphURIs) {
|
---|
275 | queryStr.append("FROM <");
|
---|
276 | queryStr.append(graphURI);
|
---|
277 | queryStr.append(">\n");
|
---|
278 | }
|
---|
279 | }
|
---|
280 | queryStr.append("WHERE{\n");
|
---|
281 | queryStr.append(" { ?c rdf:type rdfs:Class. }\n UNION\n { ?c rdf:type owl:Class. }\n");
|
---|
282 | queryStr.append(" { ?p rdfs:domain <");
|
---|
283 | queryStr.append(originClass);
|
---|
284 | queryStr.append(">.\n ?p rdfs:range ?c.\n ?o ?p ?s.}\n UNION\n");
|
---|
285 | queryStr.append(" { ?p rdfs:range <");
|
---|
286 | queryStr.append(originClass);
|
---|
287 | queryStr.append(">.\n");
|
---|
288 | queryStr.append(" ?p rdfs:domain ?c.\n");
|
---|
289 | queryStr.append(" ?s ?p ?o.}\n");
|
---|
290 |
|
---|
291 | queryStr.append(" ?p ?pDirection ?c.\n");
|
---|
292 |
|
---|
293 |
|
---|
294 | queryStr.append(" ?s rdf:type ?c.\n");
|
---|
295 | queryStr.append(" ?o rdf:type <");
|
---|
296 | queryStr.append(originClass);
|
---|
297 | queryStr.append(">.\n");
|
---|
298 | queryStr.append("}\nGROUP BY ?p ?pDirection ?c\n");
|
---|
299 | if (limit > 0) {
|
---|
300 | queryStr.append("limit ");
|
---|
301 | queryStr.append(limit);
|
---|
302 | queryStr.append("\n");
|
---|
303 | }
|
---|
304 |
|
---|
305 | System.out.println(queryStr.toString());
|
---|
306 |
|
---|
307 | Query query = QueryFactory.create(queryStr.toString());
|
---|
308 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
309 | query);
|
---|
310 |
|
---|
311 | ResultSet results = qexec.execSelect();
|
---|
312 | ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
|
---|
313 | for (; results.hasNext();) {
|
---|
314 | QuerySolution sol = results.next();
|
---|
315 | Resource pro = sol.getResource("p");
|
---|
316 | Resource cls = sol.getResource("c");
|
---|
317 | Resource dir = sol.getResource("pDirection");
|
---|
318 | String proURI = pro.getURI();
|
---|
319 | String clsURI = cls.getURI();
|
---|
320 | String dirURI = dir.getURI();
|
---|
321 |
|
---|
322 | // System.out.println(pro.getURI() + " " + cls.getURI() + " " + dir.getURI());
|
---|
323 |
|
---|
324 |
|
---|
325 | Direction direction = null;
|
---|
326 | if( dirURI.equals("http://www.w3.org/2000/01/rdf-schema#domain") || dirURI.equals("rdfs:domain")){
|
---|
327 | direction = Direction.reverse;
|
---|
328 | }
|
---|
329 | if( dirURI.equals("http://www.w3.org/2000/01/rdf-schema#range") || dirURI.equals("rdfs:range")){
|
---|
330 | if( direction != null ){
|
---|
331 | direction = Direction.both;
|
---|
332 | }else{
|
---|
333 | direction = Direction.forward;
|
---|
334 | }
|
---|
335 | }
|
---|
336 | // System.out.println(direction);
|
---|
337 | int numOfLinks = sol.getLiteral("numOfLinks").getInt();
|
---|
338 | // System.out.println(numOfLinks);
|
---|
339 | ClassLink cl = new ClassLink(proURI, clsURI, direction, numOfLinks);
|
---|
340 | solCLs.add(cl);
|
---|
341 | }
|
---|
342 | qexec.close();
|
---|
343 | return solCLs.toArray(new ClassLink[0]);
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | /**
|
---|
348 | *縲謖�ョ壹&繧後◆繧ッ繝ゥ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧ッ繝ゥ繧ケ縺ォ螻槭@縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励@縲∝叙蠕励@縺溘う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧堤カイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
349 | * <p>
|
---|
350 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺� |
---|
351 | * <br>
|
---|
352 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k
|
---|
353 | * <br>
|
---|
354 | * </p>
|
---|
355 | *
|
---|
356 | * @param graphURIs縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
357 | * @param originClass 襍キ轤ケ縺ィ縺ェ繧九け繝ゥ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
358 | * @param limit 隗」縺ィ縺励※霑斐&繧後kClassLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
359 | * @return ClassLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
360 | * @throws Exception
|
---|
361 | * @since 28.01.2014
|
---|
362 | */
|
---|
363 | public ClassLink[] getNextClassViaInstanceLink(String[] graphURIs, String originClass,
|
---|
364 | int limit) throws Exception {
|
---|
365 | StringBuffer queryStr = new StringBuffer();
|
---|
366 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
367 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
368 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
369 | queryStr.append("SELECT DISTINCT ?pf ?pr ?c \n");
|
---|
370 | if (graphURIs != null) {
|
---|
371 | for (String graphURI : graphURIs) {
|
---|
372 | queryStr.append("FROM <");
|
---|
373 | queryStr.append(graphURI);
|
---|
374 | queryStr.append(">\n");
|
---|
375 | }
|
---|
376 | }
|
---|
377 | queryStr.append("WHERE{\n");
|
---|
378 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
379 | queryStr.append(" ?insOrg rdf:type <");
|
---|
380 | queryStr.append(originClass);
|
---|
381 | queryStr.append(">.\n");
|
---|
382 | queryStr.append(" { ?ins ?pr ?insOrg. }\n UNION { ?insOrg ?pf ?ins. }\n");
|
---|
383 | queryStr.append("}\n");
|
---|
384 | if (limit > 0) {
|
---|
385 | queryStr.append("limit ");
|
---|
386 | queryStr.append(limit);
|
---|
387 | queryStr.append("\n");
|
---|
388 | }
|
---|
389 |
|
---|
390 | // System.out.println(queryStr.toString());
|
---|
391 |
|
---|
392 | Query query = QueryFactory.create(queryStr.toString());
|
---|
393 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
394 | query);
|
---|
395 |
|
---|
396 | ResultSet results = qexec.execSelect();
|
---|
397 | ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
|
---|
398 | for (; results.hasNext();) {
|
---|
399 | QuerySolution sol = results.next();
|
---|
400 | Resource proForward = sol.getResource("pf");
|
---|
401 | Resource proReverse = sol.getResource("pr");
|
---|
402 | Resource cls = sol.getResource("c");
|
---|
403 | Direction direction = null;
|
---|
404 | String propURI = null;
|
---|
405 | if( proForward != null ){
|
---|
406 | if( proReverse != null ){
|
---|
407 | direction = Direction.both;
|
---|
408 | }else{
|
---|
409 | direction = Direction.forward;
|
---|
410 | }
|
---|
411 | propURI = proForward.getURI();
|
---|
412 | }else{
|
---|
413 | direction = Direction.reverse;
|
---|
414 | propURI = proReverse.getURI();
|
---|
415 | }
|
---|
416 | String clsURI = cls.getURI();
|
---|
417 | // System.out.println(propURI + " " + clsURI + " " + direction);
|
---|
418 |
|
---|
419 | ClassLink cl = new ClassLink(propURI, clsURI, direction, 0);
|
---|
420 | solCLs.add(cl);
|
---|
421 | }
|
---|
422 | qexec.close();
|
---|
423 | return solCLs.toArray(new ClassLink[0]);
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 |
|
---|
428 | public Path[] getPaths(String startClass, String endClass) throws Exception {
|
---|
429 | OWLClassGraph graph = new OWLClassGraph(startClass, endClass);
|
---|
430 | return graph.getPaths(this);
|
---|
431 | }
|
---|
432 |
|
---|
433 | public String createSPARQL(Path path) throws Exception {
|
---|
434 | return null;
|
---|
435 | }
|
---|
436 |
|
---|
437 |
|
---|
438 |
|
---|
439 |
|
---|
440 | /*
|
---|
441 | private String executeSelect(String sparqlQuery) throws Exception {
|
---|
442 | HttpClient client = HttpClientBuilder.create().build();
|
---|
443 | HttpPost httppost = new HttpPost(endpointURI);
|
---|
444 | httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
|
---|
445 | httppost.setHeader("Accept", "application/sparql-results+xml");
|
---|
446 | List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
|
---|
447 | nvpList.add(new BasicNameValuePair("query", sparqlQuery));
|
---|
448 | // nvpList.add(new BasicNameValuePair("format",
|
---|
449 | // outputFormat.getMime()));
|
---|
450 | httppost.setEntity(new UrlEncodedFormEntity(nvpList, Charset
|
---|
451 | .forName("UTF-8")));
|
---|
452 | HttpResponse response = client.execute(httppost);
|
---|
453 | // System.out.println("[StatusLine] " + response.getStatusLine());
|
---|
454 | HttpEntity entity = response.getEntity();
|
---|
455 | String entityString = EntityUtils.toString(entity, "UTF-8");
|
---|
456 | return entityString;
|
---|
457 | }
|
---|
458 | */
|
---|
459 |
|
---|
460 | /**
|
---|
461 | *縲謖�ョ壹&繧後◆繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧定オキ轤ケ縺ィ縺励√◎縺ョ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ォ繝ェ繝ウ繧ッ縺悟シオ繧峨l縺ヲ縺�k繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ髮�粋繧貞叙蠕励☆繧九� |
---|
462 | *縲蜿門セ励&繧後◆蜷�う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョ繧ッ繝ゥ繧ケ繧らカイ鄒�噪縺ォ蜿門セ励☆繧� |
---|
463 | * <p>
|
---|
464 | * 縺薙%縺ァ繧、繝ウ繧ケ繧ソ繝ウ繧ケ縺ィ縺ッ縲〉df:type縺ョ荳サ隱槭→縺励※險倩ソー縺輔l縺ヲ縺�k繧ゅ�繧偵>縺� |
---|
465 | * <br>
|
---|
466 | * 蜃ヲ逅�ッセ雎。繝��繧ソ繧暖raphURIs縺ァ謖�ョ壹☆繧九%縺ィ縺後〒縺阪k
|
---|
467 | * <br>
|
---|
468 | * </p>
|
---|
469 | *
|
---|
470 | * @param graphURIs縲讀懃エ「蟇セ雎。縺ョgraph縺ョURI驟榊� 縲(null繧�聞縺�縺ョ驟榊�繧ょ庄)
|
---|
471 | * @param originInstance 襍キ轤ケ縺ィ縺ェ繧九う繝ウ繧ケ繧ソ繝ウ繧ケ縺ョURI縲��ull荳榊庄�� |
---|
472 | * @param limit 隗」縺ィ縺励※霑斐&繧後kInstanceLink驟榊�縺ョ髟キ縺輔�荳企剞蛟、繧呈欠螳壹☆繧九�荳企剞蛟、繧定ィュ螳壹@縺ェ縺�エ蜷医�0莉・荳九�蛟、繧呈欠螳壹☆繧�
|
---|
473 | * @return InstanceLink縺ョ驟榊�縲ゅ%縺薙↓縺ッ縲∝叙蠕励&繧後◆繧ッ繝ゥ繧ケ縺ィ髢「菫ゅ▼縺代i繧後※縺�k繝励Ο繝代ユ繧」縲�未菫ゅ�蜷代″縺悟性縺セ繧後k縲� |
---|
474 | * @throws Exception
|
---|
475 | * @since 28.01.2014
|
---|
476 | */
|
---|
477 | public InstanceLink[] getNextInstancesViaInstanceLink(String[] graphURIs, String originInstance,
|
---|
478 | int limit) throws Exception {
|
---|
479 | StringBuffer queryStr = new StringBuffer();
|
---|
480 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
481 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
482 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
483 | queryStr.append("SELECT DISTINCT ?pf ?pr ?ins ?c \n");
|
---|
484 | if (graphURIs != null) {
|
---|
485 | for (String graphURI : graphURIs) {
|
---|
486 | queryStr.append("FROM <");
|
---|
487 | queryStr.append(graphURI);
|
---|
488 | queryStr.append(">\n");
|
---|
489 | }
|
---|
490 | }
|
---|
491 | queryStr.append("WHERE{\n");
|
---|
492 | // queryStr.append(" { ?c rdf:type rdfs:Class. }\n UNION\n { ?c rdf:type owl:Class. }\n");
|
---|
493 | queryStr.append(" ?ins rdf:type ?c.\n");
|
---|
494 | queryStr.append(" { ?ins ?pr <");
|
---|
495 | queryStr.append(originInstance);
|
---|
496 | queryStr.append(">. }\n UNION { <");
|
---|
497 | queryStr.append(originInstance);
|
---|
498 | queryStr.append("> ?pf ?ins. }\n");
|
---|
499 |
|
---|
500 | queryStr.append(" FILTER (?c != rdf:Property)");
|
---|
501 |
|
---|
502 | queryStr.append("}\n");
|
---|
503 | if (limit > 0) {
|
---|
504 | queryStr.append("limit ");
|
---|
505 | queryStr.append(limit);
|
---|
506 | queryStr.append("\n");
|
---|
507 | }
|
---|
508 |
|
---|
509 | // System.out.println(queryStr.toString());
|
---|
510 |
|
---|
511 | Query query = QueryFactory.create(queryStr.toString());
|
---|
512 | QueryExecution qexec = QueryExecutionFactory.sparqlService(endpointURI,
|
---|
513 | query);
|
---|
514 |
|
---|
515 | ResultSet results = qexec.execSelect();
|
---|
516 | HashMap<String, InstanceLink> insLinkMap = new HashMap<String, InstanceLink>();
|
---|
517 | for (; results.hasNext();) {
|
---|
518 | QuerySolution sol = results.next();
|
---|
519 | Resource proForward = sol.getResource("pf");
|
---|
520 | Resource proReverse = sol.getResource("pr");
|
---|
521 | Resource ins = sol.getResource("ins");
|
---|
522 | Resource cls = sol.getResource("c");
|
---|
523 | Direction direction = null;
|
---|
524 | String propURI = null;
|
---|
525 | if( proForward != null ){
|
---|
526 | if( proReverse != null ){
|
---|
527 | direction = Direction.both;
|
---|
528 | }else{
|
---|
529 | direction = Direction.forward;
|
---|
530 | }
|
---|
531 | propURI = proForward.getURI();
|
---|
532 | }else{
|
---|
533 | direction = Direction.reverse;
|
---|
534 | propURI = proReverse.getURI();
|
---|
535 | }
|
---|
536 | String clsURI = cls.getURI();
|
---|
537 | String insURI = ins.getURI();
|
---|
538 | String key = propURI + "\t" + insURI + "\t" + direction;
|
---|
539 | if( insLinkMap.containsKey(key)){
|
---|
540 | InstanceLink insLink = insLinkMap.get(key);
|
---|
541 | insLink.addLinkedClassURI(clsURI);
|
---|
542 | }else{
|
---|
543 | InstanceLink insLink = new InstanceLink(propURI, insURI, new String[]{clsURI}, direction);
|
---|
544 | insLinkMap.put(key, insLink);
|
---|
545 | }
|
---|
546 | }
|
---|
547 | qexec.close();
|
---|
548 | Collection<InstanceLink> values = insLinkMap.values();
|
---|
549 | return values.toArray(new InstanceLink[0]);
|
---|
550 | }
|
---|
551 |
|
---|
552 | }
|
---|