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; |
---|
17 | int limit; |
---|
18 | int th; |
---|
19 | int prunecut; |
---|
20 | |
---|
21 | public class LinkAndPath{ |
---|
22 | String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL |
---|
23 | ClassLink classLink; |
---|
24 | List<ClassLink> path; |
---|
25 | Set<String> classURIs; // apearing class URIs in the path |
---|
26 | //boolean converge; |
---|
27 | |
---|
28 | public LinkAndPath(ClassLink classLink, List<ClassLink> path){ |
---|
29 | this.classLink = classLink; |
---|
30 | this.path = path; |
---|
31 | //this.converge = false; |
---|
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 | //this.converge = converge; |
---|
39 | } |
---|
40 | |
---|
41 | public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI, Set<String> classURIs){ |
---|
42 | this.classLink = classLink; |
---|
43 | this.path = path; |
---|
44 | this.originalClassURI = originalClassURI; |
---|
45 | this.classURIs = classURIs; |
---|
46 | //this.converge = converge; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public OWLClassGraph(String startClass, String endClass){ |
---|
51 | super(); |
---|
52 | |
---|
53 | // start & end |
---|
54 | this.startClass = startClass; |
---|
55 | this.endClass = endClass; |
---|
56 | |
---|
57 | // parameters |
---|
58 | nsteps = 3; |
---|
59 | limit = 1000; |
---|
60 | prunecut = 100; |
---|
61 | th = 5; |
---|
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 | prunecut = 100; |
---|
77 | } |
---|
78 | |
---|
79 | public Path[] getPaths(RDFSchemaAnalyzer rdfsa, boolean countLink){ |
---|
80 | List<List<ClassLink>> paths = null; |
---|
81 | //paths = searchPaths(rdfsa, countLink); |
---|
82 | paths = searchPathsbyVisitingNodes(rdfsa, countLink); |
---|
83 | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
---|
84 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
85 | int j = 0; |
---|
86 | while ( pit.hasNext() ){ |
---|
87 | Path path = new Path(); |
---|
88 | path.setStartClass(startClass); |
---|
89 | List<ClassLink> crrpath = pit.next(); |
---|
90 | path.setClassLinks(crrpath); |
---|
91 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
---|
92 | int min = Integer.MAX_VALUE; |
---|
93 | while ( cit.hasNext() ){ |
---|
94 | ClassLink cl = cit.next(); |
---|
95 | if ( cl.getNumOfLinks() < min ){ |
---|
96 | min = cl.getNumOfLinks(); |
---|
97 | } |
---|
98 | } |
---|
99 | path.setWidth(min); |
---|
100 | sortedpath.add(path); |
---|
101 | j++; |
---|
102 | } |
---|
103 | Path[] patharray = new Path[paths.size()]; |
---|
104 | Iterator<Path> pait = sortedpath.descendingIterator(); |
---|
105 | int i = 0; |
---|
106 | while ( pait.hasNext() ){ |
---|
107 | patharray[i] = pait.next(); |
---|
108 | i++; |
---|
109 | } |
---|
110 | return patharray; |
---|
111 | } |
---|
112 | |
---|
113 | private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ |
---|
114 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
115 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
116 | lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), "")); |
---|
117 | try{ |
---|
118 | for ( int i = 0; i < nsteps; i++ ){ |
---|
119 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
120 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
121 | while ( lit.hasNext() ){ |
---|
122 | LinkAndPath crrlp = lit.next(); |
---|
123 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
124 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
125 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
126 | crrpath.add(classLinks[j]); |
---|
127 | if ( classLinks[j].getLinkedClassURI() == null ){ continue; } |
---|
128 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
129 | paths.add(new LinkedList<>(crrpath)); |
---|
130 | continue; |
---|
131 | } |
---|
132 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
---|
133 | continue; |
---|
134 | } |
---|
135 | if ( i >= 2 ){ |
---|
136 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
---|
137 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
---|
138 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
---|
139 | continue; |
---|
140 | } |
---|
141 | } |
---|
142 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI())); |
---|
143 | } |
---|
144 | } |
---|
145 | lp = nextlp; |
---|
146 | } |
---|
147 | }catch(Exception e){ |
---|
148 | System.err.println(e); |
---|
149 | } |
---|
150 | return paths; |
---|
151 | } |
---|
152 | |
---|
153 | private List<List<ClassLink>> searchPathsbyVisitingNodes(RDFSchemaAnalyzer rdfsa, boolean countLinks){ |
---|
154 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
155 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
156 | Set<String> urls = new HashSet<String>(); |
---|
157 | urls.add(startClass); |
---|
158 | lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), |
---|
159 | new LinkedList<ClassLink>(), startClass, urls)); |
---|
160 | try{ |
---|
161 | for ( int i = 0; i < nsteps; i++ ){ |
---|
162 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
163 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
164 | while ( lit.hasNext() ){ |
---|
165 | LinkAndPath crrlp = lit.next(); |
---|
166 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
167 | // limit |
---|
168 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
169 | if (crrlp.classURIs.contains(classLinks[j].getLinkedClassURI()) ){ |
---|
170 | continue; // visited |
---|
171 | } |
---|
172 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
173 | crrpath.add(classLinks[j]); |
---|
174 | Set<String> crrurls = new HashSet<String>(crrlp.classURIs); |
---|
175 | crrurls.add(classLinks[j].getLinkedClassURI()); |
---|
176 | if ( classLinks[j].getLinkedClassURI() == null ){ continue; } |
---|
177 | if ( classLinks[j].getNumOfLinks() <= th){ |
---|
178 | continue; |
---|
179 | } |
---|
180 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
181 | paths.add(new LinkedList<>(crrpath)); |
---|
182 | continue; |
---|
183 | } |
---|
184 | //crrlp.classURIs.add() |
---|
185 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(),crrurls)); |
---|
186 | } |
---|
187 | } |
---|
188 | lp = nextlp; |
---|
189 | } |
---|
190 | }catch(Exception e){ |
---|
191 | System.err.println(e); |
---|
192 | } |
---|
193 | return paths; |
---|
194 | } |
---|
195 | |
---|
196 | /* |
---|
197 | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ |
---|
198 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
199 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
---|
200 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
201 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
---|
202 | try{ |
---|
203 | for ( int i = 0; i < nsteps; i++ ){ |
---|
204 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
205 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
206 | while ( lit.hasNext() ){ |
---|
207 | LinkAndPath crrlp = lit.next(); |
---|
208 | ClassLink[] classLinks = null; |
---|
209 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, true); |
---|
210 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
211 | if ( classLinks[j].getNumOfLinks() == 0 ){ continue; } |
---|
212 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
213 | crrpath.add(classLinks[j]); |
---|
214 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
215 | paths.add(new LinkedList<>(crrpath)); |
---|
216 | continue; |
---|
217 | } |
---|
218 | if (classLinks[j].getNumOfLinks() <= th ){ |
---|
219 | continue; //cut by the number of instances |
---|
220 | } |
---|
221 | // Divergence & Convergence Decision |
---|
222 | boolean con = false; |
---|
223 | boolean div = false; |
---|
224 | if ( decideConvergence(classLinks[j]) ){ // convergence |
---|
225 | con = true; |
---|
226 | } |
---|
227 | if ( decideDivergence(classLinks[j]) ){ // divergence |
---|
228 | div = true; |
---|
229 | } |
---|
230 | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge |
---|
231 | continue; // cut by the differences of entropies |
---|
232 | } |
---|
233 | // crr & next are the same arcs |
---|
234 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
---|
235 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
---|
236 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
---|
237 | continue; |
---|
238 | } |
---|
239 | |
---|
240 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), con)); |
---|
241 | } |
---|
242 | } |
---|
243 | lp = nextlp; |
---|
244 | } |
---|
245 | }catch(Exception e){ |
---|
246 | System.err.println(e); |
---|
247 | } |
---|
248 | return paths; |
---|
249 | } |
---|
250 | */ |
---|
251 | |
---|
252 | private boolean checkPruning(ClassLink classLink1, ClassLink classLink2){ |
---|
253 | // true -> prune link2, false -> add link2 |
---|
254 | int noi1 = classLink1.getNumOfOriginInstances(); |
---|
255 | int nli1 = classLink1.getNumOfLinkedInstances(); |
---|
256 | int noi2 = classLink2.getNumOfOriginInstances(); |
---|
257 | int nli2 = classLink2.getNumOfLinkedInstances(); |
---|
258 | if ( noi1 == 0 || nli1 == 0 || noi2 == 0 || nli2 == 0 ){ |
---|
259 | return true; |
---|
260 | } |
---|
261 | return ( noi1 / nli1 > prunecut && nli2 / noi2 > prunecut ); |
---|
262 | } |
---|
263 | |
---|
264 | /* private boolean decideConvergence(ClassLink classLink){ |
---|
265 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
---|
266 | classLink.getNumOfLinkedInstances(), |
---|
267 | classLink.getNumOfLinks()); |
---|
268 | if ( con > concut ){ |
---|
269 | return true; |
---|
270 | } |
---|
271 | return false; |
---|
272 | } |
---|
273 | */ |
---|
274 | |
---|
275 | /* |
---|
276 | private boolean decideDivergence(ClassLink classLink){ |
---|
277 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
---|
278 | classLink.getNumOfLinkedInstances(), |
---|
279 | classLink.getNumOfLinks()); |
---|
280 | if ( con < divcut ){ |
---|
281 | return true; |
---|
282 | } |
---|
283 | return false; |
---|
284 | } |
---|
285 | */ |
---|
286 | |
---|
287 | /* |
---|
288 | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
---|
289 | //return (double) numOfLinks / (double) numOfLinkedInstances ; |
---|
290 | // Convergence plus, Divergence minus |
---|
291 | return Math.log((double)numOfOriginInstances) - Math.log((double)numOfLinkedInstances); |
---|
292 | } |
---|
293 | */ |
---|
294 | |
---|
295 | public void setWholeGraph(RDFSchemaAnalyzer rdfsa){ |
---|
296 | // setNodes |
---|
297 | SClass[] classes = null; |
---|
298 | try{ |
---|
299 | classes = rdfsa.getOWLClasses(null, null, null, true); |
---|
300 | }catch(Exception e){ |
---|
301 | System.err.println(e); return; |
---|
302 | } |
---|
303 | for (int i = 0 ; i < classes.length; i++){ |
---|
304 | addNode(classes[i].getClassURI()); |
---|
305 | } |
---|
306 | // setEdges |
---|
307 | for (int i = 0 ; i < classes.length; i++){ |
---|
308 | try{ |
---|
309 | ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true); |
---|
310 | for (int j = 0 ; j < classLinks.length; j++){ |
---|
311 | addEdge(i, labelednodes.get(classLinks[j].getLinkedClassURI()), |
---|
312 | classLinks[j].getPropertyURI(), |
---|
313 | classLinks[j].getDirection(), |
---|
314 | classLinks[j].getNumOfLinkedInstances()); |
---|
315 | } |
---|
316 | }catch(Exception e){ |
---|
317 | System.err.println(e); |
---|
318 | } |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | /* |
---|
323 | public Path[] getPathsByWholeGraph(RDFSchemaAnalyzer rdfsa){ |
---|
324 | setWholeGraph(rdfsa); |
---|
325 | List<List<ClassLink>> paths = null; |
---|
326 | //paths = searchPaths(rdfsa, countLink); |
---|
327 | paths = searchPathsbyVisitingNodes(rdfsa, countLink); |
---|
328 | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
---|
329 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
330 | int j = 0; |
---|
331 | while ( pit.hasNext() ){ |
---|
332 | Path path = new Path(); |
---|
333 | path.setStartClass(startClass); |
---|
334 | List<ClassLink> crrpath = pit.next(); |
---|
335 | path.setClassLinks(crrpath); |
---|
336 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
---|
337 | int min = Integer.MAX_VALUE; |
---|
338 | while ( cit.hasNext() ){ |
---|
339 | ClassLink cl = cit.next(); |
---|
340 | if ( cl.getNumOfLinks() < min ){ |
---|
341 | min = cl.getNumOfLinks(); |
---|
342 | } |
---|
343 | } |
---|
344 | path.setWidth(min); |
---|
345 | sortedpath.add(path); |
---|
346 | j++; |
---|
347 | } |
---|
348 | Path[] patharray = new Path[paths.size()]; |
---|
349 | Iterator<Path> pait = sortedpath.descendingIterator(); |
---|
350 | int i = 0; |
---|
351 | while ( pait.hasNext() ){ |
---|
352 | patharray[i] = pait.next(); |
---|
353 | i++; |
---|
354 | } |
---|
355 | return patharray; |
---|
356 | } |
---|
357 | */ |
---|
358 | } |
---|