Passed
Push — master ( e0b928...937cde )
by Julian
03:04 queued 12s
created

addTo(int,RI3Axiom,OptMap)   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
dl 0
loc 7
rs 10
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.Objects;
53
import java.util.Optional;
54
import java.util.Set;
55
56
import de.tudresden.inf.lat.util.map.OptMap;
57
import de.tudresden.inf.lat.util.map.OptMapImpl;
58
59
/**
60
 * This class models an ontology with the property to look up by axiom type.
61
 * 
62
 * @author Julian Mendez
63
 */
64
public class ExtendedOntologyImpl implements ExtendedOntology, NormalizedIntegerAxiomVisitor<Boolean> {
65
66
	private final OptMap<Integer, Set<GCI0Axiom>> mapOfGCI0 = new OptMapImpl<>(new HashMap<>());
67
	private final OptMap<Integer, Set<GCI1Axiom>> mapOfGCI1 = new OptMapImpl<>(new HashMap<>());
68
	private final OptMap<Integer, Set<GCI2Axiom>> mapOfGCI2 = new OptMapImpl<>(new HashMap<>());
69
	private final OptMap<Integer, Set<GCI3Axiom>> mapOfGCI3A = new OptMapImpl<>(new HashMap<>());
70
	private final OptMap<Integer, Set<GCI3Axiom>> mapOfGCI3r = new OptMapImpl<>(new HashMap<>());
71
	private final OptMap<Integer, OptMap<Integer, Set<GCI3Axiom>>> mapOfGCI3rA = new OptMapImpl<>(new HashMap<>());
72
	private final OptMap<Integer, Set<NominalAxiom>> mapOfNominalAxiom = new OptMapImpl<>(new HashMap<>());
73
	private final OptMap<Integer, Set<RangeAxiom>> mapOfRangeAxiom = new OptMapImpl<>(new HashMap<>());
74
	private final OptMap<Integer, Set<RI2Axiom>> mapOfRI2r = new OptMapImpl<>(new HashMap<>());
75
	private final OptMap<Integer, Set<RI2Axiom>> mapOfRI2s = new OptMapImpl<>(new HashMap<>());
76
	private final OptMap<Integer, Set<RI3Axiom>> mapOfRI3ByLeft = new OptMapImpl<>(new HashMap<>());
77
	private final OptMap<Integer, Set<RI3Axiom>> mapOfRI3ByRight = new OptMapImpl<>(new HashMap<>());
78
	private final Set<Integer> setOfAllObjectProperties = new HashSet<>();
79
	private final Set<Integer> setOfClasses = new HashSet<>();
80
	private final Set<Integer> setOfFunctionalObjectProperties = new HashSet<>();
81
	private final Set<Integer> setOfReflexiveObjectProperties = new HashSet<>();
82
	private final Set<Integer> setOfTransitiveObjectProperties = new HashSet<>();
83
84
	/**
85
	 * Constructs an empty ontology.
86
	 */
87
	public ExtendedOntologyImpl() {
88
	}
89
90
	@Override
91
	public void addClass(int classId) {
92
		this.setOfClasses.add(classId);
93
	}
94
95
	private void addEntities(NormalizedIntegerAxiom axiom) {
96
		this.setOfAllObjectProperties.addAll(axiom.getObjectPropertiesInSignature());
97
		this.setOfClasses.addAll(axiom.getClassesInSignature());
98
	}
99
100
	private void addGCI0Axiom(int classId, GCI0Axiom axiom) {
101
		if (!this.mapOfGCI0.get(classId).isPresent()) {
102
			this.mapOfGCI0.put(classId, new HashSet<>());
103
		}
104
		this.mapOfGCI0.get(classId).get().add(axiom);
105
	}
106
107
	private void addGCI1Axiom(int classId, GCI1Axiom axiom) {
108
		if (!this.mapOfGCI1.get(classId).isPresent()) {
109
			this.mapOfGCI1.put(classId, new HashSet<>());
110
		}
111
		this.mapOfGCI1.get(classId).get().add(axiom);
112
	}
113
114
	private void addGCI2Axiom(int classId, GCI2Axiom axiom) {
115
		if (!this.mapOfGCI2.get(classId).isPresent()) {
116
			this.mapOfGCI2.put(classId, new HashSet<>());
117
		}
118
		this.mapOfGCI2.get(classId).get().add(axiom);
119
	}
120
121
	private void addGCI3Axiom(GCI3Axiom axiom, int objectPropertyId, int classId) {
122
		if (!this.mapOfGCI3r.get(objectPropertyId).isPresent()) {
123
			this.mapOfGCI3r.put(objectPropertyId, new HashSet<>());
124
		}
125
		this.mapOfGCI3r.get(objectPropertyId).get().add(axiom);
126
127
		if (!this.mapOfGCI3A.get(classId).isPresent()) {
128
			this.mapOfGCI3A.put(classId, new HashSet<>());
129
		}
130
		this.mapOfGCI3A.get(classId).get().add(axiom);
131
132
		Optional<OptMap<Integer, Set<GCI3Axiom>>> optMap = this.mapOfGCI3rA.get(objectPropertyId);
133
		if (!optMap.isPresent()) {
134
			optMap = Optional.of(new OptMapImpl<>(new HashMap<>()));
135
			this.mapOfGCI3rA.put(objectPropertyId, optMap.get());
136
		}
137
		if (!optMap.get().get(classId).isPresent()) {
138
			optMap.get().put(classId, new HashSet<>());
139
		}
140
		optMap.get().get(classId).get().add(axiom);
141
	}
142
143
	private void addNominalAxiom(int individualId, NominalAxiom axiom) {
144
		if (!this.mapOfNominalAxiom.get(individualId).isPresent()) {
145
			this.mapOfNominalAxiom.put(individualId, new HashSet<>());
146
		}
147
		this.mapOfNominalAxiom.get(individualId).get().add(axiom);
148
	}
149
150
	@Override
151
	public void addObjectProperty(int objectProperty) {
152
		this.setOfAllObjectProperties.add(objectProperty);
153
	}
154
155
	private void addRangeAxiom(int propertyId, RangeAxiom axiom) {
156
		if (!this.mapOfRangeAxiom.get(propertyId).isPresent()) {
157
			this.mapOfRangeAxiom.put(propertyId, new HashSet<>());
158
		}
159
		this.mapOfRangeAxiom.get(propertyId).get().add(axiom);
160
	}
161
162
	private void addTo(int property, RI3Axiom axiom, OptMap<Integer, Set<RI3Axiom>> map) {
163
		Optional<Set<RI3Axiom>> optAxiomSet = map.get(property);
164
		if (!optAxiomSet.isPresent()) {
165
			optAxiomSet = Optional.of(new HashSet<>());
166
			map.put(property, optAxiomSet.get());
167
		}
168
		optAxiomSet.get().add(axiom);
169
	}
170
171
	@Override
172
	public void clear() {
173
		this.setOfClasses.clear();
174
		this.setOfAllObjectProperties.clear();
175
		this.mapOfGCI0.clear();
176
		this.mapOfGCI1.clear();
177
		this.mapOfGCI2.clear();
178
		this.mapOfGCI3A.clear();
179
		this.mapOfGCI3r.clear();
180
		this.mapOfGCI3rA.clear();
181
		this.mapOfRI2r.clear();
182
		this.mapOfRI2s.clear();
183
		this.mapOfRI3ByLeft.clear();
184
		this.mapOfRI3ByRight.clear();
185
		this.mapOfNominalAxiom.clear();
186
		this.mapOfRangeAxiom.clear();
187
		this.setOfTransitiveObjectProperties.clear();
188
		this.setOfFunctionalObjectProperties.clear();
189
		this.setOfReflexiveObjectProperties.clear();
190
	}
191
192
	@Override
193
	public Set<Integer> getClassSet() {
194
		return Collections.unmodifiableSet(this.setOfClasses);
195
	}
196
197
	@Override
198
	public Set<Integer> getFunctionalObjectProperties() {
199
		return Collections.unmodifiableSet(this.setOfFunctionalObjectProperties);
200
	}
201
202
	@Override
203
	public Set<GCI0Axiom> getGCI0Axioms(int classId) {
204
		Optional<Set<GCI0Axiom>> optSet = this.mapOfGCI0.get(classId);
205
		if (!optSet.isPresent()) {
206
			optSet = Optional.of(Collections.emptySet());
207
		}
208
		return Collections.unmodifiableSet(optSet.get());
209
	}
210
211
	@Override
212
	public Set<GCI1Axiom> getGCI1Axioms(int classId) {
213
		Optional<Set<GCI1Axiom>> optSet = this.mapOfGCI1.get(classId);
214
		if (!optSet.isPresent()) {
215
			optSet = Optional.of(Collections.emptySet());
216
		}
217
		return Collections.unmodifiableSet(optSet.get());
218
	}
219
220
	@Override
221
	public Set<GCI2Axiom> getGCI2Axioms(int classId) {
222
		Optional<Set<GCI2Axiom>> optSet = this.mapOfGCI2.get(classId);
223
		if (!optSet.isPresent()) {
224
			optSet = Optional.of(Collections.emptySet());
225
		}
226
		return Collections.unmodifiableSet(optSet.get());
227
	}
228
229
	@Override
230
	public Set<GCI3Axiom> getGCI3AAxioms(int classId) {
231
		Optional<Set<GCI3Axiom>> optSet = this.mapOfGCI3A.get(classId);
232
		if (!optSet.isPresent()) {
233
			optSet = Optional.of(Collections.emptySet());
234
		}
235
		return Collections.unmodifiableSet(optSet.get());
236
	}
237
238
	@Override
239
	public Set<GCI3Axiom> getGCI3rAAxioms(int objectPropertyId, int leftClassId) {
240
		Optional<Set<GCI3Axiom>> optSet = Optional.empty();
241
		Optional<OptMap<Integer, Set<GCI3Axiom>>> optMap = this.mapOfGCI3rA.get(objectPropertyId);
242
		if (optMap.isPresent()) {
243
			optSet = optMap.get().get(leftClassId);
244
		}
245
		if (!optSet.isPresent()) {
246
			optSet = Optional.of(Collections.emptySet());
247
		}
248
		return Collections.unmodifiableSet(optSet.get());
249
	}
250
251
	@Override
252
	public Set<GCI3Axiom> getGCI3rAxioms(int objectPropertyId) {
253
		Optional<Set<GCI3Axiom>> optSet = this.mapOfGCI3r.get(objectPropertyId);
254
		if (!optSet.isPresent()) {
255
			optSet = Optional.of(Collections.emptySet());
256
		}
257
		return Collections.unmodifiableSet(optSet.get());
258
	}
259
260
	@Override
261
	public Set<Integer> getObjectPropertySet() {
262
		return Collections.unmodifiableSet(this.setOfAllObjectProperties);
263
	}
264
265
	@Override
266
	public Set<Integer> getReflexiveObjectProperties() {
267
		return Collections.unmodifiableSet(this.setOfReflexiveObjectProperties);
268
	}
269
270
	@Override
271
	public Set<RI2Axiom> getRI2rAxioms(int elem) {
272
		Optional<Set<RI2Axiom>> optSet = this.mapOfRI2r.get(elem);
273
		if (!optSet.isPresent()) {
274
			optSet = Optional.of(Collections.emptySet());
275
		}
276
		return Collections.unmodifiableSet(optSet.get());
277
	}
278
279
	@Override
280
	public Set<RI2Axiom> getRI2sAxioms(int elem) {
281
		Optional<Set<RI2Axiom>> optSet = this.mapOfRI2s.get(elem);
282
		if (!optSet.isPresent()) {
283
			optSet = Optional.of(Collections.emptySet());
284
		}
285
		return Collections.unmodifiableSet(optSet.get());
286
	}
287
288
	@Override
289
	public Set<RI3Axiom> getRI3AxiomsByLeft(int elem) {
290
		Optional<Set<RI3Axiom>> optSet = this.mapOfRI3ByLeft.get(elem);
291
		if (!optSet.isPresent()) {
292
			optSet = Optional.of(Collections.emptySet());
293
		}
294
		return Collections.unmodifiableSet(optSet.get());
295
	}
296
297
	@Override
298
	public Set<RI3Axiom> getRI3AxiomsByRight(int elem) {
299
		Optional<Set<RI3Axiom>> optSet = this.mapOfRI3ByRight.get(elem);
300
		if (!optSet.isPresent()) {
301
			optSet = Optional.of(Collections.emptySet());
302
		}
303
		return Collections.unmodifiableSet(optSet.get());
304
	}
305
306
	@Override
307
	public Set<Integer> getTransitiveObjectProperties() {
308
		return Collections.unmodifiableSet(this.setOfTransitiveObjectProperties);
309
	}
310
311
	@Override
312
	public void load(Set<NormalizedIntegerAxiom> axiomSet) {
313
		Objects.requireNonNull(axiomSet);
314
		axiomSet.forEach(axiom -> {
315
			axiom.accept(this);
316
			addEntities(axiom);
317
		});
318
	}
319
320
	@Override
321
	public String toString() {
322
		StringBuffer sbuf = new StringBuffer();
323
		sbuf.append("[");
324
		sbuf.append("map of GCI0 =" + this.mapOfGCI0.toString());
325
		sbuf.append("map of GCI1 =" + this.mapOfGCI1.toString());
326
		sbuf.append("map of GCI2 =" + this.mapOfGCI2.toString());
327
		sbuf.append("map of GCI3 =" + this.mapOfGCI3r.toString());
328
		sbuf.append("map of RI2 =" + this.mapOfRI2r.toString());
329
		sbuf.append("set of functional =" + this.setOfFunctionalObjectProperties.toString());
330
		sbuf.append("set of reflexive =" + this.setOfReflexiveObjectProperties.toString());
331
		sbuf.append("set of transitive =" + this.setOfTransitiveObjectProperties.toString());
332
		sbuf.append("]");
333
		return sbuf.toString();
334
	}
335
336
	@Override
337
	public Boolean visit(FunctObjectPropAxiom axiom) {
338
		Objects.requireNonNull(axiom);
339
		this.setOfFunctionalObjectProperties.add(axiom.getProperty());
340
		return true;
341
	}
342
343
	@Override
344
	public Boolean visit(GCI0Axiom axiom) {
345
		Objects.requireNonNull(axiom);
346
		addGCI0Axiom(axiom.getSubClass(), axiom);
347
		return true;
348
	}
349
350
	@Override
351
	public Boolean visit(GCI1Axiom axiom) {
352
		Objects.requireNonNull(axiom);
353
		addGCI1Axiom(axiom.getLeftSubClass(), axiom);
354
		addGCI1Axiom(axiom.getRightSubClass(), axiom);
355
		return true;
356
	}
357
358
	@Override
359
	public Boolean visit(GCI2Axiom axiom) {
360
		Objects.requireNonNull(axiom);
361
		addGCI2Axiom(axiom.getSubClass(), axiom);
362
		return true;
363
	}
364
365
	@Override
366
	public Boolean visit(GCI3Axiom axiom) {
367
		Objects.requireNonNull(axiom);
368
		addGCI3Axiom(axiom, axiom.getPropertyInSubClass(), axiom.getClassInSubClass());
369
		return true;
370
	}
371
372
	@Override
373
	public Boolean visit(NominalAxiom axiom) {
374
		Objects.requireNonNull(axiom);
375
		addNominalAxiom(axiom.getIndividual(), axiom);
376
		return true;
377
	}
378
379
	@Override
380
	public Boolean visit(RangeAxiom axiom) {
381
		Objects.requireNonNull(axiom);
382
		addRangeAxiom(axiom.getProperty(), axiom);
383
		return true;
384
	}
385
386
	@Override
387
	public Boolean visit(RI1Axiom axiom) {
388
		Objects.requireNonNull(axiom);
389
		this.setOfReflexiveObjectProperties.add(axiom.getSuperProperty());
390
		return true;
391
	}
392
393
	@Override
394
	public Boolean visit(RI2Axiom axiom) {
395
		Objects.requireNonNull(axiom);
396
		Integer subProperty = axiom.getSubProperty();
397
		if (!this.mapOfRI2r.get(subProperty).isPresent()) {
398
			this.mapOfRI2r.put(subProperty, new HashSet<>());
399
		}
400
		this.mapOfRI2r.get(subProperty).get().add(axiom);
401
402
		Integer superProperty = axiom.getSuperProperty();
403
		if (!this.mapOfRI2s.get(superProperty).isPresent()) {
404
			this.mapOfRI2s.put(superProperty, new HashSet<>());
405
		}
406
		this.mapOfRI2s.get(superProperty).get().add(axiom);
407
408
		return true;
409
	}
410
411
	@Override
412
	public Boolean visit(RI3Axiom axiom) {
413
		Objects.requireNonNull(axiom);
414
		Integer left = axiom.getLeftSubProperty();
415
		Integer right = axiom.getRightSubProperty();
416
		addTo(left, axiom, this.mapOfRI3ByLeft);
417
		addTo(right, axiom, this.mapOfRI3ByRight);
418
		if (left.equals(axiom.getSuperProperty()) && right.equals(axiom.getSuperProperty())) {
419
			this.setOfTransitiveObjectProperties.add(left);
420
		}
421
		return true;
422
	}
423
424
}
425