root/SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/OWL/OWLClassGraph.java @ 261

リビジョン 261, 12.4 KB (コミッタ: atsuko, 9 年 前)

エンドポイントを指定しないと全部のクラスを出すように変更

  • 属性 svn:mime-type の設定値 text/plain
行番号 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package org.biohackathon.SPARQLBuilder.OWL;
6
7/**
8 *
9 * @author atsuko
10 */
11import java.util.*;
12
13public class OWLClassGraph extends LabeledMultiDigraph{
14    int nsteps = 4;
15    int limit = 100;
16   
17    List<String> nodeType;
18    String sparqlEndpoint;
19    Set<Integer> visited;
20    List<Map<Integer, Integer>> edgeweight;
21    List<Integer> nodeweight;
22    Map<String, Boolean> checkedpaths;
23   
24    public class LinkAndPath{
25        String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL
26        ClassLink classLink;
27        List<ClassLink> path;
28        Set<String> classURIs; // apearing class URIs in the path
29       
30       
31        public LinkAndPath(ClassLink classLink, List<ClassLink> path){
32           this.classLink = classLink;
33           this.path = path;
34        }
35       
36        public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI){
37           this.classLink = classLink;
38           this.path = path;
39           this.originalClassURI = originalClassURI;
40        }
41
42        public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI, Set<String> classURIs){
43           this.classLink = classLink;
44           this.path = path;
45           this.originalClassURI = originalClassURI;
46           this.classURIs = classURIs;
47        }
48    }
49
50    public OWLClassGraph(){ // not used
51        super();
52        nodeType = new LinkedList<String>();
53    }
54       
55    public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ // for experiment
56        super();
57        nodeType = new LinkedList<String>();
58        setClassGraph(rdfsa);
59    }
60   
61    public OWLClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){ // used
62        super();
63        nodeType = new LinkedList<String>();
64        setPartClassGraph(rdfsa, sparqlEndpoint, startClass);
65    }
66
67    public int getNumberOfEdge(String url){
68        Integer node = labelednodes.get(url);
69        if (node == null){
70            return 0;
71        }
72        return adjlist.get(node).size();
73    }
74   
75    public boolean visitedNode(String classURI){
76        if ( visited.contains(labelednodes.get(classURI)) ){
77            return true;
78        }
79        return false;
80    }
81   
82    public Path[] getPaths(String startClass, String endClass){
83        List<List<ClassLink>> paths = searchPaths(startClass, endClass);
84
85        List<Path> sortedpaths = new LinkedList<Path>();
86        ListIterator<List<ClassLink>> pit = paths.listIterator();
87        int j = 0;
88        while ( pit.hasNext() ){
89            Path path = new Path();
90            path.setStartClass(startClass);
91            List<ClassLink> crrpath = pit.next();
92            path.setClassLinks(crrpath);
93            ListIterator<ClassLink> cit = crrpath.listIterator();
94            int min = Integer.MAX_VALUE;
95            while ( cit.hasNext() ){
96                ClassLink cl = cit.next();
97                if ( cl.getNumOfLinks() < min ){
98                    min = cl.getNumOfLinks();
99                }
100            }
101            // using length of path
102            int rankwidth = (int) ( ( min * nsteps )/ crrpath.size() );
103            path.setWidth(rankwidth);
104            sortedpaths.add(path);
105            j++;
106        }
107        Path[] patharray = new Path[paths.size()];
108        Collections.sort(sortedpaths);
109        Iterator<Path> pait = sortedpaths.listIterator();
110        int i = 0;
111        while ( pait.hasNext() ){
112            patharray[paths.size()-i-1] = pait.next();
113            i++;
114        }
115        return patharray;
116    }
117   
118    private List<List<ClassLink>> searchPaths(String startClass, String endClass){
119        //int asked = 0;
120        checkedpaths = new HashMap<String, Boolean>();
121        List<List<ClassLink>> paths = new ArrayList<>();
122        Integer snode = labelednodes.get(startClass);
123        Integer enode = labelednodes.get(endClass);
124        List<List<Integer>> simplePaths = searchSimplePaths(snode, enode);
125       
126        ListIterator<List<Integer>> pit = simplePaths.listIterator();
127        //System.out.println("SPATH:");
128        //System.out.println(simplePaths.size());
129        while( pit.hasNext()){
130            List<Integer> spath = pit.next();
131            List<List<ClassLink>> convertedPaths = convertSimplePathToPaths(spath);
132            paths.addAll(convertedPaths);
133        }
134        //System.out.println("PATH:");
135        //System.out.println(paths.size());
136        return paths;
137    }
138
139    private List<List<Integer>> searchSimplePaths(Integer snode, Integer enode){
140        List<List<Integer>> simplePaths = new LinkedList<>();
141        List<List<Integer>> lp = new LinkedList<>();
142        List<Integer> ini = new LinkedList<Integer>(); // initial path
143        ini.add(snode);
144        lp.add(ini);
145        for (int i = 0; i < nsteps; i++ ){
146            ListIterator<List<Integer>> lit = lp.listIterator();
147            List<List<Integer>> nextlp = new LinkedList<>();
148            while ( lit.hasNext() ){
149                List<Integer> crrpath = lit.next();
150                Integer crrnode = crrpath.get(crrpath.size()-1);
151                Set<Integer> nexts = gadjlist.get(crrnode).keySet();
152                Iterator<Integer> nit = nexts.iterator();
153                while( nit.hasNext() ){
154                    Integer nextnode = nit.next();
155                    if ( crrpath.contains(nextnode) ){ continue; }
156                    List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy
157                    nextpath.add(nextnode);
158                    if ( nextnode.equals(enode) ){
159                        simplePaths.add(nextpath);
160                        continue;
161                    }
162                    nextlp.add(nextpath);
163                }
164            }
165            lp = nextlp;
166        }       
167        return simplePaths;
168    }
169   
170   
171    private List<List<ClassLink>> convertSimplePathToPaths(List<Integer> simplePath){
172        List<List<ClassLink>> paths = new LinkedList<List<ClassLink>>();
173        ListIterator<Integer> spit = simplePath.listIterator();
174        Integer start = spit.next();
175        String startClass = this.labels.get(start);
176        Integer end = spit.next();
177        List<LabeledEdge> edges = gadjlist.get(start).get(end);
178        ListIterator<LabeledEdge> eit = edges.listIterator();
179        while ( eit.hasNext() ){
180            List<ClassLink> cl = new LinkedList<ClassLink>();
181            cl.add((ClassLink)eit.next().getLabel());
182            paths.add(cl);
183        }
184        start = end;
185        while( spit.hasNext() ){
186            end = spit.next();
187            // start-end
188            edges = gadjlist.get(start).get(end);
189            List<List<ClassLink>> tmppaths = new LinkedList<List<ClassLink>>();           
190            // current path
191            ListIterator<List<ClassLink>> pit = paths.listIterator();
192            while ( pit.hasNext() ){
193                List<ClassLink> basepath = pit.next();
194                eit = edges.listIterator();
195                while ( eit.hasNext() ){
196                    ClassLink cl = (ClassLink) eit.next().label;
197                    List<ClassLink> addedpath = new LinkedList<ClassLink>(basepath);
198                    addedpath.add(cl);
199                    // check
200                    //if (checkPath(startClass, addedpath)){
201                        tmppaths.add(addedpath);
202                    //}
203                }
204            }
205            paths = tmppaths;
206            start = end;
207        }       
208        return paths;
209    }
210   
211    private void setClassGraph(RDFSchemaAnalyzer rdfsa){
212        // setNodes
213        SClass[] classes = null;
214        try{
215            classes = rdfsa.getOWLClasses(null, null, null, true);
216        }catch(Exception e){
217            System.err.println(e); return;
218        }
219        for (int i = 0 ; i < classes.length; i++){
220            addNode(classes[i].getClassURI());
221            nodeType.add("class");
222        }
223        // setEdges
224        for (int i = 0 ; i < classes.length; i++ ){
225            try{
226                ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true);
227                for (int j = 0 ; j < classLinks.length; j++){
228                    Integer n = labelednodes.get(classLinks[j].getLinkedClassURI());
229                    if ( n != null ){
230                        addEdge(i, n, classLinks[j]);
231                    }else{
232                        n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI());
233                        if ( n == null ){
234                           addNode(classLinks[j].getLinkedLiteralDatatypeURI());
235                           n = nodeType.size();
236                           nodeType.add("literal");
237                        }
238                        addEdge(i, n, classLinks[j]);
239                    }
240                }
241            }catch(Exception e){
242                System.err.println(e);
243            }
244        }       
245    }
246
247    public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){
248        // set endpoint
249        this.sparqlEndpoint = sparqlEndpoint;
250        visited = new HashSet<Integer>();
251        edgeweight = new LinkedList<Map<Integer,Integer>>();
252        nodeweight = new LinkedList<Integer>();
253        // setNodes for all classes
254        SClass[] classes = null;
255        try{
256           classes = rdfsa.getOWLClasses(null, null, null, true);
257        }catch(Exception e){
258           System.err.println(e); return;
259        }
260        for (int i = 0 ; i < classes.length; i++){
261           addNode(classes[i].getClassURI());
262           nodeType.add("class");
263           edgeweight.add(new HashMap<Integer,Integer>());
264           nodeweight.add(classes[i].getNumOfInstances());
265        }
266        // setEdges
267        Integer snode = labelednodes.get(startClass);
268        Set<Integer> nodes = new HashSet<Integer>();
269        nodes.add(snode);
270        visited.add(snode);
271        for (int i = 0 ; i < nsteps; i++ ){
272            Iterator<Integer> nit = nodes.iterator();
273            Set<Integer> nextnodes = new HashSet<Integer>();
274            while ( nit.hasNext() ){
275                Integer crr = nit.next();
276                try{
277                    ClassLink[] classLinks = rdfsa.getNextClass(null, labels.get(crr), limit, true);
278                    for (int j = 0 ; j < classLinks.length; j++){
279                        Integer nn = labelednodes.get(classLinks[j].getLinkedClassURI());
280                        if ( nn == null ){
281                            continue;
282                        }
283                        if ( !visited.contains(nn) ){
284                            nextnodes.add(nn);
285                        }
286                        addEdge(crr, nn, classLinks[j]);
287                        updateWeight(crr, nn, classLinks[j]);
288                    }
289                }catch(Exception e){
290                    e.printStackTrace();
291                }
292            }
293            nodes = nextnodes;
294            visited.addAll(nodes);
295        }
296        // cut visited
297        Iterator<Integer> nit = visited.iterator();
298        while(nit.hasNext()){
299            Integer node = nit.next();
300            if ( ! node.equals(snode) ){
301                List<List<Integer>> paths = searchSimplePaths(snode, node);
302                if ( paths.isEmpty()){
303                    nit.remove();
304                }
305            }
306        }
307    }       
308   
309    private void updateWeight(Integer node1, Integer node2, ClassLink edge){
310        Map<Integer, Integer> weight = edgeweight.get(node1);
311        Integer crr = weight.get(node2);
312        if (crr == null ){
313            crr = edge.getNumOfLinkedClassInstances();
314            weight.put(node2, crr);           
315        }
316        if ( crr < edge.getNumOfLinkedClassInstances() ){
317            crr = edge.getNumOfLinkedClassInstances();
318            weight.put(node2, crr);
319        }
320        weight = edgeweight.get(node2);
321        crr = weight.get(node1);
322        if (crr == null ){
323            crr = edge.getNumOfOriginClassInstances();
324            weight.put(node1, crr);
325        }
326        if ( crr < edge.getNumOfOriginClassInstances() ){
327            crr = edge.getNumOfOriginInstances();
328            weight.put(node1, crr);
329        }
330    }
331   
332    public List<String> getReachableClasses(){
333        List<String> clURIs = new LinkedList<String>();
334        if ( visited == null ){
335            return null;
336        }
337        Iterator<Integer> vit = visited.iterator();
338        while( vit.hasNext() ){
339            Integer vn = vit.next();
340            clURIs.add(labels.get(vn));
341        }
342        return clURIs;
343    }
344}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。