Skip to content

Commit

Permalink
Improve getJenaGraph() error handling for connections with nonexisten…
Browse files Browse the repository at this point in the history
…t/empty graphs
  • Loading branch information
weisenje committed Oct 18, 2023
1 parent d2d8781 commit 7f12dfb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.ge.research.semtk.utility.LocalLogger;

public class SparqlConnection {
/**
* SparqlConnection is a "SparqlGraph" connection consisting of multiple Sparql Endpoints
Expand Down Expand Up @@ -605,31 +607,45 @@ public String getUniqueKey() {

/**
* Create a Jena Graph from this SparqlConnection
* @throws Exception if nothing loaded from connection
*/
public Graph getJenaGraph() {
ArrayList<String> fetched = new ArrayList<String>();

public Graph getJenaGraph() throws Exception {

Model model = null;
String serverAndPort = null;
String graphName = null;
RDFConnection rdfConn = null;
try {
for(SparqlEndpointInterface sei : this.getAllInterfaces()) {
rdfConn = RDFConnection.connect(sei.getServerAndPort());
String graphName = sei.getGraph();
if(fetched.contains(sei.getServerAndPort() + graphName)) {
continue; // skip if already fetched it (e.g. if model graph is the same as data graph)
ArrayList<String> fetched = new ArrayList<String>(); // remember which graphs are already fetched, to avoid repeat work (e.g. if data and model are using the same graph)

for (SparqlEndpointInterface sei : this.getAllInterfaces()) {

try {
serverAndPort = sei.getServerAndPort();
graphName = sei.getGraph();
if (fetched.contains(serverAndPort + graphName)) {
continue; // skip if already fetched it
}
fetched.add(serverAndPort + graphName);

rdfConn = RDFConnection.connect(serverAndPort);
Model m = rdfConn.fetch(graphName);
fetched.add(sei.getServerAndPort() + graphName);
if(model == null) {
if (model == null) {
model = m;
}else {
} else {
model.add(m);
}
} catch (Exception e) {
// log but do not error (allow a subset of graphs in the connection to be nonexistent/empty)
LocalLogger.logToStdErr("Cannot get Jena graph from graph '" + graphName + "' in " + serverAndPort + " (graph may be empty)");
} finally {
rdfConn.close();
}
return model.getGraph();
}finally {
rdfConn.close();

}
// if nothing was loaded from the connection, error
if (model == null) {
throw new Exception("Cannot get Jena graph for this connection (all graphs may be empty)");
}
return model.getGraph();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public BufferedReader execLoadIngestionPackage(File ingestionPackageFile, String
*/
@SuppressWarnings("unchecked")
public String execGetShaclResults(File shaclTtlFile, SparqlConnection conn, Severity severity) throws Exception {

Thread.sleep(5000); // TODO remove

if(!shaclTtlFile.exists()) {
throw new Exception("File does not exist: " + shaclTtlFile.getAbsolutePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,42 @@
package com.ge.research.semtk.sparqlX.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.apache.jena.graph.Graph;
import org.junit.Test;

import com.ge.research.semtk.sparqlX.SparqlConnection;
import com.ge.research.semtk.sparqlX.SparqlEndpointInterface;
import com.ge.research.semtk.test.TestGraph;

public class SparqlConnectionTest_IT {

@Test
public void testGetJenaGraph() throws Exception {

// test connection with all (1) graphs containing data
TestGraph.clearGraph();
TestGraph.uploadTurtleResource(this, "musicTestDataset_2017.q2.ttl");
Graph g = TestGraph.getSparqlConn().getJenaGraph();
SparqlConnection conn = TestGraph.getSparqlConn();
Graph g = conn.getJenaGraph();
assertEquals(g.size(), 215);

// test connection with one graph containing data, one graph empty
SparqlEndpointInterface sei = TestGraph.getSei("nonexistentGraph");
conn.addDataInterface(sei);
g = conn.getJenaGraph();
assertEquals(g.size(), 215);

// test connection with no graphs containing data
try {
TestGraph.clearGraph();
conn = TestGraph.getSparqlConn();
g = conn.getJenaGraph();
fail(); // shouldn't get here
}catch(Exception e) {
assertTrue(e.getMessage().equals("Cannot get Jena graph for this connection (all graphs may be empty)"));
}
}

}

0 comments on commit 7f12dfb

Please sign in to comment.