Skip to content

Package: PolymorphicEntityTypeResolver

PolymorphicEntityTypeResolver

nameinstructionbranchcomplexitylinemethod
PolymorphicEntityTypeResolver(NamedResource, IdentifiableEntityType, Collection)
M: 0 C: 24
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
addMatchingType(IdentifiableEntityType, Set)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
determineActualEntityType()
M: 0 C: 43
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
findMatchingEntityType(AbstractIdentifiableType, Set)
M: 4 C: 53
93%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 12
100%
M: 0 C: 1
100%
lambda$new$0(Axiom)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
resolveMatchingEntityTypes()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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