root/BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/OWLQueryBuilderImpl.java @ 2

リビジョン 2, 18.8 KB (コミッタ: nori, 11 年 前)

Initial

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