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

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