Skip to content

Package: PropertiesFieldStrategy

PropertiesFieldStrategy

nameinstructionbranchcomplexitylinemethod
PropertiesFieldStrategy(EntityType, PropertiesSpecification, 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: 36
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
buildAxiomValuesFromInstance(Object, AxiomValueGatherer)
M: 4 C: 82
95%
M: 2 C: 14
88%
M: 2 C: 7
78%
M: 0 C: 22
100%
M: 0 C: 1
100%
buildInstanceFieldValue(Object)
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
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%
createAssertionsForAll(Map)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
isMappedAttribute(Axiom)
M: 0 C: 28
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
lambda$createAssertionsForAll$1(Map, String, Set)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$propertyDiff$0(Set, String)
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%
prepareProperties(Map)
M: 0 C: 33
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
propertyDiff(Map, Map)
M: 0 C: 104
100%
M: 2 C: 16
89%
M: 2 C: 8
80%
M: 0 C: 17
100%
M: 0 C: 1
100%
resolvePropertiesToAdd(Map, Map)
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%
resolvePropertiesToRemove(Map, Map)
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%
shouldSkipAxiom(Axiom)
M: 0 C: 17
100%
M: 0 C: 4
100%
M: 0 C: 3
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%
stringToAssertion(String)
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
stringsToValues(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%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.oom;
14:
15: import cz.cvut.kbss.jopa.CommonVocabulary;
16: import cz.cvut.kbss.jopa.exceptions.InvalidAssertionIdentifierException;
17: import cz.cvut.kbss.jopa.model.IRI;
18: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
19: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
20: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
21: import cz.cvut.kbss.jopa.model.metamodel.PropertiesSpecification;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.Value;
25:
26: import java.net.URI;
27: import java.util.*;
28: import java.util.Map.Entry;
29: import java.util.stream.Collectors;
30:
31: public class PropertiesFieldStrategy<X> extends
32: FieldStrategy<PropertiesSpecification<? super X, ?>, X> {
33:
34: private final Map<String, Set<String>> values;
35:
36: PropertiesFieldStrategy(EntityType<X> et, PropertiesSpecification<? super X, ?> att,
37: Descriptor descriptor, EntityMappingHelper mapper) {
38: super(et, att, descriptor, mapper);
39: this.values = new HashMap<>();
40: }
41:
42: @Override
43: void addValueFromAxiom(Axiom<?> ax) {
44: final String property = ax.getAssertion().getIdentifier().toString();
45:• if (shouldSkipAxiom(ax)) {
46: return;
47: }
48:• if (!values.containsKey(property)) {
49: values.put(property, new HashSet<>());
50: }
51: final String value = ax.getValue().stringValue();
52: values.get(property).add(value);
53: }
54:
55: private boolean shouldSkipAxiom(Axiom<?> ax) {
56: final String property = ax.getAssertion().getIdentifier().toString();
57: // This is class assertion for entities without types
58:• return property.equals(CommonVocabulary.RDF_TYPE) || isMappedAttribute(ax);
59: }
60:
61: private boolean isMappedAttribute(Axiom<?> ax) {
62: // TODO This is too simple, in case the assertion corresponds to a mapped attribute,
63: // we also have to check whether the value is suitable for the attribute. If not, it belongs to properties
64: final IRI propertyAsIri = IRI.create(ax.getAssertion().getIdentifier().toString());
65:• for (Attribute<?, ?> att : et.getAttributes()) {
66:• if (att.getIRI().equals(propertyAsIri)) {
67: return true;
68: }
69: }
70: return false;
71: }
72:
73: @Override
74: void buildInstanceFieldValue(Object instance) throws IllegalAccessException {
75:• if (values.isEmpty()) {
76: return;
77: }
78: setValueOnInstance(instance, values);
79: }
80:
81: @Override
82: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
83: final Object val = extractFieldValueFromInstance(instance);
84: final X original = mapper.getOriginalInstance(instance);
85:• if (val == null) {
86:• if (original == null) {
87: return;
88: }
89: final Map<String, Set<String>> origProps = (Map<String, Set<String>>) extractFieldValueFromInstance(
90: original);
91:• if (origProps == null || origProps.isEmpty()) {
92: return;
93: }
94: valueBuilder.removeProperties(prepareProperties(origProps), getAttributeContext());
95: return;
96: }
97:• assert val instanceof Map;
98: final Map<String, Set<String>> props = (Map<String, Set<String>>) val;
99:• if (original == null) {
100: valueBuilder.addProperties(prepareProperties(props), getAttributeContext());
101: } else {
102: final Map<String, Set<String>> origProps = (Map<String, Set<String>>) extractFieldValueFromInstance(
103: original);
104: final Map<Assertion, Set<Value<?>>> toRemove = resolvePropertiesToRemove(props, origProps);
105:• if (!toRemove.isEmpty()) {
106: valueBuilder.removeProperties(toRemove, getAttributeContext());
107: }
108: final Map<Assertion, Set<Value<?>>> toAdd = resolvePropertiesToAdd(props, origProps);
109:• if (!toAdd.isEmpty()) {
110: valueBuilder.addProperties(toAdd, getAttributeContext());
111: }
112: }
113: }
114:
115: private Map<Assertion, Set<Value<?>>> prepareProperties(Map<String, Set<String>> props) {
116: final Map<Assertion, Set<Value<?>>> result = new HashMap<>(props.size());
117:• for (Entry<String, Set<String>> entry : props.entrySet()) {
118: result.put(stringToAssertion(entry.getKey()), stringsToValues(entry.getValue()));
119: }
120: return result;
121: }
122:
123: private Assertion stringToAssertion(String property) {
124: final Assertion assertion;
125: try {
126: assertion = Assertion.createPropertyAssertion(URI.create(property), attribute.isInferred());
127: } catch (IllegalArgumentException e) {
128: throw new InvalidAssertionIdentifierException(property + " is not a valid URI.", e);
129: }
130: return assertion;
131: }
132:
133: private Set<Value<?>> stringsToValues(Set<String> strValues) {
134: final Set<Value<?>> ontoValues = new HashSet<>(strValues.size());
135: ontoValues.addAll(strValues.stream().map(Value::new).collect(Collectors.toList()));
136: return ontoValues;
137: }
138:
139: private Map<Assertion, Set<Value<?>>> resolvePropertiesToRemove(Map<String, Set<String>> current,
140: Map<String, Set<String>> original) {
141: return propertyDiff(original, current);
142: }
143:
144: /**
145: * The difference is counted as the properties and values which were in base but are not in the updated version.
146: */
147: private Map<Assertion, Set<Value<?>>> propertyDiff(Map<String, Set<String>> base,
148: Map<String, Set<String>> updated) {
149:• if (base == null || base.isEmpty()) {
150: return Collections.emptyMap();
151: }
152: final Map<Assertion, Set<Value<?>>> diff = new HashMap<>();
153:• if (updated == null || updated.isEmpty()) {
154: diff.putAll(createAssertionsForAll(base));
155: } else {
156:• for (Entry<String, Set<String>> entry : base.entrySet()) {
157:• if (!updated.containsKey(entry.getKey())
158:• || updated.get(entry.getKey()) == null || updated.get(entry.getKey()).isEmpty()) {
159: // All values of the property are missing
160: diff.put(stringToAssertion(entry.getKey()), stringsToValues(entry.getValue()));
161: } else {
162: final Set<String> currentValues = updated.get(entry.getKey());
163: Set<String> removed = new HashSet<>();
164: // Check which property values are missing
165:• removed.addAll(entry.getValue().stream().filter(origVal -> !currentValues.contains(origVal))
166: .collect(Collectors.toList()));
167:• if (!removed.isEmpty()) {
168: diff.put(stringToAssertion(entry.getKey()), stringsToValues(removed));
169: }
170: }
171: }
172: }
173: return diff;
174: }
175:
176: private Map<Assertion, Set<Value<?>>> createAssertionsForAll(Map<String, Set<String>> map) {
177: final Map<Assertion, Set<Value<?>>> diff = new HashMap<>(map.size());
178: map.forEach((key, value) -> diff.put(stringToAssertion(key), stringsToValues(value)));
179: return diff;
180: }
181:
182: private Map<Assertion, Set<Value<?>>> resolvePropertiesToAdd(Map<String, Set<String>> current,
183: Map<String, Set<String>> original) {
184: return propertyDiff(current, original);
185: }
186:
187: @Override
188: Assertion createAssertion() {
189: return Assertion.createUnspecifiedPropertyAssertion(attribute.isInferred());
190: }
191:
192: }