Search in sources :

Example 16 with Person

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

the class MetricLogUtilsTest method testAccumulate.

@Test
public void testAccumulate() {
    String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + "  $p1 : Person()\n" + "  accumulate ( $p2: Person ( getName().startsWith(\"J\"), this != $p1);\n" + "                $average : average($p2.getAge());\n" + "                $average > $p1.age, $average > 3\n" + "             )\n" + "then\n" + // "  System.out.println(\"$p1.name = \" + $p1.getName() + \", other's $average = \" + $average);\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(5, 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 17 with Person

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

the class MetricLogUtilsTest method testNot.

@Test
public void testNot() {
    String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + "  $p1 : Person()\n" + "  $p2 : Person(this != $p1)\n" + "  not Person(this != $p1, this != $p2, (age == $p1.age || age == $p2.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(90, 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) 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 18 with Person

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

the class ConstraintsTest method testQuadroupleBetaConstraints.

@Test
public void testQuadroupleBetaConstraints() {
    String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule R when\n" + "  Person( $age : age, $name : name, $happy : happy, $alive : alive )\n" + "  Person( name == $name, age == $age + 1, happy == $happy, alive == $alive )\n" + "then\n" + "  list.add($age);\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    List<Integer> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    Person p1 = new Person("AAA", 31, true);
    p1.setAlive(true);
    Person p2 = new Person("AAA", 34, true);
    p2.setAlive(true);
    Person p3 = new Person("AAA", 33, true);
    p3.setAlive(true);
    ksession.insert(p1);
    ksession.insert(p2);
    ksession.insert(p3);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(33, (int) list.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 19 with Person

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

the class JTMSTest method testChangeInPositivePrime.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInPositivePrime() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.mvel.compiler.Person;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(1); \n" + "    insertLogical( p );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + "    String( this == 'go2' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(2); \n" + "    insertLogical( p );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + "    String( this == 'go3' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(3); \n" + "    insertLogical( p );\n" + "end\n" + "\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    // We want to make sure go1 is prime, and then that it switches to go2
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    FactHandle fhGo2 = kSession.insert("go2");
    kSession.fireAllRules();
    FactHandle fhGo3 = kSession.insert("go3");
    kSession.fireAllRules();
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    // just go1, go2, go3, Person(darth)
    assertEquals(4, ep.getObjects().size());
    int count = 0;
    for (Object object : ep.getObjects()) {
        if (object instanceof Person) {
            assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
            count++;
        }
    }
    assertEquals(1, count);
    ObjectHashMap equalityMap = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
    // Only Person type is logical
    assertEquals(1, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    assertEquals(new Integer(1), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo1);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(2, key.getBeliefSet().size());
    assertEquals(new Integer(3), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo3);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(1, key.getBeliefSet().size());
    assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) ArrayList(java.util.ArrayList) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with Person

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

the class JTMSTest method testChangeInNegativePrime.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInNegativePrime() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.mvel.compiler.Person;" + "global java.util.List list;\n" + "\n" + "declare entry-point 'neg' end \n" + "" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(1); \n" + "    insertLogical( p, 'neg' );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + "    String( this == 'go2' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(2); \n" + "    insertLogical( p, 'neg' );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + "    String( this == 'go3' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(3); \n" + "    insertLogical( p, 'neg' );\n" + "end\n" + "\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    // We want to make sure go1 is prime, and then that it switches to go2
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    FactHandle fhGo2 = kSession.insert("go2");
    kSession.fireAllRules();
    FactHandle fhGo3 = kSession.insert("go3");
    kSession.fireAllRules();
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    // just go1, go2, go3
    assertEquals(3, ep.getObjects().size());
    // Person(darth)
    assertEquals(1, getNegativeObjects(kSession).size());
    int count = 0;
    for (Object object : getNegativeObjects(kSession)) {
        if (object instanceof Person) {
            assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
            count++;
        }
    }
    assertEquals(1, count);
    ObjectHashMap equalityMap = TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(ep).getEqualityKeyMap();
    // Only Person type is logical
    assertEquals(1, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    TruthMaintenanceSystemEqualityKey key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    assertEquals(new Integer(1), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo1);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(2, key.getBeliefSet().size());
    assertEquals(new Integer(3), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo3);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (TruthMaintenanceSystemEqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(1, key.getBeliefSet().size());
    assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) ArrayList(java.util.ArrayList) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) TruthMaintenanceSystemEqualityKey(org.drools.tms.TruthMaintenanceSystemEqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Ignore(org.junit.Ignore) 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