Skip to content

Method: addValueFromAxiom(Axiom)

1: /**
2: * Copyright (C) 2016 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: public class TypesFieldStrategy<X> extends FieldStrategy<TypesSpecification<? super X, ?>, X> {
31:
32: private final Set<Object> values = new HashSet<>();
33:
34: public TypesFieldStrategy(EntityType<X> et, TypesSpecification<? super X, ?> att,
35: Descriptor descriptor, 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.getJavaType());
46: values.add(type);
47: }
48:
49: @Override
50: void buildInstanceFieldValue(Object instance) throws IllegalAccessException {
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) throws IllegalAccessException {
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 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 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: }