-
Notifications
You must be signed in to change notification settings - Fork 14
/
PolymorphicEntityTypeResolver.java
102 lines (92 loc) · 4.42 KB
/
PolymorphicEntityTypeResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.oom.metamodel;
import cz.cvut.kbss.jopa.model.metamodel.AbstractIdentifiableType;
import cz.cvut.kbss.jopa.model.metamodel.EntityType;
import cz.cvut.kbss.jopa.model.metamodel.EntityTypeImpl;
import cz.cvut.kbss.jopa.model.metamodel.Type;
import cz.cvut.kbss.jopa.oom.exceptions.AmbiguousEntityTypeException;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import java.net.URI;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class PolymorphicEntityTypeResolver<T> {
private final NamedResource individual;
private final Set<URI> types;
private final EntityTypeImpl<T> root;
private final Set<EntityType<? extends T>> matches = new HashSet<>(2);
public PolymorphicEntityTypeResolver(NamedResource individual, EntityTypeImpl<T> root,
Collection<Axiom<URI>> typeAxioms) {
this.individual = individual;
this.types = typeAxioms.stream().map(a -> a.getValue().getValue()).collect(Collectors.toSet());
this.root = root;
}
/**
* Returns entity type suitable for instance loading. This entity type is
* <pre>
* <ul>
* <li>either the specified {@code root} in case the type axioms contain type corresponding to the root entity
* type,</li>
* <li>or the most specific non-abstract entity type from the hierarchy of the specified root entity type
* present in the specified type axioms.</li>
* </ul>
* </pre>
*
* @return The specified root entity type or the most specific non-abstract unique entity type
* @throws AmbiguousEntityTypeException When multiple entity types match the specified types
*/
public EntityType<? extends T> determineActualEntityType() {
if (types.contains(root.getIRI().toURI()) && !root.isAbstract()) {
return root;
}
resolveMatchingEntityTypes();
if (matches.size() > 1) {
throw new AmbiguousEntityTypeException(
"Unable to determine unique entity type for loading individual " + individual +
". Matching types are " + matches + '.');
}
return !matches.isEmpty() ? matches.iterator().next() : null;
}
/**
* The algorithm uses DFS with remembering the path taken. If a matching entity type is found, but there already
* exists a more general one (an ancestor of the ET), then the ancestor is removed from the matches, because it is
* superseded by the more specific entity type just found.
*/
private void resolveMatchingEntityTypes() {
findMatchingEntityType(root, new HashSet<>());
}
private void findMatchingEntityType(AbstractIdentifiableType<? extends T> parent,
Set<EntityType<? extends T>> ancestors) {
for (AbstractIdentifiableType<? extends T> subtype : parent.getSubtypes()) {
final Set<EntityType<? extends T>> updatedAncestors = new HashSet<>(ancestors);
if (subtype.getPersistenceType() == Type.PersistenceType.ENTITY && !subtype.isAbstract()) {
final EntityTypeImpl<? extends T> et = (EntityTypeImpl<? extends T>) subtype;
final URI etUri = et.getIRI().toURI();
if (types.contains(etUri)) {
addMatchingType(et, ancestors);
}
updatedAncestors.add(et);
}
findMatchingEntityType(subtype, updatedAncestors);
}
}
private void addMatchingType(EntityType<? extends T> et, Set<EntityType<? extends T>> ancestors) {
matches.add(et);
ancestors.forEach(matches::remove);
}
}