Skip to content

Method: addValueFromAxiom(Axiom)

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.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
19: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
20: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
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 SingularAnnotationPropertyStrategy<X> extends SingularDataPropertyStrategy<X> {
27:
28: SingularAnnotationPropertyStrategy(EntityType<X> et, Attribute<? super X, ?> att, Descriptor descriptor,
29: EntityMappingHelper mapper) {
30: super(et, att, descriptor, mapper);
31: }
32:
33: @Override
34: void addValueFromAxiom(Axiom<?> ax) {
35: final Value<?> val = ax.getValue();
36:• if (!isValidRange(val.getValue())) {
37: return;
38: }
39: verifyCardinalityConstraint(ax.getSubject());
40:• if (super.isValidRange(val.getValue())) {
41: this.value = val.getValue();
42: } else {
43: this.value = IdentifierTransformer.transformToIdentifier(val.getValue(), attribute.getJavaType());
44: }
45: }
46:
47: @Override
48: boolean isValidRange(Object value) {
49: assert value != null;
50:
51: return attribute.getJavaType().isAssignableFrom(value.getClass()) ||
52: value instanceof NamedResource && IdentifierTransformer.isValidIdentifierType(attribute.getJavaType());
53: }
54:
55: @Override
56: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) throws IllegalAccessException {
57: final Object value = extractFieldValueFromInstance(instance);
58: if (value == null) {
59: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeContext());
60: return;
61: }
62:
63: if (IdentifierTransformer.isValidIdentifierType(attribute.getJavaType()) &&
64: !attribute.getJavaType().isAssignableFrom(String.class)) {
65: valueBuilder.addValue(createAssertion(),
66: new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(value))), getAttributeContext());
67: } else {
68: valueBuilder.addValue(createAssertion(), new Value<>(value), getAttributeContext());
69: }
70: }
71:
72: @Override
73: Assertion createAssertion() {
74: return Assertion.createAnnotationPropertyAssertion(attribute.getIRI().toURI(), attribute.isInferred());
75: }
76: }