| 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, boolean converge){ | 
|---|
| 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>(), "", false)); | 
|---|
| 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 | ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); | 
|---|
| 97 | for ( int j = 0 ; j < classLinks.length; j++ ){ | 
|---|
| 98 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); | 
|---|
| 99 | crrpath.add(classLinks[j]); | 
|---|
| 100 | if ( classLinks[j].getLinkedClassURI() == null ){ continue; } | 
|---|
| 101 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ | 
|---|
| 102 | paths.add(new LinkedList<>(crrpath)); | 
|---|
| 103 | continue; | 
|---|
| 104 | } | 
|---|
| 105 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ | 
|---|
| 106 | continue; | 
|---|
| 107 | } | 
|---|
| 108 | if ( i >= 2 ){ | 
|---|
| 109 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && | 
|---|
| 110 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && | 
|---|
| 111 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ | 
|---|
| 112 | System.out.println("P1"); | 
|---|
| 113 | continue; | 
|---|
| 114 | } | 
|---|
| 115 | if ( checkPruning(crrlp.classLink, classLinks[j]) ){ | 
|---|
| 116 | System.out.println("P2"); | 
|---|
| 117 | continue; | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), false)); | 
|---|
| 122 | } | 
|---|
| 123 | } | 
|---|
| 124 | lp = nextlp; | 
|---|
| 125 | } | 
|---|
| 126 | }catch(Exception e){ | 
|---|
| 127 | System.err.println(e); | 
|---|
| 128 | } | 
|---|
| 129 | return paths; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | /* | 
|---|
| 133 | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ | 
|---|
| 134 | List<List<ClassLink>> paths = new ArrayList<>(); | 
|---|
| 135 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0,0,0); | 
|---|
| 136 | List<LinkAndPath> lp = new LinkedList<>(); | 
|---|
| 137 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); | 
|---|
| 138 | try{ | 
|---|
| 139 | for ( int i = 0; i < nsteps; i++ ){ | 
|---|
| 140 | ListIterator<LinkAndPath> lit = lp.listIterator(); | 
|---|
| 141 | List<LinkAndPath> nextlp = new LinkedList<>(); | 
|---|
| 142 | while ( lit.hasNext() ){ | 
|---|
| 143 | LinkAndPath crrlp = lit.next(); | 
|---|
| 144 | ClassLink[] classLinks = null; | 
|---|
| 145 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, true); | 
|---|
| 146 | for ( int j = 0 ; j < classLinks.length; j++ ){ | 
|---|
| 147 | if ( classLinks[j].getNumOfLinks() == 0 ){ continue; } | 
|---|
| 148 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); | 
|---|
| 149 | crrpath.add(classLinks[j]); | 
|---|
| 150 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ | 
|---|
| 151 | paths.add(new LinkedList<>(crrpath)); | 
|---|
| 152 | continue; | 
|---|
| 153 | } | 
|---|
| 154 | if (classLinks[j].getNumOfLinks() <= th ){ | 
|---|
| 155 | continue; //cut by the number of instances | 
|---|
| 156 | } | 
|---|
| 157 | // Divergence & Convergence Decision | 
|---|
| 158 | boolean con = false; | 
|---|
| 159 | boolean div = false; | 
|---|
| 160 | if ( decideConvergence(classLinks[j]) ){ // convergence | 
|---|
| 161 | con = true; | 
|---|
| 162 | } | 
|---|
| 163 | if ( decideDivergence(classLinks[j]) ){ // divergence | 
|---|
| 164 | div = true; | 
|---|
| 165 | } | 
|---|
| 166 | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge | 
|---|
| 167 | continue; // cut by the differences of entropies | 
|---|
| 168 | } | 
|---|
| 169 | // crr & next are the same arcs | 
|---|
| 170 | if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) && | 
|---|
| 171 | crrlp.classLink.getDirection() != classLinks[j].getDirection() && | 
|---|
| 172 | crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){ | 
|---|
| 173 | continue; | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI(), con)); | 
|---|
| 177 | } | 
|---|
| 178 | } | 
|---|
| 179 | lp = nextlp; | 
|---|
| 180 | } | 
|---|
| 181 | }catch(Exception e){ | 
|---|
| 182 | System.err.println(e); | 
|---|
| 183 | } | 
|---|
| 184 | return paths; | 
|---|
| 185 | } | 
|---|
| 186 | */ | 
|---|
| 187 |  | 
|---|
| 188 | private boolean checkPruning(ClassLink classLink1, ClassLink classLink2){ | 
|---|
| 189 | // true -> prune link2, false -> add link2 | 
|---|
| 190 | int noi1 = classLink1.getNumOfOriginInstances(); | 
|---|
| 191 | int nli1 = classLink1.getNumOfLinkedInstances(); | 
|---|
| 192 | int noi2 = classLink2.getNumOfOriginInstances(); | 
|---|
| 193 | int nli2 = classLink2.getNumOfLinkedInstances(); | 
|---|
| 194 | if ( noi1 == 0 || nli1 == 0 || noi2 == 0 || nli2 == 0 ){ | 
|---|
| 195 | return true; | 
|---|
| 196 | } | 
|---|
| 197 | return ( noi1 / nli1 > prunecut && nli2 / noi2 > prunecut ); | 
|---|
| 198 | } | 
|---|
| 199 |  | 
|---|
| 200 | /*    private boolean decideConvergence(ClassLink classLink){ | 
|---|
| 201 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), | 
|---|
| 202 | classLink.getNumOfLinkedInstances(), | 
|---|
| 203 | classLink.getNumOfLinks()); | 
|---|
| 204 | if ( con > concut  ){ | 
|---|
| 205 | return true; | 
|---|
| 206 | } | 
|---|
| 207 | return false; | 
|---|
| 208 | } | 
|---|
| 209 | */ | 
|---|
| 210 |  | 
|---|
| 211 | /* | 
|---|
| 212 | private boolean decideDivergence(ClassLink classLink){ | 
|---|
| 213 | double con = getValueForConvergence(classLink.getNumOfOriginInstances(), | 
|---|
| 214 | classLink.getNumOfLinkedInstances(), | 
|---|
| 215 | classLink.getNumOfLinks()); | 
|---|
| 216 | if ( con < divcut  ){ | 
|---|
| 217 | return true; | 
|---|
| 218 | } | 
|---|
| 219 | return false; | 
|---|
| 220 | } | 
|---|
| 221 | */ | 
|---|
| 222 |  | 
|---|
| 223 | /* | 
|---|
| 224 | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ | 
|---|
| 225 | //return (double) numOfLinks / (double) numOfLinkedInstances ; | 
|---|
| 226 | // Convergence plus, Divergence minus | 
|---|
| 227 | return Math.log((double)numOfOriginInstances) - Math.log((double)numOfLinkedInstances); | 
|---|
| 228 | } | 
|---|
| 229 | */ | 
|---|
| 230 |  | 
|---|
| 231 | } | 
|---|