Skip to content

Commit

Permalink
[DROOLS-2625] Support reverse in exec model's accumulator (apache#1976)
Browse files Browse the repository at this point in the history
* Support reverse

* If not present in the reverse map take it from the memory
  • Loading branch information
lucamolteni authored and mariofusco committed Jul 8, 2018
1 parent b5a9cd5 commit e2bfa06
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Expand Up @@ -2,7 +2,9 @@

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.drools.core.WorkingMemory;
import org.drools.core.common.InternalFactHandle;
Expand All @@ -16,6 +18,8 @@ public abstract class LambdaAccumulator implements Accumulator {

private final org.kie.api.runtime.rule.AccumulateFunction accumulateFunction;
protected final List<String> sourceVariables;
private Map<Integer, Object> reverseSupport;


protected LambdaAccumulator(org.kie.api.runtime.rule.AccumulateFunction accumulateFunction, List<String> sourceVariables) {
this.accumulateFunction = accumulateFunction;
Expand All @@ -40,11 +44,18 @@ public Serializable createContext() {
@Override
public void init(Object workingMemoryContext, Object context, Tuple leftTuple, Declaration[] declarations, WorkingMemory workingMemory) throws Exception {
accumulateFunction.init((Serializable) context);
if(supportsReverse()) {
reverseSupport = new HashMap<>();
}
}

@Override
public void accumulate(Object workingMemoryContext, Object context, Tuple leftTuple, InternalFactHandle handle, Declaration[] declarations, Declaration[] innerDeclarations, WorkingMemory workingMemory) throws Exception {
accumulateFunction.accumulate((Serializable) context, getAccumulatedObject( declarations, innerDeclarations, handle, leftTuple, ( InternalWorkingMemory ) workingMemory ));
final Object accumulatedObject = getAccumulatedObject(declarations, innerDeclarations, handle, leftTuple, (InternalWorkingMemory) workingMemory);
if (supportsReverse()) {
reverseSupport.put(handle.getId(), accumulatedObject);
}
accumulateFunction.accumulate((Serializable) context, accumulatedObject);
}

protected abstract Object getAccumulatedObject( Declaration[] declarations, Declaration[] innerDeclarations, InternalFactHandle handle, Tuple tuple, InternalWorkingMemory wm );
Expand All @@ -56,7 +67,13 @@ public boolean supportsReverse() {

@Override
public void reverse(Object workingMemoryContext, Object context, Tuple leftTuple, InternalFactHandle handle, Declaration[] declarations, Declaration[] innerDeclarations, WorkingMemory workingMemory) throws Exception {
accumulateFunction.reverse((Serializable) context, getAccumulatedObject( declarations, innerDeclarations, handle, leftTuple, ( InternalWorkingMemory ) workingMemory ));
final Object accumulatedObject = reverseSupport.remove(handle.getId());
if(accumulatedObject == null) {
final Object accumulatedObject2 = getAccumulatedObject(declarations, innerDeclarations, handle, leftTuple, (InternalWorkingMemory) workingMemory);
accumulateFunction.reverse((Serializable) context, accumulatedObject2);
} else {
accumulateFunction.reverse((Serializable) context, accumulatedObject);
}
}

@Override
Expand Down
Expand Up @@ -47,5 +47,14 @@ public int getCoefficient() {
public void setCoefficient(int coefficient) {
this.coefficient = coefficient;
}

@Override
public String toString() {
return "TargetPolicy{" +
"customerCode='" + customerCode + '\'' +
", productCode='" + productCode + '\'' +
", coefficient=" + coefficient +
'}';
}
}

0 comments on commit e2bfa06

Please sign in to comment.