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.ArrayList; |
---|
12 | import java.util.LinkedList; |
---|
13 | import java.util.List; |
---|
14 | import java.util.ListIterator; |
---|
15 | |
---|
16 | public class OWLClassGraph extends LabeledMultiDigraph{ |
---|
17 | String startClass; |
---|
18 | String endClass; |
---|
19 | int nsteps; |
---|
20 | int limit; |
---|
21 | int th; |
---|
22 | double concut; |
---|
23 | double divcut; |
---|
24 | |
---|
25 | public class LinkAndPath{ |
---|
26 | String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL |
---|
27 | ClassLink classLink; |
---|
28 | List<ClassLink> path; |
---|
29 | boolean converge; |
---|
30 | |
---|
31 | public LinkAndPath(ClassLink classLink, List<ClassLink> path){ |
---|
32 | this.classLink = classLink; |
---|
33 | this.path = path; |
---|
34 | this.converge = false; |
---|
35 | } |
---|
36 | |
---|
37 | public LinkAndPath(ClassLink classLink, List<ClassLink> path, String orinalClassURI, boolean converge){ |
---|
38 | this.classLink = classLink; |
---|
39 | this.path = path; |
---|
40 | this.originalClassURI = originalClassURI; |
---|
41 | this.converge = converge; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | public OWLClassGraph(String startClass, String endClass){ |
---|
46 | super(); |
---|
47 | this.startClass = startClass; |
---|
48 | addNode(startClass); |
---|
49 | this.endClass = endClass; |
---|
50 | addNode(endClass); |
---|
51 | nsteps = 3; |
---|
52 | limit = 1000; |
---|
53 | th = 0; |
---|
54 | concut = 2.0; |
---|
55 | divcut = - 2.0; |
---|
56 | } |
---|
57 | |
---|
58 | /* |
---|
59 | public void generateGraph(List<List<ClassLink>> paths){ |
---|
60 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
61 | while( pit.hasNext() ){ |
---|
62 | List<ClassLink> cls = pit.next(); |
---|
63 | String start = startClass; |
---|
64 | ListIterator<ClassLink> cit = cls.listIterator(); |
---|
65 | while ( cit.hasNext() ){ |
---|
66 | // KOKO |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | */ |
---|
71 | |
---|
72 | public Path[] getPaths(OWLQueryBuilderImpl qb, int mode, boolean countLink){ |
---|
73 | List<List<ClassLink>> paths = null; |
---|
74 | if ( mode <= 1){ |
---|
75 | paths = searchPaths(qb, mode, countLink); |
---|
76 | }else if ( mode == 2 ){ |
---|
77 | paths = searchPathsWithCut(qb); |
---|
78 | }else{ |
---|
79 | System.err.println("Mode is not correct"); |
---|
80 | return null; |
---|
81 | } |
---|
82 | Path[] patharray = new Path[paths.size()]; |
---|
83 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
84 | int i = 0; |
---|
85 | while ( pit.hasNext() ){ |
---|
86 | patharray[i] = new Path(); |
---|
87 | patharray[i].setStartClass(startClass); |
---|
88 | List<ClassLink> path = pit.next(); |
---|
89 | patharray[i].setClassLinks(path); |
---|
90 | ListIterator<ClassLink> cit = path.listIterator(); |
---|
91 | int min = Integer.MAX_VALUE; |
---|
92 | while ( cit.hasNext() ){ |
---|
93 | ClassLink cl = cit.next(); |
---|
94 | if ( cl.getNumOfLinks() < min ){ |
---|
95 | min = cl.getNumOfLinks(); |
---|
96 | } |
---|
97 | } |
---|
98 | patharray[i].setWidth(min); |
---|
99 | i++; |
---|
100 | } |
---|
101 | return patharray; |
---|
102 | } |
---|
103 | |
---|
104 | private List<List<ClassLink>> searchPaths(OWLQueryBuilderImpl qb, int mode, boolean countLinks){ |
---|
105 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
106 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
---|
107 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
108 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
---|
109 | try{ |
---|
110 | for ( int i = 0; i < nsteps; i++ ){ |
---|
111 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
112 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
113 | while ( lit.hasNext() ){ |
---|
114 | LinkAndPath crrlp = lit.next(); |
---|
115 | ClassLink[] classLinks = null; |
---|
116 | // Mode |
---|
117 | if ( mode == 0 ){ |
---|
118 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
119 | }else if ( mode == 1 ){ |
---|
120 | classLinks = qb.getNextClassViaInstanceLink(null, crrlp.classLink.getLinkedClassURI(), limit); |
---|
121 | }else{ System.err.println("Mode is not correct."); } |
---|
122 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
123 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
124 | crrpath.add(classLinks[j]); |
---|
125 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
126 | paths.add(new LinkedList<>(crrpath)); |
---|
127 | continue; |
---|
128 | } |
---|
129 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
---|
130 | continue; |
---|
131 | } |
---|
132 | nextlp.add(new LinkAndPath(classLinks[j],crrpath)); |
---|
133 | } |
---|
134 | } |
---|
135 | lp = nextlp; |
---|
136 | } |
---|
137 | }catch(Exception e){ |
---|
138 | System.err.println(e); |
---|
139 | } |
---|
140 | return paths; |
---|
141 | } |
---|
142 | |
---|
143 | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ |
---|
144 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
145 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
---|
146 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
147 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
---|
148 | try{ |
---|
149 | for ( int i = 0; i < nsteps; i++ ){ |
---|
150 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
151 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
152 | while ( lit.hasNext() ){ |
---|
153 | LinkAndPath crrlp = lit.next(); |
---|
154 | ClassLink[] classLinks = null; |
---|
155 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, true); |
---|
156 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
157 | if ( classLinks[j].getNumOfLinks() == 0 ){ continue; } |
---|
158 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
159 | crrpath.add(classLinks[j]); |
---|
160 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
161 | paths.add(new LinkedList<>(crrpath)); |
---|
162 | continue; |
---|
163 | } |
---|
164 | if (classLinks[j].getNumOfLinks() <= th ){ |
---|
165 | continue; //cut by the number of instances |
---|
166 | } |
---|
167 | // Divergence & Convergence Decision |
---|
168 | boolean con = false; |
---|
169 | boolean div = false; |
---|
170 | if ( decideConvergence(classLinks[j]) ){ // convergence |
---|
171 | con = true; |
---|
172 | } |
---|
173 | if ( decideDivergence(classLinks[j]) ){ // divergence |
---|
174 | div = true; |
---|
175 | } |
---|
176 | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge |
---|
177 | continue; // cut by the differences of entropies |
---|
178 | } |
---|
179 | // crr & next are the same arcs |
---|
180 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
---|
181 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
---|
182 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
---|
183 | continue; |
---|
184 | } |
---|
185 | |
---|
186 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), con)); |
---|
187 | } |
---|
188 | } |
---|
189 | lp = nextlp; |
---|
190 | } |
---|
191 | }catch(Exception e){ |
---|
192 | System.err.println(e); |
---|
193 | } |
---|
194 | return paths; |
---|
195 | } |
---|
196 | |
---|
197 | private boolean decideConvergence(ClassLink classLink){ |
---|
198 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
---|
199 | classLink.getNumOfLinkedInstances(), |
---|
200 | classLink.getNumOfLinks()); |
---|
201 | if ( con > concut ){ |
---|
202 | return true; |
---|
203 | } |
---|
204 | return false; |
---|
205 | } |
---|
206 | |
---|
207 | private boolean decideDivergence(ClassLink classLink){ |
---|
208 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
---|
209 | classLink.getNumOfLinkedInstances(), |
---|
210 | classLink.getNumOfLinks()); |
---|
211 | if ( con < divcut ){ |
---|
212 | return true; |
---|
213 | } |
---|
214 | return false; |
---|
215 | } |
---|
216 | |
---|
217 | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
---|
218 | //return (double) numOfLinks / (double) numOfLinkedInstances ; |
---|
219 | // Convergence plus, Divergence minus |
---|
220 | return Math.log((double)numOfOriginInstances) - Math.log((double)numOfLinkedInstances); |
---|
221 | } |
---|
222 | } |
---|