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