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

addTo(Integer,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.core.algorithm.cel;
48
49
import java.util.ArrayList;
50
import java.util.Collections;
51
import java.util.HashMap;
52
import java.util.HashSet;
53
import java.util.List;
54
import java.util.Objects;
55
import java.util.Optional;
56
import java.util.Set;
57
58
import de.tudresden.inf.lat.jcel.coreontology.axiom.FunctObjectPropAxiom;
59
import de.tudresden.inf.lat.jcel.coreontology.axiom.GCI0Axiom;
60
import de.tudresden.inf.lat.jcel.coreontology.axiom.GCI1Axiom;
61
import de.tudresden.inf.lat.jcel.coreontology.axiom.GCI2Axiom;
62
import de.tudresden.inf.lat.jcel.coreontology.axiom.GCI3Axiom;
63
import de.tudresden.inf.lat.jcel.coreontology.axiom.NominalAxiom;
64
import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiom;
65
import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiomVisitor;
66
import de.tudresden.inf.lat.jcel.coreontology.axiom.RI1Axiom;
67
import de.tudresden.inf.lat.jcel.coreontology.axiom.RI2Axiom;
68
import de.tudresden.inf.lat.jcel.coreontology.axiom.RI3Axiom;
69
import de.tudresden.inf.lat.jcel.coreontology.axiom.RangeAxiom;
70
import de.tudresden.inf.lat.util.map.OptMap;
71
import de.tudresden.inf.lat.util.map.OptMapImpl;
72
73
/**
74
 * This class models an extended ontology. This is referred in the documentation
75
 * as <i>&Ocirc;</i>.
76
 * 
77
 * @author Julian Mendez
78
 */
79
public class CelExtendedOntology implements NormalizedIntegerAxiomVisitor<Boolean> {
80
81
	private final OptMap<Integer, Set<ExtensionEntry>> ohatOfClass = new OptMapImpl<>(new HashMap<>());
82
	private final OptMap<Integer, OptMap<Integer, Set<ExtensionEntry>>> ohatOfExistential = new OptMapImpl<>(
83
			new HashMap<>());
84
	private final OptMap<Integer, Set<RI3Axiom>> subPropertyAxiomSetByLeft = new OptMapImpl<>(new HashMap<>());
85
	private final OptMap<Integer, Set<RI3Axiom>> subPropertyAxiomSetByRight = new OptMapImpl<>(new HashMap<>());
86
87
	/**
88
	 * Constructs a new CEL extended ontology.
89
	 */
90
	public CelExtendedOntology() {
91
	}
92
93
	private void addClassEntry(Integer classId, ExtensionEntry entry) {
94
		if (!this.ohatOfClass.get(classId).isPresent()) {
95
			this.ohatOfClass.put(classId, new HashSet<>());
96
		}
97
		this.ohatOfClass.get(classId).get().add(entry);
98
	}
99
100
	private void addTo(Integer property, RI3Axiom axiom, OptMap<Integer, Set<RI3Axiom>> map) {
101
		Optional<Set<RI3Axiom>> optAxiomSet = map.get(property);
102
		if (!optAxiomSet.isPresent()) {
103
			optAxiomSet = Optional.of(new HashSet<>());
104
			map.put(property, optAxiomSet.get());
105
		}
106
		optAxiomSet.get().add(axiom);
107
	}
108
109
	/**
110
	 * Clears all the internal sets.
111
	 */
112
	public void clear() {
113
		this.ohatOfClass.clear();
114
		this.ohatOfExistential.clear();
115
		this.subPropertyAxiomSetByLeft.clear();
116
		this.subPropertyAxiomSetByRight.clear();
117
	}
118
119
	/**
120
	 * Returns the class entries related to the specified class.
121
	 * 
122
	 * @param classId
123
	 *            class identifier
124
	 * @return the class entries related to the specified class
125
	 */
126
	public Set<ExtensionEntry> getClassEntries(Integer classId) {
127
		Objects.requireNonNull(classId);
128
		Optional<Set<ExtensionEntry>> optSet = this.ohatOfClass.get(classId);
129
		if (!optSet.isPresent()) {
130
			optSet = Optional.of(Collections.emptySet());
131
		}
132
		return Collections.unmodifiableSet(optSet.get());
133
	}
134
135
	/**
136
	 * Returns the set of classes.
137
	 * 
138
	 * @return the set of classes
139
	 */
140
	public Set<Integer> getClassSet() {
141
		return Collections.unmodifiableSet(this.ohatOfClass.keySet());
142
	}
143
144
	/**
145
	 * Returns the existential entries related to the specified object property
146
	 * and class.
147
	 * 
148
	 * @param propertyId
149
	 *            object property identifier
150
	 * @param classId
151
	 *            class identifier
152
	 * @return the existential entries related to the specified object property
153
	 *         and class.
154
	 */
155
	public Set<ExtensionEntry> getExistentialEntries(Integer propertyId, Integer classId) {
156
		Objects.requireNonNull(propertyId);
157
		Objects.requireNonNull(classId);
158
		Set<ExtensionEntry> ret = Collections.emptySet();
159
		Optional<OptMap<Integer, Set<ExtensionEntry>>> optMap = this.ohatOfExistential.get(propertyId);
160
		if (optMap.isPresent()) {
161
			Optional<Set<ExtensionEntry>> optSet = optMap.get().get(classId);
162
			if (optSet.isPresent()) {
163
				ret = optSet.get();
164
			} else {
165
				ret = Collections.emptySet();
166
			}
167
		}
168
		return Collections.unmodifiableSet(ret);
169
170
	}
171
172
	/**
173
	 * Returns the set of sub object property axioms related where the specified
174
	 * object property occurs on the left-hand part of the composition.
175
	 * 
176
	 * @param elem
177
	 *            object property
178
	 * @return the set of sub object property axioms related where the specified
179
	 *         object property occurs on the left-hand part of the composition.
180
	 */
181
	public Set<RI3Axiom> getSubPropertyAxiomSetByLeft(Integer elem) {
182
		Objects.requireNonNull(elem);
183
		Optional<Set<RI3Axiom>> optSet = this.subPropertyAxiomSetByLeft.get(elem);
184
		if (!optSet.isPresent()) {
185
			optSet = Optional.of(Collections.emptySet());
186
		}
187
		return Collections.unmodifiableSet(optSet.get());
188
	}
189
190
	/**
191
	 * Returns the set of sub object property axioms related where the specified
192
	 * object property occurs on the right-hand part of the composition.
193
	 * 
194
	 * @param elem
195
	 *            object property
196
	 * @return the set of sub object property axioms related where the specified
197
	 *         object property occurs on the right-hand part of the composition.
198
	 */
199
	public Set<RI3Axiom> getSubPropertyAxiomSetByRight(Integer elem) {
200
		Objects.requireNonNull(elem);
201
		Optional<Set<RI3Axiom>> optSet = this.subPropertyAxiomSetByRight.get(elem);
202
		if (!optSet.isPresent()) {
203
			optSet = Optional.of(Collections.emptySet());
204
		}
205
		return Collections.unmodifiableSet(optSet.get());
206
	}
207
208
	/**
209
	 * Loads a set of normalized axioms.
210
	 * 
211
	 * @param axiomSet
212
	 *            set of normalized axioms
213
	 */
214
	public void load(Set<NormalizedIntegerAxiom> axiomSet) {
215
		Objects.requireNonNull(axiomSet);
216
		clear();
217
		axiomSet.forEach(axiom -> {
218
			axiom.accept(this);
219
			if (axiom instanceof RI3Axiom) {
220
				RI3Axiom subPropAxiom = (RI3Axiom) axiom;
221
				Integer left = subPropAxiom.getLeftSubProperty();
222
				Integer right = subPropAxiom.getRightSubProperty();
223
				addTo(left, subPropAxiom, this.subPropertyAxiomSetByLeft);
224
				addTo(right, subPropAxiom, this.subPropertyAxiomSetByRight);
225
			}
226
		});
227
	}
228
229
	@Override
230
	public String toString() {
231
		StringBuffer sbuf = new StringBuffer();
232
		sbuf.append("[");
233
		sbuf.append("OHat(cl)=" + this.ohatOfClass.toString());
234
		sbuf.append("OHat(ex)=" + this.ohatOfExistential.toString());
235
		sbuf.append("]");
236
		return sbuf.toString();
237
	}
238
239
	@Override
240
	public Boolean visit(FunctObjectPropAxiom axiom) {
241
		Objects.requireNonNull(axiom);
242
		return false;
243
	}
244
245
	@Override
246
	public Boolean visit(GCI0Axiom axiom) {
247
		Objects.requireNonNull(axiom);
248
		addClassEntry(axiom.getSubClass(), new ImplicationEntry(new HashSet<>(), axiom.getSuperClass()));
249
		return true;
250
	}
251
252
	@Override
253
	public Boolean visit(GCI1Axiom axiom) {
254
		Objects.requireNonNull(axiom);
255
		Integer superClass = axiom.getSuperClass();
256
		List<Integer> operandSet = new ArrayList<>();
257
		operandSet.add(axiom.getLeftSubClass());
258
		operandSet.add(axiom.getRightSubClass());
259
		operandSet.forEach(currentOperand -> {
260
			Set<Integer> currentSet = new HashSet<>();
261
			currentSet.addAll(operandSet);
262
			currentSet.remove(currentOperand);
263
			addClassEntry(currentOperand, new ImplicationEntry(currentSet, superClass));
264
		});
265
		return true;
266
	}
267
268
	@Override
269
	public Boolean visit(GCI2Axiom axiom) {
270
		Objects.requireNonNull(axiom);
271
		addClassEntry(axiom.getSubClass(),
272
				new ExistentialEntry(axiom.getPropertyInSuperClass(), axiom.getClassInSuperClass()));
273
		return true;
274
	}
275
276
	@Override
277
	public Boolean visit(GCI3Axiom axiom) {
278
		Objects.requireNonNull(axiom);
279
		ExtensionEntry entry = new ImplicationEntry(new HashSet<>(), axiom.getSuperClass());
280
		Integer propertyId = axiom.getPropertyInSubClass();
281
		Integer classId = axiom.getClassInSubClass();
282
		Optional<OptMap<Integer, Set<ExtensionEntry>>> optMap = this.ohatOfExistential.get(propertyId);
283
		OptMap<Integer, Set<ExtensionEntry>> map = null;
284
		if (optMap.isPresent()) {
285
			map = optMap.get();
286
		} else {
287
			map = new OptMapImpl<>(new HashMap<>());
288
			this.ohatOfExistential.put(propertyId, map);
289
		}
290
		Optional<Set<ExtensionEntry>> optSet = map.get(classId);
291
		Set<ExtensionEntry> set = null;
292
		if (optSet.isPresent()) {
293
			set = optSet.get();
294
		} else {
295
			set = new HashSet<>();
296
			map.put(classId, set);
297
		}
298
		set.add(entry);
299
		return true;
300
	}
301
302
	@Override
303
	public Boolean visit(NominalAxiom axiom) {
304
		Objects.requireNonNull(axiom);
305
		return false;
306
	}
307
308
	@Override
309
	public Boolean visit(RangeAxiom axiom) {
310
		Objects.requireNonNull(axiom);
311
		return false;
312
	}
313
314
	@Override
315
	public Boolean visit(RI1Axiom axiom) {
316
		Objects.requireNonNull(axiom);
317
		return false;
318
	}
319
320
	@Override
321
	public Boolean visit(RI2Axiom axiom) {
322
		Objects.requireNonNull(axiom);
323
		return false;
324
	}
325
326
	@Override
327
	public Boolean visit(RI3Axiom axiom) {
328
		Objects.requireNonNull(axiom);
329
		return false;
330
	}
331
332
}
333