Skip to content

Commit

Permalink
[DROOLS-44] add more test cases to check declarations scope using ORs
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Feb 20, 2013
1 parent 18d47bd commit 7a0c0c8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Expand Up @@ -1279,4 +1279,78 @@ public void testDeclarationsScopeUsingOR() {
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
assertEquals(2, ksession.fireAllRules());
}

@Test
public void testDeclarationsScopeUsingOR2() {
// DROOLS-44
String str =
"declare A\n" +
" a1 : String\n" +
"end\n" +
"\n" +
"declare B\n" +
" b1 : String\n" +
"end\n" +
"\n" +
"rule Init salience 10 when \n" +
"then\n" +
" insert( new A( \"A\" ) );\n" +
" insert( new B( \"B\" ) );\n" +
"end\n" +
"\n" +
"rule R when \n" +
" A ( $a1 : a1 != null )\n" +
" (or\n" +
" B( $b1 : b1 != null )\n" +
" B( $b1 : b1 == null )\n" +
" )\n" +
" eval( $a1.compareTo( $b1 ) < 0 )\n" +
"then\n" +
" System.out.println( $b1 );\n" +
"end\n";

KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
assertEquals(2, ksession.fireAllRules());
}

@Test
public void testDeclarationsScopeUsingOR3() {
// DROOLS-44
String str =
"declare A\n" +
" a1 : String\n" +
"end\n" +
"\n" +
"declare B\n" +
" b1 : String\n" +
"end\n" +
"\n" +
"rule Init salience 10 when \n" +
"then\n" +
" insert( new A( \"A\" ) );\n" +
" insert( new B( null ) );\n" +
"end\n" +
"\n" +
"rule R when \n" +
" (or \n" +
" A ( $a1 : a1 != null )\n" +
" A ( $a1 : a1 != null ) ) \n" +
" (or\n" +
" (and\n" +
" B( $b1 : b1 != null )\n" +
" eval( $a1.compareTo( $b1 ) < 0 )\n" +
" )\n" +
" (and\n" +
" B( b1 == null )\n" +
" eval( $a1.compareTo(\"B\") < 0 )\n" +
" )\n" +
" )\n" +
"then\n" +
"end\n";

KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
assertEquals(3, ksession.fireAllRules());
}
}
Expand Up @@ -220,6 +220,7 @@ public Map<String, Declaration> getDeclarations(Rule rule) {
public Map<String, Declaration> getDeclarations(Rule rule, String consequenceName) {
final Map<String, Declaration> declarations = new HashMap<String, Declaration>();
for (RuleConditionElement aBuildStack : this.buildStack) {
// if we are inside of an OR we don't want each previous stack entry added because we can't see those variables
if (aBuildStack instanceof GroupElement && ((GroupElement)aBuildStack).getType() == GroupElement.Type.OR) {
continue;
}
Expand Down

0 comments on commit 7a0c0c8

Please sign in to comment.