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

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

クラスグラフ変更準備

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