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

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

新API仕様に対応

行番号 
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){
124        if (startClass == null || endClass == null){
125            return null;
126        }
127        if ( graph == null ){
128            //System.err.println("Class graph is null.");
129            setOWLClassGraph(startClass);
130        }
131        return graph.getPaths(startClass, endClass);
132    }
133   
134    public void setSPARQLendpoint(String sparqlEndpoint){
135        this.sparqlEndpoint = sparqlEndpoint;
136        setAnalyzer();
137    }
138   
139    public RDFSchemaAnalyzerFactory getFactory(){
140        return factory;
141    }
142
143    private void setAnalyzer(){
144        try {
145            analyzer = factory.create(sparqlEndpoint);
146        } catch (Exception e) {
147            System.err.println(e);
148        }
149    }
150   
151    public String getClassLabel(String classURI){
152        return clabels.get(classURI);
153    }
154   
155    public void setClassLabels(SClass[] classes){
156        clabels = new HashMap<String, String>();
157        Map<String, String> extLabels = getClassLabelsFromExternal();
158       
159        for ( int i = 0 ; i < classes.length; i++ ){
160            String classURI = classes[i].getClassURI();
161
162            Label[] labels = classes[i].getLabels();
163            String label = null;
164            for ( int j = 0 ; j < labels.length; j++ ){
165                if ( labels[j].getLanguage() == null ){
166                    label = labels[j].getLabel();
167                    break;
168                }else if ( labels[j].getLanguage().equals("en") || labels[j].getLanguage().equals("") ){
169                    label = labels[j].getLabel();
170                    break;
171                }
172            }
173            if ( label == null ){
174                label = extLabels.get(classURI);
175            }
176            if ( label == null ){
177                String[] uris = classURI.split("/");
178                String tmplabel = uris[uris.length-1];
179                String[] tmplabel2 = tmplabel.split("#");
180                label = tmplabel2[tmplabel2.length-1];
181            }
182            clabels.put(classURI, label);
183        }
184    }
185
186    public static String getClassLabelfromList(String classURI, SClass[] classes){
187        if ( classURI == null ){
188                    return "";
189        }
190        SClass sclass = null;
191        for ( int i = 0 ; i < classes.length; i++ ){
192            if ( classURI.equals(classes[i].getClassURI()) ){
193                return getClassLabelfromClass(classes[i]);
194            }
195        }
196        return "";
197    }
198   
199    public static String getClassLabelfromClass(SClass sclass){
200        Label[] labels = sclass.getLabels();
201        for ( int i = 0 ; i < labels.length; i++ ){
202            if ( labels[i].getLanguage() == null ){
203                return labels[i].getLabel();
204            }else if ( labels[i].getLanguage().equals("en") ){
205                return labels[i].getLabel();
206            }
207        }
208        String[] url = sclass.getClassURI().split("/");
209        String tmplabel = url[url.length-1];
210        String[] tmplabel2 = tmplabel.split("#");
211        String label = tmplabel2[tmplabel2.length-1];
212        return label;
213    }
214       
215    public Map<String, String> getClassLabelsFromExternal(){
216        return  OWLLabelReader.readLabels(ODIR);
217    }
218   
219    public OWLClassGraph getOWLClassGraph(){
220        /*if ( graph == null ){
221            graph = new OWLClassGraph(analyzer);             
222        }*/
223        return graph;
224    }
225   
226    public SClass[] getReachableClasses(){
227        List<String> clURIs = graph.getReachableClasses();
228        SClass[] orgclasses = null;
229        try {
230            orgclasses = analyzer.getOWLClasses(null, null, null, true);
231        }catch( Exception e ){
232            System.err.println(e);
233            return null;
234        }
235        HashMap<String, SClass> orgmap = new HashMap<String, SClass>();
236        for (int i = 0; i < orgclasses.length; i++ ){
237            orgmap.put(orgclasses[i].getClassURI(), orgclasses[i]);
238        }
239       
240        SClass[] classes = new SClass[clURIs.size()];
241        int j = 0;
242        ListIterator<String> uit = clURIs.listIterator();
243        while(uit.hasNext()){
244            String u = uit.next();
245            SClass cl = orgmap.get(u);
246            classes[j] = cl;
247            j++;
248        }
249        return classes;
250    }
251   
252    public SortedSet<String> getSortedClasses(SClass[] classes){
253        setClassLabels(classes);
254        SortedSet<String> sortedClasses = new TreeSet<String>();
255        for (int i = 0 ; i < classes.length; i++ ){
256            String uri = classes[i].getClassURI();
257            String label = getClassLabel(uri);
258            StringBuilder classbuilder = new StringBuilder(label);
259            classbuilder.append("\t");
260            classbuilder.append(classes[i].getNumOfInstances());
261            classbuilder.append("\t");
262            classbuilder.append(uri);
263            sortedClasses.add(classbuilder.toString());
264        }
265
266        return sortedClasses;
267    }
268}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。