Skip to content

Commit

Permalink
[Bug #178] Fix an issue with commutativity of and operator when using…
Browse files Browse the repository at this point in the history
… selection by identifier in SOQL.
  • Loading branch information
ledsoft committed Jun 22, 2023
1 parent f25c67d commit eb459e4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public void exitComparisonExpression(SoqlParser.ComparisonExpressionContext ctx)
if (attrPointer.isProjected()) {
this.rootVariable = SoqlUtils.soqlVariableToSparqlVariable(whereClauseValue.getText());
}
if (attributes.size() > 1) {
if (attributes.size() > 1 && !attrPointer.getFirstNode().getChild().hasChild()) {
final String varName = whereClauseValue.getText();
attrPointer.getFirstNode().getChild().setValue(
varName.charAt(0) == SoqlConstants.VARIABLE_PREFIX ? varName.substring(1) : varName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public static void initOWLClassCMocks(EntityTypeImpl<OWLClassC> etMock,
* Initializes the specified mock objects to return reasonable values.
*/
public static void initOWLClassDMocks(EntityTypeImpl<OWLClassD> etMock, SingularAttributeImpl clsAMock,
EntityTypeImpl<OWLClassA> etA,
Identifier idMock)
throws NoSuchFieldException, SecurityException {
when(etMock.getJavaType()).thenReturn(OWLClassD.class);
Expand All @@ -253,8 +254,7 @@ public static void initOWLClassDMocks(EntityTypeImpl<OWLClassD> etMock, Singular
.thenReturn(new HashSet<>(Arrays.<FieldSpecification<? super OWLClassD, ?>>asList(clsAMock, idMock)));
when(clsAMock.getJavaField()).thenReturn(OWLClassD.getOwlClassAField());
when(clsAMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.OBJECT);
final String clsAIri = OWLClassD.getOwlClassAField().getAnnotation(OWLObjectProperty.class)
.iri();
final String clsAIri = OWLClassD.getOwlClassAField().getAnnotation(OWLObjectProperty.class).iri();
when(clsAMock.getIRI()).thenReturn(IRI.create(clsAIri));
when(clsAMock.getJavaType()).thenReturn(OWLClassA.class);
when(clsAMock.getName()).thenReturn(OWLClassD.getOwlClassAField().getName());
Expand All @@ -263,6 +263,7 @@ public static void initOWLClassDMocks(EntityTypeImpl<OWLClassD> etMock, Singular
when(clsAMock.getDeclaringType()).thenReturn(etMock);
when(clsAMock.isAssociation()).thenReturn(true);
when(clsAMock.getBindableJavaType()).thenReturn(OWLClassA.class);
when(clsAMock.getType()).thenReturn(etA);
when(etMock.getFieldSpecification(clsAMock.getName())).thenReturn(clsAMock);
when(etMock.getIdentifier()).thenReturn(idMock);
when(idMock.getJavaField()).thenReturn(OWLClassD.getUriField());
Expand Down Expand Up @@ -480,7 +481,7 @@ public static void initOWLClassLMocks(EntityTypeImpl<OWLClassL> etMock, ListAttr
Arrays.<Attribute<OWLClassL, ?>>asList(refListMock, simpleListMock, setMock, singleAMock)));
when(etMock.getFieldSpecifications()).thenReturn(new HashSet<>(
Arrays.<FieldSpecification<? super OWLClassL, ?>>asList(refListMock, simpleListMock, setMock,
simpleListMock, idMock)));
simpleListMock, idMock)));

when(refListMock.getJavaField()).thenReturn(OWLClassL.getReferencedListField());
when(refListMock.getName()).thenReturn(OWLClassL.getReferencedListField().getName());
Expand Down Expand Up @@ -569,16 +570,16 @@ public static void initOWLClassMMock(EntityTypeImpl<OWLClassM> etMock, AbstractA
when(etMock.getFieldSpecification(idMock.getName())).thenReturn(idMock);
when(etMock.getAttributes()).thenReturn(
new HashSet<>(Arrays.<Attribute<? super OWLClassM, ?>>asList(booleanAtt, intAtt, longAtt, doubleAtt,
dateAtt, enumAtt, ordinalEnumAtt,
intSetAtt, lexicalFormAtt,
simpleLiteralAtt, explicitDatatypeAtt,
mObjectOneOfEnumAttribute)));
dateAtt, enumAtt, ordinalEnumAtt,
intSetAtt, lexicalFormAtt,
simpleLiteralAtt, explicitDatatypeAtt,
mObjectOneOfEnumAttribute)));
when(etMock.getFieldSpecifications()).thenReturn(new HashSet<>(
Arrays.<FieldSpecification<? super OWLClassM, ?>>asList(booleanAtt, intAtt, longAtt, doubleAtt, dateAtt,
enumAtt, ordinalEnumAtt, intSetAtt,
lexicalFormAtt, simpleLiteralAtt,
explicitDatatypeAtt, mObjectOneOfEnumAttribute,
idMock)));
enumAtt, ordinalEnumAtt, intSetAtt,
lexicalFormAtt, simpleLiteralAtt,
explicitDatatypeAtt, mObjectOneOfEnumAttribute,
idMock)));

