root/SPARQLBuilderWWW2016/src/java/org/biohackathon/SPARQLBuilder/OWL/OWLClassGraph.java @ 268

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

明らかな間違いを訂正

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