Skip to content

Commit

Permalink
Merge branch 'main' into 191-thingaddvalue-is-not-tight-enough
Browse files Browse the repository at this point in the history
  • Loading branch information
GCHQDeveloper42 committed Feb 21, 2024
2 parents be63fb6 + a14403b commit 3b399cb
Show file tree
Hide file tree
Showing 232 changed files with 1,886 additions and 1,494 deletions.
104 changes: 63 additions & 41 deletions core/src/main/java/uk/gov/gchq/magmacore/service/MagmaCoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class MagmaCoreService {
public Set<ParticipantDetails> findParticipantDetails(final Individual individual1, final Individual individual2,
final KindOfAssociation kind, final PointInTime pointInTime) {

final Instant when = Instant.parse(pointInTime.value(HQDM.ENTITY_NAME).iterator().next().toString());
final Instant when = Instant.parse(pointInTime.oneValue(HQDM.ENTITY_NAME));

final QueryResultList queryResultList = database
.executeQuery(String.format(MagmaCoreServiceQueries.FIND_PARTICIPANT_DETAILS_QUERY, individual1.getId(),
Expand All @@ -113,7 +113,7 @@ public Set<ParticipantDetails> findParticipantDetails(final Individual individua
// Map them to ParticipantDetails objects.
.map(p -> {
// Get the Roles of the Participant.
final Set<Role> roles = p.value(HQDM.MEMBER_OF_KIND)
final Set<Role> roles = p.values(HQDM.MEMBER_OF_KIND)
.stream()
.map(o -> (IRI) o)
.map(roleIri -> database.get(roleIri))
Expand Down Expand Up @@ -171,12 +171,12 @@ public List<? extends Thing> findBySignValue(
final String value,
final PointInTime pointInTime) throws MagmaCoreException {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(String.format(MagmaCoreServiceQueries.FIND_BY_SIGN_VALUE_QUERY,
Expand Down Expand Up @@ -212,12 +212,12 @@ public List<? extends Thing> findByPartialSignValue(
final String value,
final PointInTime pointInTime) throws MagmaCoreException {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(String.format(MagmaCoreServiceQueries.FIND_BY_PARTIAL_SIGN_VALUE_CASE_INSENSITIVE_QUERY,
Expand Down Expand Up @@ -246,12 +246,12 @@ public List<? extends Thing> findByTypeClassAndSignPattern(
final IRI pattern,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(String.format(MagmaCoreServiceQueries.FIND_OBJECTS_BY_TYPE_CLASS_AND_SIGN_PATTERN,
Expand All @@ -278,12 +278,12 @@ public List<? extends Thing> findByTypeKindAndSignPattern(
final IRI pattern,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(String.format(MagmaCoreServiceQueries.FIND_OBJECTS_BY_TYPE_AND_SIGN_PATTERN,
Expand All @@ -305,12 +305,12 @@ public List<? extends Thing> findByTypeKindAndSignPattern(
*/
public List<? extends Thing> findByKindOfAssociation(final IRI kindOfAssociation, final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(String.format(MagmaCoreServiceQueries.FIND_BY_KIND_OF_ASSOCIATION,
Expand Down Expand Up @@ -352,12 +352,12 @@ public List<? extends Thing> findAssociated(final IRI item, final IRI kindOfAsso
*/
public List<? extends Thing> findAssociated(final IRI item, final IRI kindOfAssociation, final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(String.format(MagmaCoreServiceQueries.FIND_ASSOCIATED,
Expand All @@ -381,12 +381,12 @@ public List<? extends Thing> findAssociated(final IRI item, final IRI kindOfAsso
public List<? extends Thing> findByPartialSignAndClassCaseSensitive(final String text, final IRI classIri,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(
Expand All @@ -413,12 +413,12 @@ public List<? extends Thing> findByPartialSignAndClassCaseSensitive(final String
public List<? extends Thing> findByPartialSignAndClass(final String text, final IRI classIri,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database
.executeQuery(
Expand Down Expand Up @@ -448,12 +448,12 @@ public List<? extends Thing> findByPartialSignByActivityReferenceAndClass(final
final String text, final IRI classIri,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database.executeQuery(String.format(
MagmaCoreServiceQueries.FIND_MEMBERS_OF_CLASS_BY_ACTIVITY_AND_PARTIAL_SIGN_CASE_INSENSITIVE,
Expand Down Expand Up @@ -482,12 +482,12 @@ public List<? extends Thing> findByPartialSignByActivityReferenceAndClassCaseSen
final String text, final IRI classIri,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database.executeQuery(String.format(
MagmaCoreServiceQueries.FIND_MEMBERS_OF_CLASS_BY_ACTIVITY_AND_PARTIAL_SIGN_CASE_SENSITIVE,
Expand Down Expand Up @@ -516,12 +516,12 @@ public List<? extends Thing> findByPartialSignCompositionAndClassCaseSensitive(f
final String text, final IRI classIri,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database.executeQuery(String.format(
MagmaCoreServiceQueries.FIND_MEMBERS_OF_CLASS_BY_COMPOSITION_AND_PARTIAL_SIGN_CASE_SENSITIVE,
Expand Down Expand Up @@ -550,12 +550,12 @@ public List<? extends Thing> findByPartialSignCompositionAndClass(final IRI whol
final String text, final IRI classIri,
final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database.executeQuery(String.format(
MagmaCoreServiceQueries.FIND_MEMBERS_OF_CLASS_BY_COMPOSITION_AND_PARTIAL_SIGN_CASE_INSENSITIVE,
Expand All @@ -579,12 +579,12 @@ public List<? extends Thing> findByPartialSignCompositionAndClass(final IRI whol
*/
public List<SignPatternDto> findSignsForEntity(final IRI entityIri, final PointInTime pointInTime) {

final Set<Object> pointInTimeValues = pointInTime.value(HQDM.ENTITY_NAME);
if (pointInTimeValues == null || pointInTimeValues.isEmpty()) {
final String pointInTimeValue = pointInTime.oneValue(HQDM.ENTITY_NAME);
if (pointInTimeValue == null) {
return List.of();
}

final Instant when = Instant.parse(pointInTimeValues.iterator().next().toString());
final Instant when = Instant.parse(pointInTimeValue);

final QueryResultList queryResultList = database.executeQuery(String.format(
MagmaCoreServiceQueries.FIND_SIGNS_FOR_ENTITY, entityIri));
Expand Down Expand Up @@ -915,4 +915,26 @@ public void abort() {
public QueryResultList executeQuery(final String query) {
return database.executeQuery(query);
}

/**
* SPARQL queries restricted to having 3 columns for the subject, predicate, and object, with any names but they must be in that order. E.g.
* SELECT ?s ?p ?o WHERE {...} order by ?s ?p ?o
* The first column must contain entity IDs.
* The second column must contain predicates.
* The third column can contain IRIs or primitive values.
*
* @param query a SELECT query {@link String}
* @return a {@link Map} of {@link IRI} to {@link Thing}
*/
public Map<IRI, Thing> executeQueryForThings(final String query) {
final QueryResultList resultsList = database.executeQuery(query);

final List<Thing> things = database.toTopObjects(resultsList);

final Map<IRI, Thing> result = new HashMap<>();

things.forEach(t -> result.put(t.getId(), t));

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public void testFindSuccess() {
// This query augments its object with HQDM.VALUE predicates for the current Sign values for the
// object.

final Set<Object> values = person.value(HQDM.VALUE_);
final Set<Object> values = person.values(HQDM.VALUE_);

assertNotNull(values);
assertEquals(1, values.size());

final Set<Object> names = person.value(HQDM.ENTITY_NAME);
final Set<Object> names = person.values(HQDM.ENTITY_NAME);
assertNotNull(names);
assertEquals(1, names.size());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void testFindSuccess() {

// This query augments its object with HQDM.VALUE predicates for the current Sign values for the
// object.
final Set<Object> values = person.value(HQDM.VALUE_);
final Set<Object> values = person.values(HQDM.VALUE_);
assertNotNull(values);
assertEquals(1, values.size());
assertEquals("person1", values.iterator().next().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;
Expand Down Expand Up @@ -120,8 +121,8 @@ public void testFindBySignSuccess() throws MagmaCoreException {
assertEquals(SignPatternTestData.stateOfPerson1.getId(), personState1.getId());
assertEquals(SignPatternTestData.stateOfPerson2.getId(), personState2.getId());

final Set<Object> parent1 = personState1.value(HQDM.TEMPORAL_PART_OF);
final Set<Object> parent2 = personState2.value(HQDM.TEMPORAL_PART_OF);
final Set<Object> parent1 = personState1.values(HQDM.TEMPORAL_PART_OF);
final Set<Object> parent2 = personState2.values(HQDM.TEMPORAL_PART_OF);

// Check that the `temporal_part_of` relationship is correct.
assertEquals(1, parent1.size());
Expand Down Expand Up @@ -388,4 +389,35 @@ public void testSparqlQuery() {
assertEquals(1, result.getQueryResults().size());
assertEquals(5, result.getQueryResults().get(0).getMap().size());
}

/**
* Check that it is possible to query for a Set of Things.
*/
@Test
public void testQueryForThingsSuccess() {
// Create an in-memory databse.
final MagmaCoreService service = MagmaCoreServiceFactory.createWithJenaDatabase();

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

new DbChangeSet(
List.of(), // no deletes
List.of(// Two creates
new DbCreateOperation(subj1, HQDM.MEMBER_OF, obj1),
new DbCreateOperation(subj1, RDFS.RDF_TYPE, HQDM.PERSON),
new DbCreateOperation(obj1, RDFS.RDF_TYPE, HQDM.CLASS_OF_PERSON)
)
).apply(service);

// Query the service by joining the two statements in a single result.
final Map<IRI, Thing> result = service.executeQueryForThings("SELECT ?s ?p ?o WHERE { ?s ?p ?o}");

// Verify the result.
assertNotNull(result);
// There should be one result record with five columns.
assertEquals(2, result.size());
result.values().forEach(t -> assertTrue(t instanceof Thing));
}
}
Loading

0 comments on commit 3b399cb

Please sign in to comment.