when(booleanAtt.getJavaField()).thenReturn(OWLClassM.getBooleanAttributeField());
when(booleanAtt.getName()).thenReturn(OWLClassM.getBooleanAttributeField().getName());
Expand Down Expand Up @@ -781,10 +782,10 @@ public static void initOWLClassNMock(EntityTypeImpl<OWLClassN> et, SingularAttri
when(idN.getJavaField()).thenReturn(OWLClassN.getUriField());
when(et.getFieldSpecifications()).thenReturn(new HashSet<>(
Arrays.<FieldSpecification<? super OWLClassN, ?>>asList(annotationAtt, annotationUriAtt, stringAtt,
pluralAnnotationAtt, props, idN)));
pluralAnnotationAtt, props, idN)));
when(et.getAttributes()).thenReturn(new HashSet<>(
Arrays.<Attribute<? super OWLClassN, ?>>asList(annotationAtt, annotationUriAtt, stringAtt,
pluralAnnotationAtt)));
pluralAnnotationAtt)));

when(annotationAtt.getJavaField()).thenReturn(OWLClassN.getAnnotationPropertyField());
when(annotationAtt.getJavaType()).thenReturn(OWLClassN.getAnnotationPropertyField().getType());
Expand Down Expand Up @@ -885,8 +886,8 @@ public static void initOWLClassPMock(EntityTypeImpl<OWLClassP> et, TypesSpecific
.thenReturn(
new HashSet<>(
Arrays.<FieldSpecification<? super OWLClassP, ?>>asList(uriAtt, urlsAtt, simpleListAtt,
refListAtt, props, types,
idP)));
refListAtt, props, types,
idP)));
when(et.getAttributes())
.thenReturn(new HashSet<>(
Arrays.<Attribute<? super OWLClassP, ?>>asList(uriAtt, urlsAtt, simpleListAtt, refListAtt)));
Expand Down Expand Up @@ -981,12 +982,12 @@ public static void initOwlClassQMock(EntityTypeImpl<OWLClassQ> et,
.thenReturn(
new HashSet<>(
Arrays.<FieldSpecification<? super OWLClassQ, ?>>asList(qStringAtt, qParentStringAtt,
qLabelAtt, qOwlClassAAtt,
idQ)));
qLabelAtt, qOwlClassAAtt,
idQ)));
when(et.getAttributes())
.thenReturn(new HashSet<>(
Arrays.<Attribute<? super OWLClassQ, ?>>asList(qStringAtt, qParentStringAtt, qLabelAtt,
qOwlClassAAtt)));
qOwlClassAAtt)));

