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