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