From 6ad973b7090d49b22a3c548afb70a1d1e081a745 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20Sch=C3=A4rtl?= <andreas@schaertl.me>
Date: Tue, 5 May 2020 10:56:44 +0200
Subject: [PATCH] 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.
---
 graphdb/graphcli/pom.xml                      | 11 ++++++-
 .../java/me/schaertl/graphcli/GraphDB.java    | 31 ++++++++++++-------
 .../main/java/me/schaertl/graphcli/Main.java  |  8 +++--
 .../graphcli/src/main/resources/logback.xml   | 11 +++++++
 4 files changed, 47 insertions(+), 14 deletions(-)
 create mode 100644 graphdb/graphcli/src/main/resources/logback.xml

diff --git a/graphdb/graphcli/pom.xml b/graphdb/graphcli/pom.xml
index 2705ebf..bf388d2 100644
--- a/graphdb/graphcli/pom.xml
+++ b/graphdb/graphcli/pom.xml
@@ -11,11 +11,20 @@
         <maven.compiler.target>1.11</maven.compiler.target>
     </properties>
     <dependencies>
-        <!-- https://mvnrepository.com/artifact/com.ontotext.graphdb/graphdb-free-runtime -->
         <dependency>
             <groupId>com.ontotext.graphdb</groupId>
             <artifactId>graphdb-free-runtime</artifactId>
             <version>9.2.0</version>
         </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>
 </project>
\ No newline at end of file
diff --git a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java
index 2b3cc44..fbbca2a 100644
--- a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java
+++ b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/GraphDB.java
@@ -7,6 +7,9 @@ import org.eclipse.rdf4j.repository.RepositoryException;
 import org.eclipse.rdf4j.repository.manager.RemoteRepositoryManager;
 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
  * instance on the network.
@@ -30,20 +33,26 @@ public class GraphDB implements AutoCloseable {
      * @throws RepositoryException If connecting to the server or repository failed.
      */
     public GraphDB(String serverURL, String repository) throws RepositoryException {
-        this.manager = new RemoteRepositoryManager(serverURL);
-        this.manager.init();
+        try {
+            this.manager = new RemoteRepositoryManager(serverURL);
+            this.manager.init();
+            final Collection<Repository> rs = this.manager.getAllRepositories();
 
-        this.repository = this.manager.getRepository(repository);
-        if (this.repository == null) {
-            this.manager.shutDown();
-            throw new RepositoryException(String.format("no repository with name %s", repository));
-        }
+            this.repository = this.manager.getRepository(repository);
+            if (this.repository == null) {
+                this.manager.shutDown();
+                throw new RepositoryException(String.format("no repository with name %s", repository));
+            }
 
-        try {
             this.connection = this.repository.getConnection();
-        } finally {
-            this.manager.shutDown();
-            this.repository.shutDown();
+        } catch (Exception e) {
+            try {
+                this.close();
+            } catch (Exception ce) {
+                // ignore the close exception; we have enough already
+            }
+
+            throw e;
         }
     }
 
diff --git a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java
index 8143cb9..bfa5318 100644
--- a/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java
+++ b/graphdb/graphcli/src/main/java/me/schaertl/graphcli/Main.java
@@ -3,6 +3,8 @@ package me.schaertl.graphcli;
 import org.eclipse.rdf4j.model.Statement;
 import org.eclipse.rdf4j.repository.RepositoryConnection;
 import org.eclipse.rdf4j.repository.RepositoryResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -12,6 +14,8 @@ public class Main {
     private static final String REPOSITORY = "dump";
     private static final int LIMIT = 100;
 
+    private static final Logger log = LoggerFactory.getLogger(Main.class);
+
     private static void hello() throws Exception {
         try (final GraphDB db = new GraphDB(SERVER_URL, REPOSITORY)) {
             final RepositoryConnection connection = db.connection();
@@ -19,7 +23,7 @@ public class Main {
             final RepositoryResult<Statement> result = connection.getStatements(null, ULO.INDUCTIVE_ON, null, true);
 
             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 {
     private static List<Statement> take(RepositoryResult<Statement> result, int 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();
             l.add(s);
         }
diff --git a/graphdb/graphcli/src/main/resources/logback.xml b/graphdb/graphcli/src/main/resources/logback.xml
new file mode 100644
index 0000000..6156c21
--- /dev/null
+++ b/graphdb/graphcli/src/main/resources/logback.xml
@@ -0,0 +1,11 @@
+<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
-- 
GitLab