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

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

support Literals

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