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

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

コスト関数導入

行番号 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7package org.biohackathon.SPARQLBuilder.OWL;
8
9import java.io.*;
10import java.util.*;
11
12/**
13 *
14 * @author atsuko
15 */
16
17public class QueryPathGenerator {
18    private String sparqlEndpoint = null;
19    private RDFSchemaAnalyzerFactory factory = null;
20    private RDFSchemaAnalyzer analyzer = null;
21    private OWLClassGraph graph;
22    private boolean askcheck;
23   
24    private static final String CDIR = "cdata";
25       
26    public static void main(String[] args){
27        QueryPathGenerator qpg = new QueryPathGenerator();
28        //String[] elist = qpg.getFactory().getEndpointURIList();
29        List<String> elist = new LinkedList<String>();
30        File file0 = new File("eplist.txt");
31        try{
32            BufferedReader br = new BufferedReader(new FileReader(file0));
33            String buf = null;
34            while( (buf = br.readLine()) != null){
35                elist.add(buf);
36            }
37        }catch(IOException e){
38            System.err.println(e);
39        }
40       
41        ListIterator<String> eit = elist.listIterator();
42        int i = 0;
43        while(eit.hasNext()){
44            String ep = eit.next();
45            qpg.setSPARQLendpoint(ep);
46            qpg.graph = new OWLClassGraph(qpg.analyzer);
47            SClass[] classes = qpg.getClasses(null);
48            File file1 = new File("path".concat(Integer.toString(i)).concat(".txt"));
49            File file2 = new File("ptable".concat(Integer.toString(i)).concat(".txt"));
50            try{
51                BufferedWriter bw1 = new BufferedWriter(new FileWriter(file1));
52                BufferedWriter bw2 = new BufferedWriter(new FileWriter(file2));
53                String jsonstr = "[";
54                int m = 0;
55                for ( int j = 0 ; j < classes.length; j ++ ){
56                    SClass start = classes[j];
57                    for ( int k = j + 1 ; k < classes.length; k++ ){
58                        SClass end = classes[k];
59                        Path[] paths = qpg.getPaths(start.getClassURI(), end.getClassURI(), false);                       
60                        for( int l = 0; l < paths.length; l++ ){
61                            if ( paths[l] == null ){
62                                continue;
63                            }
64                            if (m > 0 ){
65                                jsonstr += ",";
66                            }
67                            double cost = paths[l].computeCost();                                                       
68                            bw2.write(Double.toString(cost));
69                            bw2.write(",");
70                            bw2.write(Boolean.toString(EndpointAccess.checkPath(paths[l], ep)));
71                            bw2.newLine();
72                            jsonstr += paths[i].toJSONString3(classes);
73                            m++;
74                        }
75                    }
76                }
77                jsonstr += "]";
78                bw1.write(jsonstr);
79                bw1.newLine();
80               
81                bw1.close();
82                bw2.close();
83            }catch(IOException e){
84                System.err.println(e);
85            }
86            i++;
87        }
88    }
89   
90    public QueryPathGenerator(){
91        factory = new RDFSchemaAnalyzerFactory(CDIR);
92    }
93
94    public QueryPathGenerator(String sparqlEndpoint){
95        factory = new RDFSchemaAnalyzerFactory(CDIR);
96        setSPARQLendpoint(sparqlEndpoint);
97    }
98   
99    public void setOWLClassGraph(String startClass){
100        //graph = new OWLClassGraph(analyzer, sparqlEndpoint, startClass, askcheck);
101        graph = new OWLClassGraph(analyzer, sparqlEndpoint, startClass, true);
102    }
103   
104    public SClass[] getClasses(String keyword){
105        String[] keywords = null;
106        if ( keyword != null ){
107            if ( keyword.length() != 0 ){
108                keywords = new String[1];
109                keywords[0] = keyword;
110            }
111        }
112        try {
113            return analyzer.getOWLClasses(null, keywords, null, true);
114        }catch(Exception e){
115            System.err.println(e);
116            return null;
117        }
118    }
119   
120    public Path[] getPaths(String startClass, String endClass, boolean askcheck){
121        if ( graph == null ){
122            //System.err.println("Class graph is null.");
123            setOWLClassGraph(startClass);
124        }
125        graph.askcheck = askcheck;
126        return graph.getPaths(startClass, endClass);
127    }
128   
129    public void setSPARQLendpoint(String sparqlEndpoint){
130        this.sparqlEndpoint = sparqlEndpoint;
131        setAnalyzer();
132    }
133   
134    public RDFSchemaAnalyzerFactory getFactory(){
135        return factory;
136    }
137
138    private void setAnalyzer(){
139        try {
140            analyzer = factory.create(sparqlEndpoint);
141        } catch (Exception e) {
142            System.err.println(e);
143        }
144    }
145   
146    public static String getClassLabelfromList(String classURI, SClass[] classes){
147        if ( classURI == null ){
148                    return "";
149        }
150        SClass sclass = null;
151        for ( int i = 0 ; i < classes.length; i++ ){
152            if ( classURI.equals(classes[i].getClassURI()) ){
153                return getClassLabelfromClass(classes[i]);
154            }                   
155        }
156        return "";
157    }
158   
159    public static String getClassLabelfromClass(SClass sclass){
160        Label[] labels = sclass.getLabels();
161        for ( int i = 0 ; i < labels.length; i++ ){
162            if ( labels[i].getLanguage() == null ){
163                return labels[i].getLabel();
164            }else if ( labels[i].getLanguage().equals("en") ){
165                return labels[i].getLabel();
166            }
167        }
168        String[] url = sclass.getClassURI().split("/");
169        String tmplabel = url[url.length-1];
170        String[] tmplabel2 = tmplabel.split("#");
171        String label = tmplabel2[tmplabel2.length-1];
172        return label;
173    }
174       
175    public OWLClassGraph getOWLClassGraph(){
176        /*if ( graph == null ){
177            graph = new OWLClassGraph(analyzer);             
178        }*/
179        return graph;
180    }
181}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。