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 originalClassURI, 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 = 10; |
---|
52 | limit = 1000; |
---|
53 | //th = 0; |
---|
54 | //concut = 2.0; |
---|
55 | //divcut = - 2.0; |
---|
56 | } |
---|
57 | |
---|
58 | public Path[] getPaths(RDFSchemaAnalyzer rdfsa, boolean countLink){ |
---|
59 | List<List<ClassLink>> paths = null; |
---|
60 | paths = searchPaths(rdfsa, countLink); |
---|
61 | Path[] patharray = new Path[paths.size()]; |
---|
62 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
63 | int i = 0; |
---|
64 | while ( pit.hasNext() ){ |
---|
65 | patharray[i] = new Path(); |
---|
66 | patharray[i].setStartClass(startClass); |
---|
67 | List<ClassLink> path = pit.next(); |
---|
68 | patharray[i].setClassLinks(path); |
---|
69 | ListIterator<ClassLink> cit = path.listIterator(); |
---|
70 | int min = Integer.MAX_VALUE; |
---|
71 | while ( cit.hasNext() ){ |
---|
72 | ClassLink cl = cit.next(); |
---|
73 | if ( cl.getNumOfLinks() < min ){ |
---|
74 | min = cl.getNumOfLinks(); |
---|
75 | } |
---|
76 | } |
---|
77 | patharray[i].setWidth(min); |
---|
78 | i++; |
---|
79 | } |
---|
80 | return patharray; |
---|
81 | } |
---|
82 | |
---|
83 | private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ |
---|
84 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
85 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
86 | lp.add(new LinkAndPath(new ClassLink("",startClass,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), "", false)); |
---|
87 | try{ |
---|
88 | for ( int i = 0; i < nsteps; i++ ){ |
---|
89 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
90 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
91 | while ( lit.hasNext() ){ |
---|
92 | LinkAndPath crrlp = lit.next(); |
---|
93 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
94 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
95 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
96 | crrpath.add(classLinks[j]); |
---|
97 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
98 | paths.add(new LinkedList<>(crrpath)); |
---|
99 | continue; |
---|
100 | } |
---|
101 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
---|
102 | continue; |
---|
103 | } |
---|
104 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
---|
105 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
---|
106 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
---|
107 | continue; |
---|
108 | } |
---|
109 | |
---|
110 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), false)); |
---|
111 | } |
---|
112 | } |
---|
113 | lp = nextlp; |
---|
114 | } |
---|
115 | }catch(Exception e){ |
---|
116 | System.err.println(e); |
---|
117 | } |
---|
118 | return paths; |
---|
119 | } |
---|
120 | |
---|
121 | /* |
---|
122 | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ |
---|
123 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
124 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
---|
125 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
126 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
---|
127 | try{ |
---|
128 | for ( int i = 0; i < nsteps; i++ ){ |
---|
129 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
130 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
131 | while ( lit.hasNext() ){ |
---|
132 | LinkAndPath crrlp = lit.next(); |
---|
133 | ClassLink[] classLinks = null; |
---|
134 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, true); |
---|
135 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
136 | if ( classLinks[j].getNumOfLinks() == 0 ){ continue; } |
---|
137 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
138 | crrpath.add(classLinks[j]); |
---|
139 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
140 | paths.add(new LinkedList<>(crrpath)); |
---|
141 | continue; |
---|
142 | } |
---|
143 | if (classLinks[j].getNumOfLinks() <= th ){ |
---|
144 | continue; //cut by the number of instances |
---|
145 | } |
---|
146 | // Divergence & Convergence Decision |
---|
147 | boolean con = false; |
---|
148 | boolean div = false; |
---|
149 | if ( decideConvergence(classLinks[j]) ){ // convergence |
---|
150 | con = true; |
---|
151 | } |
---|
152 | if ( decideDivergence(classLinks[j]) ){ // divergence |
---|
153 | div = true; |
---|
154 | } |
---|
155 | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge |
---|
156 | continue; // cut by the differences of entropies |
---|
157 | } |
---|
158 | // crr & next are the same arcs |
---|
159 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
---|
160 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
---|
161 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
---|
162 | continue; |
---|
163 | } |
---|
164 | |
---|
165 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), con)); |
---|
166 | } |
---|
167 | } |
---|
168 | lp = nextlp; |
---|
169 | } |
---|
170 | }catch(Exception e){ |
---|
171 | System.err.println(e); |
---|
172 | } |
---|
173 | return paths; |
---|
174 | } |
---|
175 | */ |
---|
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 | |
---|
189 | /* |
---|
190 | private boolean decideDivergence(ClassLink classLink){ |
---|
191 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
---|
192 | classLink.getNumOfLinkedInstances(), |
---|
193 | classLink.getNumOfLinks()); |
---|
194 | if ( con < divcut ){ |
---|
195 | return true; |
---|
196 | } |
---|
197 | return false; |
---|
198 | } |
---|
199 | */ |
---|
200 | |
---|
201 | /* |
---|
202 | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
---|
203 | //return (double) numOfLinks / (double) numOfLinkedInstances ; |
---|
204 | // Convergence plus, Divergence minus |
---|
205 | return Math.log((double)numOfOriginInstances) - Math.log((double)numOfLinkedInstances); |
---|
206 | } |
---|
207 | */ |
---|
208 | } |
---|