Skip to content

Package: SingularDataPropertyStrategy

SingularDataPropertyStrategy

nameinstructionbranchcomplexitylinemethod
SingularDataPropertyStrategy(EntityType, Attribute, Descriptor, EntityMappingHelper)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addValueFromAxiom(Axiom)
M: 0 C: 34
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: 0 C: 21
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
buildInstanceFieldValue(Object)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createAssertion()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
isFieldEnum()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
isValidRange(Object)
M: 0 C: 16
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
resolveEnumValue()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

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.exceptions.CardinalityConstraintViolatedException;
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.ontodriver.model.Assertion;
22: import cz.cvut.kbss.ontodriver.model.Axiom;
23: import cz.cvut.kbss.ontodriver.model.Value;
24:
25: class SingularDataPropertyStrategy<X> extends FieldStrategy<Attribute<? super X, ?>, X> {
26:
27: private Object value;
28:
29: SingularDataPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att,
30: Descriptor descriptor, EntityMappingHelper mapper) {
31: super(et, att, descriptor, mapper);
32: }
33:
34: @Override
35: void addValueFromAxiom(Axiom<?> ax) {
36: final Value<?> val = ax.getValue();
37:• if (!isValidRange(val.getValue())) {
38: return;
39: }
40:• if (value != null) {
41: throw new CardinalityConstraintViolatedException(
42: "Expected single value of attribute " + attribute.getName() + ", but got multiple");
43: }
44: this.value = val.getValue();
45: }
46:
47: private boolean isValidRange(Object value) {
48:• return attribute.getJavaField().getType().isAssignableFrom(value.getClass()) || isFieldEnum();
49: }
50:
51: private boolean isFieldEnum() {
52: final Class<?> cls = attribute.getJavaField().getType();
53: return cls.isEnum();
54: }
55:
56: @Override
57: void buildInstanceFieldValue(Object entity) throws IllegalAccessException {
58:• final Object toAssign = isFieldEnum() ? resolveEnumValue() : value;
59: setValueOnInstance(entity, toAssign);
60: }
61:
62: private Object resolveEnumValue() {
63: final Class cls = attribute.getJavaField().getType();
64: return Enum.valueOf(cls, value.toString());
65: }
66:
67: @Override
68: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
69: final Object extractedValue = extractFieldValueFromInstance(instance);
70:
71:• final Value<?> val = extractedValue != null ? new Value<>(extractedValue) : Value.nullValue();
72: valueBuilder.addValue(createAssertion(), val, getAttributeContext());
73: }
74:
75: @Override
76: Assertion createAssertion() {
77: return Assertion.createDataPropertyAssertion(attribute.getIRI().toURI(),
78: attribute.isInferred());
79: }
80: }