Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Merge branch 'ExpectEngineFixes' into 0.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
lanceleverich committed Jun 23, 2015
2 parents d64d8ca + d88a3ff commit 19e89f2
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@
import org.drools.compiler.lang.api.PackageDescrBuilder;
import org.drools.compiler.lang.api.PatternDescrBuilder;
import org.drools.compiler.lang.api.RuleDescrBuilder;
import org.drools.compiler.lang.descr.AndDescr;
import org.drools.compiler.lang.descr.BaseDescr;
import org.drools.compiler.lang.descr.ConditionalElementDescr;
import org.drools.compiler.lang.descr.ExpectationDescr;
import org.drools.compiler.lang.descr.ExpectationRuleDescr;
import org.drools.compiler.lang.descr.ExprConstraintDescr;
import org.drools.compiler.lang.descr.NamedConsequenceDescr;
import org.drools.compiler.lang.descr.NotDescr;
import org.drools.compiler.lang.descr.OrDescr;
import org.drools.compiler.lang.descr.PatternDescr;
import org.drools.compiler.lang.descr.RuleDescr;
import org.drools.compiler.lang.descr.*;
import org.drools.core.util.StringUtils;

import java.util.*;
Expand Down Expand Up @@ -337,7 +327,9 @@ public void injectMainExpectationRuleWithContext( ExpectationRuleDescr xp, Expec
public void buildFulfillRule( PackageDescrBuilder builder, ExpectationRuleDescr rule, ExpectationDescr expectations ) {

RuleDescr fulfillRule = expectations.getFulfill();
AttributeDescr noloop = new AttributeDescr("no-loop","");
fulfillRule.addAnnotation( "Propagation", "EAGER" );
fulfillRule.addAttribute(noloop);
boolean addMatchConsequence = ! StringUtils.isEmpty( fulfillRule.getConsequence().toString().trim() );

AndDescr fulfillLhs = fulfillRule.getLhs();
Expand Down Expand Up @@ -378,6 +370,8 @@ public void buildFulfillRule( PackageDescrBuilder builder, ExpectationRuleDescr
public void buildViolationRule( PackageDescrBuilder builder, ExpectationRuleDescr rule, ExpectationDescr expectations ) {

RuleDescr violRule = expectations.getViolation();
AttributeDescr noloop = new AttributeDescr("no-loop","");
violRule.addAttribute(noloop);
violRule.addAnnotation( "Propagation", "EAGER" );

AndDescr violLhs = violRule.getLhs();
Expand All @@ -387,6 +381,15 @@ public void buildViolationRule( PackageDescrBuilder builder, ExpectationRuleDesc

NotDescr not = new NotDescr( expectations.getExpectLhs() );
violLhs.addDescr( not );
if (expectations.getExpired() != null) {
RuleDescr expireRule = expectations.getExpired();
AndDescr additional = expireRule.getLhs();
for (BaseDescr descr: additional.getDescrs()) {
if (descr instanceof PatternDescr) {
violLhs.addDescr(descr);
}
}
}

if ( ! rule.isExpectationDisabled() ) {
violLhs.addDescr( expectPattern( expectations.getLabel(), extractVars( rule.getLhs(), expectations.getLabel() ), true ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@
import org.drools.compiler.compiler.DescrBuildError;
import org.drools.compiler.compiler.DrlExprParser;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.lang.descr.AndDescr;
import org.drools.compiler.lang.descr.BaseDescr;
import org.drools.compiler.lang.descr.ConditionalElementDescr;
import org.drools.compiler.lang.descr.ConstraintConnectiveDescr;
import org.drools.compiler.lang.descr.ExprConstraintDescr;
import org.drools.compiler.lang.descr.NotDescr;
import org.drools.compiler.lang.descr.OperatorDescr;
import org.drools.compiler.lang.descr.OrDescr;
import org.drools.compiler.lang.descr.PatternDescr;
import org.drools.compiler.lang.descr.RelationalExprDescr;
import org.drools.compiler.lang.descr.*;
import org.drools.core.base.evaluators.Operator;
import org.drools.core.base.evaluators.TimeIntervalParser;
import org.kie.internal.builder.conf.LanguageLevelOption;
Expand All @@ -33,8 +24,14 @@ public static long calcExpirationOffset( AndDescr trigger, BaseDescr ce ) {
return offset;
} else if ( ce instanceof OrDescr ) {
long offset = 0;
for ( BaseDescr child : ( (AndDescr) ce ).getDescrs() ) {
offset = Math.max( offset, calcExpirationOffset( trigger, child ) );
for (BaseDescr child : ((AndDescr) ce).getDescrs()) {
offset = Math.max(offset, calcExpirationOffset(trigger, child));
}
return offset;
} else if ( ce instanceof ForallDescr) {
long offset = 0;
for (BaseDescr child: ((ForallDescr)ce).getDescrs()) {
offset = Math.max(offset, calcExpirationOffset(trigger, child));
}
return offset;
} else if ( ce instanceof PatternDescr ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.drools.compiler.lang.api.*;
import org.drools.compiler.lang.api.impl.CEDescrBuilderImpl;
import org.drools.compiler.lang.api.impl.ExpectationDescrBuilderImpl;
import org.drools.compiler.lang.descr.AndDescr;
import org.drools.compiler.lang.descr.BaseDescr;
import org.drools.compiler.lang.descr.ExpectationRuleDescr;
import org.drools.compiler.lang.descr.*;
import it.unibo.deis.lia.org.drools.expectations.DRLExpectationHelper;
import org.drools.compiler.lang.descr.PatternDescr;

import java.util.List;

Expand Down Expand Up @@ -202,37 +200,55 @@ private String expectation( ECEPackageDescrBuilder packageDescr, ECERuleDescrBui
null,
DroolsEditorType.KEYWORD);

boolean negated = false;
if (helper.validateIdentifierKey(ExpectationSoftKeywords.ONE)) {
parser.match(input,
DRL6Lexer.ID,
ExpectationSoftKeywords.ONE,
null,
DroolsEditorType.KEYWORD);
expectDescr.one();
} else {
if (helper.validateIdentifierKey( DroolsSoftKeywords.NOT )) {
CEDescrBuilder lhs = new CEDescrBuilderImpl(expectDescr, new AndDescr());
do {

boolean negated = false;
if (helper.validateIdentifierKey(ExpectationSoftKeywords.ONE)) {
parser.match(input,
DRL6Lexer.ID,
DroolsSoftKeywords.NOT,
ExpectationSoftKeywords.ONE,
null,
DroolsEditorType.KEYWORD);
negated = true;
expectDescr.one();
} else {
if (helper.validateIdentifierKey(DroolsSoftKeywords.NOT)) {
parser.match(input,
DRL6Lexer.ID,
DroolsSoftKeywords.NOT,
null,
DroolsEditorType.KEYWORD);
negated = true;
}
}
}

String label = null;
if ( input.LA( 1 ) == DRL6Lexer.ID && input.LA( 2 ) == DRL6Lexer.COLON && !helper.validateCEKeyword( 1 ) ) {
label = parser.label(DroolsEditorType.IDENTIFIER_PATTERN);
if ( state.failed ) return null;
}
String label = null;
boolean contained = false;
if (input.LA(1) == DRL6Lexer.ID && input.LA(2) == DRL6Lexer.COLON && !helper.validateCEKeyword(1)) {
label = parser.label(DroolsEditorType.IDENTIFIER_PATTERN);
if (state.failed) return null;
} else if (helper.validateCEKeyword(1)) {
if (helper.validateIdentifierKey(DroolsSoftKeywords.FORALL)) {
contained = true;
}
}

if ( state.backtracking == 0 ) {
parser.lhsPattern( negated ? expectDescr.expectLhs().not().pattern() : expectDescr.expectLhs().pattern(), label, false );
} else {
parser.lhsPattern( null, label, false );
}
if (state.backtracking == 0) {
if (contained) {
parser.lhsForall(negated ? lhs.not():lhs);
} else {
parser.lhsPattern(negated ? lhs.not().pattern():lhs.pattern(), label, false);
}

} else {
parser.lhsPattern(null, label, false);
}
} while (!helper.validateIdentifierKey(ExpectationSoftKeywords.FAILSON)
&& !helper.validateIdentifierKey(ExpectationSoftKeywords.ONFULFILL)
&& !helper.validateIdentifierKey(ExpectationSoftKeywords.ONVIOLATION)
&& !helper.validateIdentifierKey(DroolsSoftKeywords.END));

((ExpectationDescr)expectDescr.getDescr()).setExpectLhs((AndDescr)lhs.getDescr());
failsOn(packageDescr, expectDescr, (ExpectationRuleDescr) rule.getDescr() );

onFulfill( packageDescr, expectDescr, (ExpectationRuleDescr) rule.getDescr() );
Expand Down Expand Up @@ -427,7 +443,13 @@ private void compensation( ECERuleDescrBuilder ruleDescrBuilder ) {


boolean isExpectationNext() {
return helper.validateIdentifierKey( ExpectationSoftKeywords.EXPECT ) || (input.LA( 1 ) == DRL6Lexer.ID && input.LA( 2 ) == DRL6Lexer.COLON);
if (helper.validateIdentifierKey( ExpectationSoftKeywords.EXPECT )) {
return true;
}
if (input.LA( 1 ) == DRL6Lexer.ID && input.LA( 2 ) == DRL6Lexer.COLON) {
return true;
}
return false;
}

boolean isRepairNext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ protected void sleep( int time ) {
((SessionPseudoClock) clock ).advanceTime( time, TimeUnit.MILLISECONDS );
}

protected Object newTextMessage( KieSession kSession, String sendingPhone, String receivingPhone, String message, String emoticon ) {
try {
FactType msgType = kSession.getKieBase().getFactType( "org.drools", "TextMsg" );
Object o = null;

o = msgType.newInstance();
msgType.set( o, "sendingPhone", sendingPhone );
msgType.set( o, "receivingPhone", receivingPhone );
msgType.set( o, "message", message);
msgType.set( o, "emoticon", emoticon);
return o;
} catch ( InstantiationException e ) {
fail( e.getMessage() );
} catch ( IllegalAccessException e ) {
fail( e.getMessage() );
}
return null;
}

protected Object newMessage( KieSession kSession, String sender, String receiver, String body, String more ) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,88 @@ public void testFailsOn() {
System.out.println( reportWMObjects( kSession ) );
}

@Test
public void testExpectForall() {

String src = "" +
"package org.drools; " +
"global java.util.List list; " +

"declare Msg " +
" @role(event) " +
" sender : String @key " +
" receiver : String @key " +
" body : String @key " +
" more : String " +
"end " +
" " +
"rule Expect_Test_Rule " +
"when " +
" $trigger : Msg( 'John' ; ) " +
"then " +
" expect Msg( 'Peter'; this after[0,100ms] $trigger ) " +
" forall( $msg: Msg( 'Peter'; ) " +
" Msg( this == $msg, receiver == 'John' ) ) " +
" onFulfill { " +
" list.add( 'AM1' ); " +
" System.out.println( 'Expectation fulfilled' ); " +
" } onViolation {" +
" list.add( 'AM2' ); " +
" System.out.println( 'Expectation violated' );" +
" } " +
" list.add( 0 ); " +
" System.out.println( 'Triggered expectation ' + $trigger ); " +
"end " +
"";

KieSession kSession = buildKnowledgeSession( src.getBytes() );
List<Object> list = new LinkedList<Object>();
kSession.setGlobal( "list", list );

System.out.println( "====================================================================================" );
kSession.insert( newMessage( kSession, "John", "Peter", "Hello", "X" ) );
kSession.fireAllRules();
assertTrue( list.contains( 0 ) );

System.out.println( "================================= SLEEP =========================================" );
sleep( 20 );
System.out.println( "================================= WAAKE =========================================" );

kSession.insert( newMessage( kSession, "Peter", "John", "Hello back", "Y" ) );
kSession.fireAllRules();
System.out.println( "================================= DONE =========================================" );




System.out.println(reportWMObjects(kSession));
assertEquals(Arrays.asList(0, "AM1"), list);
assertEquals( 1, countMeta( Fulfill.class.getName(), kSession ) );
assertEquals( 1, countMeta( Pending.class.getName(), kSession ) );
assertEquals( 0, countMeta( Viol.class.getName(), kSession ) );

sleep(30);
kSession.insert( newMessage( kSession, "John", "Peter", "Hello again", "Y" ) );
sleep(10);
kSession.insert( newMessage( kSession, "Peter", "Steve", "Hello", "X" ) );
kSession.fireAllRules();
assertEquals( Arrays.asList( 0, "AM1", 0 ), list );
assertEquals( 1, countMeta(Fulfill.class.getName(), kSession) );
assertEquals( 2, countMeta( Pending.class.getName(), kSession ) );
assertEquals( 0, countMeta( Viol.class.getName(), kSession ) );

sleep( 150 );

kSession.fireAllRules();

assertEquals( Arrays.asList( 0, "AM1", 0, "AM2" ), list );
assertEquals( 1, countMeta( Fulfill.class.getName(), kSession ) );
assertEquals( 0, countMeta( Pending.class.getName(), kSession ) );
assertEquals( 1, countMeta( Viol.class.getName(), kSession ) );


}

@Test
public void testSimpleFulfill() {

Expand Down

0 comments on commit 19e89f2

Please sign in to comment.