Search in sources :

Example 1 with Global

use of org.drools.model.Global in project drools by kiegroup.

the class FlowTest method testFromGlobal.

@Test
public void testFromGlobal() throws Exception {
    // global java.util.List list
    // rule R when
    // $o : String(length > 3) from list
    // then
    // insert($o);
    // end
    final Global<List> var_list = globalOf(List.class, "defaultpkg", "list");
    final Variable<String> var_$o = declarationOf(String.class, "$o", // cannot use Function.identity() here because target type is ?.
    from(var_list, x -> x));
    Rule rule = rule("R").build(expr("$expr$1$", var_$o, (_this) -> _this.length() > 3).indexedBy(int.class, org.drools.model.Index.ConstraintType.GREATER_THAN, 0, _this -> _this.length(), 3).reactOn("length"), on(var_$o).execute((drools, $o) -> {
        drools.insert($o);
    }));
    Model model = new ModelImpl().addGlobal(var_list).addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    List<String> messages = Arrays.asList("a", "Hello World!", "b");
    ksession.setGlobal("list", messages);
    ksession.fireAllRules();
    List<String> results = getObjectsIntoList(ksession, String.class);
    assertFalse(results.contains("a"));
    assertTrue(results.contains("Hello World!"));
    assertFalse(results.contains("b"));
}
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) KieBase(org.kie.api.KieBase) 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) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Test(org.junit.Test)

Example 2 with Global

use of org.drools.model.Global in project drools by kiegroup.

the class FlowTest method testGlobalInConsequence.

@Test
public void testGlobalInConsequence() {
    Variable<Person> markV = declarationOf(Person.class);
    Global<Result> resultG = globalOf(Result.class, "org.mypkg");
    Rule rule = rule("org.mypkg", "global").build(expr("exprA", markV, p -> p.getName().equals("Mark")).indexedBy(String.class, ConstraintType.EQUAL, 0, Person::getName, "Mark").reactOn("name"), on(markV, resultG).execute((p, r) -> r.setValue(p.getName() + " is " + p.getAge())));
    Model model = new ModelImpl().addRule(rule).addGlobal(resultG);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    Result result = new Result();
    ksession.setGlobal(resultG.getName(), result);
    Person mark = new Person("Mark", 37);
    Person edson = new Person("Edson", 35);
    Person mario = new Person("Mario", 40);
    ksession.insert(mark);
    ksession.insert(edson);
    ksession.insert(mario);
    ksession.fireAllRules();
    assertEquals("Mark is 37", result.getValue());
}
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) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 3 with Global

use of org.drools.model.Global in project drools by kiegroup.

the class OOPathFlowTest method testOOPathCast.

@Test
public void testOOPathCast() {
    Global<List> listG = globalOf(List.class, "defaultpkg", "list");
    Variable<Man> manV = declarationOf(Man.class, "$man");
    // Drools doc: In this way subsequent constraints can also safely access to properties declared only in that subclass
    Variable<InternationalAddress> addressV = declarationOf(InternationalAddress.class, "$italy", reactiveFrom(manV, Man::getAddress));
    Rule rule = rule("oopath").build(expr("exprA", addressV, c -> c.getState().equals("Italy")), on(manV, listG).execute((t, l) -> l.add(t.getName())));
    Model model = new ModelImpl().addGlobal(listG).addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Man bob = new Man("Bob", 40);
    bob.setAddress(new InternationalAddress("Via Verdi", "Italy"));
    ksession.insert(bob);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
Also used : FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) QueryDef(org.drools.model.QueryDef) Child(org.drools.modelcompiler.domain.Child) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) 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) FlowDSL.on(org.drools.model.FlowDSL.on) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) List(java.util.List) Rule(org.drools.model.Rule) Assert.assertEquals(org.junit.Assert.assertEquals) ModelImpl(org.drools.model.impl.ModelImpl) ArrayList(java.util.ArrayList) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Test(org.junit.Test)

Example 4 with Global

use of org.drools.model.Global in project drools by kiegroup.

