1 | package org.biohackathon.SPARQLBuilder.OWL;
|
---|
2 |
|
---|
3 | import java.util.*;
|
---|
4 |
|
---|
5 | import org.biohackathon.SPARQLBuilder.endpointMetadata.MetadataManager;
|
---|
6 |
|
---|
7 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.ClassPartition;
|
---|
8 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.ClassRelation;
|
---|
9 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.CrawledMetadata;
|
---|
10 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.Dataset;
|
---|
11 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.Label;
|
---|
12 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.PropertyPartition;
|
---|
13 |
|
---|
14 |
|
---|
15 | //public class OWLQueryBuilderForCrawlerImpl implements OWLQueryBuilder {
|
---|
16 | public class AcquiredStructureAnalyzer implements RDFSchemaAnalyzer {
|
---|
17 |
|
---|
18 | // key: endpointURI, value: crawled metadata
|
---|
19 | private HashMap<String, CrawledMetadata> crawledMetadataTable= null;
|
---|
20 |
|
---|
21 | public String[] getEndpointURIs(){
|
---|
22 | if( crawledMetadataTable == null ){
|
---|
23 | return new String[0];
|
---|
24 | }else{
|
---|
25 | return crawledMetadataTable.keySet().toArray(new String[0]);
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | public String[] getGraphURIs(String endpointURI){
|
---|
30 | if( crawledMetadataTable == null ){
|
---|
31 | return new String[0];
|
---|
32 | }else{
|
---|
33 | CrawledMetadata crawledMetadata = crawledMetadataTable.get(endpointURI);
|
---|
34 | if( crawledMetadata == null ){
|
---|
35 | return new String[0];
|
---|
36 | }else{
|
---|
37 | return crawledMetadata.getGraphURIs();
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | public AcquiredStructureAnalyzer(MetadataManager metadataManager){
|
---|
44 | CrawledMetadata[] cmList = metadataManager.getCrawlerMetadataList();
|
---|
45 | crawledMetadataTable = new HashMap<String, CrawledMetadata>();
|
---|
46 |
|
---|
47 | if( cmList != null ){
|
---|
48 | for(CrawledMetadata cm: cmList){
|
---|
49 | String endpointURI = cm.getEndpointURI();
|
---|
50 | crawledMetadataTable.put(endpointURI, cm);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | public SClass[] listClasses() throws Exception{
|
---|
58 | return getOWLClasses(null, null);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | public SClass[] getOWLClasses(String[] keywords, String language) throws Exception{
|
---|
63 | return getOWLClassList(keywords, language).toArray(new SClass[0]);
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | public List<SClass> getOWLClassList(String[] keywords, String language) throws Exception{
|
---|
68 | ArrayList<SClass> classList = new ArrayList<SClass>();
|
---|
69 |
|
---|
70 | Set<String> endpointURISet = crawledMetadataTable.keySet();
|
---|
71 | for(String endpointURI: endpointURISet){
|
---|
72 | CrawledMetadata cm = crawledMetadataTable.get(endpointURI);
|
---|
73 | // default
|
---|
74 | Dataset dataset = cm.getDefaultDataset();
|
---|
75 | List<SClass> tempClassList = getOWLClassList(endpointURI, null, dataset, keywords, language);
|
---|
76 | for(SClass sClass: tempClassList){
|
---|
77 | classList.add(sClass);
|
---|
78 | }
|
---|
79 |
|
---|
80 | // graphs
|
---|
81 | String[] graphURIs = cm.getGraphURIs();
|
---|
82 | if( graphURIs != null ){
|
---|
83 | for(String graphURI: graphURIs){
|
---|
84 | dataset = cm.getDataset(graphURI);
|
---|
85 | tempClassList = getOWLClassList(endpointURI, graphURI, dataset, keywords, language);
|
---|
86 | for(SClass sClass: tempClassList){
|
---|
87 | classList.add(sClass);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return classList;
|
---|
93 | }
|
---|
94 |
|
---|
95 | private List<SClass> getOWLClassList(String endpointURI, String graphURI, Dataset dataset, String[] keywords, String language) throws Exception{
|
---|
96 | ArrayList<SClass> results = new ArrayList<SClass>();
|
---|
97 |
|
---|
98 | ClassPartition[] classPartitionList = dataset.getClassPartitions();
|
---|
99 | if( classPartitionList == null || classPartitionList.length == 0 ){
|
---|
100 | return new ArrayList<SClass>();
|
---|
101 | }
|
---|
102 | for( ClassPartition cp: classPartitionList){
|
---|
103 | String classURI = cp.classDef.classURI;
|
---|
104 | Label[] rLabels = cp.classDef.labels;
|
---|
105 | org.biohackathon.SPARQLBuilder.OWL.Label[] labels = null;
|
---|
106 | if( rLabels == null ){
|
---|
107 | labels = new org.biohackathon.SPARQLBuilder.OWL.Label[0];
|
---|
108 | }else{
|
---|
109 | labels = new org.biohackathon.SPARQLBuilder.OWL.Label[rLabels.length];
|
---|
110 | for(int i = 0; i < rLabels.length; i++ ) {
|
---|
111 | labels[i] = new org.biohackathon.SPARQLBuilder.OWL.Label(rLabels[i].value, rLabels[i].language);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | int entities = cp.entities;
|
---|
115 |
|
---|
116 | if( keywords == null || keywords.length == 0 ){
|
---|
117 | SClass sClass = new SClass(classURI, labels, entities, endpointURI, graphURI);
|
---|
118 | results.add(sClass);
|
---|
119 | }else{
|
---|
120 | boolean hit = false;
|
---|
121 | for(org.biohackathon.SPARQLBuilder.OWL.Label label: labels){
|
---|
122 | if( language == null || label.getLanguage().equals(language)){
|
---|
123 | String value = label.getLabel();
|
---|
124 | if( value != null ){
|
---|
125 | value = value.toLowerCase().trim();
|
---|
126 | for(String keyword: keywords){
|
---|
127 | if( value.contains(keyword.toLowerCase().trim())){
|
---|
128 | hit = true;
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | if( hit ){
|
---|
135 | SClass sClass = new SClass(classURI, labels, entities, endpointURI, graphURI);
|
---|
136 | results.add(sClass);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return results;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 | public ClassLink[] getNextClass(String originClass, int limit) throws Exception{
|
---|
149 |
|
---|
150 | ArrayList<ClassLink> classLinkList = new ArrayList<ClassLink>();
|
---|
151 |
|
---|
152 | Set<String> endpointURISet = crawledMetadataTable.keySet();
|
---|
153 | for(String endpointURI: endpointURISet){
|
---|
154 | CrawledMetadata cm = crawledMetadataTable.get(endpointURI);
|
---|
155 | // default
|
---|
156 | Dataset dataset = cm.getDefaultDataset();
|
---|
157 | List<ClassLink> tempClassLinkList = getNextClass(endpointURI, null, dataset, originClass, limit);
|
---|
158 | for(ClassLink classLink: tempClassLinkList){
|
---|
159 | classLinkList.add(classLink);
|
---|
160 | }
|
---|
161 |
|
---|
162 | // graphs
|
---|
163 | String[] graphURIs = cm.getGraphURIs();
|
---|
164 | if( graphURIs != null ){
|
---|
165 | for(String graphURI: graphURIs){
|
---|
166 | dataset = cm.getDataset(graphURI);
|
---|
167 | tempClassLinkList = getNextClass(endpointURI, graphURI, dataset, originClass, limit);
|
---|
168 | for(ClassLink classLink: tempClassLinkList){
|
---|
169 | classLinkList.add(classLink);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | return classLinkList.toArray(new ClassLink[0]);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|
179 | private List<ClassLink> getNextClass(String endpointURI, String graphURI, Dataset dataset, String originClass, int limit) throws Exception{
|
---|
180 | ArrayList<ClassLink> classLinkList = new ArrayList<ClassLink>();
|
---|
181 |
|
---|
182 | PropertyPartition[] pps = dataset.getPropertyPartitions();
|
---|
183 | if( pps == null ){
|
---|
184 | return classLinkList;
|
---|
185 | }
|
---|
186 |
|
---|
187 | for(PropertyPartition pp: pps){
|
---|
188 | ClassRelation[] classRelations = pp.classRelations;
|
---|
189 | if( classRelations != null ){
|
---|
190 | for(ClassRelation classRelation: classRelations){
|
---|
191 | String subjClassURI = classRelation.subjectClassURI;
|
---|
192 | String objClassURI = classRelation.objectClassURI;
|
---|
193 | boolean forward = false;
|
---|
194 | boolean reverse = false;
|
---|
195 | if( objClassURI != null && objClassURI.equals(originClass) ){
|
---|
196 | if( subjClassURI != null ){
|
---|
197 | reverse = true;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | if(subjClassURI != null && subjClassURI.equals(originClass)){
|
---|
201 | if( objClassURI != null || classRelation.objectDatatypeURI != null ){
|
---|
202 | forward = true;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | ClassLink classLink = null;
|
---|
206 | if( forward && !reverse ){
|
---|
207 | classLink = new ClassLink();
|
---|
208 | classLink.setDirection(Direction.forward);
|
---|
209 | classLink.setNumOfOriginClassInstances(classRelation.distinctSubjects);
|
---|
210 | if( objClassURI != null ){
|
---|
211 | classLink.setLinkedClassURI(objClassURI);
|
---|
212 | classLink.setNumOfLinkedClassInstances(classRelation.distinctObjects);
|
---|
213 | }else{
|
---|
214 | classLink.setLinkedLiteralDatatypeURI(classRelation.objectDatatypeURI);
|
---|
215 | classLink.setNumOfLinkedInstances(classRelation.triples);
|
---|
216 | }
|
---|
217 | classLink.setNumOfOriginInstances(pp.distinctSubjects);
|
---|
218 | classLink.setNumOfLinkedInstances(pp.distinctObjects);
|
---|
219 |
|
---|
220 | }
|
---|
221 | if( !forward && reverse ){
|
---|
222 | classLink = new ClassLink();
|
---|
223 | classLink.setDirection(Direction.reverse);
|
---|
224 | classLink.setLinkedClassURI(objClassURI);
|
---|
225 | classLink.setNumOfOriginClassInstances(classRelation.distinctObjects);
|
---|
226 | classLink.setNumOfOriginInstances(pp.distinctObjects);
|
---|
227 | classLink.setNumOfLinkedInstances(pp.distinctSubjects);
|
---|
228 | classLink.setNumOfLinkedClassInstances(classRelation.distinctSubjects);
|
---|
229 | }
|
---|
230 | if( forward && reverse){
|
---|
231 | classLink = new ClassLink();
|
---|
232 | classLink.setDirection(Direction.both);
|
---|
233 | classLink.setLinkedClassURI(objClassURI);
|
---|
234 | classLink.setNumOfOriginClassInstances(classRelation.distinctSubjects);
|
---|
235 | classLink.setNumOfOriginInstances(pp.distinctSubjects);
|
---|
236 | classLink.setNumOfLinkedInstances(pp.distinctObjects);
|
---|
237 | classLink.setNumOfLinkedClassInstances(classRelation.distinctObjects);
|
---|
238 | }
|
---|
239 | // hit
|
---|
240 | if( classLink != null ){
|
---|
241 | classLink.setEndpointURI(endpointURI);
|
---|
242 | classLink.setGraphURI(graphURI);
|
---|
243 | classLink.setPropertyURI(pp.propertyDef.propertyURI);
|
---|
244 | classLink.setNumOfLinks(classRelation.triples);
|
---|
245 | classLinkList.add(classLink);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|
250 | return classLinkList;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 |
|
---|
259 | public LabelMap[] getLabels(String[] resourceURIs, String language) throws Exception {
|
---|
260 | if( resourceURIs == null || resourceURIs.length == 0 ){
|
---|
261 | return new LabelMap[0];
|
---|
262 | }
|
---|
263 |
|
---|
264 | HashSet<String> resourceURIset = new HashSet<String>();
|
---|
265 | for(String resourceURI: resourceURIs){
|
---|
266 | resourceURIset.add(resourceURI);
|
---|
267 | }
|
---|
268 |
|
---|
269 | HashMap<String, LabelMap> labelMapTable = new HashMap<String, LabelMap>();
|
---|
270 |
|
---|
271 | Set<String> endpointURISet = crawledMetadataTable.keySet();
|
---|
272 | for(String endpointURI: endpointURISet){
|
---|
273 | CrawledMetadata cm = crawledMetadataTable.get(endpointURI);
|
---|
274 | // default
|
---|
275 | Dataset dataset = cm.getDefaultDataset();
|
---|
276 | labelMapTable = getLabels(dataset, resourceURIset, language, labelMapTable);
|
---|
277 |
|
---|
278 | // graphs
|
---|
279 | String[] graphURIs = cm.getGraphURIs();
|
---|
280 | if( graphURIs != null ){
|
---|
281 | for(String graphURI: graphURIs){
|
---|
282 | dataset = cm.getDataset(graphURI);
|
---|
283 | labelMapTable = getLabels(dataset, resourceURIset, language, labelMapTable);
|
---|
284 | }
|
---|
285 | }
|
---|
286 | }
|
---|
287 | return labelMapTable.values().toArray(new LabelMap[0]);
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | private HashMap<String, LabelMap> getLabels(Dataset dataset, HashSet<String> resourceURISet , String language, HashMap<String, LabelMap> labelMapTable) throws Exception {
|
---|
292 | ClassPartition[] cps = dataset.getClassPartitions();
|
---|
293 | if( cps != null ){
|
---|
294 | for(ClassPartition cp: cps){
|
---|
295 | String uri = cp.classDef.classURI;
|
---|
296 | if( resourceURISet.contains(uri)){
|
---|
297 | Label[] rLabels = cp.classDef.labels;
|
---|
298 | if( rLabels != null ){
|
---|
299 | for(Label rLabel: rLabels){
|
---|
300 | if( language == null || ( rLabel.language == null || rLabel.language.equals(language))){
|
---|
301 | LabelMap labelMap = null;
|
---|
302 | if( labelMapTable.containsKey(uri)){
|
---|
303 | labelMap = labelMapTable.get(uri);
|
---|
304 | }else{
|
---|
305 | labelMap = new LabelMap();
|
---|
306 | labelMapTable.put(uri, labelMap);
|
---|
307 | labelMap.setResourceURI(uri);
|
---|
308 | }
|
---|
309 | labelMap.addLabel(new org.biohackathon.SPARQLBuilder.OWL.Label(rLabel.value, rLabel.language));
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | PropertyPartition[] pps = dataset.getPropertyPartitions();
|
---|
318 | if( pps != null ){
|
---|
319 | for(PropertyPartition pp: pps){
|
---|
320 | String uri = pp.propertyDef.propertyURI;
|
---|
321 | if( resourceURISet.contains(uri)){
|
---|
322 | Label[] rLabels = pp.propertyDef.labels;
|
---|
323 | if( rLabels != null ){
|
---|
324 | for(Label rLabel: rLabels){
|
---|
325 | if( language == null || ( rLabel.language == null || rLabel.language.equals(language))){
|
---|
326 | LabelMap labelMap = null;
|
---|
327 | if( labelMapTable.containsKey(uri)){
|
---|
328 | labelMap = labelMapTable.get(uri);
|
---|
329 | }else{
|
---|
330 | labelMap = new LabelMap();
|
---|
331 | labelMapTable.put(uri, labelMap);
|
---|
332 | labelMap.setResourceURI(uri);
|
---|
333 | }
|
---|
334 | labelMap.addLabel(new org.biohackathon.SPARQLBuilder.OWL.Label(rLabel.value, rLabel.language));
|
---|
335 | }
|
---|
336 | }
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|
340 | }
|
---|
341 | return labelMapTable;
|
---|
342 | }
|
---|
343 |
|
---|
344 | } |
---|