Search in sources :

Example 11 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class FieldAccessTest method testAccessFieldsFromSubClass.

@Test
public // unless typesafe=false is used
void testAccessFieldsFromSubClass() throws Exception {
    String rule = "";
    rule += "package org.drools.compiler;\n";
    rule += "import org.drools.mvel.compiler.Person;\n";
    rule += "import org.drools.mvel.compiler.Pet;\n";
    rule += "import org.drools.mvel.compiler.Cat;\n";
    rule += "declare Person @typesafe(false) end\n";
    rule += "rule \"Test Rule\"\n";
    rule += "when\n";
    rule += "    Person(\n";
    rule += "      pet.breed == \"Siamise\"\n";
    rule += "    )\n";
    rule += "then\n";
    rule += "    System.out.println(\"hello person\");\n";
    rule += "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, rule);
    KieSession session = kbase.newKieSession();
    final Person person = new Person();
    person.setPet(new Cat("Mittens"));
    session.insert(person);
    session.fireAllRules();
}
Also used : KieBase(org.kie.api.KieBase) Cat(org.drools.mvel.compiler.Cat) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 12 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class TypeCoercionTest method testPrimitiveToBoxedCoercionInMethodArgument.

@Test
public void testPrimitiveToBoxedCoercionInMethodArgument() throws Exception {
    final String str = "package org.drools.mvel.compiler.test;\n" + "import " + TypeCoercionTest.class.getName() + "\n" + "import org.drools.mvel.compiler.*\n" + "rule R1 when\n" + "   Person( $ag1 : age )" + "   $p2 : Person( name == TypeCoercionTest.integer2String($ag1) )" + "then\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    final KieSession ksession = kbase.newKieSession();
    final Person p = new Person("42", 42);
    ksession.insert(p);
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 13 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class ReteEvaluatorTest method testPropertyReactivity.

@Test
public void testPropertyReactivity() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $s : String()\n" + "  $p : Person(name == $s)\n" + "then\n" + "  modify($p) { setAge($p.getAge()+1) }\n" + "end";
    ReteEvaluator reteEvaluator = new StatefulKnowledgeSessionImpl(1L, getKBase(str));
    Person me = new Person("Mario", 40);
    reteEvaluator.insert("Mario");
    reteEvaluator.insert(me);
    assertEquals(1, reteEvaluator.fireAllRules());
    assertEquals(41, me.getAge());
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 14 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class MetricLogUtilsTest method testEval.

@Test
public void testEval() {
    String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "dialect \"mvel\"\n" + "when\n" + "  $p1 : Person()\n" + "  eval($p1.age > 6)" + "then\n" + // "  System.out.println(\"$p1.name = \" + $p1.getName());\n" +
    "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<Person> personList = IntStream.range(0, 10).mapToObj(i -> new Person("John" + i, i)).collect(Collectors.toList());
    KieSession ksession = kbase.newKieSession();
    personList.stream().forEach(ksession::insert);
    int fired = ksession.fireAllRules();
    ksession.dispose();
    assertEquals(3, fired);
    // 1 node expected
    Collection<Timer> timers = Search.in(registry).name("org.drools.metric.elapsed.time.per.evaluation").timers();
    assertThat(timers).hasSize(1);
    Collection<Timer> timers2 = Search.in(registry).name("org.drools.metric.elapsed.time").timers();
    assertThat(timers2).hasSize(1);
    Collection<Counter> counters = Search.in(registry).name("org.drools.metric.evaluation.count").counters();
    assertThat(counters).hasSize(1);
}
Also used : IntStream(java.util.stream.IntStream) Counter(io.micrometer.core.instrument.Counter) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Timer(io.micrometer.core.instrument.Timer) Search(io.micrometer.core.instrument.search.Search) Address(org.drools.mvel.compiler.Address) Person(org.drools.mvel.compiler.Person) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Assert.assertEquals(org.junit.Assert.assertEquals) Counter(io.micrometer.core.instrument.Counter) Timer(io.micrometer.core.instrument.Timer) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 15 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class MetricLogUtilsTest method testJoin.

@Test
public void testJoin() {
    String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + "  $p1 : Person(age > 5)\n" + "  $p2 : Person(age > $p1.age)\n" + "then\n" + "end\n" + "rule R2\n" + "when\n" + "  $p1 : Person(age > 5)\n" + "  $p2 : Person(age < $p1.age)\n" + "then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<Person> personList = IntStream.range(0, 10).mapToObj(i -> new Person("John" + i, i)).collect(Collectors.toList());
    KieSession ksession = kbase.newKieSession();
    personList.stream().forEach(ksession::insert);
    int fired = ksession.fireAllRules();
    ksession.dispose();
    assertEquals(36, fired);
    // 2 nodes expected
    Collection<Timer> timers = Search.in(registry).name("org.drools.metric.elapsed.time.per.evaluation").timers();
    assertThat(timers).hasSize(2);
    Collection<Timer> timers2 = Search.in(registry).name("org.drools.metric.elapsed.time").timers();
    assertThat(timers2).hasSize(2);
    Collection<Counter> counters = Search.in(registry).name("org.drools.metric.evaluation.count").counters();
    assertThat(counters).hasSize(2);
}
Also used : IntStream(java.util.stream.IntStream) Counter(io.micrometer.core.instrument.Counter) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Collectors(java.util.stream.Collectors) List(java.util.List) Timer(io.micrometer.core.instrument.Timer) Search(io.micrometer.core.instrument.search.Search) Address(org.drools.mvel.compiler.Address) Person(org.drools.mvel.compiler.Person) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Assert.assertEquals(org.junit.Assert.assertEquals) Address(org.drools.mvel.compiler.Address) Counter(io.micrometer.core.instrument.Counter) Timer(io.micrometer.core.instrument.Timer) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

Person (org.drools.mvel.compiler.Person)196 Test (org.junit.Test)185 KieSession (org.kie.api.runtime.KieSession)178 KieBase (org.kie.api.KieBase)171 ArrayList (java.util.ArrayList)98 List (java.util.List)72 Cheese (org.drools.mvel.compiler.Cheese)46 FactHandle (org.kie.api.runtime.rule.FactHandle)38 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 Address (org.drools.mvel.compiler.Address)33 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)23 KiePackage (org.kie.api.definition.KiePackage)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)16 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)15 HashMap (java.util.HashMap)12 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)12 Collection (java.util.Collection)11 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)8 AlphaNode (org.drools.core.reteoo.AlphaNode)8