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

Run ULO query from code

- This uses the RDF4J library which is what GraphDB is based on.
  Though it also supports other backends.

- The API is kinda nice tbh.
parent a0f4dda8
No related branches found
No related tags found
No related merge requests found
...@@ -11,11 +11,20 @@ ...@@ -11,11 +11,20 @@
<maven.compiler.target>1.11</maven.compiler.target> <maven.compiler.target>1.11</maven.compiler.target>
</properties> </properties>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/com.ontotext.graphdb/graphdb-free-runtime -->
<dependency> <dependency>
<groupId>com.ontotext.graphdb</groupId> <groupId>com.ontotext.graphdb</groupId>
<artifactId>graphdb-free-runtime</artifactId> <artifactId>graphdb-free-runtime</artifactId>
<version>9.2.0</version> <version>9.2.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -7,6 +7,9 @@ import org.eclipse.rdf4j.repository.RepositoryException; ...@@ -7,6 +7,9 @@ import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.repository.manager.RemoteRepositoryManager; import org.eclipse.rdf4j.repository.manager.RemoteRepositoryManager;
import org.eclipse.rdf4j.repository.manager.RepositoryManager; import org.eclipse.rdf4j.repository.manager.RepositoryManager;
import java.util.Collection;
import java.util.logging.LogManager;
/** /**
* Wrapper around various RDF4J methods. Represents a connection to a GraphDB * Wrapper around various RDF4J methods. Represents a connection to a GraphDB
* instance on the network. * instance on the network.
...@@ -30,20 +33,26 @@ public class GraphDB implements AutoCloseable { ...@@ -30,20 +33,26 @@ public class GraphDB implements AutoCloseable {
* @throws RepositoryException If connecting to the server or repository failed. * @throws RepositoryException If connecting to the server or repository failed.
*/ */
public GraphDB(String serverURL, String repository) throws RepositoryException { public GraphDB(String serverURL, String repository) throws RepositoryException {
this.manager = new RemoteRepositoryManager(serverURL); try {
this.manager.init(); this.manager = new RemoteRepositoryManager(serverURL);
this.manager.init();
final Collection<Repository> rs = this.manager.getAllRepositories();
this.repository = this.manager.getRepository(repository); this.repository = this.manager.getRepository(repository);
if (this.repository == null) { if (this.repository == null) {
this.manager.shutDown(); this.manager.shutDown();
throw new RepositoryException(String.format("no repository with name %s", repository)); throw new RepositoryException(String.format("no repository with name %s", repository));
} }
try {
this.connection = this.repository.getConnection(); this.connection = this.repository.getConnection();
} finally { } catch (Exception e) {
this.manager.shutDown(); try {
this.repository.shutDown(); this.close();
} catch (Exception ce) {
// ignore the close exception; we have enough already
}
throw e;
} }
} }
......
...@@ -3,6 +3,8 @@ package me.schaertl.graphcli; ...@@ -3,6 +3,8 @@ package me.schaertl.graphcli;
import org.eclipse.rdf4j.model.Statement; import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.repository.RepositoryConnection; import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.RepositoryResult; import org.eclipse.rdf4j.repository.RepositoryResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -12,6 +14,8 @@ public class Main { ...@@ -12,6 +14,8 @@ public class Main {
private static final String REPOSITORY = "dump"; private static final String REPOSITORY = "dump";
private static final int LIMIT = 100; private static final int LIMIT = 100;
private static final Logger log = LoggerFactory.getLogger(Main.class);
private static void hello() throws Exception { private static void hello() throws Exception {
try (final GraphDB db = new GraphDB(SERVER_URL, REPOSITORY)) { try (final GraphDB db = new GraphDB(SERVER_URL, REPOSITORY)) {
final RepositoryConnection connection = db.connection(); final RepositoryConnection connection = db.connection();
...@@ -19,7 +23,7 @@ public class Main { ...@@ -19,7 +23,7 @@ public class Main {
final RepositoryResult<Statement> result = connection.getStatements(null, ULO.INDUCTIVE_ON, null, true); final RepositoryResult<Statement> result = connection.getStatements(null, ULO.INDUCTIVE_ON, null, true);
for (Statement s : take(result, LIMIT)) { for (Statement s : take(result, LIMIT)) {
System.out.printf("%s INDUCTIVE ON %s", s.getSubject(), s.getObject()); log.info("{} INDUCTIVE ON {}", s.getSubject(), s.getObject());
} }
} }
} }
...@@ -33,7 +37,7 @@ public class Main { ...@@ -33,7 +37,7 @@ public class Main {
private static List<Statement> take(RepositoryResult<Statement> result, int n) { private static List<Statement> take(RepositoryResult<Statement> result, int n) {
final List<Statement> l = new ArrayList<>(n); final List<Statement> l = new ArrayList<>(n);
for (int i = 0; i < LIMIT && result.hasNext(); i++) { for (int i = 0; i < n && result.hasNext(); i++) {
final Statement s = result.next(); final Statement s = result.next();
l.add(s); l.add(s);
} }
......
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
\ 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