Skip to content

Package: MetamodelImpl

MetamodelImpl

nameinstructionbranchcomplexitylinemethod
MetamodelImpl(Configuration, EntityLoader)
M: 0 C: 29
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
addInferredClass(Class)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addUriToModuleExtractionSignature(URI)
M: 4 C: 15
79%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 5
83%
M: 0 C: 1
100%
build(EntityLoader)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
checkForWeaver()
M: 9 C: 7
44%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 3
50%
M: 0 C: 1
100%
embeddable(Class)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
entity(Class)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getEmbeddables()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getEntities()
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getEntityClass(Class)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getInferredClasses()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getManagedTypes()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getModuleExtractionExtraSignature()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSignatureInternal()
M: 43 C: 19
31%
M: 3 C: 3
50%
M: 2 C: 2
50%
M: 7 C: 5
42%
M: 0 C: 1
100%
lambda$build$0(EntityFieldSeeker, Class)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
loadEntities(EntityLoader)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
managedType(Class)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
processOWLClass(Class, EntityFieldSeeker)
M: 0 C: 56
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 11
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
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: * <p>
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.model;
16:
17: import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
18: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
19: import cz.cvut.kbss.jopa.loaders.EntityLoader;
20: import cz.cvut.kbss.jopa.model.metamodel.*;
21: import cz.cvut.kbss.jopa.utils.Configuration;
22: import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
23: import org.slf4j.Logger;
24: import org.slf4j.LoggerFactory;
25:
26: import java.net.URI;
27: import java.net.URISyntaxException;
28: import java.util.*;
29:
30: public class MetamodelImpl implements Metamodel {
31:
32: private static final Logger LOG = LoggerFactory.getLogger(Metamodel.class);
33:
34: private static final String ASPECTJ_CLASS = "org.aspectj.weaver.loadtime.Agent";
35:
36: private final Map<Class<?>, EntityType<?>> typeMap = new HashMap<>();
37:
38: private final Set<Class<?>> inferredClasses = new HashSet<>();
39:
40: private final Configuration configuration;
41:
42: private Set<URI> moduleExtractionSignature;
43:
44: private final Set<Class<?>> entities = new HashSet<>();
45:
46: public MetamodelImpl(Configuration configuration, EntityLoader entityLoader) {
47: this.configuration = Objects.requireNonNull(configuration);
48: Objects.requireNonNull(entityLoader);
49: build(entityLoader);
50: }
51:
52: private void build(EntityLoader entityLoader) {
53: LOG.debug("Building metamodel ... ");
54: checkForWeaver();
55:
56: loadEntities(entityLoader);
57:
58: final EntityFieldSeeker fieldSeeker = new EntityFieldSeeker();
59:
60: entities.forEach(c -> processOWLClass(c, fieldSeeker));
61: }
62:
63: /**
64: * Check the class path for aspectj weaver, which is vital for using lazy loading.
65: */
66: private void checkForWeaver() {
67: try {
68: MetamodelImpl.class.getClassLoader().loadClass(ASPECTJ_CLASS);
69: } catch (ClassNotFoundException e) {
70: LOG.error("AspectJ not found on classpath. Cannot run without AspectJ.");
71: throw new OWLPersistenceException(e);
72: }
73: }
74:
75: private void loadEntities(EntityLoader entityLoader) {
76: Set<Class<?>> discoveredEntities = entityLoader.discoverEntityClasses(configuration);
77: entities.addAll(discoveredEntities);
78: }
79:
80: private <X> void processOWLClass(final Class<X> cls, EntityFieldSeeker fieldSeeker) {
81:• if (typeMap.containsKey(cls)) {
82: return;
83: }
84:
85: LOG.debug("processing OWL class: {}", cls);
86:
87: final EntityClassProcessor classProcessor = new EntityClassProcessor();
88:
89: final EntityTypeImpl<X> et = classProcessor.processEntityType(cls);
90:
91: typeMap.put(cls, et);
92: final EntityFieldMetamodelProcessor<X> fieldProcessor = new EntityFieldMetamodelProcessor<>(cls, et, this);
93:
94: fieldSeeker.discoverFields(cls).forEach(fieldProcessor::processField);
95:
96:• if (et.getIdentifier() == null) {
97: throw new MetamodelInitializationException("Missing identifier field in entity class " + cls);
98: }
99: }
100:
101: @SuppressWarnings("unchecked")
102: @Override
103: public <X> EntityType<X> entity(Class<X> cls) {
104:• if (!typeMap.containsKey(cls)) {
105: throw new IllegalArgumentException(
106: "Class " + cls.getName() + " is not a known entity in this persistence unit.");
107: }
108: return (EntityType<X>) typeMap.get(cls);
109: }
110:
111: @SuppressWarnings("unchecked")
112: public <X> EntityType<X> getEntityClass(Class<X> cls) {
113:• if (!typeMap.containsKey(cls)) {
114: processOWLClass(cls, new EntityFieldSeeker());
115: }
116: return (EntityType<X>) typeMap.get(cls);
117: }
118:
119: @Override
120: public <X> EmbeddableType<X> embeddable(Class<X> cls) {
121: throw new IllegalArgumentException("Embeddable entities not supported.");
122: }
123:
124: @Override
125: public Set<EmbeddableType<?>> getEmbeddables() {
126: return Collections.emptySet();
127: }
128:
129: @Override
130: public Set<EntityType<?>> getEntities() {
131: return new HashSet<>(typeMap.values());
132: }
133:
134: @Override
135: public Set<ManagedType<?>> getManagedTypes() {
136: return new HashSet<>(typeMap.values());
137: }
138:
139: @Override
140: public <X> ManagedType<X> managedType(Class<X> cls) {
141: return entity(cls);
142: }
143:
144: @Override
145: public Set<Class<?>> getInferredClasses() {
146: return Collections.unmodifiableSet(inferredClasses);
147: }
148:
149: public void addInferredClass(Class<?> cls) {
150: inferredClasses.add(cls);
151: }
152:
153: @Override
154: public Set<URI> getModuleExtractionExtraSignature() {
155: return Collections.unmodifiableSet(getSignatureInternal());
156: }
157:
158: @Override
159: public void addUriToModuleExtractionSignature(URI uri) {
160:• if (uri == null) {
161: throw new NullPointerException();
162: }
163: synchronized (this) {
164: getSignatureInternal().add(uri);
165: }
166: }
167:
168: private synchronized Set<URI> getSignatureInternal() {
169: // This can be lazily loaded since we don'attributeType know if we'll need it
170:• if (moduleExtractionSignature == null) {
171: final String sig = configuration.get(OntoDriverProperties.MODULE_EXTRACTION_SIGNATURE);
172:• if (sig == null) {
173: this.moduleExtractionSignature = new HashSet<>();
174: } else {
175: final String[] signature = sig.split(OntoDriverProperties.SIGNATURE_DELIMITER);
176: this.moduleExtractionSignature = new HashSet<>(signature.length);
177: try {
178:• for (String uri : signature) {
179: moduleExtractionSignature.add(new URI(uri));
180: }
181: } catch (URISyntaxException e) {
182: throw new OWLPersistenceException("Invalid URI encountered in module extraction signature.", e);
183: }
184: }
185: }
186: return moduleExtractionSignature;
187: }
188: }