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

[DROOLS-7295] Implement from collect #39

Merged
merged 1 commit into from
Oct 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ DRL_EXISTS : 'exists';
DRL_NOT : 'not';
DRL_IN : 'in';
DRL_FROM : 'from';
DRL_COLLECT : 'collect';
DRL_MATCHES : 'matches';
DRL_MEMBEROF : 'memberOf';
DRL_ACCUMULATE : 'accumulate' | 'acc';
Expand Down
17 changes: 11 additions & 6 deletions drools-parser/src/main/antlr4/org/drools/parser/DRLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ lhsExpression : LPAREN lhsExpression RPAREN #lhsExpressionEnclosed
| lhsExpression (DRL_OR lhsExpression)+ #lhsOr
;

// and is accepted for accumulate
lhsAndForAccumulate : lhsUnary (DRL_AND lhsUnary)*
| LPAREN DRL_AND lhsUnary+ RPAREN
;
// lhsAnd is used as a label in lhsExpression rule. But some other rules also use it.
lhsAndDef : lhsUnary (DRL_AND lhsUnary)*
| LPAREN DRL_AND lhsUnary+ RPAREN
;

/*
lhsUnary : ( lhsExists namedConsequence?
Expand Down Expand Up @@ -298,6 +298,7 @@ patternFilter : label IDENTIFIER LPAREN expressionList RPAREN ;
| fromExpression )
*/
patternSource : fromAccumulate
| fromCollect
| fromEntryPoint
| fromExpression
;
Expand All @@ -312,7 +313,7 @@ fromAccumulate := ACCUMULATE LEFT_PAREN lhsAnd (COMMA|SEMICOLON)
| accumulateFunction
) RIGHT_PAREN
*/
fromAccumulate : DRL_ACCUMULATE LPAREN lhsAndForAccumulate (COMMA|SEMI)
fromAccumulate : DRL_ACCUMULATE LPAREN lhsAndDef (COMMA|SEMI)
( DRL_INIT LPAREN initBlockStatements=blockStatements RPAREN COMMA DRL_ACTION LPAREN actionBlockStatements=blockStatements RPAREN COMMA ( DRL_REVERSE LPAREN reverseBlockStatements=blockStatements RPAREN COMMA)? DRL_RESULT LPAREN expression RPAREN
| accumulateFunction
)
Expand All @@ -326,6 +327,10 @@ accumulateFunction := label? ID parameters
*/
accumulateFunction : label? IDENTIFIER LPAREN drlExpression RPAREN;

// fromCollect := COLLECT LEFT_PAREN lhsPatternBind RIGHT_PAREN

fromCollect : DRL_COLLECT LPAREN lhsPatternBind RPAREN ;

fromEntryPoint : DRL_ENTRY_POINT stringId ;

/*
Expand Down Expand Up @@ -363,7 +368,7 @@ lhsForall : DRL_FORALL LPAREN lhsPatternBind+ RPAREN ;
* RIGHT_PAREN SEMICOLON?
*/

lhsAccumulate : DRL_ACCUMULATE LPAREN lhsAndForAccumulate (COMMA|SEMI)
lhsAccumulate : DRL_ACCUMULATE LPAREN lhsAndDef (COMMA|SEMI)
accumulateFunction (COMMA accumulateFunction)*
(SEMI constraints)?
RPAREN (SEMI)?
Expand Down
14 changes: 11 additions & 3 deletions drools-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.drools.drl.ast.descr.AttributeDescr;
import org.drools.drl.ast.descr.BaseDescr;
import org.drools.drl.ast.descr.BehaviorDescr;
import org.drools.drl.ast.descr.CollectDescr;
import org.drools.drl.ast.descr.EntryPointDescr;
import org.drools.drl.ast.descr.EvalDescr;
import org.drools.drl.ast.descr.ExistsDescr;
Expand Down Expand Up @@ -314,7 +315,7 @@ public ForallDescr visitLhsForall(DRLParser.LhsForallContext ctx) {
@Override
public PatternDescr visitLhsAccumulate(DRLParser.LhsAccumulateContext ctx) {
AccumulateDescr accumulateDescr = new AccumulateDescr();
accumulateDescr.setInput(visitLhsAndForAccumulate(ctx.lhsAndForAccumulate()));
accumulateDescr.setInput(visitLhsAndDef(ctx.lhsAndDef()));

// accumulate function
for (DRLParser.AccumulateFunctionContext accumulateFunctionContext : ctx.accumulateFunction()) {
Expand Down Expand Up @@ -346,10 +347,17 @@ public FromDescr visitFromExpression(DRLParser.FromExpressionContext ctx) {
return fromDescr;
}

@Override
public CollectDescr visitFromCollect(DRLParser.FromCollectContext ctx) {
CollectDescr collectDescr = new CollectDescr();
collectDescr.setInputPattern((PatternDescr) visitLhsPatternBind(ctx.lhsPatternBind()));
return collectDescr;
}

@Override
public AccumulateDescr visitFromAccumulate(DRLParser.FromAccumulateContext ctx) {
AccumulateDescr accumulateDescr = new AccumulateDescr();
accumulateDescr.setInput(visitLhsAndForAccumulate(ctx.lhsAndForAccumulate()));
accumulateDescr.setInput(visitLhsAndDef(ctx.lhsAndDef()));
if (ctx.DRL_INIT() != null) {
// inline custom accumulate
accumulateDescr.setInitCode(getTextPreservingWhitespace(ctx.initBlockStatements));
Expand Down Expand Up @@ -469,7 +477,7 @@ private AndDescr createAndDescr(List<BaseDescr> descrList) {
}

@Override
public BaseDescr visitLhsAndForAccumulate(DRLParser.LhsAndForAccumulateContext ctx) {
public BaseDescr visitLhsAndDef(DRLParser.LhsAndDefContext ctx) {
return createAndDescr(visitDescrChildren(ctx));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2047,7 +2047,6 @@ public void parse_AccumulateWithBindings() throws Exception {
assertThat(pattern.getObjectType()).isEqualTo("Person");
}

@Disabled("Priority : High | Implement from collect")
@Test
public void parse_Collect() throws Exception {
final PackageDescr pkg = parseAndGetPackageDescrFromFile(
Expand Down Expand Up @@ -2310,7 +2309,6 @@ public void parse_Restrictions() throws Exception {
assertThat(fieldConstr.getExpression()).isEqualTo("bar > 1 || == 1");
}

@Disabled("Priority : High | Implement semicolon delimiter | Implement from collect")
@Test
public void parse_Semicolon() throws Exception {
final PackageDescr pkg = parseAndGetPackageDescrFromFile(
Expand Down Expand Up @@ -2390,7 +2388,6 @@ public void parse_AccumulateExternalFunction() throws Exception {
assertThat(pattern.getObjectType()).isEqualTo("Person");
}

@Disabled("Priority : High | Implement from collect")
@Test
public void parse_CollectWithNestedFrom() throws Exception {
final PackageDescr pkg = parseAndGetPackageDescrFromFile(
Expand All @@ -2412,7 +2409,6 @@ public void parse_CollectWithNestedFrom() throws Exception {
assertThat(people.getObjectType()).isEqualTo("People");
}

@Disabled("Priority : High | Implement accumulate and Implement from collect")
@Test
public void parse_AccumulateWithNestedFrom() throws Exception {
final PackageDescr pkg = parseAndGetPackageDescrFromFile(
Expand Down
Loading