Skip to content

Method: createAxiomValueBuilder(URI, Descriptor)

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.FieldSpecification;
23: import cz.cvut.kbss.jopa.model.metamodel.QueryAttribute;
24: import cz.cvut.kbss.jopa.oom.exception.EntityDeconstructionException;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26:
27: import java.net.URI;
28:
29: class EntityDeconstructor {
30:
31: private final EntityMappingHelper mapper;
32: private ReferenceSavingResolver referenceSavingResolver;
33:
34: EntityDeconstructor(ObjectOntologyMapperImpl mapper) {
35: this.mapper = mapper;
36: }
37:
38: void setReferenceSavingResolver(ReferenceSavingResolver referenceSavingResolver) {
39: this.referenceSavingResolver = referenceSavingResolver;
40: }
41:
42: <T> AxiomValueGatherer mapEntityToAxioms(URI identifier, T entity, EntityType<T> et, Descriptor descriptor) {
43: assert identifier != null;
44:
45: final AxiomValueGatherer valueBuilder = createAxiomValueBuilder(identifier, descriptor);
46: try {
47: for (FieldSpecification<? super T, ?> att : et.getFieldSpecifications()) {
48: if (att instanceof QueryAttribute) {
49: // Skip query based attributes, they are not mapped to axioms
50: continue;
51: }
52: addAssertions(entity, et, att, descriptor, valueBuilder);
53: }
54:
55: } catch (IllegalArgumentException e) {
56: throw new EntityDeconstructionException(e);
57: }
58: return valueBuilder;
59: }
60:
61: private static AxiomValueGatherer createAxiomValueBuilder(URI identifier, Descriptor descriptor) {
62: return new AxiomValueGatherer(NamedResource.create(identifier), descriptor.getSingleContext().orElse(null));
63: }
64:
65: private <T> void addAssertions(T entity, EntityType<T> et,
66: FieldSpecification<? super T, ?> fieldSpec, Descriptor entityDescriptor,
67: final AxiomValueGatherer valueBuilder) {
68: final FieldStrategy<? extends FieldSpecification<? super T, ?>, T> fs = FieldStrategy
69: .createFieldStrategy(et, fieldSpec, entityDescriptor, mapper);
70: fs.setReferenceSavingResolver(referenceSavingResolver);
71: fs.buildAxiomValuesFromInstance(entity, valueBuilder);
72: }
73:
74: <T> AxiomValueGatherer mapFieldToAxioms(URI identifier, T entity, FieldSpecification<? super T, ?> fieldSpec,
75: EntityType<T> et, Descriptor descriptor) {
76: final AxiomValueGatherer valueBuilder = createAxiomValueBuilder(identifier, descriptor);
77: addAssertions(entity, et, fieldSpec, descriptor, valueBuilder);
78: return valueBuilder;
79: }
80: }