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

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

クローラー結果から、StartClass?, EndClassにそれぞれ含まれるインスタンス数を取得できるようにした

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