the class OOPathFlowTest method testOOPath.

@Test
public void testOOPath() {
    Global<List> listG = globalOf(List.class, "defaultpkg", "list");
    Variable<Man> manV = declarationOf(Man.class);
    Variable<Woman> wifeV = declarationOf(Woman.class, reactiveFrom(manV, Man::getWife));
    Variable<Child> childV = declarationOf(Child.class, reactiveFrom(wifeV, Woman::getChildren));
    Rule rule = rule("oopath").build(expr("exprA", childV, c -> c.getAge() > 10), on(manV, listG).execute((t, l) -> l.add(t.getName())));
    Model model = new ModelImpl().addGlobal(listG).addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    final Woman alice = new Woman("Alice", 38);
    final Man bob = new Man("Bob", 40);
    final Man carl = new Man("Bob", 42);
    bob.setWife(alice);
    final Child charlie = new Child("Charles", 12);
    final Child debbie = new Child("Debbie", 10);
    alice.addChild(charlie);
    alice.addChild(debbie);
    ksession.insert(bob);
    ksession.insert(carl);
    ksession.fireAllRules();
    Assertions.assertThat(list).containsExactlyInAnyOrder("Bob");
}
Also used : FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) QueryDef(org.drools.model.QueryDef) Child(org.drools.modelcompiler.domain.Child) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Model(org.drools.model.Model) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) 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) FlowDSL.on(org.drools.model.FlowDSL.on) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) List(java.util.List) Rule(org.drools.model.Rule) Assert.assertEquals(org.junit.Assert.assertEquals) ModelImpl(org.drools.model.impl.ModelImpl) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) Man(org.drools.modelcompiler.domain.Man) Rule(org.drools.model.Rule) Woman(org.drools.modelcompiler.domain.Woman) ModelImpl(org.drools.model.impl.ModelImpl) Child(org.drools.modelcompiler.domain.Child) Test(org.junit.Test)

Example 5 with Global

use of org.drools.model.Global in project drools by kiegroup.

the class RuleContext method getDeclaration.

Declaration getDeclaration(Variable variable) {
    if (variable.isFact()) {
        Declaration declaration = innerDeclaration.get(variable);
        if (declaration == null) {
            declaration = queryDeclaration.get(variable);
        }
        if (declaration == null) {
            Pattern pattern = patterns.get(variable);
            declaration = pattern != null ? pattern.getDeclaration() : null;
        }
        return declaration;
    } else {
        Global global = ((Global) variable);
        ObjectType objectType = builder.getObjectType(global);
        InternalReadAccessor globalExtractor = new GlobalExtractor(global.getName(), objectType);
        return new Declaration(global.getName(), globalExtractor, new Pattern(0, objectType));
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ObjectType(org.drools.core.spi.ObjectType) GlobalExtractor(org.drools.core.spi.GlobalExtractor) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) Global(org.drools.model.Global)

Aggregations

Global (org.drools.model.Global)12 Model (org.drools.model.Model)11 Rule (org.drools.model.Rule)11 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Assertions (org.assertj.core.api.Assertions)10 Variable (org.drools.model.Variable)10 ModelImpl (org.drools.model.impl.ModelImpl)10 KieBaseBuilder (org.drools.modelcompiler.builder.KieBaseBuilder)10 Child (org.drools.modelcompiler.domain.Child)10 Man (org.drools.modelcompiler.domain.Man)10 Toy (org.drools.modelcompiler.domain.Toy)10 Woman (org.drools.modelcompiler.domain.Woman)10 Assert.assertEquals (org.junit.Assert.assertEquals)10 Test (org.junit.Test)10 KieBase (org.kie.api.KieBase)10 KieSession (org.kie.api.runtime.KieSession)10 QueryResults (org.kie.api.runtime.rule.QueryResults)10 FlowDSL.accFunction (org.drools.model.FlowDSL.accFunction)9 FlowDSL.accumulate (org.drools.model.FlowDSL.accumulate)9