use of org.drools.modelcompiler.domain.TargetPolicy in project drools by kiegroup.
the class AccumulateTest method checkCollect.
private void checkCollect(String str) {
KieSession ksession = getKieSession(str);
Customer customer = new Customer();
customer.setCode("code1");
TargetPolicy target1 = new TargetPolicy();
target1.setCustomerCode("code1");
target1.setProductCode("p1");
target1.setCoefficient(1);
TargetPolicy target2 = new TargetPolicy();
target2.setCustomerCode("code1");
target2.setProductCode("p1");
target2.setCoefficient(1);
TargetPolicy target3 = new TargetPolicy();
target3.setCustomerCode("code1");
target3.setProductCode("p1");
target3.setCoefficient(1);
ksession.insert(customer);
ksession.insert(target1);
ksession.insert(target2);
ksession.insert(target3);
ksession.fireAllRules();
List<TargetPolicy> targetPolicyList = Arrays.asList(target1, target2, target3);
long filtered = targetPolicyList.stream().filter(c -> c.getCoefficient() == 1).count();
assertEquals(1, filtered);
}
use of org.drools.modelcompiler.domain.TargetPolicy in project drools by kiegroup.
the class FlowTest method testCustomAccumulate.
@Test
public void testCustomAccumulate() {
final Variable<Customer> var_$customer = declarationOf(Customer.class, "$customer");
final Variable<String> var_$code = declarationOf(String.class, "$code");
final Variable<TargetPolicy> var_$target = declarationOf(TargetPolicy.class, "$target");
final Variable<java.util.List> var_$pattern_List$1$ = declarationOf(java.util.List.class, "$pattern_List$1$");
final Variable<TargetPolicy> var_$tp = declarationOf(TargetPolicy.class, "$tp");
final org.drools.model.BitMask mask_$target = org.drools.model.BitMask.getPatternMask(TargetPolicy.class, "coefficient");
org.drools.model.Rule rule = rule("Customer can only have one Target Policy for Product p1 with coefficient 1").build(bind(var_$code).as(var_$customer, (_this) -> _this.getCode()).reactOn("code"), expr("$expr$2$", var_$target, var_$code, (_this, $code) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCustomerCode(), $code)).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCustomerCode(), $code -> $code).reactOn("customerCode"), expr("$expr$3$", var_$target, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getProductCode(), "p1")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 1, _this -> _this.getProductCode(), "p1").reactOn("productCode"), expr("$expr$4$", var_$target, (_this) -> _this.getCoefficient() == 1).indexedBy(int.class, org.drools.model.Index.ConstraintType.EQUAL, 2, _this -> _this.getCoefficient(), 1).reactOn("coefficient"), expr("$expr$5$", var_$pattern_List$1$, (_this) -> _this.size() > 1).indexedBy(int.class, org.drools.model.Index.ConstraintType.GREATER_THAN, 0, _this -> _this.size(), 1).reactOn("size"), accumulate(and(expr("$expr$2$", var_$tp, var_$code, (_this, $code) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCustomerCode(), $code)).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCustomerCode(), $code -> $code).reactOn("customerCode"), expr("$expr$3$", var_$tp, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getProductCode(), "p1")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 1, _this -> _this.getProductCode(), "p1").reactOn("productCode"), expr("$expr$4$", var_$tp, (_this) -> _this.getCoefficient() == 1).indexedBy(int.class, org.drools.model.Index.ConstraintType.EQUAL, 2, _this -> _this.getCoefficient(), 1).reactOn("coefficient")), accFunction(MyAccumulateFunction.class, var_$tp).as(var_$pattern_List$1$)), on(var_$target).execute((drools, $target) -> {
$target.setCoefficient(0);
drools.update($target, mask_$target);
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
KieSession ksession = kieBase.newKieSession();
Customer customer = new Customer();
customer.setCode("code1");
TargetPolicy target1 = new TargetPolicy();
target1.setCustomerCode("code1");
target1.setProductCode("p1");
target1.setCoefficient(1);
TargetPolicy target2 = new TargetPolicy();
target2.setCustomerCode("code1");
target2.setProductCode("p1");
target2.setCoefficient(1);
TargetPolicy target3 = new TargetPolicy();
target3.setCustomerCode("code1");
target3.setProductCode("p1");
target3.setCoefficient(1);
ksession.insert(customer);
ksession.insert(target1);
ksession.insert(target2);
ksession.insert(target3);
ksession.fireAllRules();
List<TargetPolicy> targetPolicyList = Arrays.asList(target1, target2, target3);
long filtered = targetPolicyList.stream().filter(c -> c.getCoefficient() == 1).count();
assertEquals(1, filtered);
}
Aggregations