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