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

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

新しいクローラデータに対応した構造アナライザー

  • 属性 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:\\temp\\dberdf.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#EachPair",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 ?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/endClass> <" + originClass + ">. \n");
203                queryStr.append(" ?cr <http://sparqlbuilder.org/startClass> ?d. \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> ?numLnkInsEnd. \n");
207                queryStr.append(" ?cr <http://sparqlbuilder.org/numberOfInstancesOfEndClass> ?numLnkInsStart. \n");
208                queryStr.append(" ?cr <http://sparqlbuilder.org/startClassLimitedQ> ?isEndClsLim. \n");
209                queryStr.append(" ?cr <http://sparqlbuilder.org/endClassLimitedQ> ?isStartClsLim. \n");
210                queryStr.append("}\n");
211               
212                queryStr.append(" ?pp rdf:type <http://sparqlbuilder.org/PropertyProfile>. \n");
213                queryStr.append(" ?pp <http://sparqlbuilder.org/property> ?p. \n");
214                queryStr.append(" ?pp <http://sparqlbuilder.org/individualPropertyCategory> ?indPropCat. \n");
215                queryStr.append(" ?pp <http://sparqlbuilder.org/numberOfInstancesOfDomainClass> ?numInsDom. \n");
216                queryStr.append(" ?pp <http://sparqlbuilder.org/numberOfInstancesOfRangeClass> ?numInsRan \n");
217       
218
219                queryStr.append("}\n");
220               
221       
222                if (limit > 0) {
223                        queryStr.append("limit ");
224                        queryStr.append(limit);
225                        queryStr.append("\n");
226                }
227
228//              System.out.println("getNextClasses SPARQL Query: ");
229                System.out.println(queryStr.toString());
230
231                Query query = QueryFactory.create(queryStr.toString());
232                QueryExecution qexec = null;
233                ResultSet results = null;
234                try {
235                        long start = System.currentTimeMillis();
236                        qexec = QueryExecutionFactory.create(query, model);
237                        results = qexec.execSelect();
238                        long end = System.currentTimeMillis();
239                        System.out.println("EXEC TIME: " + (end - start));
240                } catch (Exception ex) {
241                        ex.printStackTrace();
242                        throw ex;
243                }
244
245                ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
246                for (; results.hasNext();) {
247                        QuerySolution sol = results.next();
248                        Resource pro = sol.getResource("p");
249                        String clsURI = null;
250                        if (pro != null) {
251                                int indPropCat = 4;
252                                Literal indPropCatLit = sol.getLiteral("indPropCat");
253                                if( indPropCatLit != null ){
254                                        indPropCat = indPropCatLit.getInt();
255                                }
256                                if( indPropCat < 4 ) {
257                                String proURI = pro.getURI();
258                                Resource ccls = sol.getResource("c");
259                                Resource dcls = sol.getResource("d");
260                                Direction direction = null;
261                                if(ccls != null && dcls == null ){
262                                        // direction forward
263                                        direction = Direction.forward;
264                                        clsURI = ccls.getURI();
265                                }else{
266                                        if( ccls == null && dcls != null ){
267                                                direction = Direction.reverse;
268                                                clsURI = dcls.getURI();
269                                        }
270                                }
271                               
272                               
273                                int numTriples = 0;
274                                Literal numTriplesLit = sol.getLiteral("numTriples");
275                                if( numTriplesLit != null ){
276                                        numTriples = numTriplesLit.getInt();
277                                }
278
279                                int numLnkInsStart = 0;
280                                Literal numInsStartLit = sol.getLiteral("numLnkInsStart");
281                                if( numInsStartLit != null ){
282                                        numLnkInsStart = numInsStartLit.getInt();
283                                }
284                                int numLnkInsEnd = 0;
285                                Literal numInsEndLit = sol.getLiteral("numLnkInsEnd");
286                                if( numInsEndLit != null ){
287                                        numLnkInsEnd = numInsEndLit.getInt();
288                                }
289
290                                int numInsDom = 0;
291                                Literal numInsDomLit = sol.getLiteral("numInsDom");
292                                if( numInsDomLit != null ){
293                                        numInsDom = numInsDomLit.getInt();
294                                }
295                                int numInsRan = 0;
296                                Literal numInsRanLit = sol.getLiteral("numInsRan");
297                                if( numInsRanLit != null ){
298                                        numInsRan = numInsRanLit.getInt();
299                                }
300
301                                boolean isStartClsLim = false;
302                                Literal isStartClsLimLit = sol.getLiteral("isStartClsLim");
303                                if( isStartClsLimLit != null ){
304                                        isStartClsLim = isStartClsLimLit.getBoolean();
305                                }
306                                boolean isEndClsLim = false;
307                                Literal isEndClsLimLit = sol.getLiteral("isEndClsLim");
308                                if( isEndClsLimLit != null ){
309                                        isEndClsLim = isEndClsLimLit.getBoolean();
310                                }
311                               
312                                ClassLink cl = new ClassLink(proURI, clsURI, direction,
313                                                numTriples, numInsDom, numInsRan,  numLnkInsStart, numLnkInsEnd, isStartClsLim, isEndClsLim);
314                                solCLs.add(cl);
315
316                                }
317                        }
318                }
319                qexec.close(); 
320                return solCLs.toArray(new ClassLink[0]);
321        }
322
323       
324       
325        /*
326
327        public ClassLink[] getNextClassViaInstanceLink(String[] graphURIs, String originClass, int limit) throws Exception;
328
329        public Path[] getPaths(String startClass, String endClass, int mode, boolean countLinks) throws Exception;
330
331        public String createSPARQL(Path path) throws Exception;
332
333 InstanceLink[] getNextInstancesViaInstanceLink(String[] graphURIs, String originInstance,
334                        int limit) throws Exception;
335*/
336
337        public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs,
338                        String language) throws Exception {
339                if (resourceURIs == null || resourceURIs.length == 0) {
340                        return new LabelMap[0];
341                }
342                StringBuffer queryStr = new StringBuffer();
343                queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
344                queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
345                queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
346                queryStr.append("SELECT DISTINCT ?res ?label \n");
347                if (graphURIs != null) {
348                        for (String graphURI : graphURIs) {
349                                queryStr.append("FROM <");
350                                queryStr.append(graphURI);
351                                queryStr.append(">\n");
352                        }
353                }
354                queryStr.append("WHERE{\n");
355                queryStr.append("  ?res rdfs:label ?label.\n");
356                queryStr.append("  FILTER(?res IN (");
357                boolean f = false;
358                for (String resourceURI : resourceURIs) {
359                        if (f) {
360                                queryStr.append(", ");
361                        }
362                        f = true;
363                        queryStr.append("<");
364                        queryStr.append(resourceURI);
365                        queryStr.append(">");
366                }
367                queryStr.append("))\n");
368                queryStr.append("}");
369
370                System.out.println(queryStr.toString());
371
372                Query query = QueryFactory.create(queryStr.toString());
373                QueryExecution qexec = QueryExecutionFactory.create(query, model);
374               
375                ResultSet results = qexec.execSelect();
376                HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>();
377                for (; results.hasNext();) {
378                        QuerySolution sol = results.next();
379                        String uri = sol.getResource("res").getURI();
380                        Literal literal = sol.getLiteral("label");
381                        if (literal != null) {
382                                String label = literal.getString();
383                                String lang = literal.getLanguage();
384                                if (language != null && language.equals(lang)) {
385                                        Label lbl = new Label(label, lang);
386                                        if (lMap.containsKey(uri)) {
387                                                LabelMap lm = lMap.get(uri);
388                                                lm.addLabel(lbl);
389                                        } else {
390                                                LabelMap lm = new LabelMap(uri, new Label[] { lbl });
391                                                lMap.put(uri, lm);
392                                        }
393                                }
394                        }
395                }
396                return lMap.values().toArray(new LabelMap[0]);
397        }
398
399/*
400        public ClassLink[] countLinks(String[] graphURIs, String startClassURI, ClassLink[] classLinks) throws Exception;
401        public SClass[] countInstances(String[] graphURIs, SClass[] classes) throws Exception;
402       
403*/
404
405}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。