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