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

リビジョン 281, 13.4 KB (コミッタ: atsuko, 8 年 前)

新ランキング関数でソート機能追加

  • 属性 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            path.setMin(min);
102            // using length of path
103            //int rankwidth = (int) ( ( min * nsteps )/ crrpath.size() );
104            //path.setWidth(500000 - crrpath.size()*100000 - min);
105            double prob = computePrOfPath(path);
106            path.setWidth(prob);
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                    tmppaths.add(addedpath);
203                }
204            }
205            paths = tmppaths;
206            start = end;
207        }       
208        return paths;
209    }
210   
211    private void setClassGraph(RDFSchemaAnalyzer rdfsa){
212        // setNodes
213        SClass[] classes = null;
214        try{
215            classes = rdfsa.getOWLClasses(null, null, null, true);
216        }catch(Exception e){
217            System.err.println(e); return;
218        }
219        for (int i = 0 ; i < classes.length; i++){
220            addNode(classes[i].getClassURI());
221            nodeType.add("class");
222        }
223        // setEdges
224        for (int i = 0 ; i < classes.length; i++ ){
225            try{
226                ClassLink[] classLinks = rdfsa.getNextClass(null, classes[i].getClassURI(), limit, true);
227                for (int j = 0 ; j < classLinks.length; j++){
228                    Integer n = labelednodes.get(classLinks[j].getLinkedClassURI());
229                    if ( n != null ){
230                        addEdge(i, n, classLinks[j]);
231                    }else{
232                        n = labelednodes.get(classLinks[j].getLinkedLiteralDatatypeURI());
233                        if ( n == null ){
234                           addNode(classLinks[j].getLinkedLiteralDatatypeURI());
235                           n = nodeType.size();
236                           nodeType.add("literal");
237                        }
238                        addEdge(i, n, classLinks[j]);
239                    }
240                }
241            }catch(Exception e){
242                System.err.println(e);
243            }
244        }       
245    }
246
247    public void setPartClassGraph(RDFSchemaAnalyzer rdfsa, String sparqlEndpoint, String startClass){
248        // set endpoint
249        this.sparqlEndpoint = sparqlEndpoint;
250        visited = new HashSet<Integer>();
251        //edgeweight = new LinkedList<Map<Integer,Integer>>();
252        nodeweight = new LinkedList<Integer>();
253        // setNodes for all classes
254        SClass[] classes = null;
255        try{
256           classes = rdfsa.getOWLClasses(null, null, null, true);
257        }catch(Exception e){
258           System.err.println(e); return;
259        }
260        for (int i = 0 ; i < classes.length; i++){
261           addNode(classes[i].getClassURI());
262           nodeType.add("class");
263           //edgeweight.add(new HashMap<Integer,Integer>());
264           nodeweight.add(classes[i].getNumOfInstances());
265        }
266        // setEdges
267        Integer snode = labelednodes.get(startClass);
268        Set<Integer> nodes = new HashSet<Integer>();
269        nodes.add(snode);
270        visited.add(snode);
271        for (int i = 0 ; i < nsteps; i++ ){
272            Iterator<Integer> nit = nodes.iterator();
273            Set<Integer> nextnodes = new HashSet<Integer>();
274            while ( nit.hasNext() ){
275                Integer crr = nit.next();
276                try{
277                    ClassLink[] classLinks = rdfsa.getNextClass(null, labels.get(crr), limit, true);
278                    for (int j = 0 ; j < classLinks.length; j++){
279                        Integer nn = labelednodes.get(classLinks[j].getLinkedClassURI());
280                        if ( nn == null ){
281                            continue;
282                        }
283                        if ( !visited.contains(nn) ){
284                            nextnodes.add(nn);
285                            //visited.add(nn);
286                        }
287                        addEdge(crr, nn, classLinks[j]);
288                        //updateWeight(crr, nn, classLinks[j]);
289                    }
290                }catch(Exception e){
291                    e.printStackTrace();
292                }
293            }
294            nodes = nextnodes;
295            visited.addAll(nodes);
296        }
297        // cut visited
298        /*
299        Iterator<Integer> nit = visited.iterator();
300        while(nit.hasNext()){
301            Integer node = nit.next();
302            if ( ! node.equals(snode) ){
303                List<List<Integer>> paths = searchSimplePaths(snode, node);
304                if ( paths.isEmpty()){
305                    nit.remove();
306                }
307            }
308        }
309        */
310    }
311       
312    public List<String> getReachableClasses(){
313        List<String> clURIs = new LinkedList<String>();
314        if ( visited == null ){
315            return null;
316        }
317        Iterator<Integer> vit = visited.iterator();
318        while( vit.hasNext() ){
319            Integer vn = vit.next();
320            clURIs.add(labels.get(vn));
321        }
322        return clURIs;
323    }
324   
325    public double computePrOfPath(Path path){
326        ListIterator<ClassLink> lit = path.getClassLinks().listIterator();
327        ClassLink prev = lit.next();
328        double prob = 1.0;
329        boolean chk = true;
330        double c1, c2;
331        while( lit.hasNext() ){
332            ClassLink crr = lit.next();
333            c1 = (double) ( crr.getNumOfOriginClassInstances()) /
334                    (double) (  nodeweight.get(labelednodes.get(prev.getLinkedClassURI())));
335            c2 = (double) ( prev.getNumOfLinkedClassInstances()) /
336                    (double) (  nodeweight.get(labelednodes.get(prev.getLinkedClassURI())));
337            if ( c1 < 0.5 && c2 < 0.5 ){ chk = false;}
338            double prob2 = 1.0 - c1;
339                    //((double) ( crr.getNumOfOriginClassInstances())/
340                    //(double) (  nodeweight.get(labelednodes.get(prev.getLinkedClassURI())) ));
341            //if ( prob2 > 1.0 || prob2 < 0 ){
342            //    System.out.println("Prob2 > 1 or Prob2 < 0");
343            //    System.out.println(prev.getLinkedClassURI());
344                //classes[labelednodes.get(prev.getLinkedClassURI())].getNumOfInstances();
345            //    System.out.println(prev.getPropertyURI());
346            //}
347            double prob3 = 1.0 - Math.pow(prob2, (double) prev.getNumOfLinkedClassInstances());
348            //if ( prob3 > 1.0 ){
349            //    System.out.println("Prob3 > 1");
350            //    System.out.println(prev.getLinkedClassURI());
351                //classes[labelednodes.get(prev.getLinkedClassURI())].getNumOfInstances();
352            //    System.out.println(prev.getPropertyURI());
353            //}
354            prob = prob * prob3 ;
355            prev = crr;
356        }
357        path.setChk(chk);
358        return prob;
359    }
360
361}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。