Package: PluralMultilingualStringFieldStrategy
PluralMultilingualStringFieldStrategy
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PluralMultilingualStringFieldStrategy(EntityType, AbstractPluralAttribute, Descriptor, EntityMappingHelper) |
|
|
|
|
|
||||||||||||||||||||
addAxiomValue(Axiom) |
|
|
|
|
|
||||||||||||||||||||
addValue(String, String) |
|
|
|
|
|
||||||||||||||||||||
buildAxiomValuesFromInstance(Object, AxiomValueGatherer) |
|
|
|
|
|
||||||||||||||||||||
buildAxiomsFromInstance(Object) |
|
|
|
|
|
||||||||||||||||||||
buildInstanceFieldValue(Object) |
|
|
|
|
|
||||||||||||||||||||
hasValue() |
|
|
|
|
|
||||||||||||||||||||
lambda$buildAxiomsFromInstance$0(NamedResource, Assertion, Value) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
toAxiomValue(Object) |
|
|
|
|
|
||||||||||||||||||||
translationsToValues(Collection) |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.model.MultilingualString;
21: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
22: import cz.cvut.kbss.jopa.model.metamodel.AbstractPluralAttribute;
23: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
24: import cz.cvut.kbss.jopa.utils.CollectionFactory;
25: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
26: import cz.cvut.kbss.ontodriver.model.Assertion;
27: import cz.cvut.kbss.ontodriver.model.Axiom;
28: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
29: import cz.cvut.kbss.ontodriver.model.LangString;
30: import cz.cvut.kbss.ontodriver.model.NamedResource;
31: import cz.cvut.kbss.ontodriver.model.Value;
32:
33: import java.util.Collection;
34: import java.util.Collections;
35: import java.util.Objects;
36: import java.util.Set;
37: import java.util.stream.Collectors;
38:
39: public class PluralMultilingualStringFieldStrategy<X>
40: extends DataPropertyFieldStrategy<AbstractPluralAttribute<? super X, ?, MultilingualString>, X> {
41:
42: private final Collection<MultilingualString> values;
43:
44: PluralMultilingualStringFieldStrategy(EntityType<X> et,
45: AbstractPluralAttribute<? super X, ?, MultilingualString> att,
46: Descriptor entityDescriptor,
47: EntityMappingHelper mapper) {
48: super(et, att, entityDescriptor, mapper);
49: this.values = CollectionFactory.createDefaultCollection(att.getCollectionType());
50: }
51:
52: @Override
53: void addAxiomValue(Axiom<?> ax) {
54: String value;
55: String language = null;
56:• if (ax.getValue().getValue() instanceof LangString ls) {
57: value = ls.getValue();
58: language = ls.getLanguage().orElse(null);
59: } else {
60: value = ax.getValue().stringValue();
61: }
62: addValue(value, language);
63: }
64:
65: @Override
66: boolean hasValue() {
67:• return !values.isEmpty();
68: }
69:
70: private void addValue(String value, String language) {
71:• for (MultilingualString ms : values) {
72:• if (!ms.contains(language)) {
73: ms.set(language, value);
74: return;
75: }
76: }
77: final MultilingualString newOne = new MultilingualString();
78: newOne.set(language, value);
79: values.add(newOne);
80: }
81:
82: @Override
83: void buildInstanceFieldValue(Object instance) {
84: setValueOnInstance(instance, values);
85: }
86:
87: @Override
88: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
89: final Object value = extractFieldValueFromInstance(instance);
90:• assert value instanceof Collection || value == null;
91: final Collection<MultilingualString> valueCollection = (Collection<MultilingualString>) value;
92:• if (valueCollection == null || valueCollection.isEmpty()) {
93: valueBuilder.addValue(createAssertion(), Value.nullValue(), getAttributeWriteContext());
94: } else {
95: final Set<Value<?>> assertionValues = translationsToValues(valueCollection);
96: valueBuilder.addValues(createAssertion(),
97: filterOutInferredValues(valueBuilder.getSubjectIdentifier(), assertionValues),
98: getAttributeWriteContext());
99: }
100: }
101:
102: private static Set<Value<?>> translationsToValues(Collection<MultilingualString> valueCollection) {
103: return valueCollection.stream()
104: .filter(Objects::nonNull)
105: .flatMap(SingularMultilingualStringFieldStrategy::translationsToLangStrings)
106: .collect(Collectors.toSet());
107: }
108:
109: @Override
110: Set<Axiom<?>> buildAxiomsFromInstance(X instance) {
111: final Object value = extractFieldValueFromInstance(instance);
112:• assert value instanceof Collection || value == null;
113: final Collection<MultilingualString> valueCollection = (Collection<MultilingualString>) value;
114:• if (valueCollection == null || valueCollection.isEmpty()) {
115: return Collections.emptySet();
116: }
117: final Set<Value<?>> assertionValues = translationsToValues(valueCollection);
118: final NamedResource subject = NamedResource.create(EntityPropertiesUtils.getIdentifier(instance, et));
119: final Assertion assertion = createAssertion();
120: return assertionValues.stream().map(v -> new AxiomImpl<>(subject, assertion, v)).collect(Collectors.toSet());
121: }
122:
123: @Override
124: Collection<Value<?>> toAxiomValue(Object value) {
125:• assert value instanceof MultilingualString;
126: return SingularMultilingualStringFieldStrategy.translationsToLangStrings((MultilingualString) value)
127: .collect(Collectors.toList());
128: }
129: }