1
|
|
|
/* |
2
|
|
|
* |
3
|
|
|
* Copyright (C) 2009-2017 Julian Mendez |
4
|
|
|
* |
5
|
|
|
* |
6
|
|
|
* This file is part of jcel. |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* The contents of this file are subject to the GNU Lesser General Public License |
10
|
|
|
* version 3 |
11
|
|
|
* |
12
|
|
|
* |
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
15
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
16
|
|
|
* (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* This program is distributed in the hope that it will be useful, |
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21
|
|
|
* GNU Lesser General Public License for more details. |
22
|
|
|
* |
23
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
25
|
|
|
* |
26
|
|
|
* |
27
|
|
|
* Alternatively, the contents of this file may be used under the terms |
28
|
|
|
* of the Apache License, Version 2.0, in which case the |
29
|
|
|
* provisions of the Apache License, Version 2.0 are applicable instead of those |
30
|
|
|
* above. |
31
|
|
|
* |
32
|
|
|
* |
33
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
34
|
|
|
* you may not use this file except in compliance with the License. |
35
|
|
|
* You may obtain a copy of the License at |
36
|
|
|
* |
37
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
38
|
|
|
* |
39
|
|
|
* Unless required by applicable law or agreed to in writing, software |
40
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
41
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
42
|
|
|
* See the License for the specific language governing permissions and |
43
|
|
|
* limitations under the License. |
44
|
|
|
* |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
package de.tudresden.inf.lat.jcel.coreontology.axiom; |
48
|
|
|
|
49
|
|
|
import java.util.Collections; |
50
|
|
|
import java.util.HashMap; |
51
|
|
|
import java.util.HashSet; |
52
|
|
|
import java.util.Map; |
53
|
|
|
import java.util.Objects; |
54
|
|
|
import java.util.Set; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This class models an ontology with the property to look up by axiom type. |
58
|
|
|
* |
59
|
|
|
* @author Julian Mendez |
60
|
|
|
*/ |
61
|
|
|
public class ExtendedOntologyImpl implements ExtendedOntology, NormalizedIntegerAxiomVisitor<Boolean> { |
62
|
|
|
|
63
|
|
|
private final Map<Integer, Set<GCI0Axiom>> mapOfGCI0 = new HashMap<>(); |
64
|
|
|
private final Map<Integer, Set<GCI1Axiom>> mapOfGCI1 = new HashMap<>(); |
65
|
|
|
private final Map<Integer, Set<GCI2Axiom>> mapOfGCI2 = new HashMap<>(); |
66
|
|
|
private final Map<Integer, Set<GCI3Axiom>> mapOfGCI3A = new HashMap<>(); |
67
|
|
|
private final Map<Integer, Set<GCI3Axiom>> mapOfGCI3r = new HashMap<>(); |
68
|
|
|
private final Map<Integer, Map<Integer, Set<GCI3Axiom>>> mapOfGCI3rA = new HashMap<>(); |
69
|
|
|
private final Map<Integer, Set<NominalAxiom>> mapOfNominalAxiom = new HashMap<>(); |
70
|
|
|
private final Map<Integer, Set<RangeAxiom>> mapOfRangeAxiom = new HashMap<>(); |
71
|
|
|
private final Map<Integer, Set<RI2Axiom>> mapOfRI2r = new HashMap<>(); |
72
|
|
|
private final Map<Integer, Set<RI2Axiom>> mapOfRI2s = new HashMap<>(); |
73
|
|
|
private final Map<Integer, Set<RI3Axiom>> mapOfRI3ByLeft = new HashMap<>(); |
74
|
|
|
private final Map<Integer, Set<RI3Axiom>> mapOfRI3ByRight = new HashMap<>(); |
75
|
|
|
private final Set<Integer> setOfAllObjectProperties = new HashSet<>(); |
76
|
|
|
private final Set<Integer> setOfClasses = new HashSet<>(); |
77
|
|
|
private final Set<Integer> setOfFunctionalObjectProperties = new HashSet<>(); |
78
|
|
|
private final Set<Integer> setOfReflexiveObjectProperties = new HashSet<>(); |
79
|
|
|
private final Set<Integer> setOfTransitiveObjectProperties = new HashSet<>(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Constructs an empty ontology. |
83
|
|
|
*/ |
84
|
|
|
public ExtendedOntologyImpl() { |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
@Override |
88
|
|
|
public void addClass(int classId) { |
89
|
|
|
this.setOfClasses.add(classId); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private void addEntities(NormalizedIntegerAxiom axiom) { |
93
|
|
|
this.setOfAllObjectProperties.addAll(axiom.getObjectPropertiesInSignature()); |
94
|
|
|
this.setOfClasses.addAll(axiom.getClassesInSignature()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private void addGCI0Axiom(int classId, GCI0Axiom axiom) { |
98
|
|
|
if (Objects.isNull(this.mapOfGCI0.get(classId))) { |
99
|
|
|
this.mapOfGCI0.put(classId, new HashSet<>()); |
100
|
|
|
} |
101
|
|
|
this.mapOfGCI0.get(classId).add(axiom); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private void addGCI1Axiom(int classId, GCI1Axiom axiom) { |
105
|
|
|
if (Objects.isNull(this.mapOfGCI1.get(classId))) { |
106
|
|
|
this.mapOfGCI1.put(classId, new HashSet<>()); |
107
|
|
|
} |
108
|
|
|
this.mapOfGCI1.get(classId).add(axiom); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private void addGCI2Axiom(int classId, GCI2Axiom axiom) { |
112
|
|
|
if (Objects.isNull(this.mapOfGCI2.get(classId))) { |
113
|
|
|
this.mapOfGCI2.put(classId, new HashSet<>()); |
114
|
|
|
} |
115
|
|
|
this.mapOfGCI2.get(classId).add(axiom); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private void addGCI3Axiom(GCI3Axiom axiom, int objectPropertyId, int classId) { |
119
|
|
|
|
120
|
|
|
if (Objects.isNull(this.mapOfGCI3r.get(objectPropertyId))) { |
121
|
|
|
this.mapOfGCI3r.put(objectPropertyId, new HashSet<>()); |
122
|
|
|
} |
123
|
|
|
this.mapOfGCI3r.get(objectPropertyId).add(axiom); |
124
|
|
|
|
125
|
|
|
if (Objects.isNull(this.mapOfGCI3A.get(classId))) { |
126
|
|
|
this.mapOfGCI3A.put(classId, new HashSet<>()); |
127
|
|
|
} |
128
|
|
|
this.mapOfGCI3A.get(classId).add(axiom); |
129
|
|
|
|
130
|
|
|
Map<Integer, Set<GCI3Axiom>> map = this.mapOfGCI3rA.get(objectPropertyId); |
131
|
|
|
if (Objects.isNull(map)) { |
132
|
|
|
map = new HashMap<>(); |
133
|
|
|
this.mapOfGCI3rA.put(objectPropertyId, map); |
134
|
|
|
} |
135
|
|
|
if (Objects.isNull(map.get(classId))) { |
136
|
|
|
map.put(classId, new HashSet<>()); |
137
|
|
|
} |
138
|
|
|
map.get(classId).add(axiom); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private void addNominalAxiom(int individualId, NominalAxiom axiom) { |
142
|
|
|
if (Objects.isNull(this.mapOfNominalAxiom.get(individualId))) { |
143
|
|
|
this.mapOfNominalAxiom.put(individualId, new HashSet<>()); |
144
|
|
|
} |
145
|
|
|
this.mapOfNominalAxiom.get(individualId).add(axiom); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
@Override |
149
|
|
|
public void addObjectProperty(int objectProperty) { |
150
|
|
|
this.setOfAllObjectProperties.add(objectProperty); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private void addRangeAxiom(int propertyId, RangeAxiom axiom) { |
154
|
|
|
if (Objects.isNull(this.mapOfRangeAxiom.get(propertyId))) { |
155
|
|
|
this.mapOfRangeAxiom.put(propertyId, new HashSet<>()); |
156
|
|
|
} |
157
|
|
|
this.mapOfRangeAxiom.get(propertyId).add(axiom); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private void addTo(int property, RI3Axiom axiom, Map<Integer, Set<RI3Axiom>> map) { |
161
|
|
|
Set<RI3Axiom> axiomSet = map.get(property); |
162
|
|
|
if (Objects.isNull(axiomSet)) { |
163
|
|
|
axiomSet = new HashSet<>(); |
164
|
|
|
map.put(property, axiomSet); |
165
|
|
|
} |
166
|
|
|
axiomSet.add(axiom); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
@Override |
170
|
|
|
public void clear() { |
171
|
|
|
this.setOfClasses.clear(); |
172
|
|
|
this.setOfAllObjectProperties.clear(); |
173
|
|
|
this.mapOfGCI0.clear(); |
174
|
|
|
this.mapOfGCI1.clear(); |
175
|
|
|
this.mapOfGCI2.clear(); |
176
|
|
|
this.mapOfGCI3A.clear(); |
177
|
|
|
this.mapOfGCI3r.clear(); |
178
|
|
|
this.mapOfGCI3rA.clear(); |
179
|
|
|
this.mapOfRI2r.clear(); |
180
|
|
|
this.mapOfRI2s.clear(); |
181
|
|
|
this.mapOfRI3ByLeft.clear(); |
182
|
|
|
this.mapOfRI3ByRight.clear(); |
183
|
|
|
this.mapOfNominalAxiom.clear(); |
184
|
|
|
this.mapOfRangeAxiom.clear(); |
185
|
|
|
this.setOfTransitiveObjectProperties.clear(); |
186
|
|
|
this.setOfFunctionalObjectProperties.clear(); |
187
|
|
|
this.setOfReflexiveObjectProperties.clear(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
@Override |
191
|
|
|
public Set<Integer> getClassSet() { |
192
|
|
|
return Collections.unmodifiableSet(this.setOfClasses); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
@Override |
196
|
|
|
public Set<Integer> getFunctionalObjectProperties() { |
197
|
|
|
return Collections.unmodifiableSet(this.setOfFunctionalObjectProperties); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
@Override |
201
|
|
|
public Set<GCI0Axiom> getGCI0Axioms(int classId) { |
202
|
|
|
Set<GCI0Axiom> ret = this.mapOfGCI0.get(classId); |
203
|
|
|
if (Objects.isNull(ret)) { |
204
|
|
|
ret = Collections.emptySet(); |
205
|
|
|
} |
206
|
|
|
return Collections.unmodifiableSet(ret); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
@Override |
210
|
|
|
public Set<GCI1Axiom> getGCI1Axioms(int classId) { |
211
|
|
|
Set<GCI1Axiom> ret = this.mapOfGCI1.get(classId); |
212
|
|
|
if (Objects.isNull(ret)) { |
213
|
|
|
ret = Collections.emptySet(); |
214
|
|
|
} |
215
|
|
|
return Collections.unmodifiableSet(ret); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
@Override |
219
|
|
|
public Set<GCI2Axiom> getGCI2Axioms(int classId) { |
220
|
|
|
Set<GCI2Axiom> ret = this.mapOfGCI2.get(classId); |
221
|
|
|
if (Objects.isNull(ret)) { |
222
|
|
|
ret = Collections.emptySet(); |
223
|
|
|
} |
224
|
|
|
return Collections.unmodifiableSet(ret); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
@Override |
228
|
|
|
public Set<GCI3Axiom> getGCI3AAxioms(int classId) { |
229
|
|
|
Set<GCI3Axiom> ret = this.mapOfGCI3A.get(classId); |
230
|
|
|
if (Objects.isNull(ret)) { |
231
|
|
|
ret = Collections.emptySet(); |
232
|
|
|
} |
233
|
|
|
return Collections.unmodifiableSet(ret); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
@Override |
237
|
|
|
public Set<GCI3Axiom> getGCI3rAAxioms(int objectPropertyId, int leftClassId) { |
238
|
|
|
Set<GCI3Axiom> ret = null; |
239
|
|
|
Map<Integer, Set<GCI3Axiom>> map = this.mapOfGCI3rA.get(objectPropertyId); |
240
|
|
|
if (Objects.nonNull(map)) { |
241
|
|
|
ret = map.get(leftClassId); |
242
|
|
|
} |
243
|
|
|
if (Objects.isNull(ret)) { |
244
|
|
|
ret = Collections.emptySet(); |
245
|
|
|
} |
246
|
|
|
return Collections.unmodifiableSet(ret); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
@Override |
250
|
|
|
public Set<GCI3Axiom> getGCI3rAxioms(int objectPropertyId) { |
251
|
|
|
Set<GCI3Axiom> ret = this.mapOfGCI3r.get(objectPropertyId); |
252
|
|
|
if (Objects.isNull(ret)) { |
253
|
|
|
ret = Collections.emptySet(); |
254
|
|
|
} |
255
|
|
|
return Collections.unmodifiableSet(ret); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
@Override |
259
|
|
|
public Set<Integer> getObjectPropertySet() { |
260
|
|
|
return Collections.unmodifiableSet(this.setOfAllObjectProperties); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
@Override |
264
|
|
|
public Set<Integer> getReflexiveObjectProperties() { |
265
|
|
|
return Collections.unmodifiableSet(this.setOfReflexiveObjectProperties); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
@Override |
269
|
|
|
public Set<RI2Axiom> getRI2rAxioms(int elem) { |
270
|
|
|
Set<RI2Axiom> ret = this.mapOfRI2r.get(elem); |
271
|
|
|
if (Objects.isNull(ret)) { |
272
|
|
|
ret = Collections.emptySet(); |
273
|
|
|
} |
274
|
|
|
return Collections.unmodifiableSet(ret); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
@Override |
278
|
|
|
public Set<RI2Axiom> getRI2sAxioms(int elem) { |
279
|
|
|
Set<RI2Axiom> ret = this.mapOfRI2s.get(elem); |
280
|
|
|
if (Objects.isNull(ret)) { |
281
|
|
|
ret = Collections.emptySet(); |
282
|
|
|
} |
283
|
|
|
return Collections.unmodifiableSet(ret); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
@Override |
287
|
|
|
public Set<RI3Axiom> getRI3AxiomsByLeft(int elem) { |
288
|
|
|
Set<RI3Axiom> ret = this.mapOfRI3ByLeft.get(elem); |
289
|
|
|
if (Objects.isNull(ret)) { |
290
|
|
|
ret = Collections.emptySet(); |
291
|
|
|
} |
292
|
|
|
return Collections.unmodifiableSet(ret); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
@Override |
296
|
|
|
public Set<RI3Axiom> getRI3AxiomsByRight(int elem) { |
297
|
|
|
Set<RI3Axiom> ret = this.mapOfRI3ByRight.get(elem); |
298
|
|
|
if (Objects.isNull(ret)) { |
299
|
|
|
ret = Collections.emptySet(); |
300
|
|
|
} |
301
|
|
|
return Collections.unmodifiableSet(ret); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
@Override |
305
|
|
|
public Set<Integer> getTransitiveObjectProperties() { |
306
|
|
|
return Collections.unmodifiableSet(this.setOfTransitiveObjectProperties); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
@Override |
310
|
|
|
public void load(Set<NormalizedIntegerAxiom> axiomSet) { |
311
|
|
|
Objects.requireNonNull(axiomSet); |
312
|
|
|
axiomSet.forEach(axiom -> { |
313
|
|
|
axiom.accept(this); |
314
|
|
|
addEntities(axiom); |
315
|
|
|
}); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
@Override |
319
|
|
|
public String toString() { |
320
|
|
|
StringBuffer sbuf = new StringBuffer(); |
321
|
|
|
sbuf.append("["); |
322
|
|
|
sbuf.append("map of GCI0 =" + this.mapOfGCI0.toString()); |
323
|
|
|
sbuf.append("map of GCI1 =" + this.mapOfGCI1.toString()); |
324
|
|
|
sbuf.append("map of GCI2 =" + this.mapOfGCI2.toString()); |
325
|
|
|
sbuf.append("map of GCI3 =" + this.mapOfGCI3r.toString()); |
326
|
|
|
sbuf.append("map of RI2 =" + this.mapOfRI2r.toString()); |
327
|
|
|
sbuf.append("set of functional =" + this.setOfFunctionalObjectProperties.toString()); |
328
|
|
|
sbuf.append("set of reflexive =" + this.setOfReflexiveObjectProperties.toString()); |
329
|
|
|
sbuf.append("set of transitive =" + this.setOfTransitiveObjectProperties.toString()); |
330
|
|
|
sbuf.append("]"); |
331
|
|
|
return sbuf.toString(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
@Override |
335
|
|
|
public Boolean visit(FunctObjectPropAxiom axiom) { |
336
|
|
|
Objects.requireNonNull(axiom); |
337
|
|
|
this.setOfFunctionalObjectProperties.add(axiom.getProperty()); |
338
|
|
|
return true; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
@Override |
342
|
|
|
public Boolean visit(GCI0Axiom axiom) { |
343
|
|
|
Objects.requireNonNull(axiom); |
344
|
|
|
addGCI0Axiom(axiom.getSubClass(), axiom); |
345
|
|
|
return true; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
@Override |
349
|
|
|
public Boolean visit(GCI1Axiom axiom) { |
350
|
|
|
Objects.requireNonNull(axiom); |
351
|
|
|
addGCI1Axiom(axiom.getLeftSubClass(), axiom); |
352
|
|
|
addGCI1Axiom(axiom.getRightSubClass(), axiom); |
353
|
|
|
return true; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
@Override |
357
|
|
|
public Boolean visit(GCI2Axiom axiom) { |
358
|
|
|
Objects.requireNonNull(axiom); |
359
|
|
|
addGCI2Axiom(axiom.getSubClass(), axiom); |
360
|
|
|
return true; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
@Override |
364
|
|
|
public Boolean visit(GCI3Axiom axiom) { |
365
|
|
|
Objects.requireNonNull(axiom); |
366
|
|
|
addGCI3Axiom(axiom, axiom.getPropertyInSubClass(), axiom.getClassInSubClass()); |
367
|
|
|
return true; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
@Override |
371
|
|
|
public Boolean visit(NominalAxiom axiom) { |
372
|
|
|
Objects.requireNonNull(axiom); |
373
|
|
|
addNominalAxiom(axiom.getIndividual(), axiom); |
374
|
|
|
return true; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
@Override |
378
|
|
|
public Boolean visit(RangeAxiom axiom) { |
379
|
|
|
Objects.requireNonNull(axiom); |
380
|
|
|
addRangeAxiom(axiom.getProperty(), axiom); |
381
|
|
|
return true; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
@Override |
385
|
|
|
public Boolean visit(RI1Axiom axiom) { |
386
|
|
|
Objects.requireNonNull(axiom); |
387
|
|
|
this.setOfReflexiveObjectProperties.add(axiom.getSuperProperty()); |
388
|
|
|
return true; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
@Override |
392
|
|
|
public Boolean visit(RI2Axiom axiom) { |
393
|
|
|
Objects.requireNonNull(axiom); |
394
|
|
|
Integer subProperty = axiom.getSubProperty(); |
395
|
|
|
if (Objects.isNull(this.mapOfRI2r.get(subProperty))) { |
396
|
|
|
this.mapOfRI2r.put(subProperty, new HashSet<>()); |
397
|
|
|
} |
398
|
|
|
this.mapOfRI2r.get(subProperty).add(axiom); |
399
|
|
|
|
400
|
|
|
Integer superProperty = axiom.getSuperProperty(); |
401
|
|
|
if (Objects.isNull(this.mapOfRI2s.get(superProperty))) { |
402
|
|
|
this.mapOfRI2s.put(superProperty, new HashSet<>()); |
403
|
|
|
} |
404
|
|
|
this.mapOfRI2s.get(superProperty).add(axiom); |
405
|
|
|
|
406
|
|
|
return true; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
@Override |
410
|
|
|
public Boolean visit(RI3Axiom axiom) { |
411
|
|
|
Objects.requireNonNull(axiom); |
412
|
|
|
Integer left = axiom.getLeftSubProperty(); |
413
|
|
|
Integer right = axiom.getRightSubProperty(); |
414
|
|
|
addTo(left, axiom, this.mapOfRI3ByLeft); |
415
|
|
|
addTo(right, axiom, this.mapOfRI3ByRight); |
416
|
|
|
if (left.equals(axiom.getSuperProperty()) && right.equals(axiom.getSuperProperty())) { |
417
|
|
|
this.setOfTransitiveObjectProperties.add(left); |
418
|
|
|
} |
419
|
|
|
return true; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
} |
423
|
|
|
|