| 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 | |
|---|
| 20 | public class LinkAndPath{ |
|---|
| 21 | ClassLink classLink; |
|---|
| 22 | List<ClassLink> path; |
|---|
| 23 | public LinkAndPath(ClassLink classLink, List<ClassLink> path){ |
|---|
| 24 | this.classLink = classLink; |
|---|
| 25 | this.path = path; |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | public OWLClassGraph(String startClass, String endClass){ |
|---|
| 30 | super(); |
|---|
| 31 | this.startClass = startClass; |
|---|
| 32 | addNode(startClass); |
|---|
| 33 | this.endClass = endClass; |
|---|
| 34 | addNode(endClass); |
|---|
| 35 | nsteps = 2; |
|---|
| 36 | limit = 1000; |
|---|
| 37 | th = 0; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | public void generateGraph(List<List<ClassLink>> paths){ |
|---|
| 41 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
|---|
| 42 | while( pit.hasNext() ){ |
|---|
| 43 | List<ClassLink> cls = pit.next(); |
|---|
| 44 | String start = startClass; |
|---|
| 45 | ListIterator<ClassLink> cit = cls.listIterator(); |
|---|
| 46 | while ( cit.hasNext() ){ |
|---|
| 47 | // KOKO |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public Path[] getPaths(OWLQueryBuilderImpl qb, int mode, boolean countLink){ |
|---|
| 53 | List<List<ClassLink>> paths = searchPaths(qb, mode, countLink); |
|---|
| 54 | Path[] patharray = new Path[paths.size()]; |
|---|
| 55 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
|---|
| 56 | int i = 0; |
|---|
| 57 | while ( pit.hasNext() ){ |
|---|
| 58 | patharray[i] = new Path(); |
|---|
| 59 | patharray[i].setStartClass(startClass); |
|---|
| 60 | List<ClassLink> path = pit.next(); |
|---|
| 61 | patharray[i].setClassLinks(path); |
|---|
| 62 | ListIterator<ClassLink> cit = path.listIterator(); |
|---|
| 63 | int min = Integer.MAX_VALUE; |
|---|
| 64 | while ( cit.hasNext() ){ |
|---|
| 65 | ClassLink cl = cit.next(); |
|---|
| 66 | if ( cl.getNumOfLinks() < min ){ |
|---|
| 67 | min = cl.getNumOfLinks(); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | patharray[i].setWidth(min); |
|---|
| 71 | i++; |
|---|
| 72 | } |
|---|
| 73 | return patharray; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /* |
|---|
| 77 | private List<List<ClassLink>> searchPathsFromOWL(OWLQueryBuilderImpl qb){ |
|---|
| 78 | return searchPathsEngine(qb, 0); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | private List<List<ClassLink>> searchPathsFromInstances(OWLQueryBuilderImpl qb){ |
|---|
| 82 | return searchPathsEngine(qb, 1); |
|---|
| 83 | } |
|---|
| 84 | */ |
|---|
| 85 | |
|---|
| 86 | private List<List<ClassLink>> searchPaths(OWLQueryBuilderImpl qb, int mode, boolean countLinks){ |
|---|
| 87 | List<List<ClassLink>> paths = new ArrayList<>(); |
|---|
| 88 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both,0); |
|---|
| 89 | List<LinkAndPath> lp = new LinkedList<>(); |
|---|
| 90 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
|---|
| 91 | try{ |
|---|
| 92 | for ( int i = 0; i < nsteps; i++ ){ |
|---|
| 93 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
|---|
| 94 | List<LinkAndPath> nextlp = new LinkedList<>(); |
|---|
| 95 | while ( lit.hasNext() ){ |
|---|
| 96 | LinkAndPath crrlp = lit.next(); |
|---|
| 97 | ClassLink[] classLinks = null; |
|---|
| 98 | // Mode |
|---|
| 99 | if ( mode == 0 ){ |
|---|
| 100 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks); |
|---|
| 101 | }else if ( mode == 1 ){ |
|---|
| 102 | classLinks = qb.getNextClassViaInstanceLink(null, crrlp.classLink.getLinkedClassURI(), limit); |
|---|
| 103 | }else{ System.err.println("Mode is not correct."); } |
|---|
| 104 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
|---|
| 105 | List<ClassLink> crrpath = new LinkedList<>(crrlp.path); |
|---|
| 106 | crrpath.add(classLinks[j]); |
|---|
| 107 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
|---|
| 108 | paths.add(new LinkedList<>(crrpath)); |
|---|
| 109 | continue; |
|---|
| 110 | } |
|---|
| 111 | if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){ |
|---|
| 112 | continue; |
|---|
| 113 | } |
|---|
| 114 | nextlp.add(new LinkAndPath(classLinks[j],crrpath)); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | lp = nextlp; |
|---|
| 118 | } |
|---|
| 119 | }catch(Exception e){ |
|---|
| 120 | System.err.println(e); |
|---|
| 121 | } |
|---|
| 122 | return paths; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|