Skip to contentMethod: AxiomDescriptorFactory()
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import cz.cvut.kbss.jopa.model.annotations.FetchType;
18: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
19: import cz.cvut.kbss.jopa.model.metamodel.*;
20: import cz.cvut.kbss.jopa.sessions.LoadingParameters;
21: import cz.cvut.kbss.ontodriver.descriptor.AxiomDescriptor;
22: import cz.cvut.kbss.ontodriver.model.Assertion;
23: import cz.cvut.kbss.ontodriver.model.NamedResource;
24:
25: import java.lang.reflect.Field;
26: import java.net.URI;
27:
28: class AxiomDescriptorFactory {
29:
30: AxiomDescriptor createForEntityLoading(LoadingParameters<?> loadingParams, EntityType<?> et) {
31: final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(loadingParams.getIdentifier()));
32: descriptor.setSubjectContext(loadingParams.getDescriptor().getContext());
33: descriptor.addAssertion(Assertion.createClassAssertion(false));
34: addForTypes(loadingParams, et, descriptor);
35: addForProperties(loadingParams, et, descriptor);
36: for (Attribute<?, ?> att : et.getAttributes()) {
37: if (!shouldLoad(att.getFetchType(), loadingParams.isForceLoad())) {
38: continue;
39: }
40: final Assertion a = createAssertion(att);
41: addAssertionToDescriptor(loadingParams.getDescriptor(), att, descriptor, a);
42: }
43: return descriptor;
44: }
45:
46: private void addForTypes(LoadingParameters<?> loadingParams, EntityType<?> et, AxiomDescriptor descriptor) {
47: final TypesSpecification<?, ?> types = et.getTypes();
48: if (types != null && shouldLoad(types.getFetchType(), loadingParams.isForceLoad())) {
49: final Assertion typesAssertion = Assertion.createClassAssertion(types.isInferred());
50: addAssertionToDescriptor(loadingParams.getDescriptor(), types, descriptor, typesAssertion);
51: }
52: }
53:
54: private boolean shouldLoad(FetchType fetchType, boolean forceLoad) {
55: return fetchType != FetchType.LAZY || forceLoad;
56: }
57:
58: private void addAssertionToDescriptor(Descriptor entityDescriptor,
59: FieldSpecification<?, ?> att, final AxiomDescriptor descriptor,
60: final Assertion assertion) {
61: descriptor.addAssertion(assertion);
62: final URI attContext = entityDescriptor.getAttributeDescriptor(att).getContext();
63: if (attContext != null) {
64: descriptor.setAssertionContext(assertion, attContext);
65: }
66: }
67:
68: private void addForProperties(LoadingParameters<?> loadingParams, EntityType<?> et, AxiomDescriptor descriptor) {
69: final PropertiesSpecification<?, ?, ?, ?> props = et.getProperties();
70: if (props != null && shouldLoad(props.getFetchType(), loadingParams.isForceLoad())) {
71: final Assertion propsAssertion = Assertion.createUnspecifiedPropertyAssertion(props.isInferred());
72: addAssertionToDescriptor(loadingParams.getDescriptor(), props, descriptor,
73: propsAssertion);
74: }
75: }
76:
77: private Assertion createAssertion(Attribute<?, ?> att) {
78: assert att != null;
79: switch (att.getPersistentAttributeType()) {
80: case OBJECT:
81: return Assertion.createObjectPropertyAssertion(att.getIRI().toURI(), att.isInferred());
82: case DATA:
83: return Assertion.createDataPropertyAssertion(att.getIRI().toURI(), att.isInferred());
84: case ANNOTATION:
85: return Assertion.createAnnotationPropertyAssertion(att.getIRI().toURI(),
86: att.isInferred());
87: default:
88: throw new IllegalArgumentException("Illegal persistent attribute type "
89: + att.getPersistentAttributeType());
90: }
91: }
92:
93: AxiomDescriptor createForFieldLoading(URI primaryKey, Field field, Descriptor entityDescriptor,
94: EntityType<?> et) {
95: final AxiomDescriptor descriptor = new AxiomDescriptor(NamedResource.create(primaryKey));
96: FieldSpecification<?, ?> fieldSpec = MappingUtils.getFieldSpecification(field, et);
97: Assertion assertion;
98: if (et.getTypes() != null && fieldSpec.equals(et.getTypes())) {
99: assertion = Assertion.createClassAssertion(et.getTypes().isInferred());
100: } else if (et.getProperties() != null && fieldSpec.equals(et.getProperties())) {
101: assertion = Assertion.createUnspecifiedPropertyAssertion(et.getProperties()
102: .isInferred());
103: } else {
104: assertion = createAssertion((Attribute<?, ?>) fieldSpec);
105: }
106: addAssertionToDescriptor(entityDescriptor, fieldSpec, descriptor, assertion);
107: return descriptor;
108: }
109: }