Search in sources :

Example 1 with Customer

use of org.drools.modelcompiler.domain.Customer 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);
}
Also used : TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) ObjectOutput(java.io.ObjectOutput) Collection(java.util.Collection) IOException(java.io.IOException) Customer(org.drools.modelcompiler.domain.Customer) Test(org.junit.Test) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) FactHandle(org.kie.api.runtime.rule.FactHandle) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) Adult(org.drools.modelcompiler.domain.Adult) Child(org.drools.modelcompiler.domain.Child) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Person(org.drools.modelcompiler.domain.Person) ObjectInput(java.io.ObjectInput) KieSession(org.kie.api.runtime.KieSession) Assert.assertEquals(org.junit.Assert.assertEquals) Customer(org.drools.modelcompiler.domain.Customer) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) KieSession(org.kie.api.runtime.KieSession)

Example 2 with Customer

use of org.drools.modelcompiler.domain.Customer 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);
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) FlowDSL.eval(org.drools.model.FlowDSL.eval) ObjectOutput(java.io.ObjectOutput) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) FlowDSL.not(org.drools.model.FlowDSL.not) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) FlowDSL.or(org.drools.model.FlowDSL.or) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) FlowDSL.on(org.drools.model.FlowDSL.on) Customer(org.drools.modelcompiler.domain.Customer) Serializable(java.io.Serializable) ConstraintType(org.drools.model.Index.ConstraintType) List(java.util.List) Query(org.drools.model.Query) FlowDSL.executeScript(org.drools.model.FlowDSL.executeScript) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Assert.assertFalse(org.junit.Assert.assertFalse) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) FlowDSL.bind(org.drools.model.FlowDSL.bind) FlowDSL.valueOf(org.drools.model.FlowDSL.valueOf) Person(org.drools.modelcompiler.domain.Person) ObjectInput(java.io.ObjectInput) InOperator(org.drools.model.operators.InOperator) FlowDSL.when(org.drools.model.FlowDSL.when) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) StockTick(org.drools.modelcompiler.domain.StockTick) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) ClockType(org.drools.core.ClockType) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) Employee(org.drools.modelcompiler.domain.Employee) FlowDSL.execute(org.drools.model.FlowDSL.execute) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) Address(org.drools.modelcompiler.domain.Address) FlowDSL.window(org.drools.model.FlowDSL.window) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) FlowDSL.forall(org.drools.model.FlowDSL.forall) Rule(org.drools.model.Rule) Query1Def(org.drools.model.Query1Def) Assert.assertEquals(org.junit.Assert.assertEquals) Customer(org.drools.modelcompiler.domain.Customer) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) KieBase(org.kie.api.KieBase) Rule(org.drools.model.Rule) Model(org.drools.model.Model) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 ObjectInput (java.io.ObjectInput)2 ObjectOutput (java.io.ObjectOutput)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 List (java.util.List)2 Arrays.asList (java.util.Arrays.asList)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 Assertions (org.assertj.core.api.Assertions)1 ClockType (org.drools.core.ClockType)1 KnowledgeBaseFactory (org.drools.core.impl.KnowledgeBaseFactory)1 FlowDSL.accFunction (org.drools.model.FlowDSL.accFunction)1 FlowDSL.accumulate (org.drools.model.FlowDSL.accumulate)1 FlowDSL.and (org.drools.model.FlowDSL.and)1 FlowDSL.bind (org.drools.model.FlowDSL.bind)1 FlowDSL.declarationOf (org.drools.model.FlowDSL.declarationOf)1 FlowDSL.eval (org.drools.model.FlowDSL.eval)1