Skip to content

Package: AxiomDescriptorFactory

AxiomDescriptorFactory

nameinstructionbranchcomplexitylinemethod
AxiomDescriptorFactory(Configuration)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
addAssertionToDescriptor(Descriptor, FieldSpecification, AxiomDescriptor, Assertion)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
addForProperties(LoadingParameters, EntityType, AxiomDescriptor)
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
addForTypes(LoadingParameters, EntityType, AxiomDescriptor)
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createAssertion(Attribute, Descriptor)
M: 17 C: 60
78%
M: 2 C: 8
80%
M: 2 C: 5
71%
M: 2 C: 10
83%
M: 0 C: 1
100%
createForEntityLoading(LoadingParameters, EntityType)
M: 0 C: 62
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
createForFieldLoading(URI, Field, Descriptor, EntityType)
M: 0 C: 58
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 11
100%
M: 0 C: 1
100%
createForReferenceLoading(URI, EntityType)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
language(Descriptor)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
shouldLoad(FetchType, boolean)
M: 0 C: 9
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
withLanguage(Attribute, Descriptor)
M: 0 C: 13
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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.JOPAPersistenceProperties;
16: import cz.cvut.kbss.jopa.model.annotations.FetchType;
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.metamodel.*;
19: import cz.cvut.kbss.jopa.sessions.LoadingParameters;
20: import cz.cvut.kbss.jopa.utils.Configuration;
21: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
22: import cz.cvut.kbss.ontodriver.model.*;
23:
24: import java.lang.reflect.Field;
25: import java.net.URI;
26: import java.util.Objects;
27:
28: import static cz.cvut.kbss.ontodriver.model.Assertion.*;
29:
30: class AxiomDescriptorFactory {
31:
32: private final String puLanguage;
33:
34: AxiomDescriptorFactory(Configuration configuration) {
35: this.puLanguage = configuration.get(JOPAPersistenceProperties.LANG);
36: }
37:
38: AxiomDescriptor createForEntityLoading(LoadingParameters<?> loadingParams, EntityType<?> et) {
39: final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(loadingParams.getIdentifier()));
40: descriptor.setSubjectContext(loadingParams.getDescriptor().getContext());
41: descriptor.addAssertion(Assertion.createClassAssertion(false));
42: addForTypes(loadingParams, et, descriptor);
43: addForProperties(loadingParams, et, descriptor);
44:• for (Attribute<?, ?> att : et.getAttributes()) {
45:• if (!shouldLoad(att.getFetchType(), loadingParams.isForceEager())) {
46: continue;
47: }
48: final Assertion a = createAssertion(att, loadingParams.getDescriptor().getAttributeDescriptor(att));
49: addAssertionToDescriptor(loadingParams.getDescriptor(), att, descriptor, a);
50: }
51: return descriptor;
52: }
53:
54: private void addForTypes(LoadingParameters<?> loadingParams, EntityType<?> et, AxiomDescriptor descriptor) {
55: final TypesSpecification<?, ?> types = et.getTypes();
56:• if (types != null && shouldLoad(types.getFetchType(), loadingParams.isForceEager())) {
57: final Assertion typesAssertion = Assertion.createClassAssertion(types.isInferred());
58: addAssertionToDescriptor(loadingParams.getDescriptor(), types, descriptor, typesAssertion);
59: }
60: }
61:
62: private static boolean shouldLoad(FetchType fetchType, boolean forceLoad) {
63:• return fetchType != FetchType.LAZY || forceLoad;
64: }
65:
66: private void addAssertionToDescriptor(Descriptor entityDescriptor, FieldSpecification<?, ?> att,
67: final AxiomDescriptor descriptor, final Assertion assertion) {
68: descriptor.addAssertion(assertion);
69: final URI attContext = entityDescriptor.getAttributeContext(att);
70:• if (!Objects.equals(entityDescriptor.getContext(), attContext)) {
71: descriptor.setAssertionContext(assertion, attContext);
72: }
73: }
74:
75: private void addForProperties(LoadingParameters<?> loadingParams, EntityType<?> et, AxiomDescriptor descriptor) {
76: final PropertiesSpecification<?, ?, ?, ?> props = et.getProperties();
77:• if (props != null && shouldLoad(props.getFetchType(), loadingParams.isForceEager())) {
78: final Assertion propsAssertion = Assertion.createUnspecifiedPropertyAssertion(props.isInferred());
79: addAssertionToDescriptor(loadingParams.getDescriptor(), props, descriptor, propsAssertion);
80: }
81: }
82:
83: private Assertion createAssertion(Attribute<?, ?> att, Descriptor descriptor) {
84:• assert att != null;
85:• switch (att.getPersistentAttributeType()) {
86: case OBJECT:
87: return createObjectPropertyAssertion(att.getIRI().toURI(), att.isInferred());
88: case DATA:
89:• if (withLanguage(att, descriptor)) {
90: return createDataPropertyAssertion(att.getIRI().toURI(), language(descriptor), att.isInferred());
91: } else {
92: return createDataPropertyAssertion(att.getIRI().toURI(), att.isInferred());
93: }
94: case ANNOTATION:
95:• if (withLanguage(att, descriptor)) {
96: return createAnnotationPropertyAssertion(att.getIRI().toURI(), language(descriptor),
97: att.isInferred());
98: } else {
99: return createAnnotationPropertyAssertion(att.getIRI().toURI(), att.isInferred());
100: }
101: default:
102: throw new IllegalArgumentException(
103: "Illegal persistent attribute type " + att.getPersistentAttributeType());
104: }
105: }
106:
107: private boolean withLanguage(Attribute<?, ?> att, Descriptor descriptor) {
108:• return !att.isSimpleLiteral() && (descriptor.hasLanguage() || puLanguage != null);
109: }
110:
111: private String language(Descriptor descriptor) {
112:• return descriptor.hasLanguage() ? descriptor.getLanguage() : puLanguage;
113: }
114:
115: /**
116: * Creates an axiom representing a class assertion.
117: * <p>
118: * This axiom can be used to load a reference to an individual with the correct type, without any other attributes.
119: *
120: * @param identifier Individual identifier
121: * @param et Entity type. Type IRI is extracted from it
122: * @return {@code Axiom}
123: */
124: Axiom<NamedResource> createForReferenceLoading(URI identifier, EntityType<?> et) {
125: return new AxiomImpl<>(NamedResource.create(identifier), Assertion.createClassAssertion(false),
126: new Value<>(NamedResource.create(et.getIRI().toString())));
127: }
128:
129: AxiomDescriptor createForFieldLoading(URI identifier, Field field, Descriptor entityDescriptor, EntityType<?> et) {
130: final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(identifier));
131: descriptor.setSubjectContext(entityDescriptor.getContext());
132: FieldSpecification<?, ?> fieldSpec = MappingUtils.getFieldSpecification(field, et);
133: final Assertion assertion;
134:• if (et.getTypes() != null && fieldSpec.equals(et.getTypes())) {
135: assertion = Assertion.createClassAssertion(et.getTypes().isInferred());
136:• } else if (et.getProperties() != null && fieldSpec.equals(et.getProperties())) {
137: assertion = Assertion.createUnspecifiedPropertyAssertion(et.getProperties().isInferred());
138: } else {
139: assertion = createAssertion((Attribute<?, ?>) fieldSpec,
140: entityDescriptor.getAttributeDescriptor(fieldSpec));
141: }
142: addAssertionToDescriptor(entityDescriptor, fieldSpec, descriptor, assertion);
143: return descriptor;
144: }
145: }