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