Skip to content

Commit

Permalink
Extend Precedence Rules Specification to Support Compound Filters and…
Browse files Browse the repository at this point in the history
… Enums (#2439)

* Mastery - Allow multiPah filter in precedence rules

* Mastery - Extend precedence Rules to define an enum path
  • Loading branch information
tobynwuba authored Nov 10, 2023
1 parent 2152c27 commit 0c8adb8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pathExtension: subPath filter?
;
subPath: '.' validString
;
filter: BRACE_OPEN '$' '.' combinedExpression BRACE_CLOSE
filter: BRACE_OPEN combinedExpression BRACE_CLOSE
;
predicate: PREDICATE COLON
lambdaFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enumeration;
import org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type;
import org.finos.legend.pure.m4.coreinstance.CoreInstance;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -303,7 +305,7 @@ private Root_meta_pure_mastery_metamodel_precedence_PropertyPath visitPath(Prope
Property<?,?> property = (Property<?,?>) context.resolveProperty(determineFullPath(parentClass), propertyPath.property);
Type propertyClass = property._genericType()._rawType();
String propertyClassName;
if (propertyClass instanceof Class)
if ((propertyClass instanceof Class) || (propertyClass instanceof Enumeration))
{
propertyClassName = determineFullPath(propertyClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public class MasteryParseTreeWalker


private static final String SIMPLE_PRECEDENCE_LAMBDA = "{input: %s[1]| true}";
private static final String PRECEDENCE_LAMBDA_WITH_FILTER = "{input: %s[1]| $input.%s}";
private static final String INPUT = "\\$input";
private static final String DOLLAR_SIGN = "\\$";
private static final String PRECEDENCE_LAMBDA_WITH_FILTER = "{input: %s[1]| %s}";
private static final String DATA_PROVIDER_STRING = "DataProvider";

public MasteryParseTreeWalker(ParseTreeWalkerSourceInformation walkerSourceInformation,
Expand Down Expand Up @@ -373,8 +375,9 @@ private PropertyPath visitPathExtension(MasteryParserGrammar.PathExtensionContex

private Lambda visitLambdaWithFilter(String propertyName, MasteryParserGrammar.CombinedExpressionContext ctx)
{
String inputFilter = ctx.getText().replaceAll(DOLLAR_SIGN, INPUT);
return domainParser.parseLambda(
format(PRECEDENCE_LAMBDA_WITH_FILTER, propertyName, ctx.getText()),
format(PRECEDENCE_LAMBDA_WITH_FILTER, propertyName, inputFilter),
"", 0, 0, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class HelperMasteryGrammarComposer
{

private static final String PRECEDENCE_LAMBDA_WITH_FILTER_PREFIX = "\\{?input: .*\\[1]\\|\\$input\\.";
private static final String INPUT = "input";
private static final String BRACKETS = "\\(|\\)";
private static final String PRECEDENCE_LAMBDA_WITH_FILTER_SUFFIX = ".*";

private HelperMasteryGrammarComposer()
Expand Down Expand Up @@ -402,10 +404,10 @@ private String visitPath(Lambda masterRecordFilter, List<PropertyPath> propertyP
private String visitLambda(Lambda lambda)
{
StringBuilder builder = new StringBuilder();
String lambdaStr = lambda.accept(DEPRECATED_PureGrammarComposerCore.Builder.newInstance(context).build());
String lambdaStr = lambda.accept(DEPRECATED_PureGrammarComposerCore.Builder.newInstance(context).build()).replaceAll(BRACKETS, "");
if (lambdaStr.matches(PRECEDENCE_LAMBDA_WITH_FILTER_PREFIX + PRECEDENCE_LAMBDA_WITH_FILTER_SUFFIX))
{
String filterPath = lambdaStr.replaceAll(PRECEDENCE_LAMBDA_WITH_FILTER_PREFIX, "");
String filterPath = lambdaStr.replaceAll(PRECEDENCE_LAMBDA_WITH_FILTER_PREFIX, "").replace(INPUT, "");
builder.append("{$.").append(filterPath).append("}");
}
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ public class TestMasteryCompilationFromGrammar extends TestCompilationFromGramma
"{\n" +
" widgetId: String[0..1];\n" +
" trigger: String[0..1];\n" +
" modelType: org::dataeng::ModelType[0..1];\n" +
" runProfile: org::dataeng::Medium[0..1];\n" +
" identifiers: org::dataeng::MilestonedIdentifier[*];\n" +
"}\n\n" +
"Enum org::dataeng::ModelType\n" +
"{\n" +
" modelA,\n" +
" modelB\n" +
"}\n\n" +
"Class org::dataeng::Medium\n" +
"{\n" +
" authorization: String[0..1];\n" +
Expand Down Expand Up @@ -134,7 +140,7 @@ public class TestMasteryCompilationFromGrammar extends TestCompilationFromGramma
" }\n" +
" precedenceRules: [\n" +
" DeleteRule: {\n" +
" path: org::dataeng::Widget.identifiers;\n" +
" path: org::dataeng::Widget.modelType;\n" +
" ruleScope: [\n" +
" RecordSourceScope {widget-rest-source}\n" +
" ];\n" +
Expand Down Expand Up @@ -168,7 +174,7 @@ public class TestMasteryCompilationFromGrammar extends TestCompilationFromGramma
" path: org::dataeng::Widget.runProfile.authorization;\n" +
" },\n" +
" SourcePrecedenceRule: {\n" +
" path: org::dataeng::Widget.identifiers{$.identifier == 'XLON'};\n" +
" path: org::dataeng::Widget.identifiers{$.identifier == 'XLON' || $.identifier == 'LSE'};\n" +
" action: Overwrite;\n" +
" ruleScope: [\n" +
" RecordSourceScope {widget-file-source-sftp, precedence: 1},\n" +
Expand Down Expand Up @@ -575,7 +581,7 @@ public void testMasteryFullModel()

Root_meta_pure_mastery_metamodel_precedence_PropertyPath propertyPath = paths.get(0);
//path property
assertEquals("identifiers", propertyPath._property()._name());
assertEquals("modelType", propertyPath._property()._name());
assertEquals("Widget", propertyPath._property()._owner()._name());
//path filter
assertEquals("true", getSimpleLambdaValue(propertyPath._filter()));
Expand Down Expand Up @@ -670,10 +676,25 @@ else if (i == 6)
Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl complexLambda = getComplexLambda(firstPropertyPath._filter());
List<? extends ValueSpecification> lambdaParameters = complexLambda._parametersValues().toList();

assertEquals("MilestonedIdentifier", getFunctionProperty(lambdaParameters.get(0))._owner()._name());
assertEquals("identifier", getFunctionProperty(lambdaParameters.get(0))._name());
assertEquals("equal", complexLambda._functionName());
assertEquals("XLON", getInstanceValue(lambdaParameters.get(1)));
assertEquals("or", complexLambda._functionName());

// first Part of filter
Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl firstFilter = (Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl) lambdaParameters.get(0);
List<? extends ValueSpecification> firstFilterLambdaParameters = firstFilter._parametersValues().toList();

assertEquals("MilestonedIdentifier", getFunctionProperty(firstFilterLambdaParameters.get(0))._owner()._name());
assertEquals("identifier", getFunctionProperty(firstFilterLambdaParameters.get(0))._name());
assertEquals("equal", firstFilter._functionName());
assertEquals("XLON", getInstanceValue(firstFilterLambdaParameters.get(1)));

// second Part of filter
Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl secondFilter = (Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl) lambdaParameters.get(1);
List<? extends ValueSpecification> secondFilterLambdaParameters = secondFilter._parametersValues().toList();

assertEquals("MilestonedIdentifier", getFunctionProperty(secondFilterLambdaParameters.get(0))._owner()._name());
assertEquals("identifier", getFunctionProperty(secondFilterLambdaParameters.get(0))._name());
assertEquals("equal", secondFilter._functionName());
assertEquals("LSE", getInstanceValue(secondFilterLambdaParameters.get(1)));

//masterRecordFilter
assertEquals("true", getSimpleLambdaValue(source._masterRecordFilter()));
Expand Down Expand Up @@ -702,10 +723,25 @@ else if (i == 7)
Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl complexLambda = getComplexLambda(firstPropertyPath._filter());
List<? extends ValueSpecification> lambdaParameters = complexLambda._parametersValues().toList();

assertEquals("MilestonedIdentifier", getFunctionProperty(lambdaParameters.get(0))._owner()._name());
assertEquals("identifier", getFunctionProperty(lambdaParameters.get(0))._name());
assertEquals("equal", complexLambda._functionName());
assertEquals("XLON", getInstanceValue(lambdaParameters.get(1)));
assertEquals("or", complexLambda._functionName());

// first Part of filter
Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl firstFilter = (Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl) lambdaParameters.get(0);
List<? extends ValueSpecification> firstFilterLambdaParameters = firstFilter._parametersValues().toList();

assertEquals("MilestonedIdentifier", getFunctionProperty(firstFilterLambdaParameters.get(0))._owner()._name());
assertEquals("identifier", getFunctionProperty(firstFilterLambdaParameters.get(0))._name());
assertEquals("equal", firstFilter._functionName());
assertEquals("XLON", getInstanceValue(firstFilterLambdaParameters.get(1)));

// second Part of filter
Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl secondFilter = (Root_meta_pure_metamodel_valuespecification_SimpleFunctionExpression_Impl) lambdaParameters.get(1);
List<? extends ValueSpecification> secondFilterLambdaParameters = secondFilter._parametersValues().toList();

assertEquals("MilestonedIdentifier", getFunctionProperty(secondFilterLambdaParameters.get(0))._owner()._name());
assertEquals("identifier", getFunctionProperty(secondFilterLambdaParameters.get(0))._name());
assertEquals("equal", secondFilter._functionName());
assertEquals("LSE", getInstanceValue(secondFilterLambdaParameters.get(1)));

//masterRecordFilter
assertEquals("true", getSimpleLambdaValue(source._masterRecordFilter()));
Expand Down

0 comments on commit 0c8adb8

Please sign in to comment.