Search in sources :

Example 1 with Employee

use of org.drools.modelcompiler.domain.Employee in project drools by kiegroup.

the class FlowTest method testOrConditional.

@Test
public void testOrConditional() {
    final org.drools.model.Global<java.util.List> var_list = globalOf(java.util.List.class, "defaultpkg", "list");
    final Variable<Employee> var_$pattern_Employee$1$ = declarationOf(Employee.class, "$pattern_Employee$1$");
    final Variable<Address> var_$address = declarationOf(Address.class, "$address", reactiveFrom(var_$pattern_Employee$1$, (_this) -> _this.getAddress()));
    org.drools.model.Rule rule = rule("R").build(or(and(input(var_$pattern_Employee$1$), expr("$expr$2$", var_$address, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCity(), "Big City")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCity(), "Big City").reactOn("city")), and(input(var_$pattern_Employee$1$), expr("$expr$4$", var_$address, (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCity(), "Small City")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCity(), "Small City").reactOn("city"))), on(var_$address, var_list).execute(($address, list) -> {
        list.add($address.getCity());
    }));
    Model model = new ModelImpl().addRule(rule).addGlobal(var_list);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession kieSession = kieBase.newKieSession();
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
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) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Address(org.drools.modelcompiler.domain.Address) ArrayList(java.util.ArrayList) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) 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)

Example 2 with Employee

use of org.drools.modelcompiler.domain.Employee in project drools by kiegroup.

the class OOPathTest method testOrConstraint.

@Test
public void testOrConstraint() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  $emp: Employee( $address: /address[ street == 'Elm' || city == 'Big City' ] )\n" + "        Employee( this != $emp, /address[ street == 'Elm' || city == 'Big City' ] )\n" + "then\n" + "  list.add( $address.getCity() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
Also used : Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) Address(org.drools.modelcompiler.domain.Address) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with Employee

use of org.drools.modelcompiler.domain.Employee in project drools by kiegroup.

the class OOPathTest method testOrConditionalElementNoBinding.

@Test
public void testOrConditionalElementNoBinding() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $employee: (\n" + "  Employee( /address[ city == 'Big City' ] )\n" + " or " + "  Employee( /address[ city == 'Small City' ] )\n" + " )\n" + "then\n" + "  list.add( $employee.getName() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Bruno", "Alice");
}
Also used : Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) Address(org.drools.modelcompiler.domain.Address) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with Employee

use of org.drools.modelcompiler.domain.Employee in project drools by kiegroup.

the class OOPathTest method testOOPathMultipleConditions.

@Test
public void testOOPathMultipleConditions() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Employee( $address: /address[ street == 'Elm', city == 'Big City' ] )\n" + "then\n" + "  list.add( $address.getCity() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Big City");
}
Also used : Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) Address(org.drools.modelcompiler.domain.Address) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 5 with Employee

use of org.drools.modelcompiler.domain.Employee in project drools by kiegroup.

the class OOPathTest method testOOPathMultipleConditionsWithBinding.

@Test
public void testOOPathMultipleConditionsWithBinding() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + " $employee: (\n" + "  Employee( /address[ street == 'Elm', city == 'Big City' ] )\n" + " )\n" + "then\n" + "  list.add( $employee.getName() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Alice");
}
Also used : Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) Address(org.drools.modelcompiler.domain.Address) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)10 Address (org.drools.modelcompiler.domain.Address)10 Employee (org.drools.modelcompiler.domain.Employee)10 Employee.createEmployee (org.drools.modelcompiler.domain.Employee.createEmployee)10 Test (org.junit.Test)10 KieSession (org.kie.api.runtime.KieSession)10 InternationalAddress (org.drools.modelcompiler.domain.InternationalAddress)7 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 List (java.util.List)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