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 | |
---|
7 | package org.biohackathon.SPARQLBuilder.OWL; |
---|
8 | |
---|
9 | import java.io.*; |
---|
10 | import java.util.*; |
---|
11 | |
---|
12 | /** |
---|
13 | * |
---|
14 | * @author atsuko |
---|
15 | */ |
---|
16 | |
---|
17 | public class QueryPathGenerator { |
---|
18 | private RDFSchemaAnalyzerFactory factory = null; |
---|
19 | private HashMap<String, RDFSchemaAnalyzer> analyzers = null; |
---|
20 | private OWLClassGraph graph; |
---|
21 | |
---|
22 | private Map<String, String> clabels; |
---|
23 | |
---|
24 | private static final String CDIR = "cdata"; |
---|
25 | private static final String ODIR = "owldata"; |
---|
26 | |
---|
27 | public QueryPathGenerator(){ |
---|
28 | factory = new RDFSchemaAnalyzerFactory(CDIR); |
---|
29 | } |
---|
30 | |
---|
31 | /* |
---|
32 | public QueryPathGenerator(String sparqlEndpoint){ |
---|
33 | factory = new RDFSchemaAnalyzerFactory(CDIR); |
---|
34 | setSPARQLendpoint(sparqlEndpoint); |
---|
35 | } |
---|
36 | */ |
---|
37 | |
---|
38 | /*public void setOWLClassGraph(String startClass){ |
---|
39 | graph = new OWLClassGraph(analyzer, sparqlEndpoint, startClass); |
---|
40 | }*/ |
---|
41 | |
---|
42 | public SClass[] getClassesByEndpoint(String ep){ |
---|
43 | RDFSchemaAnalyzer analyzer = analyzers.get(ep); |
---|
44 | try { |
---|
45 | return analyzer.getOWLClasses(null, null, null, true); |
---|
46 | }catch(Exception e){ |
---|
47 | System.err.println(e); |
---|
48 | return null; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | public SClass[] getClasses(String keyword){ |
---|
53 | return getClassList(keyword).toArray(new SClass[0]); |
---|
54 | } |
---|
55 | |
---|
56 | public List<SClass> getClassList(String keyword){ |
---|
57 | Iterator<RDFSchemaAnalyzer> ait = analyzers.values().iterator(); |
---|
58 | List<SClass> cl = new LinkedList<SClass>(); |
---|
59 | try { |
---|
60 | while ( ait.hasNext() ){ |
---|
61 | RDFSchemaAnalyzer analyzer = ait.next(); |
---|
62 | cl.addAll(analyzer.getOWLClassList(null, null, null, true)); |
---|
63 | } |
---|
64 | }catch(Exception e){ |
---|
65 | System.err.println(e); |
---|
66 | return null; |
---|
67 | } |
---|
68 | return cl; |
---|
69 | } |
---|
70 | |
---|
71 | public Path[] getPaths(String startClass, String endClass){ |
---|
72 | if (startClass == null || endClass == null){ |
---|
73 | return null; |
---|
74 | } |
---|
75 | if ( graph == null ){ |
---|
76 | //setOWLClassGraph(startClass); |
---|
77 | } |
---|
78 | return graph.getPaths(startClass, endClass); |
---|
79 | } |
---|
80 | |
---|
81 | /* |
---|
82 | public void setSPARQLendpoint(String sparqlEndpoint){ |
---|
83 | this.sparqlEndpoint = sparqlEndpoint; |
---|
84 | setAnalyzer(); |
---|
85 | } |
---|
86 | */ |
---|
87 | |
---|
88 | public RDFSchemaAnalyzerFactory getFactory(){ |
---|
89 | return factory; |
---|
90 | } |
---|
91 | |
---|
92 | /* |
---|
93 | private void setAnalyzer(){ |
---|
94 | try { |
---|
95 | analyzer = factory.create(sparqlEndpoint); |
---|
96 | } catch (Exception e) { |
---|
97 | System.err.println(e); |
---|
98 | } |
---|
99 | } |
---|
100 | */ |
---|
101 | |
---|
102 | public String getClassLabel(String classURI){ |
---|
103 | return clabels.get(classURI); |
---|
104 | } |
---|
105 | |
---|
106 | public void setClassLabels(SClass[] classes){ |
---|
107 | clabels = new HashMap<String, String>(); |
---|
108 | Map<String, String> extLabels = getClassLabelsFromExternal(); |
---|
109 | |
---|
110 | for ( int i = 0 ; i < classes.length; i++ ){ |
---|
111 | String classURI = classes[i].getClassURI(); |
---|
112 | |
---|
113 | Label[] labels = classes[i].getLabels(); |
---|
114 | String label = null; |
---|
115 | for ( int j = 0 ; j < labels.length; j++ ){ |
---|
116 | if ( labels[j].getLanguage() == null ){ |
---|
117 | label = labels[j].getLabel(); |
---|
118 | break; |
---|
119 | }else if ( labels[j].getLanguage().equals("en") || labels[j].getLanguage().equals("") ){ |
---|
120 | label = labels[j].getLabel(); |
---|
121 | break; |
---|
122 | } |
---|
123 | } |
---|
124 | if ( label == null ){ |
---|
125 | label = extLabels.get(classURI); |
---|
126 | } |
---|
127 | if ( label == null ){ |
---|
128 | String[] uris = classURI.split("/"); |
---|
129 | String tmplabel = uris[uris.length-1]; |
---|
130 | String[] tmplabel2 = tmplabel.split("#"); |
---|
131 | label = tmplabel2[tmplabel2.length-1]; |
---|
132 | } |
---|
133 | clabels.put(classURI, label); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | public static String getClassLabelfromList(String classURI, SClass[] classes){ |
---|
138 | if ( classURI == null ){ |
---|
139 | return ""; |
---|
140 | } |
---|
141 | SClass sclass = null; |
---|
142 | for ( int i = 0 ; i < classes.length; i++ ){ |
---|
143 | if ( classURI.equals(classes[i].getClassURI()) ){ |
---|
144 | return getClassLabelfromClass(classes[i]); |
---|
145 | } |
---|
146 | } |
---|
147 | return ""; |
---|
148 | } |
---|
149 | |
---|
150 | public static String getClassLabelfromClass(SClass sclass){ |
---|
151 | Label[] labels = sclass.getLabels(); |
---|
152 | for ( int i = 0 ; i < labels.length; i++ ){ |
---|
153 | if ( labels[i].getLanguage() == null ){ |
---|
154 | return labels[i].getLabel(); |
---|
155 | }else if ( labels[i].getLanguage().equals("en") ){ |
---|
156 | return labels[i].getLabel(); |
---|
157 | } |
---|
158 | } |
---|
159 | String[] url = sclass.getClassURI().split("/"); |
---|
160 | String tmplabel = url[url.length-1]; |
---|
161 | String[] tmplabel2 = tmplabel.split("#"); |
---|
162 | String label = tmplabel2[tmplabel2.length-1]; |
---|
163 | return label; |
---|
164 | } |
---|
165 | |
---|
166 | public Map<String, String> getClassLabelsFromExternal(){ |
---|
167 | return OWLLabelReader.readLabels(ODIR); |
---|
168 | } |
---|
169 | |
---|
170 | public OWLClassGraph getOWLClassGraph(){ |
---|
171 | /*if ( graph == null ){ |
---|
172 | graph = new OWLClassGraph(analyzer); |
---|
173 | }*/ |
---|
174 | return graph; |
---|
175 | } |
---|
176 | |
---|
177 | /* |
---|
178 | public SClass[] getReachableClasses(){ |
---|
179 | List<String> clURIs = graph.getReachableClasses(); |
---|
180 | SClass[] orgclasses = null; |
---|
181 | try { |
---|
182 | orgclasses = analyzer.getOWLClasses(null, null, null, true); |
---|
183 | }catch( Exception e ){ |
---|
184 | System.err.println(e); |
---|
185 | return null; |
---|
186 | } |
---|
187 | HashMap<String, SClass> orgmap = new HashMap<String, SClass>(); |
---|
188 | for (int i = 0; i < orgclasses.length; i++ ){ |
---|
189 | orgmap.put(orgclasses[i].getClassURI(), orgclasses[i]); |
---|
190 | } |
---|
191 | |
---|
192 | SClass[] classes = new SClass[clURIs.size()]; |
---|
193 | int j = 0; |
---|
194 | ListIterator<String> uit = clURIs.listIterator(); |
---|
195 | while(uit.hasNext()){ |
---|
196 | String u = uit.next(); |
---|
197 | SClass cl = orgmap.get(u); |
---|
198 | classes[j] = cl; |
---|
199 | j++; |
---|
200 | } |
---|
201 | return classes; |
---|
202 | } |
---|
203 | */ |
---|
204 | |
---|
205 | public SortedSet<String> getSortedClasses(SClass[] classes){ |
---|
206 | setClassLabels(classes); |
---|
207 | SortedSet<String> sortedClasses = new TreeSet<String>(); |
---|
208 | for (int i = 0 ; i < classes.length; i++ ){ |
---|
209 | String uri = classes[i].getClassURI(); |
---|
210 | String label = getClassLabel(uri); |
---|
211 | StringBuilder classbuilder = new StringBuilder(label); |
---|
212 | classbuilder.append(" "); |
---|
213 | classbuilder.append(classes[i].getNumOfInstances()); |
---|
214 | classbuilder.append(" "); |
---|
215 | classbuilder.append(uri); |
---|
216 | sortedClasses.add(classbuilder.toString()); |
---|
217 | } |
---|
218 | |
---|
219 | return sortedClasses; |
---|
220 | } |
---|
221 | } |
---|