Skip to content

Package: FactoryOfFactories

FactoryOfFactories

nameinstructionbranchcomplexitylinemethod
FactoryOfFactories(DriverConfiguration)
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%
createConnectorFactory()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createStatementLoaderFactory()
M: 8 C: 12
60%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 3
60%
M: 0 C: 1
100%
isRepositoryGraphDB()
M: 9 C: 26
74%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 7
70%
M: 0 C: 1
100%
lambda$resolveFactoryConfig$0(String, IsolationLevels)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
resolveFactoryConfig()
M: 6 C: 38
86%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 9
90%
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: * 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.ontodriver.rdf4j.connector.init;
19:
20: import cz.cvut.kbss.ontodriver.config.DriverConfiguration;
21: import cz.cvut.kbss.ontodriver.rdf4j.config.Rdf4jConfigParam;
22: import cz.cvut.kbss.ontodriver.rdf4j.connector.ConnectionFactory;
23: import cz.cvut.kbss.ontodriver.rdf4j.connector.ConnectionFactoryConfig;
24: import cz.cvut.kbss.ontodriver.rdf4j.connector.ConnectionFactoryImpl;
25: import cz.cvut.kbss.ontodriver.rdf4j.connector.StorageConnector;
26: import cz.cvut.kbss.ontodriver.rdf4j.exception.Rdf4jDriverException;
27: import cz.cvut.kbss.ontodriver.rdf4j.loader.DefaultContextInferenceStatementLoaderFactory;
28: import cz.cvut.kbss.ontodriver.rdf4j.loader.DefaultStatementLoaderFactory;
29: import cz.cvut.kbss.ontodriver.rdf4j.loader.GraphDBStatementLoaderFactory;
30: import cz.cvut.kbss.ontodriver.rdf4j.loader.StatementLoaderFactory;
31: import org.eclipse.rdf4j.common.transaction.IsolationLevels;
32: import org.eclipse.rdf4j.model.ValueFactory;
33: import org.eclipse.rdf4j.query.BooleanQuery;
34: import org.eclipse.rdf4j.repository.RepositoryConnection;
35: import org.slf4j.Logger;
36: import org.slf4j.LoggerFactory;
37:
38: import java.util.Optional;
39: import java.util.stream.Stream;
40:
41: /**
42: * Builds factories for the driver.
43: */
44: public class FactoryOfFactories {
45:
46: private static final Logger LOG = LoggerFactory.getLogger(FactoryOfFactories.class);
47:
48: /**
49: * Property representing internal entity IDs in GraphDB.
50: */
51: static final String GRAPHDB_INTERNAL_ID_PROPERTY = "http://www.ontotext.com/owlim/entity#id";
52:
53: private final DriverConfiguration config;
54: private final StorageConnector connector;
55:
56: private final boolean isGraphDB;
57:
58: public FactoryOfFactories(DriverConfiguration config) throws Rdf4jDriverException {
59: this.config = config;
60: this.connector = new StorageConnector(config);
61: connector.initializeRepository();
62: this.isGraphDB = isRepositoryGraphDB();
63: }
64:
65: public ConnectionFactory createConnectorFactory() throws Rdf4jDriverException {
66: return new ConnectionFactoryImpl(connector, resolveFactoryConfig());
67: }
68:
69: public StatementLoaderFactory createStatementLoaderFactory() {
70:• if (config.is(Rdf4jConfigParam.INFERENCE_IN_DEFAULT_CONTEXT)) {
71: return new DefaultContextInferenceStatementLoaderFactory();
72: }
73:• if (isGraphDB) {
74: return new GraphDBStatementLoaderFactory();
75: }
76: return new DefaultStatementLoaderFactory();
77: }
78:
79: ConnectionFactoryConfig resolveFactoryConfig() throws Rdf4jDriverException {
80: final String isolationLevelConfig = config.getProperty(Rdf4jConfigParam.TRANSACTION_ISOLATION_LEVEL);
81:• if (isolationLevelConfig != null) {
82: final Optional<IsolationLevels> optionalLevel = Stream.of(IsolationLevels.values())
83: .filter(level -> level.toString()
84: .equals(isolationLevelConfig))
85: .findAny();
86:• if (optionalLevel.isEmpty()) {
87: throw new Rdf4jDriverException("Unsupported transaction isolation level value '" + isolationLevelConfig + "'.");
88: }
89: LOG.debug("Configured to use RDF4J transaction isolation level '{}'.", optionalLevel.get());
90: return new ConnectionFactoryConfig(isGraphDB, optionalLevel.get());
91: }
92: return new ConnectionFactoryConfig(isGraphDB, null);
93: }
94:
95: /**
96: * Checks whether the underlying repository is in fact GraphDB.
97: * <p>
98: * It asks the repository if any subject has an internal GraphDB entity ID (represented by the
99: * {@link #GRAPHDB_INTERNAL_ID_PROPERTY}). Such a triple does not normally show in the data, but is accessing in
100: * GraphDB. If, for some reason, data stored in a non-GraphDB repository explicitly use these identifiers, this
101: * method will return false positive result.
102: *
103: * @return {@code true} if repository is determined to be GraphDB, {@code false} otherwise
104: */
105: private boolean isRepositoryGraphDB() throws Rdf4jDriverException {
106: try {
107: try (final RepositoryConnection connection = connector.acquireConnection()) {
108: final ValueFactory vf = connection.getValueFactory();
109: // Have to use a SPARQL query, because RDF4J API hasStatement call ended with an error
110: // See https://graphdb.ontotext.com/documentation/standard/query-behaviour.html#how-to-access-internal-identifiers-for-entities
111: final BooleanQuery query = connection.prepareBooleanQuery("ASK { ?x ?internalId ?y }");
112: query.setBinding("internalId", vf.createIRI(GRAPHDB_INTERNAL_ID_PROPERTY));
113: final boolean result = query.evaluate();
114:• if (result) {
115: LOG.debug("Underlying repository is GraphDB.");
116: }
117: return result;
118: }
119: } catch (RuntimeException e) {
120: throw new Rdf4jDriverException(e);
121: }
122: }
123: }