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