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