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

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