when(qStringAtt.getJavaField()).thenReturn(OWLClassQ.getStringAttributeField());
when(qStringAtt.getJavaType()).thenReturn(OWLClassQ.getStringAttributeField().getType());
Expand Down Expand Up @@ -1339,7 +1340,7 @@ static void initOWLClassWithQueryAttrMocks(EntityTypeImpl<OWLClassWithQueryAttr>
new HashSet<>(Arrays.<Attribute<? super OWLClassWithQueryAttr, ?>>asList(strAttMock, entityAttMock)));
when(etMock.getQueryAttributes()).thenReturn(
new HashSet<>(Arrays.<QueryAttribute<? super OWLClassWithQueryAttr, ?>>asList(strQueryAttMock,
entityQueryAttMock)));
entityQueryAttMock)));
when(etMock.getFieldSpecifications()).thenReturn(
new HashSet<>(Arrays.<FieldSpecification<? super OWLClassWithQueryAttr, ?>>asList(
strAttMock, strQueryAttMock, entityAttMock, entityQueryAttMock, idMock)));
Expand Down Expand Up @@ -1436,10 +1437,10 @@ public static void initPersonMocks(EntityTypeImpl<Person> etMock, AbstractAttrib
when(etMock.getFieldSpecification(idMock.getName())).thenReturn(idMock);
when(etMock.getAttributes()).thenReturn(
new HashSet<>(Arrays.<Attribute<? super Person, ?>>asList(usernameAttMock, genderAttMock, ageAttMock,
phoneAttMock)));
phoneAttMock)));
when(etMock.getFieldSpecifications()).thenReturn(new HashSet<>(
Arrays.<FieldSpecification<? super Person, ?>>asList(usernameAttMock, genderAttMock, ageAttMock,
phoneAttMock, idMock)));
phoneAttMock, idMock)));
when(usernameAttMock.getJavaField()).thenReturn(Person.class.getDeclaredField("username"));
when(usernameAttMock.getIRI()).thenReturn(IRI.create(Vocabulary.p_p_username));
when(usernameAttMock.getPersistentAttributeType()).thenReturn(Attribute.PersistentAttributeType.DATA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public MetamodelMocks() throws Exception {
MetamodelClassInitializer.initMetamodelClassOWLClassB(bStringAtt, bProperties, idB);
MetamodelFactory.initOWLClassCMocks(etC, cSimpleList, cReferencedList, idC);
MetamodelClassInitializer.initMetamodelClassOWLClassC(cSimpleList, cReferencedList, idC);
MetamodelFactory.initOWLClassDMocks(etD, dOwlClassAAtt, idD);
MetamodelFactory.initOWLClassDMocks(etD, dOwlClassAAtt, etA, idD);
MetamodelClassInitializer.initMetamodelClassOWLClassD(dOwlClassAAtt, idD);
MetamodelFactory.initOWLClassEMocks(etE, eStringAtt, idE);
MetamodelFactory.initOWLClassFMocks(etF, fSetAtt, fStringAtt, idF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ void testParseQueryWithNumericFunction(String soqlFunction, String sparqlFunctio
parseAndAssertEquality(soql, expectedSparql);
}

private static String strUri(String c_OwlClassM) {
return IdentifierTransformer.stringifyIri(c_OwlClassM);
private static String strUri(String uri) {
return IdentifierTransformer.stringifyIri(uri);
}

@Test
Expand All @@ -723,4 +723,18 @@ void parseQueryWithIdentifierAndAttributeIsCommutative() {
parseAndAssertEquality(soqlIdFirst, expectedSparql);
parseAndAssertEquality(soqlIdSecond, expectedSparql);
}

/**
* Bug #178
*/
@Test
void parseQueryWithRelatedAttributeAndIdentifierIsCommutative() {
final String soqlIdFirst = "SELECT d FROM OWLClassD d WHERE d.uri = :uri AND d.owlClassA.stringAttribute = :stringAtt";
final String soqlIdSecond = "SELECT d FROM OWLClassD d WHERE d.owlClassA.stringAttribute = :stringAtt AND d.uri = :uri";
final String expectedSparql = "SELECT ?uri WHERE { ?uri a " + strUri(Vocabulary.c_OwlClassD) + " . " +
"?uri " + strUri(Vocabulary.p_h_hasA) + " ?owlClassA . " +
"?owlClassA " + strUri(Vocabulary.p_a_stringAttribute) + " ?stringAtt . }";
parseAndAssertEquality(soqlIdFirst, expectedSparql);
parseAndAssertEquality(soqlIdSecond, expectedSparql);
}
}

0 comments on commit eb459e4

Please sign in to comment.