Skip to content

Commit

Permalink
[JBRULES-3658] share alpha nodes even if the right parts of their exp…
Browse files Browse the repository at this point in the history
…ressions are constants with different names but same values
  • Loading branch information
mariofusco committed Oct 17, 2012
1 parent 0ab27a6 commit 0a5659c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11774,4 +11774,26 @@ public void testCommentWithCommaInRHS() {
KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
}

@Test
public void testAlphaHashingWithConstants() {
// JBRULES-3658
String str = "import org.drools.Person;\n" +
"import org.drools.integrationtests.MiscTest;\n" +
"rule R1 when\n" +
" $p : Person( age == 38 )\n" +
"then end\n" +
"rule R2 when\n" +
" $p : Person( age == 37+1 )\n" +
"then end\n" +
"rule R3 when\n" +
" $p : Person( age == 36+2 )\n" +
"then end\n";

KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

ksession.insert(new Person("Mario", 38));
assertEquals(3, ksession.fireAllRules());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.drools.rule.constraint.ConditionAnalyzer.SingleCondition;
import org.drools.runtime.rule.Variable;
import org.drools.spi.AcceptsReadAccessor;
import org.drools.spi.Constraint;
import org.drools.spi.FieldValue;
import org.drools.spi.InternalReadAccessor;
import org.drools.util.CompositeClassLoader;
Expand Down Expand Up @@ -477,9 +478,20 @@ public MvelConstraint clone() {
}

public int hashCode() {
if (isAlphaHashable()) {
return 29 * getLeftForEqualExpression().hashCode() + 31 * fieldValue.hashCode();
}
return expression.hashCode();
}

private String getLeftForEqualExpression() {
return expression.substring(0, expression.indexOf("==")).trim();
}

private boolean isAlphaHashable() {
return fieldValue != null && constraintType == IndexUtil.ConstraintType.EQUAL && getType() == ConstraintType.ALPHA;
}

public boolean equals(final Object object) {
if ( this == object ) {
return true;
Expand All @@ -488,8 +500,16 @@ public boolean equals(final Object object) {
return false;
}
MvelConstraint other = (MvelConstraint) object;
if (!expression.equals(other.expression)) {
return false;
if (isAlphaHashable()) {
if ( !other.isAlphaHashable() ||
!getLeftForEqualExpression().equals(other.getLeftForEqualExpression()) ||
!fieldValue.equals(other.fieldValue) ) {
return false;
}
} else {
if (!expression.equals(other.expression)) {
return false;
}
}
if (declarations.length != other.declarations.length) {
return false;
Expand Down

0 comments on commit 0a5659c

Please sign in to comment.