Skip to content

Package: SingularObjectPropertyStrategy

SingularObjectPropertyStrategy

nameinstructionbranchcomplexitylinemethod
SingularObjectPropertyStrategy(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: 4 C: 47
92%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 9
100%
M: 0 C: 1
100%
buildAxiomValuesFromInstance(Object, AxiomValueGatherer)
M: 0 C: 20
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: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
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%
extractReferenceIdentifier(Object)
M: 17 C: 27
61%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 5
83%
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: * 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.jopa.oom.exceptions.EntityDeconstructionException;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.NamedResource;
25: import cz.cvut.kbss.ontodriver.model.Value;
26:
27: import java.net.URI;
28:
29: class SingularObjectPropertyStrategy<X> extends FieldStrategy<Attribute<? super X, ?>, X> {
30:
31: private Object value;
32:
33: SingularObjectPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att,
34: Descriptor descriptor, EntityMappingHelper mapper) {
35: super(et, att, descriptor, mapper);
36: }
37:
38: @Override
39: void addValueFromAxiom(Axiom<?> ax) {
40:• assert ax.getValue().getValue() instanceof NamedResource;
41: final NamedResource valueIdentifier = (NamedResource) ax.getValue().getValue();
42: final Object newValue = mapper
43: .getEntityFromCacheOrOntology(attribute.getJavaType(), valueIdentifier.getIdentifier(),
44: attributeDescriptor);
45:• if (value != null) {
46: throw new CardinalityConstraintViolatedException(
47: "Expected single value of attribute " + attribute.getName() + " but got multiple.");
48: }
49: this.value = newValue;
50: }
51:
52: @Override
53: void buildInstanceFieldValue(Object instance) throws IllegalAccessException {
54: setValueOnInstance(instance, value);
55: }
56:
57: @Override
58: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
59: final Object extractedValue = extractFieldValueFromInstance(instance);
60:• Value<?> val = extractedValue != null ? extractReferenceIdentifier(extractedValue) : Value.nullValue();
61: valueBuilder.addValue(createAssertion(), val, getAttributeContext());
62: }
63:
64: private <V> Value<NamedResource> extractReferenceIdentifier(final V value) {
65: final EntityType<V> valEt = (EntityType<V>) mapper.getEntityType(value.getClass());
66:• if (valEt == null) {
67: throw new EntityDeconstructionException("Value of field " + attribute.getJavaField()
68: + " is not a recognized entity.");
69: }
70: final URI id = resolveValueIdentifier(value, valEt);
71: cascadeResolver.resolveFieldCascading(attribute, value, getAttributeContext());
72: return new Value<>(NamedResource.create(id));
73: }
74:
75: @Override
76: Assertion createAssertion() {
77: return Assertion.createObjectPropertyAssertion(attribute.getIRI().toURI(),
78: attribute.isInferred());
79: }
80: }