Skip to content

Package: TypesFieldStrategy

TypesFieldStrategy

nameinstructionbranchcomplexitylinemethod
TypesFieldStrategy(EntityType, TypesSpecification, Descriptor, EntityMappingHelper)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addValueFromAxiom(Axiom)
M: 0 C: 21
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
buildAxiomValuesFromInstance(Object, AxiomValueGatherer)
M: 4 C: 68
94%
M: 2 C: 12
86%
M: 2 C: 6
75%
M: 0 C: 20
100%
M: 0 C: 1
100%
buildInstanceFieldValue(Object)
M: 4 C: 20
83%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 5
100%
M: 0 C: 1
100%
createAssertion()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
extractTypesToAdd(AxiomValueGatherer, Set, Set)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
extractTypesToRemove(AxiomValueGatherer, Set, Set)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$prepareTypes$2(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$typesDiff$0(Set, Object)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$typesDiff$1(Object)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
prepareTypes(Set)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
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%
typesDiff(Set, Set)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
19: import cz.cvut.kbss.jopa.model.metamodel.TypesSpecification;
20: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
21: import cz.cvut.kbss.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23:
24: import java.net.URI;
25: import java.util.Collections;
26: import java.util.HashSet;
27: import java.util.Set;
28: import java.util.stream.Collectors;
29:
30: class TypesFieldStrategy<X> extends FieldStrategy<TypesSpecification<? super X, ?>, X> {
31:
32: private final Set<Object> values = new HashSet<>();
33:
34: TypesFieldStrategy(EntityType<X> et, TypesSpecification<? super X, ?> att, Descriptor descriptor,
35: EntityMappingHelper mapper) {
36: super(et, att, descriptor, mapper);
37: }
38:
39: @Override
40: void addValueFromAxiom(Axiom<?> ax) {
41:• if (MappingUtils.isEntityClassAssertion(ax, et)) {
42: return;
43: }
44: final Object type =
45: IdentifierTransformer.transformToIdentifier(ax.getValue().getValue(), attribute.getElementType());
46: values.add(type);
47: }
48:
49: @Override
50: void buildInstanceFieldValue(Object instance) {
51:• assert attribute.getJavaField().getType().isAssignableFrom(Set.class);
52:
53:• if (values.isEmpty()) {
54: return;
55: }
56: setValueOnInstance(instance, values);
57: }
58:
59: @Override
60: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
61: final Object val = extractFieldValueFromInstance(instance);
62: final X original = mapper.getOriginalInstance(instance);
63:• if (val == null) {
64:• if (original == null) {
65: return;
66: }
67: final Set<?> origTypes = (Set<?>) extractFieldValueFromInstance(original);
68:• if (origTypes == null || origTypes.isEmpty()) {
69: return;
70: }
71: valueBuilder.removeTypes(prepareTypes(origTypes), getAttributeContext());
72: } else {
73:• assert val instanceof Set; // This is verified when the metamodel is built
74: final Set<?> types = (Set<?>) val;
75:• if (original == null) {
76: valueBuilder.addTypes(prepareTypes(types), getAttributeContext());
77: } else {
78: Set<?> origTypes = (Set<?>) extractFieldValueFromInstance(original);
79:• if (origTypes == null) {
80: origTypes = Collections.emptySet();
81: }
82: extractTypesToAdd(valueBuilder, types, origTypes);
83: extractTypesToRemove(valueBuilder, types, origTypes);
84: }
85: }
86: }
87:
88: private void extractTypesToAdd(AxiomValueGatherer valueBuilder, Set<?> types, Set<?> origTypes) {
89: final Set<URI> toAdd = typesDiff(origTypes, types);
90: valueBuilder.addTypes(toAdd, getAttributeContext());
91: }
92:
93: private static Set<URI> typesDiff(Set<?> base, Set<?> difference) {
94: final Set<URI> addedDiff = new HashSet<>(base.size());
95:• addedDiff.addAll(difference.stream().filter(t -> !base.contains(t)).map(t -> URI.create(t.toString()))
96: .collect(Collectors.toList()));
97: return addedDiff;
98: }
99:
100: private void extractTypesToRemove(AxiomValueGatherer valueBuilder, Set<?> types, Set<?> origTypes) {
101: final Set<URI> toRemove = typesDiff(types, origTypes);
102: valueBuilder.removeTypes(toRemove, getAttributeContext());
103: }
104:
105: private static Set<URI> prepareTypes(Set<?> types) {
106: final Set<URI> toAdd = new HashSet<>(types.size());
107: toAdd.addAll(types.stream().map(t -> URI.create(t.toString())).collect(Collectors.toList()));
108: return toAdd;
109: }
110:
111: @Override
112: Assertion createAssertion() {
113: return Assertion.createClassAssertion(attribute.isInferred());
114: }
115: }