Skip to content

Commit

Permalink
Merge pull request #192 from gchq/191-thingaddvalue-is-not-tight-enough
Browse files Browse the repository at this point in the history
Stronger types signature for Top.addValue() method.
  • Loading branch information
GCHQDeveloper42 authored Feb 21, 2024
2 parents a14403b + 184d98b commit 440d9aa
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ private final QueryResultList getQueryResultList(final QueryExecution queryExec)
* @return a {@link List} of {@link Thing}
*/
public final List<Thing> toTopObjects(final QueryResultList queryResultsList) {
final Map<RDFNode, List<Pair<Object, Object>>> objectMap = new HashMap<>();
final Map<RDFNode, List<Pair<IRI, Object>>> objectMap = new HashMap<>();
final List<String> varNames = (List<String>) queryResultsList.getVarNames();

final String subjectVarName = varNames.get(0);
Expand All @@ -413,7 +413,7 @@ public final List<Thing> toTopObjects(final QueryResultList queryResultsList) {
final RDFNode predicateValue = queryResult.get(predicateVarName);
final RDFNode objectValue = queryResult.get(objectVarName);

List<Pair<Object, Object>> dataModelObject = objectMap.get(subjectValue);
List<Pair<IRI, Object>> dataModelObject = objectMap.get(subjectValue);
if (dataModelObject == null) {
dataModelObject = new ArrayList<>();
objectMap.put(subjectValue, dataModelObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private final QueryResultList getQueryResultList(final QueryExecution queryExec)
* @return a {@link List} of {@link Thing}
*/
public final List<Thing> toTopObjects(final QueryResultList queryResultsList) {
final Map<RDFNode, List<Pair<Object, Object>>> objectMap = new HashMap<>();
final Map<RDFNode, List<Pair<IRI, Object>>> objectMap = new HashMap<>();
final List<String> varNames = (List<String>) queryResultsList.getVarNames();
final String subjectVarName = varNames.get(0);
final String predicateVarName = varNames.get(1);
Expand All @@ -387,7 +387,7 @@ public final List<Thing> toTopObjects(final QueryResultList queryResultsList) {
final RDFNode predicateValue = queryResult.get(predicateVarName);
final RDFNode objectValue = queryResult.get(objectVarName);

List<Pair<Object, Object>> dataModelObject = objectMap.get(subjectValue);
List<Pair<IRI, Object>> dataModelObject = objectMap.get(subjectValue);
if (dataModelObject == null) {
dataModelObject = new ArrayList<>();
objectMap.put(subjectValue, dataModelObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,27 @@ public MagmaCoreService apply(final MagmaCoreService mcService) {

if (thing == null) {
final Thing newThing = SpatioTemporalExtentServices.createThing(subject);
newThing.addValue(predicate, object);
if (object instanceof IRI iri) {
newThing.addValue(predicate, iri);
} else if (object instanceof String s) {
newThing.addStringValue(predicate, s);
} else if (object instanceof Double d) {
newThing.addRealValue(predicate, d);
} else {
throw new ClassCastException("Unknown object type: " + object.getClass().getName());
}
mcService.create(newThing);
} else {
if (!thing.hasThisValue(predicate, object)) {
thing.addValue(predicate, object);
if (object instanceof IRI iri) {
thing.addValue(predicate, iri);
} else if (object instanceof String s) {
thing.addStringValue(predicate, s);
} else if (object instanceof Double d) {
thing.addRealValue(predicate, d);
} else {
throw new ClassCastException("Unknown object type: " + object.getClass().getName());
}
mcService.update(thing);
} else {
throw new DbTransformationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,36 @@ static void createAssociationPattern(final MagmaCoreDatabase db) {
userAssociationKindIri = new IRI(TEST_BASE, "userOfSystemKindOfAssociation");
userAssociationKind = ClassServices.createKindOfAssociation(userAssociationKindIri);
userAssociationKind.addValue(RDFS.RDF_TYPE, HQDM.KIND_OF_ASSOCIATION);
userAssociationKind.addValue(HQDM.ENTITY_NAME, "userOfSystemKindOfAssociation");
userAssociationKind.addStringValue(HQDM.ENTITY_NAME, "userOfSystemKindOfAssociation");

final IRI managerKindIri = new IRI(TEST_BASE, "managerKindOfAssociation");
final KindOfAssociation managerKind = ClassServices.createKindOfAssociation(managerKindIri);
managerKind.addValue(RDFS.RDF_TYPE, HQDM.KIND_OF_ASSOCIATION);
managerKind.addValue(HQDM.ENTITY_NAME, "managerKindOfAssociation");
managerKind.addStringValue(HQDM.ENTITY_NAME, "managerKindOfAssociation");

// Create Roles
userRoleIri = new IRI(TEST_BASE, "userRole");
userRole = ClassServices.createRole(userRoleIri);
userRole.addValue(RDFS.RDF_TYPE, HQDM.ROLE);
userRole.addValue(HQDM.ENTITY_NAME, "userRole");
userRole.addStringValue(HQDM.ENTITY_NAME, "userRole");
userRole.addValue(HQDM.PART_OF_BY_CLASS_, userAssociationKindIri);

systemRoleIri = new IRI(TEST_BASE, "systemRole");
systemRole = ClassServices.createRole(systemRoleIri);
systemRole.addValue(RDFS.RDF_TYPE, HQDM.ROLE);
systemRole.addValue(HQDM.ENTITY_NAME, "systemRole");
systemRole.addStringValue(HQDM.ENTITY_NAME, "systemRole");
systemRole.addValue(HQDM.PART_OF_BY_CLASS_, userAssociationKindIri);

final IRI managerRoleIri = new IRI(TEST_BASE, "managerRole");
final Role managerRole = ClassServices.createRole(managerRoleIri);
managerRole.addValue(RDFS.RDF_TYPE, HQDM.ROLE);
managerRole.addValue(HQDM.ENTITY_NAME, "managerRole");
managerRole.addStringValue(HQDM.ENTITY_NAME, "managerRole");
managerRole.addValue(HQDM.PART_OF_BY_CLASS_, managerKindIri);

final IRI workerRoleIri = new IRI(TEST_BASE, "workerRole");
final Role workerRole = ClassServices.createRole(workerRoleIri);
workerRole.addValue(RDFS.RDF_TYPE, HQDM.ROLE);
workerRole.addValue(HQDM.ENTITY_NAME, "workerRole");
workerRole.addStringValue(HQDM.ENTITY_NAME, "workerRole");
workerRole.addValue(HQDM.PART_OF_BY_CLASS_, managerKindIri);

// Create People
Expand Down Expand Up @@ -263,7 +263,7 @@ static void createAssociationPattern(final MagmaCoreDatabase db) {
final IRI patternIri = new IRI(TEST_BASE, UID.uid());
final Pattern pattern = ClassServices.createPattern(patternIri);
pattern.addValue(RDFS.RDF_TYPE, HQDM.PATTERN);
pattern.addValue(HQDM.ENTITY_NAME, "pattern");
pattern.addStringValue(HQDM.ENTITY_NAME, "pattern");

final IRI repByPatternIri = new IRI(TEST_BASE, UID.uid());
final RepresentationByPattern repByPattern = ClassServices
Expand All @@ -284,7 +284,7 @@ static void createAssociationPattern(final MagmaCoreDatabase db) {
sign1.addValue(RDFS.RDF_TYPE, HQDM.SIGN);
sign1.addValue(HQDM.PART_OF_POSSIBLE_WORLD, possibleWorldIri);
sign1.addValue(HQDM.MEMBER_OF_, patternIri);
sign1.addValue(HQDM.VALUE_, "sign1Value");
sign1.addStringValue(HQDM.VALUE_, "sign1Value");

final IRI stateOfSign1Iri = new IRI(TEST_BASE, UID.uid());
final StateOfSign stateOfSign1 = SpatioTemporalExtentServices.createStateOfSign(stateOfSign1Iri);
Expand All @@ -310,7 +310,7 @@ static void createAssociationPattern(final MagmaCoreDatabase db) {
sign2.addValue(RDFS.RDF_TYPE, HQDM.SIGN);
sign2.addValue(HQDM.PART_OF_POSSIBLE_WORLD, possibleWorldIri);
sign2.addValue(HQDM.MEMBER_OF_, patternIri);
sign2.addValue(HQDM.VALUE_, "sign2Value");
sign2.addStringValue(HQDM.VALUE_, "sign2Value");

final IRI stateOfSign2Iri = new IRI(TEST_BASE, UID.uid());
final StateOfSign stateOfSign2 = SpatioTemporalExtentServices.createStateOfSign(stateOfSign2Iri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void testCreateSuccess() {
final IRI iri = new IRI(TEST_IRI);
final Thing newThing = SpatioTemporalExtentServices.createThing(iri);
newThing.addValue(RDFS.RDF_TYPE, HQDM.PERSON);
newThing.addValue(HQDM.ENTITY_NAME, TEST_ENTITY_NAME);
newThing.addStringValue(HQDM.ENTITY_NAME, TEST_ENTITY_NAME);

// Convert the new Thing to a DbTransformation and use it to persist the Thing.
final DbTransformation transformation = svc.createDbTransformation(List.of(newThing));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testFindSuccess() {

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI("http://example.com/entity#now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginRead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testFindSuccess() {

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI("http://example.com/entity#now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginRead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testFindParticipantDetailsSuccess() throws MagmaCoreException {

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI("http://example.com/entity#now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginRead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public void test() {
final IRI individualIri = new IRI(SignPatternTestData.TEST_BASE, "individual");
final Individual individual = SpatioTemporalExtentServices.createIndividual(individualIri);

individual.addValue(HQDM.MEMBER_OF, "classOfIndividual");
individual.addValue(HQDM.MEMBER_OF, new IRI(TEST_BASE, "classOfIndividual"));

database.beginWrite();
database.create(individual);
database.commit();

individual.removeValue(HQDM.MEMBER_OF, "classOfIndividual");
individual.removeValue(HQDM.MEMBER_OF, new IRI(TEST_BASE, "classOfIndividual"));
individual.removeValue(RDFS.RDF_TYPE, HQDM.INDIVIDUAL);

database.beginWrite();
Expand Down Expand Up @@ -99,7 +99,7 @@ public void testFindBySignSuccess() throws MagmaCoreException {

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI("http://example.com/entity#now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginRead();
Expand Down Expand Up @@ -152,7 +152,7 @@ public void testFindByPartialSignSuccess() throws MagmaCoreException {

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI(TEST_BASE, "now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginWrite();
Expand Down Expand Up @@ -184,7 +184,7 @@ public void testFindBySignWithNullSignValue() throws MagmaCoreException {

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI("http://example.com/entity#now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginRead();
Expand Down Expand Up @@ -235,8 +235,8 @@ public void testFindByPredicateOnly() {
final Individual individual1 = SpatioTemporalExtentServices.createIndividual(individual1Iri);
final Individual individual2 = SpatioTemporalExtentServices.createIndividual(individual2Iri);

individual1.addValue(HQDM.MEMBER_OF, "classOfIndividual");
individual2.addValue(HQDM.MEMBER_OF_KIND, "kindOfIndividual");
individual1.addValue(HQDM.MEMBER_OF, new IRI(TEST_BASE, "classOfIndividual"));
individual2.addValue(HQDM.MEMBER_OF_KIND, new IRI(TEST_BASE, "kindOfIndividual"));
individual1.addValue(RDFS.RDF_TYPE, HQDM.INDIVIDUAL);
individual2.addValue(RDFS.RDF_TYPE, HQDM.INDIVIDUAL);

Expand Down Expand Up @@ -269,7 +269,9 @@ public void testFindByPredicateOnly() {

// Find individual1 by a String value
svc.runInReadTransaction(mc -> {
final List<Thing> result = mc.findByPredicateIriAndValue(HQDM.MEMBER_OF, "classOfIndividual");
final List<Thing> result = mc.findByPredicateIriAndValue(
HQDM.MEMBER_OF,
new IRI(TEST_BASE, "classOfIndividual"));

assertEquals(1, result.size());
assertTrue(result.contains(individual1));
Expand All @@ -293,7 +295,7 @@ public void testFindByPartialSignAndClassCaseInsensitive() throws MagmaCoreExcep

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI(TEST_BASE, "now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginRead();
Expand Down Expand Up @@ -331,7 +333,7 @@ public void testFindByPartialSignAndClassCaseSensitive() throws MagmaCoreExcepti

// Create the PointInTime we're looking for
final PointInTime now = SpatioTemporalExtentServices.createPointInTime(new IRI(TEST_BASE, "now"));
now.addValue(HQDM.ENTITY_NAME, Instant.now().toString());
now.addStringValue(HQDM.ENTITY_NAME, Instant.now().toString());

// Find the required Things by sign in a transaction.
db.beginRead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,17 @@ static void createSignPattern(final MagmaCoreDatabase db) {
final Sign sign1 = SpatioTemporalExtentServices.createSign(new IRI(TEST_BASE, "sign1"));
sign1.addValue(RDFS.RDF_TYPE, HQDM.SIGN);
sign1.addValue(HQDM.MEMBER_OF_, pattern1Iri);
sign1.addValue(HQDM.VALUE_, "person1");
sign1.addStringValue(HQDM.VALUE_, "person1");

final Sign sign2 = SpatioTemporalExtentServices.createSign(new IRI(TEST_BASE, "sign2"));
sign2.addValue(RDFS.RDF_TYPE, HQDM.SIGN);
sign2.addValue(HQDM.MEMBER_OF_, pattern2Iri);
sign2.addValue(HQDM.VALUE_, "person2");
sign2.addStringValue(HQDM.VALUE_, "person2");

final Sign sign3 = SpatioTemporalExtentServices.createSign(new IRI(TEST_BASE, "sign3"));
sign3.addValue(RDFS.RDF_TYPE, HQDM.SIGN);
sign3.addValue(HQDM.MEMBER_OF_, pattern2Iri);
sign3.addValue(HQDM.VALUE_, "person3");
sign3.addStringValue(HQDM.VALUE_, "person3");

// Create states for the Signs
final StateOfSign stateOfSign1 = SpatioTemporalExtentServices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private static class Context {
*/
ctx.startOfResearch = SpatioTemporalExtentServices
.createPointInTime(randomIri());
ctx.startOfResearch.addValue(HQDM.VALUE_, now);
ctx.startOfResearch.addStringValue(HQDM.VALUE_, now);

/*
* Create the Activity.
Expand Down Expand Up @@ -253,21 +253,21 @@ private static final IRI randomIri() {
* Create a Class of Person.
*/
final ClassOfPerson cop = ClassServices.createClassOfPerson(randomIri());
cop.addValue(HQDM.ENTITY_NAME, RESEARCHERS_CLASS_ENTITY_NAME);
cop.addStringValue(HQDM.ENTITY_NAME, RESEARCHERS_CLASS_ENTITY_NAME);
ctx.magmaCore.create(cop);

/*
* Create a Kind of Activity.
*/
final KindOfActivity koa = ClassServices.createKindOfActivity(randomIri());
koa.addValue(HQDM.ENTITY_NAME, RESEARCH_ACTIVITY_KIND_ENTITY_NAME);
koa.addStringValue(HQDM.ENTITY_NAME, RESEARCH_ACTIVITY_KIND_ENTITY_NAME);
ctx.magmaCore.create(koa);

/*
* Create a Role.
*/
final Role role = ClassServices.createRole(randomIri());
role.addValue(HQDM.ENTITY_NAME, RESEARCHER_ROLE_ENTITY_NAME);
role.addStringValue(HQDM.ENTITY_NAME, RESEARCHER_ROLE_ENTITY_NAME);
ctx.magmaCore.create(role);

return ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class MultiInheritFromDataApp {
public static void main(final String[] args) {

// Create a new type specification.
final List<Pair<Object, Object>> newTypeSpecification = List.of(
final List<Pair<IRI, Object>> newTypeSpecification = List.of(
new Pair<>(RDFS.RDF_TYPE, HQDM.STATE_OF_ORGANIZATION),
new Pair<>(RDFS.RDF_TYPE, HQDM.PARTICIPANT));

Expand Down
Loading

0 comments on commit 440d9aa

Please sign in to comment.