1 | package org.biohackathon.SPARQLBuilder.OWL;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 |
|
---|
6 | import jp.riken.accc.db.rdf.crawler.dataStructure.sparql.JenaModelGenerator;
|
---|
7 |
|
---|
8 | import com.hp.hpl.jena.query.Query;
|
---|
9 | import com.hp.hpl.jena.query.QueryExecution;
|
---|
10 | import com.hp.hpl.jena.query.QueryExecutionFactory;
|
---|
11 | import com.hp.hpl.jena.query.QueryFactory;
|
---|
12 | import com.hp.hpl.jena.query.QuerySolution;
|
---|
13 | import com.hp.hpl.jena.query.ResultSet;
|
---|
14 | import com.hp.hpl.jena.rdf.model.Literal;
|
---|
15 | import com.hp.hpl.jena.rdf.model.Model;
|
---|
16 | import com.hp.hpl.jena.rdf.model.Property;
|
---|
17 | import com.hp.hpl.jena.rdf.model.Resource;
|
---|
18 |
|
---|
19 | //public class OWLQueryBuilderForCrawlerImpl implements OWLQueryBuilder {
|
---|
20 | public 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 ?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 | queryStr.append("}\n");
|
---|
219 |
|
---|
220 |
|
---|
221 | if (limit > 0) {
|
---|
222 | queryStr.append("limit ");
|
---|
223 | queryStr.append(limit);
|
---|
224 | queryStr.append("\n");
|
---|
225 | }
|
---|
226 |
|
---|
227 | // System.out.println("getNextClasses SPARQL Query: ");
|
---|
228 | System.out.println(queryStr.toString());
|
---|
229 |
|
---|
230 | Query query = QueryFactory.create(queryStr.toString());
|
---|
231 | QueryExecution qexec = null;
|
---|
232 | ResultSet results = null;
|
---|
233 | try {
|
---|
234 | long start = System.currentTimeMillis();
|
---|
235 | qexec = QueryExecutionFactory.create(query, model);
|
---|
236 | results = qexec.execSelect();
|
---|
237 | long end = System.currentTimeMillis();
|
---|
238 | System.out.println("EXEC TIME: " + (end - start));
|
---|
239 | } catch (Exception ex) {
|
---|
240 | ex.printStackTrace();
|
---|
241 | throw ex;
|
---|
242 | }
|
---|
243 |
|
---|
244 | ArrayList<ClassLink> solCLs = new ArrayList<ClassLink>();
|
---|
245 | for (; results.hasNext();) {
|
---|
246 | QuerySolution sol = results.next();
|
---|
247 | Resource pro = sol.getResource("p");
|
---|
248 | String clsURI = null;
|
---|
249 | if (pro != null) {
|
---|
250 | int indPropCat = 4;
|
---|
251 | Literal indPropCatLit = sol.getLiteral("indPropCat");
|
---|
252 | if( indPropCatLit != null ){
|
---|
253 | indPropCat = indPropCatLit.getInt();
|
---|
254 | }
|
---|
255 | if( indPropCat < 4 ) {
|
---|
256 | String proURI = pro.getURI();
|
---|
257 | Resource ccls = sol.getResource("c");
|
---|
258 | Resource dcls = sol.getResource("d");
|
---|
259 | Direction direction = null;
|
---|
260 | if(ccls != null && dcls == null ){
|
---|
261 | // direction forward
|
---|
262 | direction = Direction.forward;
|
---|
263 | clsURI = ccls.getURI();
|
---|
264 | }else{
|
---|
265 | if( ccls == null && dcls != null ){
|
---|
266 | direction = Direction.reverse;
|
---|
267 | clsURI = dcls.getURI();
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | int numTriples = 0;
|
---|
273 | Literal numTriplesLit = sol.getLiteral("numTriples");
|
---|
274 | if( numTriplesLit != null ){
|
---|
275 | numTriples = numTriplesLit.getInt();
|
---|
276 | }
|
---|
277 |
|
---|
278 | int numLnkInsStart = 0;
|
---|
279 | Literal numInsStartLit = sol.getLiteral("numLnkInsStart");
|
---|
280 | if( numInsStartLit != null ){
|
---|
281 | numLnkInsStart = numInsStartLit.getInt();
|
---|
282 | }
|
---|
283 | int numLnkInsEnd = 0;
|
---|
284 | Literal numInsEndLit = sol.getLiteral("numLnkInsEnd");
|
---|
285 | if( numInsEndLit != null ){
|
---|
286 | numLnkInsEnd = numInsEndLit.getInt();
|
---|
287 | }
|
---|
288 |
|
---|
289 | int numInsDom = 0;
|
---|
290 | Literal numInsDomLit = sol.getLiteral("numInsDom");
|
---|
291 | if( numInsDomLit != null ){
|
---|
292 | numInsDom = numInsDomLit.getInt();
|
---|
293 | }
|
---|
294 | int numInsRan = 0;
|
---|
295 | Literal numInsRanLit = sol.getLiteral("numInsRan");
|
---|
296 | if( numInsRanLit != null ){
|
---|
297 | numInsRan = numInsRanLit.getInt();
|
---|
298 | }
|
---|
299 |
|
---|
300 | boolean isStartClsLim = false;
|
---|
301 | Literal isStartClsLimLit = sol.getLiteral("isStartClsLim");
|
---|
302 | if( isStartClsLimLit != null ){
|
---|
303 | isStartClsLim = isStartClsLimLit.getBoolean();
|
---|
304 | }
|
---|
305 | boolean isEndClsLim = false;
|
---|
306 | Literal isEndClsLimLit = sol.getLiteral("isEndClsLim");
|
---|
307 | if( isEndClsLimLit != null ){
|
---|
308 | isEndClsLim = isEndClsLimLit.getBoolean();
|
---|
309 | }
|
---|
310 |
|
---|
311 | ClassLink cl = new ClassLink(proURI, clsURI, direction,
|
---|
312 | numTriples, numInsDom, numInsRan, numLnkInsStart, numLnkInsEnd, isStartClsLim, isEndClsLim);
|
---|
313 | solCLs.add(cl);
|
---|
314 |
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 | qexec.close();
|
---|
319 | return solCLs.toArray(new ClassLink[0]);
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 |
|
---|
324 | /*
|
---|
325 |
|
---|
326 | public ClassLink[] getNextClassViaInstanceLink(String[] graphURIs, String originClass, int limit) throws Exception;
|
---|
327 |
|
---|
328 | public Path[] getPaths(String startClass, String endClass, int mode, boolean countLinks) throws Exception;
|
---|
329 |
|
---|
330 | public String createSPARQL(Path path) throws Exception;
|
---|
331 |
|
---|
332 | InstanceLink[] getNextInstancesViaInstanceLink(String[] graphURIs, String originInstance,
|
---|
333 | int limit) throws Exception;
|
---|
334 | */
|
---|
335 |
|
---|
336 | public LabelMap[] getLabels(String[] graphURIs, String[] resourceURIs,
|
---|
337 | String language) throws Exception {
|
---|
338 | if (resourceURIs == null || resourceURIs.length == 0) {
|
---|
339 | return new LabelMap[0];
|
---|
340 | }
|
---|
341 | StringBuffer queryStr = new StringBuffer();
|
---|
342 | queryStr.append("PREFIX owl: <http://www.w3.org/2002/07/owl#>\n");
|
---|
343 | queryStr.append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n");
|
---|
344 | queryStr.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
|
---|
345 | queryStr.append("SELECT DISTINCT ?res ?label \n");
|
---|
346 | if (graphURIs != null) {
|
---|
347 | for (String graphURI : graphURIs) {
|
---|
348 | queryStr.append("FROM <");
|
---|
349 | queryStr.append(graphURI);
|
---|
350 | queryStr.append(">\n");
|
---|
351 | }
|
---|
352 | }
|
---|
353 | queryStr.append("WHERE{\n");
|
---|
354 | queryStr.append(" ?res rdfs:label ?label.\n");
|
---|
355 | queryStr.append(" FILTER(?res IN (");
|
---|
356 | boolean f = false;
|
---|
357 | for (String resourceURI : resourceURIs) {
|
---|
358 | if (f) {
|
---|
359 | queryStr.append(", ");
|
---|
360 | }
|
---|
361 | f = true;
|
---|
362 | queryStr.append("<");
|
---|
363 | queryStr.append(resourceURI);
|
---|
364 | queryStr.append(">");
|
---|
365 | }
|
---|
366 | queryStr.append("))\n");
|
---|
367 | queryStr.append("}");
|
---|
368 |
|
---|
369 | System.out.println(queryStr.toString());
|
---|
370 |
|
---|
371 | Query query = QueryFactory.create(queryStr.toString());
|
---|
372 | QueryExecution qexec = QueryExecutionFactory.create(query, model);
|
---|
373 |
|
---|
374 | ResultSet results = qexec.execSelect();
|
---|
375 | HashMap<String, LabelMap> lMap = new HashMap<String, LabelMap>();
|
---|
376 | for (; results.hasNext();) {
|
---|
377 | QuerySolution sol = results.next();
|
---|
378 | String uri = sol.getResource("res").getURI();
|
---|
379 | Literal literal = sol.getLiteral("label");
|
---|
380 | if (literal != null) {
|
---|
381 | String label = literal.getString();
|
---|
382 | String lang = literal.getLanguage();
|
---|
383 | if (language != null && language.equals(lang)) {
|
---|
384 | Label lbl = new Label(label, lang);
|
---|
385 | if (lMap.containsKey(uri)) {
|
---|
386 | LabelMap lm = lMap.get(uri);
|
---|
387 | lm.addLabel(lbl);
|
---|
388 | } else {
|
---|
389 | LabelMap lm = new LabelMap(uri, new Label[] { lbl });
|
---|
390 | lMap.put(uri, lm);
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 | }
|
---|
395 | return lMap.values().toArray(new LabelMap[0]);
|
---|
396 | }
|
---|
397 |
|
---|
398 | /*
|
---|
399 | public ClassLink[] countLinks(String[] graphURIs, String startClassURI, ClassLink[] classLinks) throws Exception;
|
---|
400 | public SClass[] countInstances(String[] graphURIs, SClass[] classes) throws Exception;
|
---|
401 |
|
---|
402 | */
|
---|
403 |
|
---|
404 | }
|
---|