| 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 | int nsteps = 4; |
|---|
| 15 | int limit = 100; |
|---|
| 16 | |
|---|
| 17 | List<String> nodeType; |
|---|
| 18 | String sparqlEndpoint; |
|---|
| 19 | |
|---|
| 20 | public OWLClassGraph(){ // not used |
|---|
| 21 | super(); |
|---|
| 22 | nodeType = new LinkedList<String>(); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ // for experiment |
|---|
| 26 | super(); |
|---|
| 27 | nodeType = new LinkedList<String>(); |
|---|
| 28 | setClassGraph(rdfsa); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | public OWLClassGraph(RDFSchemaAnalyzerFactory rdfsaf){ |
|---|
| 32 | super(); |
|---|
| 33 | nodeType = new LinkedList<String>(); |
|---|
| 34 | //setClassGraph(rdfsaf); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | public Path[] getPaths(String startClass, String endClass){ |
|---|
| 38 | List<List<ClassLink>> paths = searchPaths(startClass, endClass); |
|---|
| 39 | |
|---|
| 40 | List<Path> sortedpaths = new LinkedList<Path>(); |
|---|
| 41 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
|---|
| 42 | int j = 0; |
|---|
| 43 | while ( pit.hasNext() ){ |
|---|
| 44 | Path path = new Path(); |
|---|
| 45 | path.setStartClass(startClass); |
|---|
| 46 | List<ClassLink> crrpath = pit.next(); |
|---|
| 47 | path.setClassLinks(crrpath); |
|---|
| 48 | ListIterator<ClassLink> cit = crrpath.listIterator(); |
|---|
| 49 | int min = Integer.MAX_VALUE; |
|---|
| 50 | while ( cit.hasNext() ){ |
|---|
| 51 | ClassLink cl = cit.next(); |
|---|
| 52 | if ( cl.getNumOfLinks() < min ){ |
|---|
| 53 | min = cl.getNumOfLinks(); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | // using length of path |
|---|
| 57 | path.setWidth(500000 - crrpath.size()*100000 + min); |
|---|
| 58 | sortedpaths.add(path); |
|---|
| 59 | j++; |
|---|
| 60 | } |
|---|
| 61 | Path[] patharray = new Path[paths.size()]; |
|---|
| 62 | Collections.sort(sortedpaths); |
|---|
| 63 | Iterator<Path> pait = sortedpaths.listIterator(); |
|---|
| 64 | int i = 0; |
|---|
| 65 | while ( pait.hasNext() ){ |
|---|
| 66 | patharray[paths.size()-i-1] = pait.next(); |
|---|
| 67 | i++; |
|---|
| 68 | } |
|---|
| 69 | return patharray; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | private List<List<ClassLink>> searchPaths(String startClass, String endClass){ |
|---|
| 73 | List<List<ClassLink>> paths = new ArrayList<>(); |
|---|
| 74 | Integer snode = labelednodes.get(startClass); |
|---|
| 75 | Integer enode = labelednodes.get(endClass); |
|---|
| 76 | List<List<Integer>> simplePaths = searchSimplePaths(snode, enode); |
|---|
| 77 | |
|---|
| 78 | ListIterator<List<Integer>> pit = simplePaths.listIterator(); |
|---|
| 79 | while( pit.hasNext()){ |
|---|
| 80 | List<Integer> spath = pit.next(); |
|---|
| 81 | List<List<ClassLink>> convertedPaths = convertSimplePathToPaths(spath); |
|---|
| 82 | paths.addAll(convertedPaths); |
|---|
| 83 | } |
|---|
| 84 | //System.out.println("PATH:"); |
|---|
| 85 | //System.out.println(paths.size()); |
|---|
| 86 | return paths; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | private List<List<Integer>> searchSimplePaths(Integer snode, Integer enode){ |
|---|
| 90 | List<List<Integer>> simplePaths = new LinkedList<>(); |
|---|
| 91 | List<List<Integer>> lp = new LinkedList<>(); |
|---|
| 92 | List<Integer> ini = new LinkedList<Integer>(); // initial path |
|---|
| 93 | ini.add(snode); |
|---|
| 94 | lp.add(ini); |
|---|
| 95 | for (int i = 0; i < nsteps; i++ ){ |
|---|
| 96 | ListIterator<List<Integer>> lit = lp.listIterator(); |
|---|
| 97 | List<List<Integer>> nextlp = new LinkedList<>(); |
|---|
| 98 | while ( lit.hasNext() ){ |
|---|
| 99 | List<Integer> crrpath = lit.next(); |
|---|
| 100 | Integer crrnode = crrpath.get(crrpath.size()-1); |
|---|
| 101 | Set<Integer> nexts = labelededges.get(crrnode).keySet(); |
|---|
| 102 | Iterator<Integer> nit = nexts.iterator(); |
|---|
| 103 | while( nit.hasNext() ){ |
|---|
| 104 | Integer nextnode = nit.next(); |
|---|
| 105 | if ( crrpath.contains(nextnode) ){ continue; } |
|---|
| 106 | List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy |
|---|
| 107 | nextpath.add(nextnode); |
|---|
| 108 | if ( nextnode.equals(enode) ){ |
|---|
| 109 | simplePaths.add(nextpath); |
|---|
| 110 | continue; |
|---|
| 111 | } |
|---|
| 112 | nextlp.add(nextpath); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | lp = nextlp; |
|---|
| 116 | } |
|---|
| 117 | return simplePaths; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | private List<List<ClassLink>> convertSimplePathToPaths(List<Integer> simplePath){ |
|---|
| 122 | List<List<ClassLink>> paths = new LinkedList<List<ClassLink>>(); |
|---|
| 123 | ListIterator<Integer> spit = simplePath.listIterator(); |
|---|
| 124 | Integer start = spit.next(); |
|---|
| 125 | String startClass = this.labels.get(start); |
|---|
| 126 | Integer end = spit.next(); |
|---|
| 127 | List<LabeledEdge> edges = labelededges.get(start).get(end); |
|---|
| 128 | ListIterator<LabeledEdge> eit = edges.listIterator(); |
|---|
| 129 | while ( eit.hasNext() ){ |
|---|
| 130 | List<ClassLink> cl = new LinkedList<ClassLink>(); |
|---|
| 131 | cl.add((ClassLink)eit.next().getLabel()); |
|---|
| 132 | paths.add(cl); |
|---|
| 133 | } |
|---|
| 134 | start = end; |
|---|
| 135 | while( spit.hasNext() ){ |
|---|
| 136 | end = spit.next(); |
|---|
| 137 | // start-end |
|---|
| 138 | edges = labelededges.get(start).get(end); |
|---|
| 139 | List<List<ClassLink>> tmppaths = new LinkedList<List<ClassLink>>(); |
|---|
| 140 | // current path |
|---|
| 141 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
|---|
| 142 | while ( pit.hasNext() ){ |
|---|
| 143 | List<ClassLink> basepath = pit.next(); |
|---|
| 144 | eit = edges.listIterator(); |
|---|
| 145 | while ( eit.hasNext() ){ |
|---|
| 146 | ClassLink cl = (ClassLink) eit.next().label; |
|---|
| 147 | List<ClassLink> addedpath = new LinkedList<ClassLink>(basepath); |
|---|
| 148 | addedpath.add(cl); |
|---|
| 149 | tmppaths.add(addedpath); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | paths = tmppaths; |
|---|
| 153 | start = end; |
|---|
| 154 | } |
|---|
| 155 | return paths; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | private void setClassGraph(RDFSchemaAnalyzer rdfsa){ |
|---|
| 159 | // setNodes |
|---|
| 160 | SClass[] classes = null; |
|---|
| 161 | try{ |
|---|
| 162 | classes = rdfsa.getOWLClasses(null, null); |
|---|
| 163 | }catch(Exception e){ |
|---|
| 164 | System.err.println(e); return; |
|---|
| 165 | } |
|---|
| 166 | for (int i = 0 ; i < classes.length; i++){ |
|---|
| 167 | addNode(classes[i].getClassURI()); |
|---|
| 168 | nodeType.add("class"); |
|---|
| 169 | } |
|---|
| 170 | // setEdges |
|---|
| 171 | for (int i = 0 ; i < classes.length; i++ ){ |
|---|
| 172 | try{ |
|---|
| 173 | ClassLink[] classLinks = rdfsa.getNextClass(classes[i].getClassURI(), limit); |
|---|
| 174 | for (int j = 0 ; j < classLinks.length; j++){ |
|---|
| 175 | Integer n = labelednodes.get(classLinks[j].getLinkedClassURI()); |
|---|
| 176 | if ( n != null ){ |
|---|
| 177 | addEdge(i, n, classLinks[j]); |
|---|
| 178 | }else{ |
|---|
| 179 | n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI()); |
|---|
| 180 | if ( n == null ){ |
|---|
| 181 | addNode(classLinks[j].getLinkedLiteralDatatypeURI()); |
|---|
| 182 | n = nodeType.size(); |
|---|
| 183 | nodeType.add("literal"); |
|---|
| 184 | } |
|---|
| 185 | addEdge(i, n, classLinks[j]); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | }catch(Exception e){ |
|---|
| 189 | System.err.println(e); |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|