[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 | */ |
---|
| 11 | import java.util.*; |
---|
| 12 | |
---|
| 13 | public class OWLClassGraph extends LabeledMultiDigraph{ |
---|
| 14 | String startClass; |
---|
| 15 | String endClass; |
---|
| 16 | int nsteps; |
---|
| 17 | int limit; |
---|
[34] | 18 | int th; |
---|
[47] | 19 | double concut; |
---|
| 20 | double divcut; |
---|
[2] | 21 | |
---|
| 22 | public class LinkAndPath{ |
---|
| 23 | ClassLink classLink; |
---|
| 24 | List<ClassLink> path; |
---|
[45] | 25 | boolean converge; |
---|
[2] | 26 | public LinkAndPath(ClassLink classLink, List<ClassLink> path){ |
---|
| 27 | this.classLink = classLink; |
---|
| 28 | this.path = path; |
---|
[45] | 29 | this.converge = false; |
---|
[2] | 30 | } |
---|
[45] | 31 | |
---|
| 32 | public LinkAndPath(ClassLink classLink, List<ClassLink> path, boolean converge){ |
---|
| 33 | this.classLink = classLink; |
---|
| 34 | this.path = path; |
---|
| 35 | this.converge = converge; |
---|
| 36 | } |
---|
[2] | 37 | } |
---|
| 38 | |
---|
[3] | 39 | public OWLClassGraph(String startClass, String endClass){ |
---|
[2] | 40 | super(); |
---|
[3] | 41 | this.startClass = startClass; |
---|
| 42 | addNode(startClass); |
---|
| 43 | this.endClass = endClass; |
---|
| 44 | addNode(endClass); |
---|
[41] | 45 | nsteps = 3; |
---|
[3] | 46 | limit = 1000; |
---|
[34] | 47 | th = 0; |
---|
[47] | 48 | concut = 2.0; |
---|
| 49 | divcut = 2.0; |
---|
[2] | 50 | } |
---|
| 51 | |
---|
[3] | 52 | public void generateGraph(List<List<ClassLink>> paths){ |
---|
[15] | 53 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
| 54 | while( pit.hasNext() ){ |
---|
[21] | 55 | List<ClassLink> cls = pit.next(); |
---|
| 56 | String start = startClass; |
---|
| 57 | ListIterator<ClassLink> cit = cls.listIterator(); |
---|
| 58 | while ( cit.hasNext() ){ |
---|
| 59 | // KOKO |
---|
| 60 | } |
---|
[15] | 61 | } |
---|
[2] | 62 | } |
---|
| 63 | |
---|
[29] | 64 | public Path[] getPaths(OWLQueryBuilderImpl qb, int mode, boolean countLink){ |
---|
[47] | 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 | } |
---|
[2] | 74 | Path[] patharray = new Path[paths.size()]; |
---|
| 75 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
| 76 | int i = 0; |
---|
| 77 | while ( pit.hasNext() ){ |
---|
[3] | 78 | patharray[i] = new Path(); |
---|
[2] | 79 | patharray[i].setStartClass(startClass); |
---|
| 80 | List<ClassLink> path = pit.next(); |
---|
| 81 | patharray[i].setClassLinks(path); |
---|
[8] | 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); |
---|
[2] | 91 | i++; |
---|
| 92 | } |
---|
| 93 | return patharray; |
---|
| 94 | } |
---|
[45] | 95 | |
---|
[32] | 96 | private List<List<ClassLink>> searchPaths(OWLQueryBuilderImpl qb, int mode, boolean countLinks){ |
---|
[15] | 97 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
[47] | 98 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0); |
---|
[15] | 99 | List<LinkAndPath> lp = new LinkedList<>(); |
---|
[2] | 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(); |
---|
[15] | 104 | List<LinkAndPath> nextlp = new LinkedList<>(); |
---|
[2] | 105 | while ( lit.hasNext() ){ |
---|
| 106 | LinkAndPath crrlp = lit.next(); |
---|
[3] | 107 | ClassLink[] classLinks = null; |
---|
| 108 | // Mode |
---|
| 109 | if ( mode == 0 ){ |
---|
[32] | 110 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
---|
[3] | 111 | }else if ( mode == 1 ){ |
---|
[41] | 112 | classLinks = qb.getNextClassViaInstanceLink(null, crrlp.classLink.getLinkedClassURI(), limit); |
---|
[3] | 113 | }else{ System.err.println("Mode is not correct."); } |
---|
[2] | 114 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
[15] | 115 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
---|
[2] | 116 | crrpath.add(classLinks[j]); |
---|
| 117 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
[15] | 118 | paths.add(new LinkedList<>(crrpath)); |
---|
[34] | 119 | continue; |
---|
[2] | 120 | } |
---|
[34] | 121 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
---|
| 122 | continue; |
---|
| 123 | } |
---|
[2] | 124 | nextlp.add(new LinkAndPath(classLinks[j],crrpath)); |
---|
| 125 | } |
---|
| 126 | } |
---|
[3] | 127 | lp = nextlp; |
---|
[2] | 128 | } |
---|
[15] | 129 | }catch(Exception e){ |
---|
| 130 | System.err.println(e); |
---|
| 131 | } |
---|
[21] | 132 | return paths; |
---|
[2] | 133 | } |
---|
[45] | 134 | |
---|
| 135 | private List<List<ClassLink>> searchPathsWithCut(OWLQueryBuilderImpl qb){ |
---|
| 136 | List<List<ClassLink>> paths = new ArrayList<>(); |
---|
[47] | 137 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0,0,0); |
---|
[45] | 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; |
---|
[47] | 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 ){ // converge |
---|
[45] | 164 | con = true; |
---|
| 165 | } |
---|
[47] | 166 | if ( divv > divcut ){ |
---|
| 167 | div = true; |
---|
| 168 | } |
---|
| 169 | if ( crrlp.converge == true && div == true ){ // converge & 縲diverge |
---|
[45] | 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 | } |
---|
[47] | 182 | |
---|
| 183 | private double getValueForConvergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
---|
| 184 | return (double) numOfLinks / (double) numOfLinkedInstances ; |
---|
| 185 | } |
---|
[45] | 186 | |
---|
[47] | 187 | private double getValueForDivergence(int numOfOriginInstances, int numOfLinkedInstances, int numOfLinks){ |
---|
| 188 | return (double) numOfLinks / (double) numOfOriginInstances ; |
---|
| 189 | } |
---|
[2] | 190 | } |
---|