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

リビジョン 263, 12.3 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(500000 - crrpath.size()*100000 - min);
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                    tmppaths.add(addedpath);
200                }
201            }
202            paths = tmppaths;
203            start = end;
204        }       
205        return paths;
206    }
207   
208    private void setClassGraph(RDFSchemaAnalyzer rdfsa){
209        // setNodes
210        SClass[] classes = null;
211        try{
212            classes = rdfsa.getOWLClasses(null, null, null, true);
213        }catch(Exception e){
214            System.err.println(e); return;
215        }
216        for (int i = 0 ; i < classes.length; i++){
217            addNode(classes[i].getClassURI());
218            nodeType.add("class");
219        }
220        // setEdges
221        for (int i = 0 ; i < classes.length; i++ ){
222            try{
223                ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true);
224                for (int j = 0 ; j < classLinks.length; j++){
225                    Integer n = labelednodes.get(classLinks[j].getLinkedClassURI());
226                    if ( n != null ){
227                        addEdge(i, n, classLinks[j]);
228                    }else{
229                        n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI());
230                        if ( n == null ){
231                           addNode(classLinks[j].getLinkedLiteralDatatypeURI());
232                           n = nodeType.size();
233                           nodeType.add("literal");
234                        }
235                        addEdge(i, n, classLinks[j]);
236                    }
237                }
238            }catch(Exception e){
239                System.err.println(e);
240            }
241        }       
242    }
243
244    public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){
245        // set endpoint
246        this.sparqlEndpoint = sparqlEndpoint;
247        visited = new HashSet<Integer>();
248        edgeweight = new LinkedList<Map<Integer,Integer>>();
249        nodeweight = new LinkedList<Integer>();
250        // setNodes for all classes
251        SClass[] classes = null;
252        try{
253           classes = rdfsa.getOWLClasses(null, null, null, true);
254        }catch(Exception e){
255           System.err.println(e); return;
256        }
257        for (int i = 0 ; i < classes.length; i++){
258           addNode(classes[i].getClassURI());
259           nodeType.add("class");
260           edgeweight.add(new HashMap<Integer,Integer>());
261           nodeweight.add(classes[i].getNumOfInstances());
262        }
263        // setEdges
264        Integer snode = labelednodes.get(startClass);
265        Set<Integer> nodes = new HashSet<Integer>();
266        nodes.add(snode);
267        visited.add(snode);
268        for (int i = 0 ; i < nsteps; i++ ){
269            Iterator<Integer> nit = nodes.iterator();
270            Set<Integer> nextnodes = new HashSet<Integer>();
271            while ( nit.hasNext() ){
272                Integer crr = nit.next();
273                try{
274                    ClassLink[] classLinks = rdfsa.getNextClass(null, labels.get(crr), limit, true);
275                    for (int j = 0 ; j < classLinks.length; j++){
276                        Integer nn = labelednodes.get(classLinks[j].getLinkedClassURI());
277                        if ( nn == null ){
278                            continue;
279                        }
280                        if ( !visited.contains(nn) ){
281                            nextnodes.add(nn);
282                        }
283                        addEdge(crr, nn, classLinks[j]);
284                        updateWeight(crr, nn, classLinks[j]);
285                    }
286                }catch(Exception e){
287                    e.printStackTrace();
288                }
289            }
290            nodes = nextnodes;
291            visited.addAll(nodes);
292        }
293        // cut visited
294        Iterator<Integer> nit = visited.iterator();
295        while(nit.hasNext()){
296            Integer node = nit.next();
297            if ( ! node.equals(snode) ){
298                List<List<Integer>> paths = searchSimplePaths(snode, node);
299                if ( paths.isEmpty()){
300                    nit.remove();
301                }
302            }
303        }
304    }       
305   
306    private void updateWeight(Integer node1, Integer node2, ClassLink edge){
307        Map<Integer, Integer> weight = edgeweight.get(node1);
308        Integer crr = weight.get(node2);
309        if (crr == null ){
310            crr = edge.getNumOfLinkedClassInstances();
311            weight.put(node2, crr);           
312        }
313        if ( crr < edge.getNumOfLinkedClassInstances() ){
314            crr = edge.getNumOfLinkedClassInstances();
315            weight.put(node2, crr);
316        }
317        weight = edgeweight.get(node2);
318        crr = weight.get(node1);
319        if (crr == null ){
320            crr = edge.getNumOfOriginClassInstances();
321            weight.put(node1, crr);
322        }
323        if ( crr < edge.getNumOfOriginClassInstances() ){
324            crr = edge.getNumOfOriginInstances();
325            weight.put(node1, crr);
326        }
327    }
328   
329    public List<String> getReachableClasses(){
330        List<String> clURIs = new LinkedList<String>();
331        if ( visited == null ){
332            return null;
333        }
334        Iterator<Integer> vit = visited.iterator();
335        while( vit.hasNext() ){
336            Integer vn = vit.next();
337            clURIs.add(labels.get(vn));
338        }
339        return clURIs;
340    }
341}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。