Search in sources :

Example 1 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class FlowTest method testNotEmptyPredicate.

@Test
public void testNotEmptyPredicate() {
    Rule rule = rule("R").build(not(input(declarationOf(Person.class))), execute((drools) -> drools.insert(new Result("ok"))));
    Model model = new ModelImpl().addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    Person mario = new Person("Mario", 40);
    ksession.insert(mario);
    ksession.fireAllRules();
    assertTrue(ksession.getObjects(new ClassObjectFilter(Result.class)).isEmpty());
}
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) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) 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 2 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class FlowTest method testQueryInRule.

@Test
public void testQueryInRule() {
    Variable<Person> personV = declarationOf(Person.class);
    Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
    Query query = qdef.build(expr("exprA", qdef.getArg1(), qdef.getArg2(), (p, a) -> p.getAge() > a));
    Variable<Person> personVRule = declarationOf(Person.class);
    Rule rule = rule("R").build(qdef.call(personVRule, valueOf(40)), on(personVRule).execute((drools, p) -> drools.insert(new Result(p.getName()))));
    Model model = new ModelImpl().addQuery(query).addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    ksession.fireAllRules();
    Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
    assertEquals(1, results.size());
    assertEquals("Mario", results.iterator().next().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) Query(org.drools.model.Query) Result(org.drools.modelcompiler.domain.Result) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 3 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndUpdate.

@Test
public void testActivateOnMatchAndUpdate() throws Exception {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Alarm.class.getCanonicalName() + " \n";
    str += "import " + Sensor.class.getCanonicalName() + " \n";
    str += "rule StringRule  @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
    str += " when \n";
    str += " $a : Alarm() \n";
    str += " $s : Sensor() \n";
    str += " then \n";
    str += "end \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void matchCreated(MatchCreatedEvent event) {
            Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
            for (FactHandle alarm : alarms) {
                event.getKieRuntime().update(alarm, new Alarm());
            }
        }
    });
    // go !
    Alarm alarm = new Alarm();
    alarm.setMessage("test");
    alarm.setNumber(123);
    ksession.insert(alarm);
    Sensor sensor = new Sensor();
    sensor.setPressure(1);
    sensor.setTemperature(25);
    ksession.insert(sensor);
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) Alarm(org.drools.compiler.Alarm) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 4 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndDelete.

@Test
public void testActivateOnMatchAndDelete() throws Exception {
    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Alarm.class.getCanonicalName() + " \n";
    str += "import " + Sensor.class.getCanonicalName() + " \n";
    str += "rule StringRule  @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
    str += " when \n";
    str += " $a : Alarm() \n";
    str += " $s : Sensor() \n";
    str += " then \n";
    str += "end \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        @Override
        public void matchCreated(MatchCreatedEvent event) {
            Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
            for (FactHandle alarm : alarms) {
                event.getKieRuntime().delete(alarm);
            }
        }
    });
    // go !
    Alarm alarm = new Alarm();
    alarm.setMessage("test");
    alarm.setNumber(123);
    ksession.insert(alarm);
    Sensor sensor = new Sensor();
    sensor.setPressure(1);
    sensor.setTemperature(25);
    ksession.insert(sensor);
    ksession.fireAllRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) Alarm(org.drools.compiler.Alarm) KieHelper(org.kie.internal.utils.KieHelper) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Sensor(org.drools.compiler.Sensor) Test(org.junit.Test)

Example 5 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class LogicalTraitTest method testHardGetSetOnLogicallyTraitedField.

@Category(ReviseTraitTestWithPRAlwaysCategory.class)
@Test
public void testHardGetSetOnLogicallyTraitedField() {
    String drl = "package org.drools.test; " + "import " + Qty.class.getCanonicalName() + "; " + "" + "global java.util.List list; " + "declare Obs @Traitable( logical = true ) value : Qty end " + "declare trait TObs @Trait( logical = true ) value : TQty end " + "declare trait TQty @Trait( logical = true ) num : Integer end " + "rule Init " + "when " + "then " + "  Obs o = new Obs( new Qty( 42 ) ); " + "  don( o, TObs.class ); " + "end " + "rule Log " + "when " + "  $o : TObs( $val : value.num ) " + "then " + "  list.add( $val ); " + "end " + "rule Change " + "when " + "  $s : String() " + "  $o : TObs() " + "then " + "  delete( $s ); " + "  modify( $o ) { getValue().setNum( 99 ); } " + "end ";
    KieBase knowledgeBase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
    TraitFactory.setMode(mode, knowledgeBase);
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    ArrayList list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.fireAllRules();
    knowledgeSession.insert("x");
    knowledgeSession.fireAllRules();
    boolean found = false;
    for (Object o : knowledgeSession.getObjects(new ClassObjectFilter(Qty.class))) {
        assertEquals((Integer) 99, ((Qty) o).getNum());
        assertEquals(99, ((CoreWrapper) o)._getFieldTMS().get("num", Integer.class));
        found = true;
    }
    assertTrue(found);
    assertEquals(Arrays.asList(42, 99), list);
    knowledgeSession.dispose();
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ReviseTraitTestWithPRAlwaysCategory(org.drools.compiler.ReviseTraitTestWithPRAlwaysCategory) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Aggregations

ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)58 Test (org.junit.Test)50 KieSession (org.kie.api.runtime.KieSession)30 FactType (org.kie.api.definition.type.FactType)17 FactHandle (org.kie.api.runtime.rule.FactHandle)14 KieBase (org.kie.api.KieBase)13 ArrayList (java.util.ArrayList)12 Verifier (org.drools.verifier.Verifier)12 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)12 Subsumption (org.drools.verifier.report.components.Subsumption)11 Collection (java.util.Collection)9 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)9 KieServices (org.kie.api.KieServices)9 Iterator (java.util.Iterator)7 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)7 InternalFactHandle (org.drools.core.common.InternalFactHandle)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Map (java.util.Map)5 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)5 KieBuilder (org.kie.api.builder.KieBuilder)5