Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.oom;
14:
15: import cz.cvut.kbss.jopa.model.MultilingualString;
16: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
17: import cz.cvut.kbss.jopa.model.metamodel.AbstractPluralAttribute;
18: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
19: import cz.cvut.kbss.jopa.oom.converter.ToLexicalFormConverter;
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: import java.util.Collection;
27: import java.util.Collections;
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
47: .transformToIdentifier(ToLexicalFormConverter.INSTANCE.convertToAttribute(value), elementType));
48: }
49: }
50:
51: @Override
52: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
53: final Object value = extractFieldValueFromInstance(instance);
54: assert value instanceof Collection || value == null;
55: final Collection<?> valueCollection = (Collection<?>) value;
56: if (valueCollection == null || valueCollection.isEmpty()) {
57: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeWriteContext());
58: } else {
59: final Function<Object, Collection<Value<?>>> mapper;
60: if (IdentifierTransformer.isValidIdentifierType(elementType) && !elementType
61: .isAssignableFrom(String.class)) {
62: mapper = v -> Collections
63: .singleton(new Value<>(NamedResource.create(IdentifierTransformer.valueAsUri(v))));
64: } else {
65: mapper = v -> v instanceof MultilingualString ?
66: SingularMultilingualStringFieldStrategy.translationsToLangStrings(
67: (MultilingualString) v) : Collections.singleton(new Value<>(toAxiomValue(v)));
68: }
69: final Set<Value<?>> assertionValues =
70: valueCollection.stream().filter(Objects::nonNull).map(mapper).flatMap(Collection::stream)
71: .collect(Collectors.toSet());
72: valueBuilder.addValues(createAssertion(), assertionValues, getAttributeWriteContext());
73: }
74: }
75:
76: @Override
77: Assertion createAssertion() {
78: return Assertion
79: .createAnnotationPropertyAssertion(attribute.getIRI().toURI(), getLanguage(), attribute.isInferred());
80: }
81: }