Skip to content

Method: buildInstanceFieldValue(Object)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.exceptions.CardinalityConstraintViolatedException;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.metamodel.AbstractAttribute;
23: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
24: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
25: import cz.cvut.kbss.ontodriver.model.Axiom;
26: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
27: import cz.cvut.kbss.ontodriver.model.NamedResource;
28: import cz.cvut.kbss.ontodriver.model.Value;
29:
30: import java.util.Collections;
31: import java.util.Set;
32:
33: class SingularDataPropertyStrategy<X> extends DataPropertyFieldStrategy<AbstractAttribute<? super X, ?>, X> {
34:
35: Object value;
36:
37: SingularDataPropertyStrategy(EntityType<X> et, AbstractAttribute<? super X, ?> att,
38: Descriptor descriptor, EntityMappingHelper mapper) {
39: super(et, att, descriptor, mapper);
40: }
41:
42: @Override
43: void addAxiomValue(Axiom<?> ax) {
44: final Object val = ax.getValue().getValue();
45: if (!isValidRange(val)) {
46: return;
47: }
48: verifyCardinalityConstraint(ax.getSubject());
49: this.value = toAttributeValue(val);
50: }
51:
52: void verifyCardinalityConstraint(NamedResource subject) {
53: if (value != null) {
54: throw new CardinalityConstraintViolatedException(
55: "Expected single value of attribute " + attribute.getName() + " of instance " + subject +
56: ", but got multiple.");
57: }
58: }
59:
60: @Override
61: boolean hasValue() {
62: return value != null;
63: }
64:
65: @Override
66: void buildInstanceFieldValue(Object entity) {
67: setValueOnInstance(entity, value);
68: }
69:
70: @Override
71: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
72: valueBuilder.addValue(createAssertion(), extractValue(instance), getAttributeWriteContext());
73: }
74:
75: private Value<?> extractValue(X instance) {
76: final Object extractedValue = extractFieldValueFromInstance(instance);
77: return extractedValue != null ? convertToAxiomValue(extractedValue) : Value.nullValue();
78: }
79:
80: @Override
81: Set<Axiom<?>> buildAxiomsFromInstance(X instance) {
82: final Value<?> val = extractValue(instance);
83: if (Value.nullValue().equals(val)) {
84: return Collections.emptySet();
85: }
86: return Collections.singleton(
87: new AxiomImpl<>(NamedResource.create(EntityPropertiesUtils.getIdentifier(instance, et)),
88: createAssertion(), val));
89: }
90: }