| 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 | //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 | int prunecut; |
|---|
| 23 | |
|---|
| 24 | public class LinkAndPath{ |
|---|
| 25 | String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL |
|---|
| 26 | ClassLink classLink; |
|---|
| 27 | List<ClassLink> path; |
|---|
| 28 | //boolean converge; |
|---|
| 29 | |
|---|
| 30 | public LinkAndPath(ClassLink classLink, List<ClassLink> path){ |
|---|
| 31 | this.classLink = classLink; |
|---|
| 32 | this.path = path; |
|---|
| 33 | //this.converge = false; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI){ |
|---|
| 37 | this.classLink = classLink; |
|---|
| 38 | this.path = path; |
|---|
| 39 | this.originalClassURI = originalClassURI; |
|---|
| 40 | //this.converge = converge; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public OWLClassGraph(String startClass, String endClass){ |
|---|
| 45 | super(); |
|---|
| 46 | |
|---|
| 47 | // start & end |
|---|
| 48 | this.startClass = startClass; |
|---|
| 49 | this.endClass = endClass; |
|---|
| 50 | |
|---|
| 51 | // parameters |
|---|
| 52 | nsteps = 3; |
|---|
| 53 | limit = 1000; |
|---|
| 54 | prunecut = 100; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | public Path[] getPaths(RDFSchemaAnalyzer rdfsa, boolean countLink){ |
|---|
| 58 | List<List<ClassLink>> paths = null; |
|---|
| 59 | paths = searchPaths(rdfsa, countLink); |
|---|
| 60 | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
|---|
| 61 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
|---|
| 62 | int j = 0; |
|---|
| 63 | while ( pit.hasNext() ){ |
|---|
| 64 | Path path = new Path(); |
|---|
| 65 | path.setStartClass(startClass); |
|---|
| 66 | List<ClassLink> crrpath = pit.next(); |
|---|
| 67 | path.setClassLinks(crrpath); |
|---|
| 68 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
|---|
| 69 | int min = Integer.MAX_VALUE; |
|---|
| 70 | while ( cit.hasNext() ){ |
|---|
| 71 | ClassLink cl = cit.next(); |
|---|
| 72 | if ( cl.getNumOfLinks() < min ){ |
|---|
| 73 | min = cl.getNumOfLinks(); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | path.setWidth(min); |
|---|
| 77 | sortedpath.add(path); |
|---|
| 78 | j++; |
|---|
| 79 | } |
|---|
| 80 | Path[] patharray = new Path[paths.size()]; |
|---|
| 81 | Iterator<Path> pait = sortedpath.descendingIterator(); |
|---|
| 82 | int i = 0; |
|---|
| 83 | while ( pait.hasNext() ){ |
|---|
| 84 | patharray[i] = pait.next(); |
|---|
| 85 | i++; |
|---|
| 86 | } |
|---|
| 87 | return patharray; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ |
|---|
| 91 | List<List<ClassLink>> paths = new ArrayList<>(); |
|---|
| 92 | List<LinkAndPath> lp = new LinkedList<>(); |
|---|
| 93 | lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), "")); |
|---|
| 94 | try{ |
|---|
| 95 | for ( int i = 0; i < nsteps; i++ ){ |
|---|
| 96 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
|---|
| 97 | List<LinkAndPath> nextlp = new LinkedList<>(); |
|---|
| 98 | while ( lit.hasNext() ){ |
|---|
| 99 | LinkAndPath crrlp = lit.next(); |
|---|
| 100 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
|---|
| 101 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
|---|
| 102 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
|---|
| 103 | crrpath.add(classLinks[j]); |
|---|
| 104 | if ( classLinks[j].getLinkedClassURI() == null ){ continue; } |
|---|
| 105 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
|---|
| 106 | paths.add(new LinkedList<>(crrpath)); |
|---|
| 107 | continue; |
|---|
| 108 | } |
|---|
| 109 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
|---|
| 110 | continue; |
|---|
| 111 | } |
|---|
| 112 | if ( i >= 2 ){ |
|---|
| 113 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
|---|
| 114 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
|---|
| 115 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
|---|
| 116 | //System.out.println("P1"); |
|---|
| 117 | continue; |
|---|
| 118 | } |
|---|
| 119 | if ( checkPruning(crrlp.classLink, classLinks[j]) ){ |
|---|
| 120 | //System.out.println("P2"); |
|---|
| 121 | continue; |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI())); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | lp = nextlp; |
|---|
| 128 | } |
|---|
| 129 | }catch(Exception e){ |
|---|
| 130 | System.err.println(e); |
|---|
| 131 | } |
|---|
| 132 | return paths; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | //private |
|---|
| 136 | |
|---|
| 137 | /* |
|---|
| 138 | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ |
|---|
| 139 | List<List<ClassLink>> paths = new ArrayList<>(); |
|---|
| 140 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); |
|---|
| 141 | List<LinkAndPath> lp = new LinkedList<>(); |
|---|
| 142 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
|---|
| 143 | try{ |
|---|
| 144 | for ( int i = 0; i < nsteps; i++ ){ |
|---|
| 145 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
|---|
| 146 | List<LinkAndPath> nextlp = new LinkedList<>(); |
|---|
| 147 | while ( lit.hasNext() ){ |
|---|
| 148 | LinkAndPath crrlp = lit.next(); |
|---|
| 149 | ClassLink[] classLinks = null; |
|---|
| 150 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, true); |
|---|
| 151 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
|---|
| 152 | if ( classLinks[j].getNumOfLinks() == 0 ){ continue; } |
|---|
| 153 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
|---|
| 154 | crrpath.add(classLinks[j]); |
|---|
| 155 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
|---|
| 156 | paths.add(new LinkedList<>(crrpath)); |
|---|
| 157 | continue; |
|---|
| 158 | } |
|---|
| 159 | if (classLinks[j].getNumOfLinks() <= th ){ |
|---|
| 160 | continue; //cut by the number of instances |
|---|
| 161 | } |
|---|
| 162 | // Divergence & Convergence Decision |
|---|
| 163 | boolean con = false; |
|---|
| 164 | boolean div = false; |
|---|
| 165 | if ( decideConvergence(classLinks[j]) ){ // convergence |
|---|
| 166 | con = true; |
|---|
| 167 | } |
|---|
| 168 | if ( decideDivergence(classLinks[j]) ){ // divergence |
|---|
| 169 | div = true; |
|---|
| 170 | } |
|---|
| 171 | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge |
|---|
| 172 | continue; // cut by the differences of entropies |
|---|
| 173 | } |
|---|
| 174 | // crr & next are the same arcs |
|---|
| 175 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
|---|
| 176 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
|---|
| 177 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
|---|
| 178 | continue; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), con)); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | lp = nextlp; |
|---|
| 185 | } |
|---|
| 186 | }catch(Exception e){ |
|---|
| 187 | System.err.println(e); |
|---|
| 188 | } |
|---|
| 189 | return paths; |
|---|
| 190 | } |
|---|
| 191 | */ |
|---|
| 192 | |
|---|
| 193 | private boolean checkPruning(ClassLink classLink1, ClassLink classLink2){ |
|---|
| 194 | // true -> prune link2, false -> add link2 |
|---|
| 195 | int noi1 = classLink1.getNumOfOriginInstances(); |
|---|
| 196 | int nli1 = classLink1.getNumOfLinkedInstances(); |
|---|
| 197 | int noi2 = classLink2.getNumOfOriginInstances(); |
|---|
| 198 | int nli2 = classLink2.getNumOfLinkedInstances(); |
|---|
| 199 | if ( noi1 == 0 || nli1 == 0 || noi2 == 0 || nli2 == 0 ){ |
|---|
| 200 | return true; |
|---|
| 201 | } |
|---|
| 202 | return ( noi1 / nli1 > prunecut && nli2 / noi2 > prunecut ); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | /* private boolean decideConvergence(ClassLink classLink){ |
|---|
| 206 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
|---|
| 207 | classLink.getNumOfLinkedInstances(), |
|---|
| 208 | classLink.getNumOfLinks()); |
|---|
| 209 | if ( con > concut ){ |
|---|
| 210 | return true; |
|---|
| 211 | } |
|---|
| 212 | return false; |
|---|
| 213 | } |
|---|
| 214 | */ |
|---|
| 215 | |
|---|
| 216 | /* |
|---|
| 217 | private boolean decideDivergence(ClassLink classLink){ |
|---|
| 218 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
|---|
| 219 | classLink.getNumOfLinkedInstances(), |
|---|
| 220 | classLink.getNumOfLinks()); |
|---|
| 221 | if ( con < divcut ){ |
|---|
| 222 | return true; |
|---|
| 223 | } |
|---|
| 224 | return false; |
|---|
| 225 | } |
|---|
| 226 | */ |
|---|
| 227 | |
|---|
| 228 | /* |
|---|
| 229 | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
|---|
| 230 | //return (double) numOfLinks / (double) numOfLinkedInstances ; |
|---|
| 231 | // Convergence plus, Divergence minus |
|---|
| 232 | return Math.log((double)numOfOriginInstances) - Math.log((double)numOfLinkedInstances); |
|---|
| 233 | } |
|---|
| 234 | */ |
|---|
| 235 | |
|---|
| 236 | public void setWholeGraph(RDFSchemaAnalyzer rdfsa){ |
|---|
| 237 | // setNodes |
|---|
| 238 | SClass[] classes = null; |
|---|
| 239 | try{ |
|---|
| 240 | classes = rdfsa.getOWLClasses(null, null, null, true); |
|---|
| 241 | }catch(Exception e){ |
|---|
| 242 | System.err.println(e); return; |
|---|
| 243 | } |
|---|
| 244 | for (int i = 0 ; i < classes.length; i++){ |
|---|
| 245 | addNode(classes[i].getClassURI()); |
|---|
| 246 | } |
|---|
| 247 | // setEdges |
|---|
| 248 | for (int i = 0 ; i < classes.length; i++){ |
|---|
| 249 | try{ |
|---|
| 250 | ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true); |
|---|
| 251 | for (int j = 0 ; j < classLinks.length; j++){ |
|---|
| 252 | addEdge(i, labelednodes.get(classLinks[j].getLinkedClassURI()), |
|---|
| 253 | classLinks[j].getPropertyURI(), |
|---|
| 254 | classLinks[j].getDirection(), |
|---|
| 255 | classLinks[j].getNumOfLinkedInstances()); |
|---|
| 256 | } |
|---|
| 257 | }catch(Exception e){ |
|---|
| 258 | System.err.println(e); |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | } |
|---|