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

リビジョン 261, 8.9 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
23    private Map<String, String> clabels;
24   
25    private static final String CDIR = "cdata";
26    private static final String ODIR = "owldata";
27       
28    /*
29    public static void main(String[] args){
30        QueryPathGenerator qpg = new QueryPathGenerator();
31        //String[] elist = qpg.getFactory().getEndpointURIList();
32        List<String> elist = new LinkedList<String>();
33        File file0 = new File("eplist.txt");
34        try{
35            BufferedReader br = new BufferedReader(new FileReader(file0));
36            String buf = null;
37            while( (buf = br.readLine()) != null){
38                elist.add(buf);
39            }
40        }catch(IOException e){
41            System.err.println(e);
42        }
43       
44        ListIterator<String> eit = elist.listIterator();
45        int i = 0;
46        while(eit.hasNext()){
47            String ep = eit.next();
48            qpg.setSPARQLendpoint(ep);
49            qpg.graph = new OWLClassGraph(qpg.analyzer);
50            SClass[] classes = qpg.getClasses(null);
51            File file1 = new File("path".concat(Integer.toString(i)).concat(".txt"));
52            File file2 = new File("ptable".concat(Integer.toString(i)).concat(".txt"));
53            try{
54                BufferedWriter bw1 = new BufferedWriter(new FileWriter(file1));
55                BufferedWriter bw2 = new BufferedWriter(new FileWriter(file2));
56                String jsonstr = "[";
57                int m = 0;
58                for ( int j = 0 ; j < classes.length; j ++ ){
59                    SClass start = classes[j];
60                    for ( int k = j + 1 ; k < classes.length; k++ ){
61                        SClass end = classes[k];
62                        Path[] paths = qpg.getPaths(start.getClassURI(), end.getClassURI(), false);                       
63                        for( int l = 0; l < paths.length; l++ ){
64                            if ( paths[l] == null ){
65                                continue;
66                            }
67                            if (m > 0 ){
68                                jsonstr += ",";
69                            }
70                            double cost = paths[l].computeCost();                                                       
71                            bw2.write(Double.toString(cost));
72                            bw2.write(",");
73                            bw2.write(Boolean.toString(EndpointAccess.checkPath(paths[l], ep)));
74                            bw2.newLine();
75                            jsonstr += paths[i].toJSONString3(classes);
76                            m++;
77                        }
78                    }
79                }
80                jsonstr += "]";
81                bw1.write(jsonstr);
82                bw1.newLine();
83               
84                bw1.close();
85                bw2.close();
86            }catch(IOException e){
87                System.err.println(e);
88            }
89            i++;
90        }
91    }
92    */
93   
94    public QueryPathGenerator(){
95        factory = new RDFSchemaAnalyzerFactory(CDIR);
96    }
97
98    public QueryPathGenerator(String sparqlEndpoint){
99        factory = new RDFSchemaAnalyzerFactory(CDIR);
100        setSPARQLendpoint(sparqlEndpoint);
101    }
102   
103    public void setOWLClassGraph(String startClass){
104        graph = new OWLClassGraph(analyzer, sparqlEndpoint, startClass);
105    }
106   
107    public SClass[] getClasses(String keyword){
108        String[] keywords = null;
109        if ( keyword != null ){
110            if ( keyword.length() != 0 ){
111                keywords = new String[1];
112                keywords[0] = keyword;
113            }
114        }
115        try {
116            return analyzer.getOWLClasses(null, keywords, null, true);
117        }catch(Exception e){
118            System.err.println(e);
119            return null;
120        }
121    }
122   
123    public Path[] getPaths(String startClass, String endClass, boolean askcheck){
124        if ( graph == null ){
125            //System.err.println("Class graph is null.");
126            setOWLClassGraph(startClass);
127        }
128        return graph.getPaths(startClass, endClass);
129    }
130   
131    public void setSPARQLendpoint(String sparqlEndpoint){
132        this.sparqlEndpoint = sparqlEndpoint;
133        setAnalyzer();
134    }
135   
136    public RDFSchemaAnalyzerFactory getFactory(){
137        return factory;
138    }
139
140    private void setAnalyzer(){
141        try {
142            analyzer = factory.create(sparqlEndpoint);
143        } catch (Exception e) {
144            System.err.println(e);
145        }
146    }
147   
148    public String getClassLabel(String classURI){
149        return clabels.get(classURI);
150    }
151   
152    public void setClassLabels(SClass[] classes){
153        clabels = new HashMap<String, String>();
154        Map<String, String> extLabels = getClassLabelsFromExternal();
155       
156        for ( int i = 0 ; i < classes.length; i++ ){
157            String classURI = classes[i].getClassURI();
158
159            Label[] labels = classes[i].getLabels();
160            String label = null;
161            for ( int j = 0 ; j < labels.length; j++ ){
162                if ( labels[j].getLanguage() == null ){
163                    label = labels[j].getLabel();
164                    break;
165                }else if ( labels[j].getLanguage().equals("en") || labels[j].getLanguage().equals("") ){
166                    label = labels[j].getLabel();
167                    break;
168                }
169            }
170            if ( label == null ){
171                label = extLabels.get(classURI);
172            }
173            if ( label == null ){
174                String[] uris = classURI.split("/");
175                String tmplabel = uris[uris.length-1];
176                String[] tmplabel2 = tmplabel.split("#");
177                label = tmplabel2[tmplabel2.length-1];
178            }
179            clabels.put(classURI, label);
180        }
181    }
182
183    public static String getClassLabelfromList(String classURI, SClass[] classes){
184        if ( classURI == null ){
185                    return "";
186        }
187        SClass sclass = null;
188        for ( int i = 0 ; i < classes.length; i++ ){
189            if ( classURI.equals(classes[i].getClassURI()) ){
190                return getClassLabelfromClass(classes[i]);
191            }
192        }
193        return "";
194    }
195   
196    public static String getClassLabelfromClass(SClass sclass){
197        Label[] labels = sclass.getLabels();
198        for ( int i = 0 ; i < labels.length; i++ ){
199            if ( labels[i].getLanguage() == null ){
200                return labels[i].getLabel();
201            }else if ( labels[i].getLanguage().equals("en") ){
202                return labels[i].getLabel();
203            }
204        }
205        String[] url = sclass.getClassURI().split("/");
206        String tmplabel = url[url.length-1];
207        String[] tmplabel2 = tmplabel.split("#");
208        String label = tmplabel2[tmplabel2.length-1];
209        return label;
210    }
211       
212    public Map<String, String> getClassLabelsFromExternal(){
213        return  OWLLabelReader.readLabels(ODIR);
214    }
215   
216    public OWLClassGraph getOWLClassGraph(){
217        /*if ( graph == null ){
218            graph = new OWLClassGraph(analyzer);             
219        }*/
220        return graph;
221    }
222   
223    public SClass[] getReachableClasses(){
224        List<String> clURIs = graph.getReachableClasses();
225        SClass[] orgclasses = null;
226        try {
227            orgclasses = analyzer.getOWLClasses(null, null, null, true);
228        }catch( Exception e ){
229            System.err.println(e);
230            return null;
231        }
232        HashMap<String, SClass> orgmap = new HashMap<String, SClass>();
233        for (int i = 0; i < orgclasses.length; i++ ){
234            orgmap.put(orgclasses[i].getClassURI(), orgclasses[i]);
235        }
236       
237        SClass[] classes = new SClass[clURIs.size()];
238        int j = 0;
239        ListIterator<String> uit = clURIs.listIterator();
240        while(uit.hasNext()){
241            String u = uit.next();
242            SClass cl = orgmap.get(u);
243            classes[j] = cl;
244            j++;
245        }
246        return classes;
247    }
248   
249    public SortedSet<String> getSortedClasses(SClass[] classes){
250        setClassLabels(classes);
251        SortedSet<String> sortedClasses = new TreeSet<String>();
252        for (int i = 0 ; i < classes.length; i++ ){
253            String uri = classes[i].getClassURI();
254            String label = getClassLabel(uri);
255            StringBuilder classbuilder = new StringBuilder(label);
256            classbuilder.append("  ");
257            classbuilder.append(classes[i].getNumOfInstances());
258            classbuilder.append("  ");
259            classbuilder.append(uri);
260            sortedClasses.add(classbuilder.toString());
261        }
262
263        return sortedClasses;
264    }
265}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。