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