Skip to content
Snippets Groups Projects
Commit d402c693 authored by Andreas Schärtl's avatar Andreas Schärtl
Browse files

uloapi: add GraphDB connection

parent 1be3c981
No related branches found
No related tags found
No related merge requests found
......@@ -73,5 +73,10 @@
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.ontotext.graphdb</groupId>
<artifactId>graphdb-free-runtime</artifactId>
<version>9.2.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package info.mathhub.uloapi.config;
import java.util.ResourceBundle;
public class Config {
private Config() {};
public static String serverUrl() {
return Config.get("info.mathhub.uloapi.config.serverurl");
}
public static String repository() {
return Config.get("info.mathhub.uloapi.config.repository");
}
private static String get(String key) {
final ResourceBundle bundle = ResourceBundle.getBundle("uloapi");
return bundle.getString(key);
}
}
package info.mathhub.uloapi.query;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.manager.RemoteRepositoryManager;
import org.eclipse.rdf4j.repository.manager.RepositoryManager;
public class GraphDB {
private GraphDB() {};
static class OperationException extends RuntimeException {
OperationException(String message) {
super(message);
}
OperationException(String message, Throwable cause) {
super(message, cause);
}
}
/**
* Functional interface that defines a function for running operations on
* a GraphDB database.
*/
private interface Operation<T> {
/**
* Run some operation connected to a database.
*
* @param manager The manager that was used for initializing the database connections.
* @param repository The repository on which to operate.
* @param connection The connection to the repository on which to operate.
* @throws OperationException If the logic of the operation itself causes some kind of error.
* @throws RepositoryException If communication with the remote database fails.
*/
T op(RepositoryManager manager, Repository repository, RepositoryConnection connection);
}
/**
* Run an operation on a remote repository.
*
* @param serverUrl The server of the remote GraphDB instance.
* @param repositoryName The repository on the remote instance on which to run the operation.
* @param op The operation to execute.
* @param <T> The return type of the operation
* @return The return value of the operation, i.e. of argument {@code op}.
* @throws OperationException If the logic of the operation itself causes some kind of error.
* @throws RepositoryException If communication with the remote database fails.
*/
public static <T> T execute(String serverUrl, String repositoryName, Operation<T> op) {
RepositoryManager manager = null;
Repository repository = null;
RepositoryConnection connection = null;
try {
manager = new RemoteRepositoryManager(serverUrl);
manager.init();
if ((repository = manager.getRepository(repositoryName)) == null) {
throw new RepositoryException("no such repository");
}
connection = repository.getConnection();
return op.op(manager, repository, connection);
} finally {
if (connection != null) {
connection.close();
}
if (repository != null) {
repository.shutDown();
}
if (manager != null) {
manager.shutDown();
}
}
}
}
package info.mathhub.uloapi.query;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Namespace;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleNamespace;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.model.vocabulary.FOAF;
/**
* Constants for use with ULO, the Upper Library Ontology [1].
*
* [1] https://gl.mathhub.info/ulo/ulo/-/tree/master/
*/
public class ULO {
private ULO() {}
/**
* The ULO namespace: https://mathhub.info/ulo
*/
public static final String NAMESPACE = "https://mathhub.info/ulo";
/**
* The recommended prefix for the ULO namespace: "ulo"
*/
public static final String PREFIX = "ulo";
/**
* An immutable {@link Namespace} constant that represents the ULO namespace.
*/
public static final Namespace NS = new SimpleNamespace(PREFIX, NAMESPACE);
public final static IRI INDUCTIVE_ON;
static {
final ValueFactory factory = SimpleValueFactory.getInstance();
INDUCTIVE_ON = factory.createIRI(ULO.NAMESPACE, "inductive-on");
}
}
info.mathhub.uloapi.config.serverurl="http://graphdb:7200"
info.mathhub.uloapi.config.repository="myulo"
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment