| 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 | this.startClass = startClass; |
|---|
| 47 | addNode(startClass); |
|---|
| 48 | this.endClass = endClass; |
|---|
| 49 | addNode(endClass); |
|---|
| 50 | nsteps = 3; |
|---|
| 51 | limit = 1000; |
|---|
| 52 | prunecut = 100; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public Path[] getPaths(RDFSchemaAnalyzer rdfsa, boolean countLink){ |
|---|
| 56 | List<List<ClassLink>> paths = null; |
|---|
| 57 | paths = searchPaths(rdfsa, countLink); |
|---|
| 58 | NavigableSet<Path> sortedpath = new TreeSet<Path>(); |
|---|
| 59 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
|---|
| 60 | while ( pit.hasNext() ){ |
|---|
| 61 | Path path = new Path(); |
|---|
| 62 | path.setStartClass(startClass); |
|---|
| 63 | List<ClassLink> crrpath = pit.next(); |
|---|
| 64 | path.setClassLinks(crrpath); |
|---|
| 65 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
|---|
| 66 | int min = Integer.MAX_VALUE; |
|---|
| 67 | while ( cit.hasNext() ){ |
|---|
| 68 | ClassLink cl = cit.next(); |
|---|
| 69 | if ( cl.getNumOfLinks() < min ){ |
|---|
| 70 | min = cl.getNumOfLinks(); |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | path.setWidth(min); |
|---|
| 74 | sortedpath.add(path); |
|---|
| 75 | } |
|---|
| 76 | Path[] patharray = new Path[paths.size()]; |
|---|
| 77 | Iterator<Path> pait = sortedpath.descendingIterator(); |
|---|
| 78 | int i = 0; |
|---|
| 79 | while ( pait.hasNext() ){ |
|---|
| 80 | patharray[i] = pait.next(); |
|---|
| 81 | i++; |
|---|
| 82 | } |
|---|
| 83 | return patharray; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){ |
|---|
| 87 | List<List<ClassLink>> paths = new ArrayList<>(); |
|---|
| 88 | List<LinkAndPath> lp = new LinkedList<>(); |
|---|
| 89 | lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), "")); |
|---|
| 90 | try{ |
|---|
| 91 | for ( int i = 0; i < nsteps; i++ ){ |
|---|
| 92 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
|---|
| 93 | List<LinkAndPath> nextlp = new LinkedList<>(); |
|---|
| 94 | while ( lit.hasNext() ){ |
|---|
| 95 | LinkAndPath crrlp = lit.next(); |
|---|
| 96 | /*if ( crrlp.classLink.getLinkedClassURI().equals("http://www.biopax.org/release/biopax-level3.owl#Pathway") ){ |
|---|
| 97 | System.out.println("here!"); |
|---|
| 98 | }*/ |
|---|
| 99 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
|---|
| 100 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
|---|
| 101 | /*if ( classLinks[j].getLinkedClassURI().endsWith("http://www.biopax.org/release/biopax-level3.owl#BiochemicalReaction") ){ |
|---|
| 102 | ClassLink cltmp = classLinks[j]; |
|---|
| 103 | }*/ |
|---|
| 104 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
|---|
| 105 | crrpath.add(classLinks[j]); |
|---|
| 106 | if ( classLinks[j].getLinkedClassURI() == null ){ continue; } |
|---|
| 107 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
|---|
| 108 | paths.add(new LinkedList<>(crrpath)); |
|---|
| 109 | continue; |
|---|
| 110 | } |
|---|
| 111 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
|---|
| 112 | continue; |
|---|
| 113 | } |
|---|
| 114 | if ( i >= 2 ){ |
|---|
| 115 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && |
|---|
| 116 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && |
|---|
| 117 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ |
|---|
| 118 | System.out.println("P1"); |
|---|
| 119 | continue; |
|---|
| 120 | } |
|---|
| 121 | if ( checkPruning(crrlp.classLink, classLinks[j]) ){ |
|---|
| 122 | System.out.println("P2"); |
|---|
| 123 | continue; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | /* |
|---|
| 127 | if ( classLinks[j].getDirection() != Direction.reverse ){ |
|---|
| 128 | System.out.println("here b"); |
|---|
| 129 | }*/ |
|---|
| 130 | |
|---|
| 131 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI())); |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | lp = nextlp; |
|---|
| 135 | } |
|---|
| 136 | }catch(Exception e){ |
|---|
| 137 | System.err.println(e); |
|---|
| 138 | } |
|---|
| 139 | return paths; |
|---|
| 140 | } |
|---|
| 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 | |
|---|
| 198 | private boolean checkPruning(ClassLink classLink1, ClassLink classLink2){ |
|---|
| 199 | // true -> prune link2, false -> add link2 |
|---|
| 200 | int noi1 = classLink1.getNumOfOriginInstances(); |
|---|
| 201 | int nli1 = classLink1.getNumOfLinkedInstances(); |
|---|
| 202 | int noi2 = classLink2.getNumOfOriginInstances(); |
|---|
| 203 | int nli2 = classLink2.getNumOfLinkedInstances(); |
|---|
| 204 | if ( noi1 == 0 || nli1 == 0 || noi2 == 0 || nli2 == 0 ){ |
|---|
| 205 | return true; |
|---|
| 206 | } |
|---|
| 207 | return ( noi1 / nli1 > prunecut && nli2 / noi2 > prunecut ); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /* private boolean decideConvergence(ClassLink classLink){ |
|---|
| 211 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
|---|
| 212 | classLink.getNumOfLinkedInstances(), |
|---|
| 213 | classLink.getNumOfLinks()); |
|---|
| 214 | if ( con > concut ){ |
|---|
| 215 | return true; |
|---|
| 216 | } |
|---|
| 217 | return false; |
|---|
| 218 | } |
|---|
| 219 | */ |
|---|
| 220 | |
|---|
| 221 | /* |
|---|
| 222 | private boolean decideDivergence(ClassLink classLink){ |
|---|
| 223 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), |
|---|
| 224 | classLink.getNumOfLinkedInstances(), |
|---|
| 225 | classLink.getNumOfLinks()); |
|---|
| 226 | if ( con < divcut ){ |
|---|
| 227 | return true; |
|---|
| 228 | } |
|---|
| 229 | return false; |
|---|
| 230 | } |
|---|
| 231 | */ |
|---|
| 232 | |
|---|
| 233 | /* |
|---|
| 234 | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
|---|
| 235 | //return (double) numOfLinks / (double) numOfLinkedInstances ; |
|---|
| 236 | // Convergence plus, Divergence minus |
|---|
| 237 | return Math.log((double)numOfOriginInstances) - Math.log((double)numOfLinkedInstances); |
|---|
| 238 | } |
|---|
| 239 | */ |
|---|
| 240 | |
|---|
| 241 | } |
|---|