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