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