Skip to content

Commit

Permalink
[DROOLS-1139] clone EvalInvoker in EvalCondition evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Apr 26, 2016
1 parent c94053d commit 8eadcaa
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ public void body(MethodVisitor mv) {
}
}).addMethod(ACC_PUBLIC, "clone", generator.methodDescr(EvalExpression.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(ARETURN);
mv.visitTypeInsn( NEW, generator.getInternalClassName() );
mv.visitInsn( DUP );
mv.visitMethodInsn( INVOKESPECIAL, generator.getInternalClassName(), "<init>", "()V", false );
mv.visitInsn( ARETURN );
}
}).addMethod(ACC_PUBLIC, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)
).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(Boolean.TYPE, Tuple.class, Declaration[].class, WorkingMemory.class, Object.class), new String[]{"java/lang/Exception"}, new ClassGenerator.MethodBody() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
import java.io.Serializable;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Timestamp;
Expand Down Expand Up @@ -8410,4 +8409,47 @@ public void testWrongVariableNameWithSameDeclarationName() {

assertDrlHasCompilationError( str, -1 );
}

@Test
public void testComplexEvals() {
// DROOLS-1139
String drl =
"rule R1 when\n" +
" $s : String()\n" +
" Integer()\n" +
" not( ( eval($s.length() < 2) and (eval(true) or eval(false))))\n" +
"then \n" +
"end\n";

KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL )
.build().newKieSession();

kieSession.insert( 42 );
kieSession.insert( "test" );
assertEquals(1, kieSession.fireAllRules());
}

@Test
public void testComplexEvals2() {
// DROOLS-1139
String drl =
"rule R1 when\n" +
" $s : String()\n" +
" Boolean()\n" +
" $i : Integer()" +
" and (eval($s.length() > 2)\n" +
" or (eval(true) and eval(true)))\n" +
" and (eval(true)\n" +
" or ( eval($i > 2) and (eval(true))))\n\n" +
"then \n" +
"end\n";

KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL )
.build().newKieSession();

kieSession.insert( 42 );
kieSession.insert( "test" );
kieSession.insert( true );
assertEquals(4, kieSession.fireAllRules());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public LeftTupleSource getLeftTupleSource() {
* @return The debug string.
*/
public String toString() {
return "[EvalConditionNode: cond=" + this.condition + "]";
return "[EvalConditionNode(" + this.id + ")]: cond=" + this.condition + "]";
}

private int calculateHashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ public void pack(final GroupElement parent) {
// for each child, pack it and add it to parent
for ( RuleConditionElement child : children ) {
// we must keep the order, so add in the same place were parent was before
parent.getChildren().add( index++,
child );
parent.addChild( index++, child );
if ( child instanceof GroupElement ) {
final int previousSize = parent.getChildren().size();
((GroupElement) child).pack( parent );
Expand All @@ -230,8 +229,7 @@ public void pack(final GroupElement parent) {
parent.getChildren().remove( this );

final RuleConditionElement child = this.children.get( 0 );
parent.getChildren().add( index,
child );
parent.addChild( index, child );

if ( child instanceof GroupElement ) {
((GroupElement) child).pack( parent );
Expand Down Expand Up @@ -302,7 +300,7 @@ protected GroupElement clone(boolean deepClone) {
GroupElement cloned = new GroupElement();
cloned.setType( this.getType() );
for ( RuleConditionElement re : children ) {
cloned.addChild( deepClone && ( re instanceof GroupElement || re instanceof Pattern ) ? re.clone() : re );
cloned.addChild( deepClone ? re.clone() : re );
}
return cloned;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ private GroupElement[] processNamedConsequences(GroupElement[] ands) {
if (child instanceof NamedConsequence) {
GroupElement clonedAnd = GroupElementFactory.newAndInstance();
for (int j = 0; j < i; j++) {
clonedAnd.getChildren().add(children.get(j).clone());
clonedAnd.addChild(children.get(j).clone());
}
((NamedConsequence) child).setTerminal(true);
clonedAnd.getChildren().add(child);
clonedAnd.addChild(child);
children.remove(i--);
result.add(clonedAnd);
}
Expand Down Expand Up @@ -467,8 +467,7 @@ public void transform(final GroupElement parent) throws InvalidPatternException
for ( int j = orsList.size() - 1; j >= 0; j-- ) {
GroupElement or = orsList.get(j);
// we must insert at the beginning to keep the order
and.getChildren().add(0,
or.getChildren().get(indexes[j]).clone());
and.addChild( 0, or.getChildren().get(indexes[j]).clone() );
if ( (i % mod) == 0 ) {
indexes[j] = (indexes[j] + 1) % or.getChildren().size();
}
Expand All @@ -482,7 +481,7 @@ public void transform(final GroupElement parent) throws InvalidPatternException
// always add clone of them to avoid offset conflicts in declarations

// HERE IS THE MESSY PROBLEM: need to change further references to the appropriate cloned ref
and.getChildren().add( j, others[j].clone() );
and.addChild( j, others[j].clone() );
}
}
parent.addChild( and );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public interface RuleConditionElement
*
* @return
*/
public Map<String,Declaration> getInnerDeclarations();
Map<String,Declaration> getInnerDeclarations();

/**
* Returns a Map of declarations that are visible
* outside this conditional element.
*
* @return
*/
public Map<String,Declaration> getOuterDeclarations();
Map<String,Declaration> getOuterDeclarations();

/**
* Resolves the given identifier in the current scope and
Expand All @@ -52,20 +52,20 @@ public interface RuleConditionElement
* @param identifier
* @return
*/
public Declaration resolveDeclaration(String identifier);
Declaration resolveDeclaration(String identifier);

/**
* Returns a clone from itself
* @return
*/
public RuleConditionElement clone();
RuleConditionElement clone();

/**
* Returs a list of RuleConditionElement's that are nested
* inside the current element
* @return
*/
public List<? extends RuleConditionElement> getNestedElements();
List<? extends RuleConditionElement> getNestedElements();

/**
* Returns true in case this RuleConditionElement delimits
Expand All @@ -75,6 +75,6 @@ public interface RuleConditionElement
* NOT CE is a scope delimiter
* @return
*/
public boolean isPatternScopeDelimiter();
boolean isPatternScopeDelimiter();

}
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,15 @@ public String toInteralName(String className) {
return arrayPrefix + typeDescriptor;
}

private String[] toInteralNames(Class<?>[] classes) {
public String getClassName() {
return className;
}

public String getInternalClassName() {
return toInteralName(className);
}

private String[] toInteralNames( Class<?>[] classes ) {
if (classes == null) return null;
String[] internals = new String[classes.length];
for (int i = 0; i < classes.length; i++) internals[i] = toInteralName(classes[i]);
Expand Down

0 comments on commit 8eadcaa

Please sign in to comment.