Skip to contentMethod: createAssertion()
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.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.AbstractPluralAttribute;
19: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
20: import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter;
21: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.NamedResource;
25: import cz.cvut.kbss.ontodriver.model.Value;
26:
27: import java.util.Collection;
28: import java.util.Objects;
29: import java.util.Set;
30: import java.util.function.Function;
31: import java.util.stream.Collectors;
32:
33: class PluralAnnotationPropertyStrategy<X> extends PluralDataPropertyStrategy<X> {
34:
35: PluralAnnotationPropertyStrategy(EntityType<X> et, AbstractPluralAttribute<? super X, ?, ?> att,
36: Descriptor attributeDescriptor, EntityMappingHelper mapper) {
37: super(et, att, attributeDescriptor, mapper);
38: }
39:
40: @Override
41: void addValueFromAxiom(Axiom<?> ax) {
42: final Object value = ax.getValue().getValue();
43: if (isValidRange(value)) {
44: values.add(toAttributeValue(value));
45: } else if (value instanceof NamedResource && IdentifierTransformer.isValidIdentifierType(elementType)) {
46: values.add(IdentifierTransformer.transformToIdentifier(ToLexicalFormConverter.INSTANCE.convertToAttribute(value), elementType));
47: }
48: }
49:
50: @Override
51: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
52: final Object value = extractFieldValueFromInstance(instance);
53: assert value instanceof Collection || value == null;
54: final Collection<?> valueCollection = (Collection<?>) value;
55: if (valueCollection == null || valueCollection.isEmpty()) {
56: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeContext());
57: } else {
58: final Function<Object, Value<?>> mapper;
59: if (IdentifierTransformer.isValidIdentifierType(elementType) && !elementType
60: .isAssignableFrom(String.class)) {
61: mapper = v -> new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(v)));
62: } else {
63: mapper = v -> new Value<>(toAxiomValue(v));
64: }
65: final Set<Value<?>> assertionValues =
66: valueCollection.stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toSet());
67: valueBuilder.addValues(createAssertion(), assertionValues, getAttributeContext());
68: }
69: }
70:
71: @Override
72: Assertion createAssertion() {
73: return Assertion
74: .createAnnotationPropertyAssertion(attribute.getIRI().toURI(), getLanguage(), attribute.isInferred());
75: }
76: }