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