root/BH13SPARQLBuilder/src/org/biohackathon/SPARQLBuilder/OWL/AcquiredStructureAnalyzer.java @ 126

リビジョン 126, 15.1 KB (コミッタ: nori, 10 年 前)

Debug ?entitiesの名前間違い

  • 属性 svn:mime-type の設定値 text/plain
行番号 
1package org.biohackathon.SPARQLBuilder.OWL;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5
6import jp.riken.accc.db.rdf.crawler.dataStructure.sparql.JenaModelGenerator;
7import jp.riken.accc.db.rdf.crawler.dataStructure.sparql.URICollection;
8
9import com.hp.hpl.jena.query.Query;
10import com.hp.hpl.jena.query.QueryExecution;
11import com.hp.hpl.jena.query.QueryExecutionFactory;
12import com.hp.hpl.jena.query.QueryFactory;
13import com.hp.hpl.jena.query.QuerySolution;
14import com.hp.hpl.jena.query.ResultSet;
15import com.hp.hpl.jena.rdf.model.Literal;
16import com.hp.hpl.jena.rdf.model.Model;
17import com.hp.hpl.jena.rdf.model.Property;
18import com.hp.hpl.jena.rdf.model.Resource;
19
20//public class OWLQueryBuilderForCrawlerImpl implements OWLQueryBuilder {
21public class AcquiredStructureAnalyzer implements RDFSchemaAnalyzer {
22
23        private Model model = null;
24        private String endpointURI = null;
25        private String[] graphURIs = null;
26
27        public String getEndpointURI(){
28                return endpointURI;
29        }
30
31        public String[] getGraphURIs(){
32                return graphURIs;
33        }
34       
35       
36        public static void main(String[] args) throws Exception{
37                JenaModelGenerator jmGene = new JenaModelGenerator("c:\\temp\\reactomeF18s.ttl");
38                AcquiredStructureAnalyzer impl
39                        = new AcquiredStructureAnalyzer(jmGene.getEndpointURI(), jmGene.getGraphURIs(), jmGene.getModel());
40                System.out.println("--------------------------");
41                SClass[] scs = impl.getOWLClasses(null, null, null, true);
42                System.out.println("list classes:---------------");
43                for(SClass sc: scs){
44                        System.out.println(sc.toString());
45                }
46                System.out.println("--------------------------");
47               
48//              ClassLink[] cls = impl.getNextClass(null,"http://purl.org/allie/ontology/201108#ShortForm",100,true );
49//              for(ClassLink cl: cls){
50//                      System.out.println(cl.toString());
51//              }
52//              System.out.println("--------------------------");
53               
54        }
55       
56       
57        public AcquiredStructureAnalyzer(String endpointURI, String[] graphURIs, Model model){
58                this.model = model;
59                this.endpointURI = endpointURI;
60                this.graphURIs = graphURIs;
61        }
62
63        private String[] filterGraphURIs(String[] orgGraphURIs){
64                // TODO
65                return graphURIs;
66        }
67
68       
69        public SClass[] listClasses(String[] graphURIs, boolean countInstances) throws Exception{
70                return getOWLClasses(graphURIs, null, null, countInstances);
71        }
72               
73               
74
75       
76        public SClass[] getOWLClasses(String[] graphURIs, String[] keywords, String language, boolean countInstances) throws Exception{
77                String[] targetGraphURIs = filterGraphURIs(graphURIs);
78
79                StringBuffer queryStr = new StringBuffer();
80                queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
81                queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
82                queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
83                queryStr.append("SELECT DISTINCT ?c ?pLabel ?entities\n");
84//              if (targetGraphURIs != null) {
85//                      for (String graphURI : targetGraphURIs) {
86//                              queryStr.append("FROM <");
87//                              queryStr.append(graphURI);
88//                              queryStr.append(">\n");
89//                      }
90//              }
91                queryStr.append("WHERE{\n");
92                queryStr.append(" ?cp <").append(URICollection.PROPERTY_VOID_CLASS).append("> ?c. \n");
93                queryStr.append(" ?cp <");
94                queryStr.append(URICollection.PROPERTY_VOID_ENTITIES);
95                queryStr.append("> ?entities. \n");
96                queryStr.append(" OPTIONAL{ ?c <");
97                queryStr.append(URICollection.PROPERTY_RDFS_LABEL);
98                queryStr.append("> ?pLabel. }\n");
99
100                if (keywords != null && keywords.length != 0) {
101
102                        queryStr.append(" ?c rdfs:label ");
103                        queryStr.append("?keywords").append(".\n");
104                        queryStr.append("  filter((LANG(?keywords) = \'").append(language);
105                        queryStr.append("\') && \n (");
106
107                        for (int i = 0; i < keywords.length; i++) {
108                                if (i > 0)
109                                        queryStr.append(" || \n ");
110
111                                queryStr.append("regex(str(").append("?keywords")
112                                                .append("),\"");
113                                queryStr.append(keywords[i]);
114                                queryStr.append("\", \"i\" )");
115
116                        }
117                        queryStr.append("))\n");
118
119                }
120                queryStr.append("}");
121                System.out.println(queryStr.toString());
122
123                Query query = QueryFactory.create(queryStr.toString());
124
125                QueryExecution qexec = null;
126                ResultSet results = null;
127                try {
128//                      long start = System.currentTimeMillis();
129                        qexec = QueryExecutionFactory.create(query, model);
130                        results = qexec.execSelect();
131//                      long end = System.currentTimeMillis();
132//                      System.out.println("EXEC TIME: " + (end - start));
133                } catch (Exception ex) {
134                        ex.printStackTrace();
135                        throw ex;
136                }
137
138                HashMap<String, SClass> classMap = new HashMap<String, SClass>();
139                for (; results.hasNext();) {
140                        QuerySolution sol = results.next();
141                        Resource res = sol.getResource("c");
142                        if (res != null && res.getURI() != null) {
143                                String uri = res.getURI();
144                                int numOfInstances = 0;
145                                if (countInstances) {
146                                        numOfInstances = sol.getLiteral("entities").getInt();
147                                } //
148                                Literal labelLiteral = sol.getLiteral("pLabel");
149                                SClass sClass = null;
150                                if (classMap.containsKey(uri)) {
151                                        sClass = classMap.get(uri);
152                                } else {
153                                        sClass = new SClass(uri, null, numOfInstances);
154                                        classMap.put(uri, sClass);
155                                }
156                                if (labelLiteral != null) {
157                                        String label = labelLiteral.getString();
158                                        String lang = labelLiteral.getLanguage();
159                                        sClass.addLabel(new Label(label, lang));
160                                }
161                        }
162                }
163                qexec.close();
164                return classMap.values().toArray(new SClass[0]);
165        }
166
167/*
168       
169        public Instance[] getInstances(String[] graphURIs, String keyword) throws Exception;
170*/
171
172
173        public ClassLink[] getNextClass(String[] graphURIs, String originClass, int limit, boolean countLinks) throws Exception{
174                String[] targetGraphURIs = filterGraphURIs(graphURIs);
175
176                StringBuffer queryStr = new StringBuffer();
177                queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
178                queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
179                queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
180
181                // SELECT
182                queryStr.append("SELECT DISTINCT ?indPropCat ?c ?dat ?d ?p ?numLnkInsStart ?numLnkInsEnd ?numInsDom ?numInsRan ?numTriples ?isStartClsLim ?isEndClsLim\n");
183
184//              if (targetGraphURIs != null) {
185//                      for (String graphURI : targetGraphURIs) {
186//                              queryStr.append("FROM <");
187//                              queryStr.append(graphURI);
188//                              queryStr.append(">\n");
189//                      }
190//              }
191
192                queryStr.append("WHERE{\n");
193                queryStr.append(" ?cr rdf:type <http://sparqlbuilder.org/ClassRelation>. \n");
194//              queryStr.append(" <" + originClass + "> <http://sparqlbuilder.org/numberOfInstances> ?numInsStart. \n");
195                queryStr.append(" {");
196                queryStr.append(" ?cr <http://sparqlbuilder.org/startClass> <" + originClass + ">. \n");
197                queryStr.append(" ?cr <http://sparqlbuilder.org/endClass> ?c. \n");
198                queryStr.append(" ?cr <http://sparqlbuilder.org/property> ?p. \n");
199                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfTriples> ?numTriples. \n");
200                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfInstancesOfStartClass> ?numLnkInsStart. \n");
201                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfInstancesOfEndClass> ?numLnkInsEnd. \n");
202                queryStr.append(" ?cr <http://sparqlbuilder.org/startClassLimitedQ> ?isStartClsLim. \n");
203                queryStr.append(" ?cr <http://sparqlbuilder.org/endClassLimitedQ> ?isEndClsLim. \n");
204                queryStr.append("}\n");
205                queryStr.append(" UNION\n");
206                queryStr.append(" {");
207                queryStr.append(" ?cr <http://sparqlbuilder.org/startClass> <" + originClass + ">. \n");
208                queryStr.append(" ?cr <http://sparqlbuilder.org/endDatatype> ?dat. \n");
209                queryStr.append(" ?cr <http://sparqlbuilder.org/property> ?p. \n");
210                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfTriples> ?numTriples. \n");
211                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfInstancesOfStartClass> ?numLnkInsStart. \n");
212                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfInstancesOfEndClass> ?numLnkInsEnd. \n");
213                queryStr.append(" ?cr <http://sparqlbuilder.org/startClassLimitedQ> ?isStartClsLim. \n");
214                queryStr.append(" ?cr <http://sparqlbuilder.org/endClassLimitedQ> ?isEndClsLim. \n");
215                queryStr.append("}\n");
216                queryStr.append(" UNION\n");
217                queryStr.append(" {");
218                queryStr.append(" ?cr <http://sparqlbuilder.org/endClass> <" + originClass + ">. \n");
219                queryStr.append(" ?cr <http://sparqlbuilder.org/startClass> ?d. \n");
220                queryStr.append(" ?cr <http://sparqlbuilder.org/property> ?p. \n");
221                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfTriples> ?numTriples.\n");
222                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfInstancesOfStartClass> ?numLnkInsEnd. \n");
223                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfInstancesOfEndClass> ?numLnkInsStart. \n");
224                queryStr.append(" ?cr <http://sparqlbuilder.org/startClassLimitedQ> ?isEndClsLim. \n");
225                queryStr.append(" ?cr <http://sparqlbuilder.org/endClassLimitedQ> ?isStartClsLim. \n");
226                queryStr.append("}\n");
227               
228                queryStr.append(" ?pp rdf:type <http://sparqlbuilder.org/PropertyProfile>. \n");
229                queryStr.append(" ?pp <http://sparqlbuilder.org/property> ?p. \n");
230                queryStr.append(" ?pp <http://sparqlbuilder.org/individualPropertyCategory> ?indPropCat. \n");
231                queryStr.append(" ?pp <http://sparqlbuilder.org/numberOfInstancesOfDomainClass> ?numInsDom. \n");
232                queryStr.append(" ?pp <http://sparqlbuilder.org/numberOfInstancesOfRangeClass> ?numInsRan \n");
233       
234                queryStr.append("}\n");
235               
236       
237                if (limit > 0) {
238                        queryStr.append("limit ");
239                        queryStr.append(limit);
240                        queryStr.append("\n");
241                }
242
243//              System.out.println("getNextClasses SPARQL Query: ");
244//              System.out.println(queryStr.toString());
245
246                Query query = QueryFactory.create(queryStr.toString());
247                QueryExecution qexec = null;
248                ResultSet results = null;
249                try {
250                        long start = System.currentTimeMillis();
251                        qexec = QueryExecutionFactory.create(query, model);
252                        results = qexec.execSelect();
253                        long end = System.currentTimeMillis();
254                        System.out.println("EXEC TIME: " + (end - start));
255                } catch (Exception ex) {
256                        ex.printStackTrace();
257                        throw ex;
258                }
259
260                ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
261                for (; results.hasNext();) {
262                        QuerySolution sol = results.next();
263                        Resource pro = sol.getResource("p");
264                        String clsURI = null;
265                        String datURI = null;
266                        if (pro != null) {
267                                int indPropCat = 4;
268                                Literal indPropCatLit = sol.getLiteral("indPropCat");
269                                if( indPropCatLit != null ){
270                                        indPropCat = indPropCatLit.getInt();
271                                }
272                                if( indPropCat < 4 ) {
273                                String proURI = pro.getURI();
274                                Resource ccls = sol.getResource("c");
275                                Resource dcls = sol.getResource("d");
276                                Resource dat = sol.getResource("dat");
277                                Direction direction = null;
278                                if(ccls != null && dcls == null ){
279                                        // direction forward
280                                        direction = Direction.forward;
281                                        clsURI = ccls.getURI();
282                                }else{
283                                        if( ccls == null && dcls != null ){
284                                                direction = Direction.reverse;
285                                                clsURI = dcls.getURI();
286                                        }else{
287                                                if( ccls == null && dat != null && dcls == null ){
288                                                        clsURI = null;
289                                                        direction = Direction.forward;
290                                                        datURI = dat.getURI();
291                                                }
292                                        }
293                                }
294                               
295                               
296                                int numTriples = 0;
297                                Literal numTriplesLit = sol.getLiteral("numTriples");
298                                if( numTriplesLit != null ){
299                                        numTriples = numTriplesLit.getInt();
300                                }
301
302                                int numLnkInsStart = 0;
303                                Literal numInsStartLit = sol.getLiteral("numLnkInsStart");
304                                if( numInsStartLit != null ){
305                                        numLnkInsStart = numInsStartLit.getInt();
306                                }
307                                int numLnkInsEnd = 0;
308                                Literal numInsEndLit = sol.getLiteral("numLnkInsEnd");
309                                if( numInsEndLit != null ){
310                                        numLnkInsEnd = numInsEndLit.getInt();
311                                }
312
313                                int numInsDom = 0;
314                                Literal numInsDomLit = sol.getLiteral("numInsDom");
315                                if( numInsDomLit != null ){
316                                        numInsDom = numInsDomLit.getInt();
317                                }
318                                int numInsRan = 0;
319                                Literal numInsRanLit = sol.getLiteral("numInsRan");
320                                if( numInsRanLit != null ){
321                                        numInsRan = numInsRanLit.getInt();
322                                }
323
324                                boolean isStartClsLim = false;
325                                Literal isStartClsLimLit = sol.getLiteral("isStartClsLim");
326                                if( isStartClsLimLit != null ){
327                                        isStartClsLim = isStartClsLimLit.getBoolean();
328                                }
329                                boolean isEndClsLim = false;
330                                Literal isEndClsLimLit = sol.getLiteral("isEndClsLim");
331                                if( isEndClsLimLit != null ){
332                                        isEndClsLim = isEndClsLimLit.getBoolean();
333                                }
334                               
335                                ClassLink cl = new ClassLink(proURI, clsURI, datURI, direction,
336                                                numTriples, numInsDom, numInsRan,  numLnkInsStart, numLnkInsEnd, isStartClsLim, isEndClsLim);
337                                solCLs.add(cl);
338
339                                }
340                        }
341                }
342                qexec.close(); 
343                return solCLs.toArray(new ClassLink[0]);
344        }
345
346       
347       
348        /*
349
350        public ClassLink[] getNextClassViaInstanceLink(String[] graphURIs, String originClass, int limit) throws Exception;
351
352        public Path[] getPaths(String startClass, String endClass, int mode, boolean countLinks) throws Exception;
353
354        public String createSPARQL(Path path) throws Exception;
355
356 InstanceLink[] getNextInstancesViaInstanceLink(String[] graphURIs, String originInstance,
357                        int limit) throws Exception;
358*/
359
360        public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs,
361                        String language) throws Exception {
362                if (resourceURIs == null || resourceURIs.length == 0) {
363                        return new LabelMap[0];
364                }
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 ?res ?label \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("  ?res rdfs:label ?label.\n");
379                queryStr.append("  FILTER(?res IN (");
380                boolean f = false;
381                for (String resourceURI : resourceURIs) {
382                        if (f) {
383                                queryStr.append(", ");
384                        }
385                        f = true;
386                        queryStr.append("<");
387                        queryStr.append(resourceURI);
388                        queryStr.append(">");
389                }
390                queryStr.append("))\n");
391                queryStr.append("}");
392
393//              System.out.println(queryStr.toString());
394
395                Query query = QueryFactory.create(queryStr.toString());
396                QueryExecution qexec = QueryExecutionFactory.create(query, model);
397               
398                ResultSet results = qexec.execSelect();
399                HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>();
400                for (; results.hasNext();) {
401                        QuerySolution sol = results.next();
402                        String uri = sol.getResource("res").getURI();
403                        Literal literal = sol.getLiteral("label");
404                        if (literal != null) {
405                                String label = literal.getString();
406                                String lang = literal.getLanguage();
407                                if (language != null && language.equals(lang)) {
408                                        Label lbl = new Label(label, lang);
409                                        if (lMap.containsKey(uri)) {
410                                                LabelMap lm = lMap.get(uri);
411                                                lm.addLabel(lbl);
412                                        } else {
413                                                LabelMap lm = new LabelMap(uri, new Label[] { lbl });
414                                                lMap.put(uri, lm);
415                                        }
416                                }
417                        }
418                }
419                return lMap.values().toArray(new LabelMap[0]);
420        }
421
422/*
423        public ClassLink[] countLinks(String[] graphURIs, String startClassURI, ClassLink[] classLinks) throws Exception;
424        public SClass[] countInstances(String[] graphURIs, SClass[] classes) throws Exception;
425       
426*/
427
428}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。