Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

65 expose a sparql query service #168

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@

requires transitive uk.gov.gchq.magmacore.hqdm;

exports uk.gov.gchq.magmacore.service;
exports uk.gov.gchq.magmacore.database.query;
exports uk.gov.gchq.magmacore.exception;
exports uk.gov.gchq.magmacore.service.dto;
exports uk.gov.gchq.magmacore.service.transformation;
exports uk.gov.gchq.magmacore.exception;
exports uk.gov.gchq.magmacore.service;
exports uk.gov.gchq.magmacore.util;
}
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,14 @@ public void importTtl(final InputStream in) {
public List<Thing> verifyModel() {
return DataIntegrityReport.verify(database);
}

/**
* Execute a SELECT query.
*
* @param query a SELECT query {@link String}
* @return a {@link QueryResultList}
*/
public QueryResultList executeQuery(final String query) {
return database.executeQuery(query);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import uk.gov.gchq.magmacore.database.MagmaCoreDatabase;
import uk.gov.gchq.magmacore.database.MagmaCoreJenaDatabase;
import uk.gov.gchq.magmacore.database.query.QueryResultList;
import uk.gov.gchq.magmacore.exception.MagmaCoreException;
import uk.gov.gchq.magmacore.hqdm.model.Individual;
import uk.gov.gchq.magmacore.hqdm.model.PointInTime;
Expand All @@ -38,6 +39,8 @@
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IriBase;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.RDFS;
import uk.gov.gchq.magmacore.hqdm.services.SpatioTemporalExtentServices;
import uk.gov.gchq.magmacore.service.transformation.DbChangeSet;
import uk.gov.gchq.magmacore.service.transformation.DbCreateOperation;

/**
* Check that {@link MagmaCoreService} works correctly.
Expand Down Expand Up @@ -343,4 +346,44 @@ public void testFindByPartialSignAndClassCaseSensitive() throws MagmaCoreExcepti
assertTrue(found1.isEmpty());
assertTrue(found2.isEmpty());
}

/**
* Check that SPARQL queries can be executed and produce arbitrary results.
*/
@Test
public void testSparqlQuery() {

// Create an in-memory databse.
final MagmaCoreService service = MagmaCoreServiceFactory.createWithJenaDatabase();

// Populate some arbitrary data.
final IRI subj1 = new IRI(TEST_BASE, "subj1");
final IRI pred1 = new IRI(TEST_BASE, "pred1");
final IRI obj1 = new IRI(TEST_BASE, "obj1");
final IRI pred2 = new IRI(TEST_BASE, "pred2");
final IRI obj2 = new IRI(TEST_BASE, "obj2");

new DbChangeSet(
List.of(), // no deletes
List.of(// Two creates
new DbCreateOperation(subj1, pred1, obj1),
new DbCreateOperation(obj1, pred2, obj2)
)
).apply(service);

// Query the service by joining the two statements in a single result.
final QueryResultList result = service.executeQuery("SELECT * WHERE { ?a ?b ?c. ?c ?d ?e}");

// Verify the result.
assertNotNull(result);
assertTrue(result.getVarNames().contains("a"));
assertTrue(result.getVarNames().contains("b"));
assertTrue(result.getVarNames().contains("c"));
assertTrue(result.getVarNames().contains("d"));
assertTrue(result.getVarNames().contains("e"));

// There should be one result record with five columns.
assertEquals(1, result.getQueryResults().size());
assertEquals(5, result.getQueryResults().get(0).getMap().size());
}
}