From da4fbf2101fb75e28c9a628f86e50b5394af0e18 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20Sch=C3=A4rtl?= <andreas@schaertl.me>
Date: Mon, 15 Jun 2020 14:29:12 +0200
Subject: [PATCH] ulo-storage-endpoint: add some counting functionality

---
 .../java/info/mathhub/uloapi/cli/Main.java    | 58 +++++++++++++++++++
 .../info/mathhub/uloapi/query/GraphDB.java    | 18 ++++++
 .../src/main/resources/uloapi.properties      |  2 +-
 3 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/cli/Main.java

diff --git a/src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/cli/Main.java b/src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/cli/Main.java
new file mode 100644
index 0000000..e15345a
--- /dev/null
+++ b/src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/cli/Main.java
@@ -0,0 +1,58 @@
+package info.mathhub.uloapi.cli;
+
+import info.mathhub.uloapi.query.GraphDB;
+import info.mathhub.uloapi.query.ULO;
+import org.eclipse.rdf4j.model.IRI;
+import org.eclipse.rdf4j.model.Statement;
+import org.eclipse.rdf4j.repository.RepositoryResult;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+public class Main {
+    public static void main(String[] args) {
+        final List<IRI> predicates = Arrays.asList(
+                ULO.action_times, ULO.aligned_with, ULO.alternative_for, ULO.antonym,
+                ULO.automatically_proved, ULO.axiom, ULO.check_time, ULO.constructs,
+                ULO.contains, ULO.counter_example_for, ULO.crossrefs, ULO.declaration,
+                ULO.defines, ULO.definition, ULO.deprecated, ULO.derived, ULO.docref,
+                ULO.example, ULO.example_for, ULO.experimental, ULO.external_size,
+                ULO.file, ULO.folder, ULO.formalizes, ULO.function, ULO.generated_by,
+                ULO.hypernym, ULO.hyponym, ULO.implementation_uses,
+                ULO.implementation_uses_implementation_of,
+                ULO.implementation_uses_interface_of, ULO.important, ULO.inductive_on,
+                ULO.inspired_by, ULO.instance_of, ULO.inter_statement,
+                ULO.interface_uses, ULO.interface_uses_implementation_of,
+                ULO.interface_uses_interface_of, ULO.internal_size, ULO.justifies,
+                ULO.last_checked_at, ULO.library, ULO.library_group, ULO.logical,
+                ULO.mutual_block, ULO.name, ULO.nyms, ULO.organizational, ULO.para,
+                ULO.paratype, ULO.phrase, ULO.physical, ULO.predicate, ULO.primitive,
+                ULO.proof, ULO.proposition, ULO.revision, ULO.rule, ULO.same_as,
+                ULO.section, ULO.see_also, ULO.similar_to, ULO.size_properties,
+                ULO.sourceref, ULO.specified_in, ULO.specifies, ULO.statement,
+                ULO.superseded_by, ULO.theorem, ULO.theory, ULO.type, ULO.typedec,
+                ULO.unimportant, ULO.universe, ULO.uses, ULO.uses_implementation,
+                ULO.uses_interface
+        );
+
+        for (final IRI predicate : predicates) {
+            final GraphDB.Operation<Long> operation = (manager, repository, connection) -> {
+                final RepositoryResult<Statement> result = connection.getStatements(null, predicate, null);
+                final Iterator<Statement> iterator = result.iterator();
+
+                long count = 0;
+
+                while (iterator.hasNext()) {
+                    count += 1;
+                    result.next();
+                }
+
+                return count;
+            };
+
+            final long count = GraphDB.execute(operation);
+            System.out.printf("%s - %s\n", count, predicate.toString());
+        }
+    }
+}
diff --git a/src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/query/GraphDB.java b/src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/query/GraphDB.java
index 3ce11ca..7dad53a 100644
--- a/src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/query/GraphDB.java
+++ b/src/ulo-storage-endpoint/src/main/java/info/mathhub/uloapi/query/GraphDB.java
@@ -1,5 +1,6 @@
 package info.mathhub.uloapi.query;
 
+import info.mathhub.uloapi.config.Config;
 import org.eclipse.rdf4j.repository.Repository;
 import org.eclipse.rdf4j.repository.RepositoryConnection;
 import org.eclipse.rdf4j.repository.RepositoryException;
@@ -48,6 +49,23 @@ public class GraphDB {
         void apply() throws Exception;
     }
 
+    /**
+     * Run an operation on a remote repository. Uses default credentials as defines in the
+     * {@code uloapi.properties} file.
+     *
+     * @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(Operation<T> op) {
+        final String serverUrl = Config.serverUrl();
+        final String repositoryName = Config.repository();
+
+        return GraphDB.execute(serverUrl, repositoryName, op);
+    }
+
     /**
      * Run an operation on a remote repository.
      *
diff --git a/src/ulo-storage-endpoint/src/main/resources/uloapi.properties b/src/ulo-storage-endpoint/src/main/resources/uloapi.properties
index ee82139..a7b5c95 100644
--- a/src/ulo-storage-endpoint/src/main/resources/uloapi.properties
+++ b/src/ulo-storage-endpoint/src/main/resources/uloapi.properties
@@ -1,2 +1,2 @@
 info.mathhub.uloapi.config.serverurl=http://rdf:7200
-info.mathhub.uloapi.config.repository=myulo
\ No newline at end of file
+info.mathhub.uloapi.config.repository=fckw
\ No newline at end of file
-- 
GitLab