Skip to contentMethod: buildAxiomsFromInstance(Object)
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.descriptors.Descriptor;
21: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
22: import cz.cvut.kbss.jopa.model.metamodel.ListAttributeImpl;
23: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
24: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
25: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
26: import cz.cvut.kbss.ontodriver.model.Axiom;
27: import cz.cvut.kbss.ontodriver.model.NamedResource;
28:
29: import java.util.ArrayList;
30: import java.util.Collection;
31: import java.util.List;
32: import java.util.Objects;
33: import java.util.Set;
34:
35: class ReferencedListDataPropertyStrategy<X> extends DataPropertyFieldStrategy<ListAttributeImpl<? super X, ?>, X> {
36:
37: private final Class<?> elementType;
38:
39: private final List<Object> values = new ArrayList<>();
40:
41: ReferencedListDataPropertyStrategy(EntityType<X> et, ListAttributeImpl<? super X, ?> att,
42: Descriptor attributeDescriptor, EntityMappingHelper mapper) {
43: super(et, att, attributeDescriptor, mapper);
44: this.elementType = att.getElementType().getJavaType();
45: }
46:
47: @Override
48: void addAxiomValue(Axiom<?> head) {
49: final ReferencedListDescriptor listDescriptor = createListDescriptor(head);
50: final Collection<Axiom<?>> sequence = mapper.loadReferencedList(listDescriptor);
51: sequence.stream()
52: .filter(item -> item.getAssertion().getIdentifier()
53: .equals(attribute.getHasContentsPropertyIRI().toURI()))
54: .forEach(item -> {
55: final Object value = item.getValue().getValue();
56: if (isValidRange(value)) {
57: values.add(toAttributeValue(value));
58: }
59: });
60: }
61:
62: ReferencedListDescriptor createListDescriptor(Axiom<?> ax) {
63: final NamedResource owner = ax.getSubject();
64: final ReferencedListDescriptor listDescriptor = ListDescriptorFactory.createReferencedListDescriptor(owner, attribute);
65: listDescriptor.setContext(getAttributeWriteContext());
66: return listDescriptor;
67: }
68:
69: @Override
70: boolean hasValue() {
71: return !values.isEmpty();
72: }
73:
74: @Override
75: boolean isValidRange(Object value) {
76: return elementType.isAssignableFrom(value.getClass()) || canBeConverted(value);
77: }
78:
79: @Override
80: void buildInstanceFieldValue(Object instance) {
81: setValueOnInstance(instance, values);
82: }
83:
84: @Override
85: void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) {
86: final Object value = extractFieldValueFromInstance(instance);
87: assert value instanceof List || value == null;
88:
89: final ReferencedListValueDescriptor<Object> listDescriptor = createListValueDescriptor(instance);
90: final List<?> list = (List<?>) value;
91: if (list != null) {
92: list.stream().filter(Objects::nonNull)
93: .forEach(v -> listDescriptor.addValue(converter.convertToAxiomValue(v)));
94: }
95: valueBuilder.addReferencedListValues(listDescriptor);
96: }
97:
98: private <V> ReferencedListValueDescriptor<V> createListValueDescriptor(X instance) {
99: final NamedResource owner = NamedResource.create(EntityPropertiesUtils.getIdentifier(instance, et));
100: final ReferencedListValueDescriptor<V> descriptor = ListDescriptorFactory.createReferencedListValueDescriptor(owner, attribute);
101: descriptor.setContext(getAttributeWriteContext());
102: return descriptor;
103: }
104:
105: @Override
106: Set<Axiom<?>> buildAxiomsFromInstance(X instance) {
107: throw new UnsupportedOperationException("Method not supported for referenced lists.");
108: }
109: }