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

リビジョン 252, 20.7 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    //String startClass;
15    //String endClass;
16    int nsteps = 4;
17    int limit = 100;
18    int th = 1;
19    double cth = 1.0; // 0.0(no path) - 1.0(all paths)
20   
21    List<String> nodeType;
22    ArrayList<HashSet<Integer>> connectionTable;
23    String sparqlEndpoint;
24    Set<Integer> visited;
25    boolean askcheck;
26    List<Map<Integer, Integer>> edgeweight;
27    List<Integer> nodeweight;
28    Map<String, Boolean> checkedpaths;
29   
30    public class LinkAndPath{
31        String originalClassURI; // originalClasssURI -classLink.propertyURI-> classLink.linkedClassURL
32        ClassLink classLink;
33        List<ClassLink> path;
34        Set<String> classURIs; // apearing class URIs in the path
35       
36       
37        public LinkAndPath(ClassLink classLink, List<ClassLink> path){
38           this.classLink = classLink;
39           this.path = path;
40        }
41       
42        public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI){
43           this.classLink = classLink;
44           this.path = path;
45           this.originalClassURI = originalClassURI;
46        }
47
48        public LinkAndPath(ClassLink classLink, List<ClassLink> path, String originalClassURI, Set<String> classURIs){
49           this.classLink = classLink;
50           this.path = path;
51           this.originalClassURI = originalClassURI;
52           this.classURIs = classURIs;
53        }
54    }
55
56    public OWLClassGraph(){ // not used
57        super();
58        nodeType = new LinkedList<String>();
59    }
60       
61    public OWLClassGraph(RDFSchemaAnalyzer rdfsa){ // for experiment
62        super();
63        nodeType = new LinkedList<String>();
64        this.askcheck = false;
65        setClassGraph(rdfsa);
66    }
67   
68    public OWLClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass, boolean askcheck){ // used
69        super();
70        nodeType = new LinkedList<String>();
71        this.askcheck = askcheck;
72        setPartClassGraph(rdfsa, sparqlEndpoint, startClass);
73    }
74
75    public int getNumberOfEdge(String url){
76        Integer node = labelednodes.get(url);
77        if (node == null){
78            return 0;
79        }
80        return adjlist.get(node).size();
81    }
82   
83    public boolean visitedNode(String classURI){
84        if ( visited.contains(labelednodes.get(classURI)) ){
85            return true;
86        }
87        return false;
88    }
89   
90    public Path[] getPaths(String startClass, String endClass){
91        List<List<ClassLink>> paths = searchPaths(startClass, endClass);
92
93        List<Path> sortedpaths = new LinkedList<Path>();
94        ListIterator<List<ClassLink>> pit = paths.listIterator();
95        int j = 0;
96        while ( pit.hasNext() ){
97            Path path = new Path();
98            path.setStartClass(startClass);
99            List<ClassLink> crrpath = pit.next();
100            path.setClassLinks(crrpath);
101            ListIterator<ClassLink> cit = crrpath.listIterator();
102            int min = Integer.MAX_VALUE;
103            while ( cit.hasNext() ){
104                ClassLink cl = cit.next();
105                if ( cl.getNumOfLinks() < min ){
106                    min = cl.getNumOfLinks();
107                }
108            }
109            // using length of path
110            int rankwidth = (int) ( ( min * nsteps )/ crrpath.size() );
111            path.setWidth(rankwidth);
112            sortedpaths.add(path);
113            j++;
114        }
115        Path[] patharray = new Path[paths.size()];
116        Collections.sort(sortedpaths);
117        Iterator<Path> pait = sortedpaths.listIterator();
118        int i = 0;
119        while ( pait.hasNext() ){
120            patharray[paths.size()-i-1] = pait.next();
121            i++;
122        }
123        return patharray;
124    }
125   
126    public HashSet<Integer> getConnectionList(Integer node){
127        return connectionTable.get(node);
128    }
129
130    private List<List<ClassLink>> searchPaths(String startClass, String endClass){
131        //int asked = 0;
132        checkedpaths = new HashMap<String, Boolean>();
133        List<List<ClassLink>> paths = new ArrayList<>();
134        Integer snode = labelednodes.get(startClass);
135        Integer enode = labelednodes.get(endClass);
136        List<List<Integer>> simplePaths = searchSimplePaths(snode, enode);
137       
138        ListIterator<List<Integer>> pit = simplePaths.listIterator();
139        //System.out.println("SPATH:");
140        //System.out.println(simplePaths.size());
141        while( pit.hasNext()){
142            List<Integer> spath = pit.next();
143            List<List<ClassLink>> convertedPaths = convertSimplePathToPaths(spath);
144            paths.addAll(convertedPaths);
145        }
146        //System.out.println("PATH:");
147        //System.out.println(paths.size());
148        return paths;
149    }
150
151    private List<List<Integer>> searchSimplePaths(Integer snode, Integer enode){
152        List<List<Integer>> simplePaths = new LinkedList<>();
153        List<List<Integer>> lp = new LinkedList<>();
154        List<Integer> ini = new LinkedList<Integer>(); // initial path
155        ini.add(snode);
156        lp.add(ini);
157        for (int i = 0; i < nsteps; i++ ){
158            ListIterator<List<Integer>> lit = lp.listIterator();
159            List<List<Integer>> nextlp = new LinkedList<>();
160            while ( lit.hasNext() ){
161                List<Integer> crrpath = lit.next();
162                Integer crrnode = crrpath.get(crrpath.size()-1);
163                Set<Integer> nexts = gadjlist.get(crrnode).keySet();
164                Iterator<Integer> nit = nexts.iterator();
165                while( nit.hasNext() ){
166                    Integer nextnode = nit.next();
167                    if ( crrpath.contains(nextnode) ){ continue; }
168                    List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy
169                    nextpath.add(nextnode);
170                    // tmp
171                    if ( i >= 1  && askcheck == true ){
172                        int wn = nodeweight.get(crrnode);
173                        int in = edgeweight.get(crrpath.get(crrpath.size()-2)).get(crrnode);
174                        int out = edgeweight.get(nextnode).get(crrnode);
175                        if ( wn > in + out ){
176                            /*
177                            String key1 = nextnode.toString().concat("-").concat(crrpath.get(crrpath.size()-1).toString())
178                                .concat("-").concat(crrpath.get(crrpath.size()-2).toString());
179                            String key2 = crrpath.get(crrpath.size()-2).toString().concat("-").concat(crrpath.get(crrpath.size()-1).toString())
180                                .concat("-").concat(nextnode.toString());
181                            if ( checkedpaths.containsKey(key1) ){
182                                if ( checkedpaths.get(key1) == false ){
183                                   continue;
184                                }
185                            }else if ( checkedpaths.containsKey(key2) ){
186                                if ( checkedpaths.get(key2) == false ){
187                                   continue;
188                                }
189                            }else{                     
190                                boolean chk = EndpointAccess.check3SimplePath(nextnode, crrpath.get(crrpath.size()-1),
191                                    crrpath.get(crrpath.size()-2), this, sparqlEndpoint);
192                                checkedpaths.put(key1, chk);
193                                if ( chk == false ){
194                                    continue;
195                                }
196                            }
197                            */
198                            continue;
199                        }
200                    }
201                    if ( nextnode.equals(enode) ){
202                        simplePaths.add(nextpath);
203                        continue;
204                    }
205                    nextlp.add(nextpath);
206                }
207            }
208            lp = nextlp;
209        }       
210        return simplePaths;
211    }
212   
213   
214    private List<List<ClassLink>> convertSimplePathToPaths(List<Integer> simplePath){
215        List<List<ClassLink>> paths = new LinkedList<List<ClassLink>>();
216        ListIterator<Integer> spit = simplePath.listIterator();
217        Integer start = spit.next();
218        String startClass = this.labels.get(start);
219        Integer end = spit.next();
220        List<LabeledEdge> edges = gadjlist.get(start).get(end);
221        ListIterator<LabeledEdge> eit = edges.listIterator();
222        while ( eit.hasNext() ){
223            List<ClassLink> cl = new LinkedList<ClassLink>();
224            cl.add((ClassLink)eit.next().getLabel());
225            paths.add(cl);
226        }
227        start = end;
228        while( spit.hasNext() ){
229            end = spit.next();
230            // start-end
231            edges = gadjlist.get(start).get(end);
232            List<List<ClassLink>> tmppaths = new LinkedList<List<ClassLink>>();           
233            // current path
234            ListIterator<List<ClassLink>> pit = paths.listIterator();
235            while ( pit.hasNext() ){
236                List<ClassLink> basepath = pit.next();
237                eit = edges.listIterator();
238                while ( eit.hasNext() ){
239                    ClassLink cl = (ClassLink) eit.next().label;
240                    List<ClassLink> addedpath = new LinkedList<ClassLink>(basepath);
241                    addedpath.add(cl);
242                    // check
243                    if (checkPath(startClass, addedpath)){
244                        tmppaths.add(addedpath);
245                    }
246                }
247            }
248            paths = tmppaths;
249            start = end;
250        }       
251        return paths;
252    }
253   
254    private void setClassGraph(RDFSchemaAnalyzer rdfsa){
255        // setNodes
256        SClass[] classes = null;
257        try{
258            classes = rdfsa.getOWLClasses(null, null, null, true);
259        }catch(Exception e){
260            System.err.println(e); return;
261        }
262        for (int i = 0 ; i < classes.length; i++){
263            addNode(classes[i].getClassURI());
264            nodeType.add("class");
265        }
266        // setEdges
267        for (int i = 0 ; i < classes.length; i++ ){
268            try{
269                ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true);
270                for (int j = 0 ; j < classLinks.length; j++){
271                    Integer n = labelednodes.get(classLinks[j].getLinkedClassURI());
272                    if ( n != null ){
273                        addEdge(i, n, classLinks[j]);
274                    }else{
275                        n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI());
276                        if ( n == null ){
277                           addNode(classLinks[j].getLinkedLiteralDatatypeURI());
278                           n = nodeType.size();
279                           nodeType.add("literal");
280                        }
281                        addEdge(i, n, classLinks[j]);
282                    }
283                }
284            }catch(Exception e){
285                System.err.println(e);
286            }
287        }       
288    }
289
290    public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){
291        // set endpoint
292        this.sparqlEndpoint = sparqlEndpoint;
293        visited = new HashSet<Integer>();
294        edgeweight = new LinkedList<Map<Integer,Integer>>();
295        nodeweight = new LinkedList<Integer>();
296        // setNodes for all classes
297        SClass[] classes = null;
298        try{
299           classes = rdfsa.getOWLClasses(null, null, null, true);
300        }catch(Exception e){
301           System.err.println(e); return;
302        }
303        for (int i = 0 ; i < classes.length; i++){
304           addNode(classes[i].getClassURI());
305           nodeType.add("class");
306           edgeweight.add(new HashMap<Integer,Integer>());
307           nodeweight.add(classes[i].getNumOfInstances());
308        }
309        // setEdges
310        Integer snode = labelednodes.get(startClass);
311        Set<Integer> nodes = new HashSet<Integer>();
312        nodes.add(snode);
313        visited.add(snode);
314        for (int i = 0 ; i < nsteps; i++ ){
315            Iterator<Integer> nit = nodes.iterator();
316            Set<Integer> nextnodes = new HashSet<Integer>();
317            while ( nit.hasNext() ){
318                Integer crr = nit.next();
319                try{
320                    ClassLink[] classLinks = rdfsa.getNextClass(null, labels.get(crr), limit, true);
321                    for (int j = 0 ; j < classLinks.length; j++){
322                        Integer nn = labelednodes.get(classLinks[j].getLinkedClassURI());
323                        if ( nn == null ){
324                            continue;
325                        }
326                        if ( !visited.contains(nn) ){
327                            nextnodes.add(nn);
328                        }
329                        addEdge(crr, nn, classLinks[j]);
330                        updateWeight(crr, nn, classLinks[j]);
331                    }
332                }catch(Exception e){
333                    e.printStackTrace();
334                }
335            }
336            nodes = nextnodes;
337            visited.addAll(nodes);
338        }
339        // cut visited         
340        Iterator<Integer> nit = visited.iterator();
341        while(nit.hasNext()){
342            Integer node = nit.next();
343            if ( ! node.equals(snode) ){
344                List<List<Integer>> paths = searchSimplePaths(snode, node);
345                if ( paths.isEmpty()){
346                    nit.remove();
347                }
348            }
349        }
350    }
351   
352   
353   private ArrayList<HashSet<Integer>> createConnectionTable(){ // not used
354       ArrayList<HashSet<Integer>> ct = new ArrayList<HashSet<Integer>>();
355       for (int i = 0; i < labels.size(); i++ ){ // each node
356           HashSet<Integer> cl = createConnectionList(i);
357           ct.add(cl);
358       }
359       return ct;
360   }
361   
362   private HashSet<Integer> createConnectionList(Integer node){ // not used
363       HashSet<Integer> cl = new HashSet<Integer>();
364       HashSet<Integer> crrnodes = new HashSet<Integer>();
365       crrnodes.add(node);
366       cl.add(node);
367       for ( int i = 0 ; i < nsteps; i++ ){
368           Set<Integer> nextnodes = new HashSet<Integer>();
369           Iterator<Integer> cit = crrnodes.iterator();
370           while ( cit.hasNext() ){
371               Integer crr = cit.next();
372               Set<Integer> nexts = gadjlist.get(crr).keySet();
373               Iterator<Integer> nit = nexts.iterator();
374               while ( nit.hasNext() ){
375                   Integer next = nit.next();
376                   if ( !cl.contains(next) ){
377                       nextnodes.add(next);
378                       cl.add(next);
379                   }
380               }
381           }
382           crrnodes.clear();
383           crrnodes.addAll(nextnodes);
384       }
385       return cl;
386   }
387   
388   
389   private void updateWeight(Integer node1, Integer node2, ClassLink edge){
390       Map<Integer, Integer> weight = edgeweight.get(node1);
391       Integer crr = weight.get(node2);
392       if (crr == null ){
393           crr = edge.getNumOfLinkedClassInstances();
394           weight.put(node2, crr);           
395       }
396       if ( crr < edge.getNumOfLinkedClassInstances() ){
397           crr = edge.getNumOfLinkedClassInstances();
398           weight.put(node2, crr);
399       }
400       weight = edgeweight.get(node2);
401       crr = weight.get(node1);
402       if (crr == null ){
403           crr = edge.getNumOfOriginClassInstances();
404           weight.put(node1, crr);
405       }
406       if ( crr < edge.getNumOfOriginClassInstances() ){
407           crr = edge.getNumOfOriginInstances();
408           weight.put(node1, crr);
409       }
410   }
411   
412   private boolean checkPath(String startClass, List<ClassLink> paths){
413       // KOKO
414       return true;
415   }
416   // old codes
417   /*
418    public Path[] getPaths_old(RDFSchemaAnalyzer rdfsa, boolean countLink, String startClass, String endClass){
419        List<List<ClassLink>> paths = null;
420        paths = searchPathsbyVisitingNodes(rdfsa, countLink, startClass, endClass);
421        NavigableSet<Path> sortedpath = new TreeSet<Path>();
422        ListIterator<List<ClassLink>> pit = paths.listIterator();
423        int j = 0;
424        while ( pit.hasNext() ){
425            Path path = new Path();
426            path.setStartClass(startClass);
427            List<ClassLink> crrpath = pit.next();
428            path.setClassLinks(crrpath);
429            ListIterator<ClassLink> cit = crrpath.listIterator();
430            int min = Integer.MAX_VALUE;
431            while ( cit.hasNext() ){
432                ClassLink cl = cit.next();
433                if ( cl.getNumOfLinks() < min ){
434                    min = cl.getNumOfLinks();
435                }
436            }
437            path.setWidth(min);
438            sortedpath.add(path);
439            j++;
440        }
441        Path[] patharray = new Path[paths.size()];
442        Iterator<Path> pait = sortedpath.descendingIterator();
443        int i = 0;
444        while ( pait.hasNext() ){
445            patharray[i] = pait.next();
446            i++;
447        }
448        return patharray;
449    }
450
451    private List<List<ClassLink>> searchPaths_old(String startClass, String endClass){
452
453        List<List<ClassLink>> paths = new ArrayList<>();
454        List<List<Integer>> simplePaths = new LinkedList<>();
455        Integer snode = labelednodes.get(startClass);
456        Integer enode = labelednodes.get(endClass);
457        List<List<Integer>> lp = new LinkedList<>();
458        List<Integer> ini = new LinkedList<Integer>(); // initial path
459        ini.add(snode);
460        lp.add(ini);
461        for (int i = 0; i < nsteps; i++ ){
462            ListIterator<List<Integer>> lit = lp.listIterator();
463            List<List<Integer>> nextlp = new LinkedList<>();
464            while ( lit.hasNext() ){
465                List<Integer> crrpath = lit.next();
466                Integer crrnode = crrpath.get(crrpath.size()-1);
467                Set<Integer> nexts = gadjlist.get(crrnode).keySet();
468                Iterator<Integer> nit = nexts.iterator();
469                while( nit.hasNext() ){
470                    Integer nextnode = nit.next();
471                    if ( crrpath.contains(nextnode) ){ continue; }
472                    List<Integer> nextpath = new LinkedList<Integer>(crrpath); // copy
473                    nextpath.add(nextnode);
474                    if ( nextnode.equals(enode) ){
475                        simplePaths.add(nextpath);
476                        continue;
477                    }
478                    nextlp.add(nextpath);
479                }
480            }
481            lp = nextlp;
482        }
483       
484        ListIterator<List<Integer>> pit = simplePaths.listIterator();
485        while( pit.hasNext()){
486            List<Integer> spath = pit.next();
487            List<List<ClassLink>> convertedPaths = convertSimplePathToPaths(spath);
488            paths.addAll(convertedPaths);
489        }
490        return paths;
491    }   
492    */
493       /*
494    private List<List<ClassLink>> searchPaths(RDFSchemaAnalyzer rdfsa, boolean countLinks){
495        List<List<ClassLink>> paths = new ArrayList<>();
496        List<LinkAndPath> lp = new LinkedList<>();
497        lp.add(new LinkAndPath(new ClassLink("",startClass,null,Direction.both,0,0,0,0,0,false,false), new LinkedList<ClassLink>(), ""));
498        try{
499          for ( int i = 0; i < nsteps; i++ ){
500              ListIterator<LinkAndPath> lit = lp.listIterator();
501              List<LinkAndPath> nextlp = new LinkedList<>();
502              while ( lit.hasNext() ){
503                  LinkAndPath crrlp = lit.next();
504                  ClassLink[] classLinks = rdfsa.getNextClass(null, crrlp.classLink.getLinkedClassURI(), limit, countLinks);
505                  for ( int j = 0 ; j < classLinks.length; j++ ){
506                      List<ClassLink> crrpath = new LinkedList<>(crrlp.path);
507                      crrpath.add(classLinks[j]);
508                      if ( classLinks[j].getLinkedClassURI() == null ){ continue; }
509                      if ( classLinks[j].getLinkedClassURI().equals(endClass) ){
510                          paths.add(new LinkedList<>(crrpath));
511                          continue;
512                      }
513                      if ( countLinks == true && classLinks[j].getNumOfLinks() <= th){
514                          continue;
515                      }
516                      if ( i >= 2 ){
517                          if ( crrlp.classLink.getPropertyURI().equals(classLinks[j].getPropertyURI()) &&
518                           crrlp.classLink.getDirection() != classLinks[j].getDirection() &&
519                           crrlp.originalClassURI.equals( classLinks[j].getLinkedClassURI()) ){
520                              continue;
521                          }
522                      }
523                      nextlp.add(new LinkAndPath(classLinks[j], crrpath, crrlp.classLink.getLinkedClassURI()));
524                  }
525              }
526              lp = nextlp;
527          }
528        }catch(Exception e){
529            System.err.println(e);
530        }
531        return paths; 
532    }
533    */
534
535}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。