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 | |
---|
40 | } |
---|
41 | |
---|
42 | public Path[] getPaths(OWLQueryBuilderImpl qb){ |
---|
43 | //List<List<ClassLink>> paths = searchPathsFromOWL(qb); |
---|
44 | List<List<ClassLink>> paths = searchPathsFromInstances(qb); |
---|
45 | Path[] patharray = new Path[paths.size()]; |
---|
46 | ListIterator<List<ClassLink>> pit = paths.listIterator(); |
---|
47 | int i = 0; |
---|
48 | while ( pit.hasNext() ){ |
---|
49 | patharray[i] = new Path(); |
---|
50 | patharray[i].setStartClass(startClass); |
---|
51 | List<ClassLink> path = pit.next(); |
---|
52 | patharray[i].setClassLinks(path); |
---|
53 | i++; |
---|
54 | } |
---|
55 | return patharray; |
---|
56 | } |
---|
57 | |
---|
58 | private List<List<ClassLink>> searchPathsFromOWL(OWLQueryBuilderImpl qb){ |
---|
59 | return searchPathsEngine(qb, 0); |
---|
60 | } |
---|
61 | |
---|
62 | private List<List<ClassLink>> searchPathsFromInstances(OWLQueryBuilderImpl qb){ |
---|
63 | return searchPathsEngine(qb, 1); |
---|
64 | } |
---|
65 | |
---|
66 | private List<List<ClassLink>> searchPathsEngine(OWLQueryBuilderImpl qb, int mode){ |
---|
67 | List<List<ClassLink>> paths = new ArrayList<List<ClassLink>>(); |
---|
68 | ClassLink crrLink = new ClassLink(null,startClass,Direction.both); |
---|
69 | List<LinkAndPath> lp = new LinkedList<LinkAndPath>(); |
---|
70 | lp.add(new LinkAndPath(crrLink, new LinkedList<ClassLink>())); |
---|
71 | try{ |
---|
72 | for ( int i = 0; i < nsteps; i++ ){ |
---|
73 | ListIterator<LinkAndPath> lit = lp.listIterator(); |
---|
74 | List<LinkAndPath> nextlp = new LinkedList<LinkAndPath>(); |
---|
75 | while ( lit.hasNext() ){ |
---|
76 | LinkAndPath crrlp = lit.next(); |
---|
77 | if ( crrlp.classLink.getLinkedClassURI().equals(endClass) ){ continue; } |
---|
78 | ClassLink[] classLinks = null; |
---|
79 | // Mode |
---|
80 | if ( mode == 0 ){ |
---|
81 | classLinks = qb.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit); |
---|
82 | }else if ( mode == 1 ){ |
---|
83 | classLinks = qb.getNextClassViaInstanceLink(null, crrlp.classLink.getLinkedClassURI(), limit); |
---|
84 | }else{ System.err.println("Mode is not correct."); } |
---|
85 | for ( int j = 0 ; j < classLinks.length; j++ ){ |
---|
86 | List<ClassLink> crrpath = new LinkedList<ClassLink>(crrlp.path); |
---|
87 | crrpath.add(classLinks[j]); |
---|
88 | if ( classLinks[j].getLinkedClassURI().equals(endClass) ){ |
---|
89 | paths.add(new LinkedList<ClassLink>(crrpath)); |
---|
90 | } |
---|
91 | nextlp.add(new LinkAndPath(classLinks[j],crrpath)); |
---|
92 | } |
---|
93 | } |
---|
94 | lp = nextlp; |
---|
95 | } |
---|
96 | }catch(Exception e){ System.err.println(e);} |
---|
97 | return paths; |
---|
98 | } |
---|
99 | } |
---|