Skip to content

Method: createAssertion()

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: 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: verifyCardinalityConstraint();
41: this.value = val.getValue();
42: }
43:
44: void verifyCardinalityConstraint() {
45: if (value != null) {
46: throw new CardinalityConstraintViolatedException(
47: "Expected single value of attribute " + attribute.getName() + ", but got multiple");
48: }
49: }
50:
51: boolean isValidRange(Object value) {
52: return attribute.getJavaType().isAssignableFrom(value.getClass()) || isFieldEnum();
53: }
54:
55: private boolean isFieldEnum() {
56: final Class<?> cls = attribute.getJavaField().getType();
57: return cls.isEnum();
58: }
59:
60: @Override
61: void buildInstanceFieldValue(Object entity) throws IllegalAccessException {
62: final Object toAssign = isFieldEnum() ? resolveEnumValue() : value;
63: setValueOnInstance(entity, toAssign);
64: }
65:
66: private Object resolveEnumValue() {
67: final Class cls = attribute.getJavaField().getType();
68: return Enum.valueOf(cls, value.toString());
69: }
70:
71: @Override
72: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
73: final Object extractedValue = extractFieldValueFromInstance(instance);
74:
75: final Value<?> val = extractedValue != null ? new Value<>(extractedValue) : Value.nullValue();
76: valueBuilder.addValue(createAssertion(), val, getAttributeContext());
77: }
78:
79: @Override
80: Assertion createAssertion() {
81: return Assertion.createDataPropertyAssertion(attribute.getIRI().toURI(),
82: attribute.isInferred());
83: }
84: }