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

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

外部OWL読み込み機能追加

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