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

uloapi: run example query

I also etst performance and it looks okay on my small laptop.
parent 67c7af54
Branches
No related tags found
No related merge requests found
Showing with 132 additions and 3 deletions
......@@ -13,6 +13,7 @@ public class Main {
public static void main(String[] args) {
get("/", Routes.index, new FreeMarkerEngine());
get("/statistics", Routes.statistics, new FreeMarkerEngine());
get("/queries", Routes.queries, new FreeMarkerEngine());
internalServerError(Errors.internalServerError);
notFound(Errors.notFound);
......
......@@ -2,9 +2,12 @@ package info.mathhub.uloapi.html;
import info.mathhub.uloapi.query.Query;
import info.mathhub.uloapi.query.Statistics;
import info.mathhub.uloapi.query.Timer;
import org.eclipse.rdf4j.model.Statement;
import spark.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
......@@ -28,4 +31,18 @@ public class Routes {
return new ModelAndView(model, "statistics.flt");
};
public static final TemplateViewRoute queries = (Request request, Response response) -> {
final Query query = Query.getSingleton();
final Timer inductiveStatementsTimer = new Timer();
final List<Statement> inductiveStatements = query.getInductiveStatements();
final long inductiveStatementsDuration = inductiveStatementsTimer.elapsedMs();
final Map<String, Object> model = new HashMap<>();
model.put("inductive_statements", inductiveStatements);
model.put("inductive_statements_duration", inductiveStatementsDuration);
return new ModelAndView(model, "queries.flt");
};
}
package info.mathhub.uloapi.query;
import info.mathhub.uloapi.config.Config;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.repository.RepositoryResult;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Class {@link Query} wraps various common queries to an RDF4j endpoint. Objects of type {@link Query}
* are safe for concurrent used by multiple threads.
*/
public class Query {
private final String serverUrl;
private final String repository;
private static Query singleton;
/**
* Create a new object for the given endpoint.
*
* @param serverUrl URL of the RDF4J endpoint. Usually some kind of HTTP(S) URI.
* @param repository Name of the repository to connect to.
*/
public Query(String serverUrl, String repository) {
this.serverUrl = serverUrl;
this.repository = repository;
}
/**
* Get {@link Query} instance that is configured to connect to the endpoint defined
* in {@code uloapi.properties}.
*/
public static synchronized Query getSingleton() {
if (Query.singleton == null) {
Query.singleton = new Query(Config.serverUrl(), Config.repository());
......@@ -23,6 +42,9 @@ public class Query {
return Query.singleton;
}
/**
* Return {@link Statistics} that contain some meta data about the connected repository.
*/
public Statistics getStatistics() {
final GraphDB.Operation<Statistics> operation = (manager, repository, connection) -> {
final Statistics statistics = new Statistics();
......@@ -43,4 +65,28 @@ public class Query {
return GraphDB.execute(this.serverUrl, this.repository, operation);
}
/**
* Return a list of triplets that define some kind of {@link ULO#INDUCTIVE_ON} relationship.
*
* @return The triplets. Limited to a small number, the actual number of matching triplets in the
* underlying RDF4j repository might be way bigger.
*/
public List<Statement> getInductiveStatements() {
final GraphDB.Operation<List<Statement>> operation = (manager, repository, connection) -> {
final RepositoryResult<Statement> result = connection.getStatements(null, ULO.INDUCTIVE_ON, null);
final Iterator<Statement> iterator = result.iterator();
final List<Statement> statements = new ArrayList<>();
for (int i = 0; i < 16 && iterator.hasNext(); i++) {
final Statement statement = iterator.next();
statements.add(statement);
}
return statements;
};
return GraphDB.execute(this.serverUrl, this.repository, operation);
}
}
package info.mathhub.uloapi.query;
/**
* Class for timing execution of methods. Create a new {@link Timer} object,
* run some operation and then get the elapsed time with {@link Timer#elapsedMs}.
*/
public class Timer {
private final long startMs;
public Timer() {
this.startMs = System.currentTimeMillis();
}
/**
* Return elapsed time since this object has been created.
*
* @return Elapsed time since object creation in milliseconds.
*/
public long elapsedMs() {
final long stopMs = System.currentTimeMillis();
return stopMs - this.startMs;
}
}
......@@ -5,7 +5,6 @@ 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].
......
......@@ -4,7 +4,6 @@ import com.google.gson.Gson;
import org.eclipse.jetty.http.HttpStatus;
import spark.Request;
import spark.Response;
import spark.ResponseTransformer;
import spark.Route;
public class Errors {
......
package info.mathhub.uloapi.rest;
import info.mathhub.uloapi.query.Query;
import org.eclipse.jetty.http.HttpStatus;
import spark.*;
import java.util.HashMap;
......
......@@ -14,6 +14,7 @@
<title><@page_title/></title>
<style>
* {
background-color: white;
font-family: "Open Sans", Helvetica, Arial;
font-size: 12pt;
}
......@@ -36,6 +37,14 @@
display: inline-block;
font-weight: bold;
}
h2 {
font-size: 14pt;
}
table {
text-align: left;
}
</style>
</head>
<body>
......
<#include "base.flt">
<#macro page_title>
Statistics
</#macro>
<#macro page_main>
<h2>Queries</h2>
<p>
This page contains various example queries. They were executed using RDF4j which you
can think of as SPARQL for JVM languages.
</p>
<h3>Inductive Queries</h3>
<p>
Show some tuples where <em>x</em> is inductive on <em>y</em>.
Query took ${inductive_statements_duration} ms.
</p>
<table>
<tr>
<th>Subject <em>x</em></th>
<th>Object <em>y</em></th>
</tr>
<#list inductive_statements as statement>
<tr>
<td>${statement.subject}</td>
<td>${statement.object}</td>
</tr>
</#list>
</table>
</#macro>
<@display_page/>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment