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 | 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 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
---|
86 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
87 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
---|
88 | try{ |
---|
89 | for ( int i = 0; i < nsteps; i++ ){ |
---|
90 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
91 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
92 | while ( lit.hasNext() ){ |
---|
93 | LinkAndPath crrlp = lit.next(); |
---|
94 | ClassLink[] classLinks = null; |
---|
95 | classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
96 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
97 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
98 | crrpath.add(classLinks[j]); |
---|
99 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
100 | paths.add(new LinkedList<>(crrpath)); |
---|
101 | continue; |
---|
102 | } |
---|
103 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
---|
104 | continue; |
---|
105 | } |
---|
106 | nextlp.add(new LinkAndPath(classLinks[j],crrpath)); |
---|
107 | } |
---|
108 | } |
---|
109 | lp = nextlp; |
---|
110 | } |
---|
111 | }catch(Exception e){ |
---|
112 | System.err.println(e); |
---|
113 | } |
---|
114 | return paths; |
---|
115 | } |
---|
116 | |
---|
117 | /* |
---|
118 | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ |
---|
119 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
120 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
---|
121 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
122 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
---|
123 | try{ |
---|
124 | for ( int i = 0; i < nsteps; i++ ){ |
---|
125 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
126 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
127 | while ( lit.hasNext() ){ |
---|
128 | LinkAndPath crrlp = lit.next(); |
---|
129 | ClassLink[] classLinks = null; |
---|
130 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, true); |
---|
131 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
132 | if ( classLinks[j].getNumOfLinks() == 0 ){ continue; } |
---|
133 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
134 | crrpath.add(classLinks[j]); |
---|
135 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
136 | paths.add(new LinkedList<>(crrpath)); |
---|
137 | continue; |
---|
138 | } |
---|
139 | if (classLinks[j].getNumOfLinks() <= th ){ |
---|
140 | continue; //cut by the number of instances |
---|
141 | } |
---|
142 | // Divergence & Convergence Decision |
---|
143 | boolean con = false; |
---|
144 | boolean div = false; |
---|
145 | if ( decideConvergence(classLinks[j]) ){ // convergence |
---|
146 | con = true; |
---|
147 | } |
---|
148 | if ( decideDivergence(classLinks[j]) ){ // divergence |
---|
149 | div = true; |
---|
150 | } |
---|
151 | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge |
---|
152 | continue; // cut by the differences of entropies |
---|
153 | } |
---|
154 | // crr & next are the same arcs |
---|
155 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
---|
156 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
---|
157 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
---|
158 | continue; |
---|
159 | } |
---|
160 | |
---|
161 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), con)); |
---|
162 | } |
---|
163 | } |
---|
164 | lp = nextlp; |
---|
165 | } |
---|
166 | }catch(Exception e){ |
---|
167 | System.err.println(e); |
---|
168 | } |
---|
169 | return paths; |
---|
170 | } |
---|
171 | */ |
---|
172 | |
---|
173 | /* |
---|
174 | private boolean decideConvergence(ClassLink classLink){ |
---|
175 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
---|
176 | classLink.getNumOfLinkedInstances(), |
---|
177 | classLink.getNumOfLinks()); |
---|
178 | if ( con > concut ){ |
---|
179 | return true; |
---|
180 | } |
---|
181 | return false; |
---|
182 | } |
---|
183 | */ |
---|
184 | |
---|
185 | /* |
---|
186 | private boolean decideDivergence(ClassLink classLink){ |
---|
187 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
---|
188 | classLink.getNumOfLinkedInstances(), |
---|
189 | classLink.getNumOfLinks()); |
---|
190 | if ( con < divcut ){ |
---|
191 | return true; |
---|
192 | } |
---|
193 | return false; |
---|
194 | } |
---|
195 | */ |
---|
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 | */ |
---|
204 | } |
---|