1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package org.biohackathon.SPARQLBuilder.OWL; |
---|
6 | |
---|
7 | /** |
---|
8 | * |
---|
9 | * @author atsuko |
---|
10 | */ |
---|
11 | import java.util.*; |
---|
12 | |
---|
13 | public class OWLClassGraph extends LabeledMultiDigraph{ |
---|
14 | //String startClass; |
---|
15 | //String endClass; |
---|
16 | int nsteps = 5; |
---|
17 | int limit = 100; |
---|
18 | int th = 1; |
---|
19 | List<String> nodeType; |
---|
20 | ArrayList<HashSet<Integer>> connectionTable; |
---|
21 | |
---|
22 | public class LinkAndPath{ |
---|
23 | String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL |
---|
24 | ClassLink classLink; |
---|
25 | List<ClassLink> path; |
---|
26 | Set<String> classURIs; // apearing class URIs in the path |
---|
27 | |
---|
28 | |
---|
29 | public LinkAndPath(ClassLink classLink, List<ClassLink> path){ |
---|
30 | this.classLink = classLink; |
---|
31 | this.path = path; |
---|
32 | } |
---|
33 | |
---|
34 | public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI){ |
---|
35 | this.classLink = classLink; |
---|
36 | this.path = path; |
---|
37 | this.originalClassURI = originalClassURI; |
---|
38 | } |
---|
39 | |
---|
40 | public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI, Set<String> classURIs){ |
---|
41 | this.classLink = classLink; |
---|
42 | this.path = path; |
---|
43 | this.originalClassURI = originalClassURI; |
---|
44 | this.classURIs = classURIs; |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | /* |
---|
49 | public OWLClassGraph(String startClass, String endClass){ |
---|
50 | super(); |
---|
51 | |
---|
52 | // start & end |
---|
53 | this.startClass = startClass; |
---|
54 | this.endClass = endClass; |
---|
55 | |
---|
56 | // parameters |
---|
57 | nsteps = 3; |
---|
58 | limit = 1000; |
---|
59 | th = 5; |
---|
60 | } |
---|
61 | */ |
---|
62 | |
---|
63 | /* |
---|
64 | public OWLClassGraph(String startClass, String endClass, int th){ |
---|
65 | super(); |
---|
66 | |
---|
67 | // start & end |
---|
68 | this.startClass = startClass; |
---|
69 | this.endClass = endClass; |
---|
70 | // th of instances |
---|
71 | this.th = th; |
---|
72 | |
---|
73 | // parameters |
---|
74 | nsteps = 3; |
---|
75 | limit = 1000; |
---|
76 | } |
---|
77 | */ |
---|
78 | |
---|
79 | public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ |
---|
80 | super(); |
---|
81 | nodeType = new LinkedList<String>(); |
---|
82 | setClassGraph(rdfsa); |
---|
83 | connectionTable = createConnectionTable(); |
---|
84 | } |
---|
85 | |
---|
86 | public int getNumberOfEdge(String url){ |
---|
87 | Integer node = labelednodes.get(url); |
---|
88 | if (node == null){ |
---|
89 | return 0; |
---|
90 | } |
---|
91 | return adjlist.get(node).size(); |
---|
92 | } |
---|
93 | |
---|
94 | public Path[] getPaths(String startClass, String endClass){ |
---|
95 | List<List<ClassLink>> paths = searchPaths(startClass, endClass); |
---|
96 | |
---|
97 | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
---|
98 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
99 | int j = 0; |
---|
100 | while ( pit.hasNext() ){ |
---|
101 | Path path = new Path(); |
---|
102 | path.setStartClass(startClass); |
---|
103 | List<ClassLink> crrpath = pit.next(); |
---|
104 | path.setClassLinks(crrpath); |
---|
105 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
---|
106 | int min = Integer.MAX_VALUE; |
---|
107 | while ( cit.hasNext() ){ |
---|
108 | ClassLink cl = cit.next(); |
---|
109 | if ( cl.getNumOfLinks() < min ){ |
---|
110 | min = cl.getNumOfLinks(); |
---|
111 | } |
---|
112 | } |
---|
113 | // using length of path |
---|
114 | int rankwidth = (int) ( ( min * nsteps )/ crrpath.size() ); |
---|
115 | path.setWidth(rankwidth); |
---|
116 | sortedpath.add(path); |
---|
117 | j++; |
---|
118 | } |
---|
119 | Path[] patharray = new Path[paths.size()]; |
---|
120 | Iterator<Path> pait = sortedpath.descendingIterator(); |
---|
121 | int i = 0; |
---|
122 | while ( pait.hasNext() ){ |
---|
123 | patharray[i] = pait.next(); |
---|
124 | i++; |
---|
125 | } |
---|
126 | return patharray; |
---|
127 | } |
---|
128 | |
---|
129 | public HashSet<Integer> getConnectionList(Integer node){ |
---|
130 | return connectionTable.get(node); |
---|
131 | } |
---|
132 | |
---|
133 | public Path[] getPaths_old(RDFSchemaAnalyzer rdfsa, boolean countLink, String startClass, String endClass){ |
---|
134 | List<List<ClassLink>> paths = null; |
---|
135 | paths = searchPathsbyVisitingNodes(rdfsa, countLink, startClass, endClass); |
---|
136 | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
---|
137 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
138 | int j = 0; |
---|
139 | while ( pit.hasNext() ){ |
---|
140 | Path path = new Path(); |
---|
141 | path.setStartClass(startClass); |
---|
142 | List<ClassLink> crrpath = pit.next(); |
---|
143 | path.setClassLinks(crrpath); |
---|
144 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
---|
145 | int min = Integer.MAX_VALUE; |
---|
146 | while ( cit.hasNext() ){ |
---|
147 | ClassLink cl = cit.next(); |
---|
148 | if ( cl.getNumOfLinks() < min ){ |
---|
149 | min = cl.getNumOfLinks(); |
---|
150 | } |
---|
151 | } |
---|
152 | path.setWidth(min); |
---|
153 | sortedpath.add(path); |
---|
154 | j++; |
---|
155 | } |
---|
156 | Path[] patharray = new Path[paths.size()]; |
---|
157 | Iterator<Path> pait = sortedpath.descendingIterator(); |
---|
158 | int i = 0; |
---|
159 | while ( pait.hasNext() ){ |
---|
160 | patharray[i] = pait.next(); |
---|
161 | i++; |
---|
162 | } |
---|
163 | return patharray; |
---|
164 | } |
---|
165 | |
---|
166 | private List<List<ClassLink>> searchPaths(String startClass, String endClass){ |
---|
167 | |
---|
168 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
169 | List<List<Integer>> simplePaths = new LinkedList<>(); |
---|
170 | Integer snode = labelednodes.get(startClass); |
---|
171 | Integer enode = labelednodes.get(endClass); |
---|
172 | List<List<Integer>> lp = new LinkedList<>(); |
---|
173 | List<Integer> ini = new LinkedList<Integer>(); // initial path |
---|
174 | ini.add(snode); |
---|
175 | lp.add(ini); |
---|
176 | for (int i = 0; i < nsteps; i++ ){ |
---|
177 | ListIterator<List<Integer>> lit = lp.listIterator(); |
---|
178 | List<List<Integer>> nextlp = new LinkedList<>(); |
---|
179 | while ( lit.hasNext() ){ |
---|
180 | List<Integer> crrpath = lit.next(); |
---|
181 | Integer crrnode = crrpath.get(crrpath.size()-1); |
---|
182 | Set<Integer> nexts = gadjlist.get(crrnode).keySet(); |
---|
183 | Iterator<Integer> nit = nexts.iterator(); |
---|
184 | while( nit.hasNext() ){ |
---|
185 | Integer nextnode = nit.next(); |
---|
186 | if ( crrpath.contains(nextnode) ){ continue; } |
---|
187 | List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy |
---|
188 | nextpath.add(nextnode); |
---|
189 | if ( nextnode.equals(enode) ){ |
---|
190 | simplePaths.add(nextpath); |
---|
191 | continue; |
---|
192 | } |
---|
193 | nextlp.add(nextpath); |
---|
194 | } |
---|
195 | } |
---|
196 | lp = nextlp; |
---|
197 | } |
---|
198 | |
---|
199 | ListIterator<List<Integer>> pit = simplePaths.listIterator(); |
---|
200 | while( pit.hasNext()){ |
---|
201 | List<Integer> spath = pit.next(); |
---|
202 | List<List<ClassLink>> convertedPaths = convertSimplePathToPaths(spath); |
---|
203 | paths.addAll(convertedPaths); |
---|
204 | } |
---|
205 | return paths; |
---|
206 | } |
---|
207 | |
---|
208 | private List<List<ClassLink>> convertSimplePathToPaths(List<Integer> simplePath){ |
---|
209 | List<List<ClassLink>> paths = new LinkedList<List<ClassLink>>(); |
---|
210 | //List<List<LabeledEdge>> multiedges = new LinkedList<List<LabeledEdge>>(); |
---|
211 | ListIterator<Integer> spit = simplePath.listIterator(); |
---|
212 | Integer start = spit.next(); |
---|
213 | Integer end = spit.next(); |
---|
214 | List<LabeledEdge> edges = gadjlist.get(start).get(end); |
---|
215 | ListIterator<LabeledEdge> eit = edges.listIterator(); |
---|
216 | while ( eit.hasNext() ){ |
---|
217 | List<ClassLink> cl = new LinkedList<ClassLink>(); |
---|
218 | cl.add((ClassLink)eit.next().getLabel()); |
---|
219 | paths.add(cl); |
---|
220 | } |
---|
221 | start = end; |
---|
222 | while( spit.hasNext() ){ |
---|
223 | end = spit.next(); |
---|
224 | // start-end |
---|
225 | edges = gadjlist.get(start).get(end); |
---|
226 | List<List<ClassLink>> tmppaths = new LinkedList<List<ClassLink>>(); |
---|
227 | // current path |
---|
228 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
229 | while ( pit.hasNext() ){ |
---|
230 | List<ClassLink> basepath = pit.next(); |
---|
231 | eit = edges.listIterator(); |
---|
232 | while ( eit.hasNext() ){ |
---|
233 | ClassLink cl = (ClassLink) eit.next().label; |
---|
234 | List<ClassLink> addedpath = new LinkedList<ClassLink>(basepath); |
---|
235 | addedpath.add(cl); |
---|
236 | tmppaths.add(addedpath); |
---|
237 | } |
---|
238 | } |
---|
239 | paths = tmppaths; |
---|
240 | start = end; |
---|
241 | } |
---|
242 | return paths; |
---|
243 | } |
---|
244 | |
---|
245 | /* |
---|
246 | private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ |
---|
247 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
248 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
249 | lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), "")); |
---|
250 | try{ |
---|
251 | for ( int i = 0; i < nsteps; i++ ){ |
---|
252 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
253 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
254 | while ( lit.hasNext() ){ |
---|
255 | LinkAndPath crrlp = lit.next(); |
---|
256 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
257 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
258 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
259 | crrpath.add(classLinks[j]); |
---|
260 | if ( classLinks[j].getLinkedClassURI() == null ){ continue; } |
---|
261 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
262 | paths.add(new LinkedList<>(crrpath)); |
---|
263 | continue; |
---|
264 | } |
---|
265 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
---|
266 | continue; |
---|
267 | } |
---|
268 | if ( i >= 2 ){ |
---|
269 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
---|
270 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
---|
271 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
---|
272 | continue; |
---|
273 | } |
---|
274 | } |
---|
275 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI())); |
---|
276 | } |
---|
277 | } |
---|
278 | lp = nextlp; |
---|
279 | } |
---|
280 | }catch(Exception e){ |
---|
281 | System.err.println(e); |
---|
282 | } |
---|
283 | return paths; |
---|
284 | } |
---|
285 | */ |
---|
286 | |
---|
287 | private List<List<ClassLink>> searchPathsbyVisitingNodes(RDFSchemaAnalyzer rdfsa, boolean countLinks, |
---|
288 | String startClass, String endClass){ |
---|
289 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
290 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
291 | Set<String> urls = new HashSet<String>(); |
---|
292 | urls.add(startClass); |
---|
293 | lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), |
---|
294 | new LinkedList<ClassLink>(), startClass, urls)); |
---|
295 | try{ |
---|
296 | for ( int i = 0; i < nsteps; i++ ){ |
---|
297 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
298 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
299 | while ( lit.hasNext() ){ |
---|
300 | LinkAndPath crrlp = lit.next(); |
---|
301 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
302 | // limit |
---|
303 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
304 | if (crrlp.classURIs.contains(classLinks[j].getLinkedClassURI()) ){ |
---|
305 | continue; // visited |
---|
306 | } |
---|
307 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
308 | crrpath.add(classLinks[j]); |
---|
309 | Set<String> crrurls = new HashSet<String>(crrlp.classURIs); |
---|
310 | crrurls.add(classLinks[j].getLinkedClassURI()); |
---|
311 | if ( classLinks[j].getLinkedClassURI() == null ){ continue; } |
---|
312 | if ( classLinks[j].getNumOfLinks() <= th){ |
---|
313 | continue; |
---|
314 | } |
---|
315 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
316 | paths.add(new LinkedList<>(crrpath)); |
---|
317 | continue; |
---|
318 | } |
---|
319 | //crrlp.classURIs.add() |
---|
320 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(),crrurls)); |
---|
321 | } |
---|
322 | } |
---|
323 | lp = nextlp; |
---|
324 | } |
---|
325 | }catch(Exception e){ |
---|
326 | System.err.println(e); |
---|
327 | } |
---|
328 | return paths; |
---|
329 | } |
---|
330 | |
---|
331 | private void setClassGraph(RDFSchemaAnalyzer rdfsa){ |
---|
332 | // setNodes |
---|
333 | SClass[] classes = null; |
---|
334 | try{ |
---|
335 | classes = rdfsa.getOWLClasses(null, null, null, true); |
---|
336 | }catch(Exception e){ |
---|
337 | System.err.println(e); return; |
---|
338 | } |
---|
339 | for (int i = 0 ; i < classes.length; i++){ |
---|
340 | addNode(classes[i].getClassURI()); |
---|
341 | nodeType.add("class"); |
---|
342 | } |
---|
343 | // setEdges |
---|
344 | for (int i = 0 ; i < classes.length; i++ ){ |
---|
345 | try{ |
---|
346 | ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true); |
---|
347 | for (int j = 0 ; j < classLinks.length; j++){ |
---|
348 | Integer n = labelednodes.get(classLinks[j].getLinkedClassURI()); |
---|
349 | if ( n != null ){ |
---|
350 | addEdge(i, n, classLinks[j]); |
---|
351 | }else{ |
---|
352 | n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI()); |
---|
353 | if ( n == null ){ |
---|
354 | addNode(classLinks[j].getLinkedLiteralDatatypeURI()); |
---|
355 | n = nodeType.size(); |
---|
356 | nodeType.add("literal"); |
---|
357 | } |
---|
358 | addEdge(i, n, classLinks[j]); |
---|
359 | /* |
---|
360 | ClassLink rev = new ClassLink( classLinks[j].getPropertyURI(), |
---|
361 | //classLinks[j].getLinkedClassURI(), |
---|
362 | classes[i].getClassURI(), |
---|
363 | classLinks[j].getLinkedLiteralDatatypeURI(), |
---|
364 | classLinks[j].getDirection(), classLinks[j].getNumOfLinks(), |
---|
365 | classLinks[j].getNumOfOriginInstances(), classLinks[j].getNumOfLinkedInstances(), |
---|
366 | classLinks[j].getNumOfOriginClassInstances(), classLinks[j].getNumOfLinkedClassInstances(), |
---|
367 | classLinks[j].isDomainClassLimitedQ(), classLinks[j].isRangeClassLimitedQ() ); |
---|
368 | rev.setDirection(Direction.reverse); |
---|
369 | addEdge(n, i, rev); |
---|
370 | */ |
---|
371 | } |
---|
372 | } |
---|
373 | }catch(Exception e){ |
---|
374 | System.err.println(e); |
---|
375 | } |
---|
376 | } |
---|
377 | } |
---|
378 | |
---|
379 | private ArrayList<HashSet<Integer>> createConnectionTable(){ |
---|
380 | ArrayList<HashSet<Integer>> ct = new ArrayList<HashSet<Integer>>(); |
---|
381 | for (int i = 0; i < labels.size(); i++ ){ // each node |
---|
382 | HashSet<Integer> cl = createConnectionList(i); |
---|
383 | ct.add(cl); |
---|
384 | } |
---|
385 | return ct; |
---|
386 | } |
---|
387 | |
---|
388 | private HashSet<Integer> createConnectionList(Integer node){ |
---|
389 | HashSet<Integer> cl = new HashSet<Integer>(); |
---|
390 | HashSet<Integer> crrnodes = new HashSet<Integer>(); |
---|
391 | crrnodes.add(node); |
---|
392 | cl.add(node); |
---|
393 | for ( int i = 0 ; i < nsteps; i++ ){ |
---|
394 | Set<Integer> nextnodes = new HashSet<Integer>(); |
---|
395 | Iterator<Integer> cit = crrnodes.iterator(); |
---|
396 | while ( cit.hasNext() ){ |
---|
397 | Integer crr = cit.next(); |
---|
398 | Set<Integer> nexts = gadjlist.get(crr).keySet(); |
---|
399 | Iterator<Integer> nit = nexts.iterator(); |
---|
400 | while ( nit.hasNext() ){ |
---|
401 | Integer next = nit.next(); |
---|
402 | if ( !cl.contains(next) ){ |
---|
403 | nextnodes.add(next); |
---|
404 | cl.add(next); |
---|
405 | } |
---|
406 | } |
---|
407 | } |
---|
408 | crrnodes.clear(); |
---|
409 | crrnodes.addAll(nextnodes); |
---|
410 | } |
---|
411 | return cl; |
---|
412 | } |
---|
413 | } |
---|