Skip to content

Commit

Permalink
[DROOLS-7304] Implement temporal operators (#38)
Browse files Browse the repository at this point in the history
- Also covers [DROOLS-7303] Implement sliding window
  • Loading branch information
tkobayas committed Oct 11, 2024
1 parent b4e11b3 commit 84d44b3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,25 @@ DRL_RESULT : 'result';
DRL_ENTRY_POINT : 'entry-point';
DRL_EVAL : 'eval';
DRL_FORALL : 'forall';

DRL_OVER : 'over';

// temporal operators
DRL_AFTER : 'after';
DRL_BEFORE : 'before';
DRL_COINCIDES : 'coincides';
DRL_DURING : 'during';
DRL_INCLUDES : 'includes';
DRL_FINISHES : 'finishes';
DRL_FINISHED_BY : 'finishedby';
DRL_MEETS : 'meets';
DRL_MET_BY : 'metby';
DRL_OVERLAPS : 'overlaps';
DRL_OVERLAPPED_BY : 'overlappedby';
DRL_STARTS : 'starts';
DRL_STARTED_BY : 'startedby';


// attributes
DRL_SALIENCE : 'salience';
DRL_ENABLED : 'enabled';
DRL_NO_LOOP : 'no-loop';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ lhsPattern : xpathPrimary (OVER patternFilter)? |
( QUESTION? qualifiedIdentifier LPAREN positionalConstraints? constraints? RPAREN (OVER patternFilter)? (FROM patternSource)? ) ;
*/

lhsPattern : QUESTION? objectType=drlQualifiedName LPAREN positionalConstraints? constraints? RPAREN (DRL_FROM patternSource)? ;
lhsPattern : QUESTION? objectType=drlQualifiedName LPAREN positionalConstraints? constraints? RPAREN (DRL_OVER patternFilter)? (DRL_FROM patternSource)? ;
positionalConstraints : constraint (COMMA constraint)* SEMI ;
constraints : constraint (COMMA constraint)* ;
constraint : ( nestedConstraint | conditionalOrExpression ) ;
Expand All @@ -105,6 +105,7 @@ relationalOperator
| GE
| GT
| LT
| temporalOperator
;

/* function := FUNCTION type? ID parameters(typed) chunk_{_} */
Expand Down Expand Up @@ -204,6 +205,7 @@ drlExpression
| drlExpression bop=(ADD|SUB) drlExpression
| drlExpression (LT LT | GT GT GT | GT GT) drlExpression
| drlExpression bop=(LE | GE | GT | LT) drlExpression
| drlExpression temporalOperator drlExpression
| drlExpression bop=INSTANCEOF (typeType | pattern)
| drlExpression bop=DRL_MATCHES drlExpression
| drlExpression DRL_NOT? DRL_MEMBEROF drlExpression
Expand All @@ -226,6 +228,10 @@ drlExpression
| classType COLONCOLON typeArguments? NEW
;

temporalOperator : DRL_NOT? bop=(DRL_AFTER | DRL_BEFORE | DRL_COINCIDES | DRL_DURING | DRL_INCLUDES | DRL_FINISHES | DRL_FINISHED_BY | DRL_MEETS | DRL_MET_BY | DRL_OVERLAPS | DRL_OVERLAPPED_BY | DRL_STARTS | DRL_STARTED_BY) timeAmount? ;

timeAmount : LBRACK (TIME_INTERVAL | DECIMAL_LITERAL | MUL | SUB MUL) (COMMA (TIME_INTERVAL | DECIMAL_LITERAL | MUL | SUB MUL))* RBRACK ;

/* extending JavaParser primary */
drlPrimary
: LPAREN drlExpression RPAREN
Expand Down Expand Up @@ -272,6 +278,12 @@ mapEntry
: drlExpression COLON drlExpression
;

/*
patternFilter := OVER filterDef
filterDef := label ID LEFT_PAREN parameters RIGHT_PAREN
*/
patternFilter : label IDENTIFIER LPAREN expressionList RPAREN ;

/*
patternSource := FROM
( fromAccumulate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.TokenStream;
Expand All @@ -14,6 +15,7 @@
import org.drools.drl.ast.descr.AnnotationDescr;
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.EntryPointDescr;
import org.drools.drl.ast.descr.EvalDescr;
import org.drools.drl.ast.descr.ExistsDescr;
Expand Down Expand Up @@ -42,7 +44,7 @@

public class DRLVisitorImpl extends DRLParserBaseVisitor<Object> {

private TokenStream tokenStream;
private final TokenStream tokenStream;

public DRLVisitorImpl(TokenStream tokenStream) {
this.tokenStream = tokenStream;
Expand Down Expand Up @@ -252,6 +254,9 @@ private OrDescr getOrDescrWithMultiplePatternDescr(DRLParser.LhsPatternBindConte
@Override
public PatternDescr visitLhsPattern(DRLParser.LhsPatternContext ctx) {
PatternDescr patternDescr = new PatternDescr(ctx.objectType.getText());
if (ctx.patternFilter() != null) {
patternDescr.addBehavior(visitPatternFilter(ctx.patternFilter()));
}
if (ctx.patternSource() != null) {
PatternSourceDescr patternSourceDescr = (PatternSourceDescr) visitPatternSource(ctx.patternSource());
patternSourceDescr.setResource(patternDescr.getResource());
Expand Down Expand Up @@ -291,6 +296,17 @@ public PatternDescr visitLhsAccumulate(DRLParser.LhsAccumulateContext ctx) {
return patternDescr;
}

@Override
public BehaviorDescr visitPatternFilter(DRLParser.PatternFilterContext ctx) {
BehaviorDescr behaviorDescr = new BehaviorDescr();
behaviorDescr.setType(ctx.label().IDENTIFIER().getText());
behaviorDescr.setSubType(ctx.IDENTIFIER().getText());
List<DRLParser.DrlExpressionContext> drlExpressionContexts = ctx.expressionList().drlExpression();
List<String> parameters = drlExpressionContexts.stream().map(ParserStringUtils::getTextPreservingWhitespace).collect(Collectors.toList());
behaviorDescr.setParameters(parameters);
return behaviorDescr;
}

@Override
public FromDescr visitFromExpression(DRLParser.FromExpressionContext ctx) {
FromDescr fromDescr = new FromDescr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,6 @@ public void parse_AccumulateMultiPattern() throws Exception {
assertThat(cheese.getObjectType()).isEqualTo("Cheese");
}

@Disabled("Priority : High | Implement temporal operators")
@Test
public void parse_PluggableOperators() throws Exception {

Expand Down Expand Up @@ -2934,13 +2933,11 @@ public void parse_EntryPoint2() throws Exception {
assertThat(entry.getEntryId()).isEqualTo("StreamA");
}

@Disabled("Priority : High | Implement sliding window")
@Test
public void parse_SlidingWindow() throws Exception {
final String text = "rule X when StockTick( symbol==\"ACME\") over window:length(10) then end";

PackageDescr pkg = parser.parse(
text );
PackageDescr pkg = parser.parse( text );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

RuleDescr rule = pkg.getRules().get( 0 );
Expand Down Expand Up @@ -3219,7 +3216,6 @@ public void parse_BindingCompositeWithMethods() throws Exception {
assertThat(((ExprConstraintDescr) constraints.get(0)).getExpression()).isEqualTo("$name : name.toUpperCase() == \"Bob\" || $loc : location[0].city == \"Montreal\"");
}

@Disabled("Priority : High | Implement temporal operators")
@Test
public void parse_PluggableOperators2() throws Exception {
final String text = "rule \"tt\"\n" +
Expand Down Expand Up @@ -3253,7 +3249,6 @@ public void parse_InlineEval() throws Exception {

}

@Disabled("Priority : High | Implement temporal operators")
@Test
public void parse_InfinityLiteral() throws Exception {
final String text = "rule \"infinity\"\n" +
Expand Down

0 comments on commit 84d44b3

Please sign in to comment.