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: 58
94%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 13
100%
M: 0 C: 1
100%
buildAxiomValuesFromInstance(Object, AxiomValueGatherer)
M: 0 C: 41
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 10
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: 1
100%
M: 0 C: 1
100%
extractReferenceIdentifier(Object)
M: 5 C: 41
89%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 0 C: 8
100%
M: 0 C: 1
100%
getAttributeValueContext()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
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%
verifyCardinality(NamedResource)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
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.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.utils.EntityPropertiesUtils;
22: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
23: import cz.cvut.kbss.ontodriver.model.Assertion;
24: import cz.cvut.kbss.ontodriver.model.Axiom;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26: import cz.cvut.kbss.ontodriver.model.Value;
27: import org.slf4j.Logger;
28: import org.slf4j.LoggerFactory;
29:
30: import java.net.URI;
31:
32: class SingularObjectPropertyStrategy<X> extends FieldStrategy<Attribute<? super X, ?>, X> {
33:
34: private static final Logger LOG = LoggerFactory.getLogger(SingularObjectPropertyStrategy.class);
35:
36: private Object value;
37:
38: SingularObjectPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att,
39: Descriptor descriptor, EntityMappingHelper mapper) {
40: super(et, att, descriptor, mapper);
41: }
42:
43: @Override
44: void addValueFromAxiom(Axiom<?> ax) {
45:• assert ax.getValue().getValue() instanceof NamedResource;
46: final NamedResource valueIdentifier = (NamedResource) ax.getValue().getValue();
47: final Object newValue;
48:• if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType())) {
49: newValue = IdentifierTransformer
50: .transformToIdentifier(valueIdentifier.getIdentifier(), attribute.getJavaType());
51: } else {
52: newValue = mapper.getEntityFromCacheOrOntology(attribute.getJavaType(), valueIdentifier.getIdentifier(),
53: entityDescriptor.getAttributeDescriptor(attribute));
54:• if (newValue == null) {
55: LOG.trace("Value of axiom {} could not be loaded as entity filling attribute {}.", ax, attribute);
56: return;
57: }
58: }
59: verifyCardinality(ax.getSubject());
60: this.value = newValue;
61: }
62:
63: private void verifyCardinality(NamedResource subject) {
64:• if (value != null) {
65: throw new CardinalityConstraintViolatedException(
66: "Expected single value of attribute " + attribute.getName() + " of instance " + subject +
67: ", but got multiple.");
68: }
69: }
70:
71: @Override
72: void buildInstanceFieldValue(Object instance) {
73: setValueOnInstance(instance, value);
74: }
75:
76: @Override
77: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
78: final Object extractedValue = extractFieldValueFromInstance(instance);
79:• if (referenceSavingResolver.shouldSaveReference(extractedValue, getAttributeValueContext())) {
80: final Value<NamedResource> val = extractReferenceIdentifier(extractedValue);
81: valueBuilder.addValue(createAssertion(), val, getAttributeContext());
82: } else {
83: referenceSavingResolver
84: .registerPendingReference(valueBuilder.getSubjectIdentifier(), createAssertion(), extractedValue,
85: getAttributeContext());
86: // This will cause the existing property assertion to be removed
87: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeContext());
88: }
89: }
90:
91: private URI getAttributeValueContext() {
92: return entityDescriptor.getAttributeDescriptor(attribute).getContext();
93: }
94:
95: private <V> Value<NamedResource> extractReferenceIdentifier(final V value) {
96:• if (value == null) {
97: return Value.nullValue();
98: }
99:• if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType())) {
100: return new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(value)));
101: }
102: final EntityType<? super V> valEt = (EntityType<? super V>) mapper.getEntityType(attribute.getJavaType());
103:• assert valEt != null;
104:
105: final URI id = EntityPropertiesUtils.getIdentifier(value, valEt);
106:• return id != null ? new Value<>(NamedResource.create(id)) : Value.nullValue();
107: }
108:
109: @Override
110: Assertion createAssertion() {
111: return Assertion.createObjectPropertyAssertion(attribute.getIRI().toURI(), attribute.isInferred());
112: }
113: }