-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a24baa4
commit befa642
Showing
5 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...s-ruleunits/drools-ruleunits-dsl/src/main/java/org/drools/ruleunits/dsl/Accumulators.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.drools.ruleunits.dsl; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.drools.core.base.accumulators.IntegerSumAccumulateFunction; | ||
import org.drools.model.PatternDSL; | ||
import org.drools.model.RuleItemBuilder; | ||
import org.drools.model.Variable; | ||
import org.drools.model.functions.Function1; | ||
|
||
import static org.drools.model.DSL.accFunction; | ||
import static org.drools.model.DSL.accumulate; | ||
import static org.drools.model.DSL.declarationOf; | ||
|
||
public class Accumulators { | ||
|
||
public static <A, B> Accumulator1<A, B> sum(Function1<A, B> bindingFunc) { | ||
return new Accumulator1<>(bindingFunc, IntegerSumAccumulateFunction::new, Integer.class); | ||
} | ||
|
||
public static class Accumulator1<A, B> { | ||
private final Function1<A, B> bindingFunc; | ||
private final Supplier<?> accFuncSupplier; | ||
private final Class<?> accClass; | ||
|
||
public Accumulator1(Function1<A, B> bindingFunc, Supplier<?> accFuncSupplier, Class<?> accClass) { | ||
this.bindingFunc = bindingFunc; | ||
this.accFuncSupplier = accFuncSupplier; | ||
this.accClass = accClass; | ||
} | ||
} | ||
|
||
public static class AccumulatePattern1<A, B> extends RuleFactory.Pattern1<B> { | ||
|
||
private final RuleFactory.Pattern1<A> pattern; | ||
private final Accumulator1<A, B> acc; | ||
|
||
public AccumulatePattern1(RuleFactory rule, RuleFactory.Pattern1<A> pattern, Accumulator1<A, B> acc) { | ||
super(rule, declarationOf( (Class<B>) acc.accClass )); | ||
this.pattern = pattern; | ||
this.acc = acc; | ||
} | ||
|
||
@Override | ||
public RuleItemBuilder toExecModelItem() { | ||
PatternDSL.PatternDef patternDef = (PatternDSL.PatternDef) pattern.toExecModelItem(); | ||
Variable<B> boundVar = declarationOf( (Class<B>) acc.accClass ); | ||
patternDef.bind(boundVar, acc.bindingFunc); | ||
return accumulate( patternDef, accFunction(acc.accFuncSupplier, boundVar).as(getVariable()) ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 13 additions & 19 deletions
32
...ruleunits/drools-ruleunits-dsl/src/test/java/org/drools/ruleunits/dsl/AccumulateUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,42 @@ | ||
package org.drools.ruleunits.dsl; | ||
|
||
import java.util.Arrays; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.drools.ruleunits.api.DataSource; | ||
import org.drools.ruleunits.api.DataStore; | ||
|
||
import static org.drools.model.Index.ConstraintType.EQUAL; | ||
import static org.drools.ruleunits.dsl.Accumulators.sum; | ||
|
||
public class AccumulateUnit implements RuleUnitDefinition { | ||
|
||
private final DataStore<String> strings; | ||
private final DataStore<Integer> ints; | ||
private final List<String> results = new ArrayList<>(); | ||
|
||
public AccumulateUnit() { | ||
this(DataSource.createStore(), DataSource.createStore()); | ||
this(DataSource.createStore()); | ||
} | ||
|
||
public AccumulateUnit(DataStore<String> strings, DataStore<Integer> ints) { | ||
public AccumulateUnit(DataStore<String> strings) { | ||
this.strings = strings; | ||
this.ints = ints; | ||
} | ||
|
||
public DataStore<String> getStrings() { | ||
return strings; | ||
} | ||
|
||
public DataStore<Integer> getInts() { | ||
return ints; | ||
public List<String> getResults() { | ||
return results; | ||
} | ||
|
||
@Override | ||
public void defineRules(RulesFactory rulesFactory) { | ||
rulesFactory.addRule() | ||
.from(strings) | ||
.filter(s -> s.substring(0, 1), EQUAL, "A") | ||
|
||
; | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<String> strings = Arrays.asList("A1", "A123", "B12", "ABCDEF"); | ||
|
||
int result = strings.stream().filter(s -> s.substring(0,1).equals("A")).reduce(0, (a,s) -> a+s.length(), (a,b) -> a+b); | ||
System.out.println(result); | ||
RuleFactory accRuleFactory = rulesFactory.addRule(); | ||
accRuleFactory.accumulate( | ||
accRuleFactory.from(strings).filter(s -> s.substring(0, 1), EQUAL, "A"), | ||
sum(String::length) | ||
) | ||
.execute(results, (r, sum) -> r.add("Sum of length of Strings starting with A is " + sum)); ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters