Search in sources :

Example 26 with Address

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

the class MVELTest method testArrayAccessorWithStaticFieldAccess.

@Test
public void testArrayAccessorWithStaticFieldAccess() {
    final String str = "" + "package org.drools.mvel.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + Triangle.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( addresses[Triangle.ZERO] == new Address('s1'), addresses[Triangle.ZERO].street == new Address('s1').street ) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p = new Person("yoda");
    p.addAddress(new Address("s1"));
    ksession.insert(p);
    ksession.fireAllRules();
    assertEquals("r1", list.get(0));
    // Check it was built with MVELReturnValueExpression constraint
    final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (final ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MVELConstraint) {
        assertTrue(((MVELConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MVELConstraint) {
        assertTrue(((MVELConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
    }
}
Also used : Address(org.drools.mvel.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MVELObjectClassFieldReader(org.drools.mvel.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 27 with Address

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

the class UpdateTest method testModifyBlockWithFrom.

@Test
public void testModifyBlockWithFrom() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_ModifyBlockWithFrom.drl");
    KieSession ksession = kbase.newKieSession();
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    final Person bob = new Person("Bob");
    final Address addr = new Address("abc");
    bob.addAddress(addr);
    ksession.insert(bob);
    ksession.insert(addr);
    ksession.fireAllRules();
    // modify worked
    assertEquals("12345", addr.getZipCode());
    // chaining worked
    assertEquals(1, results.size());
    assertEquals(addr, results.get(0));
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 28 with Address

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

the class MetricLogUtilsTest method testFrom.

@Test
public void testFrom() {
    String str = "import " + Address.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule R1\n" + "when\n" + "  $p : Person()\n" + "  $a1 : Address() from $p.addresses\n" + "  $a2 : Address(suburb != \"XYZ\", zipCode == $a1.zipCode, this != $a1) from $p.addresses\n" + "then\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    List<Person> personList = IntStream.range(0, 10).mapToObj(i -> new Person("John" + i, i)).map(p -> {
        p.addAddress(new Address("StreetX" + p.getAge(), "ABC", "111"));
        p.addAddress(new Address("StreetY" + p.getAge(), "ABC", "111"));
        p.addAddress(new Address("StreetZ" + p.getAge(), "ABC", "999"));
        return p;
    }).collect(Collectors.toList());
    KieSession ksession = kbase.newKieSession();
    personList.stream().forEach(ksession::insert);
    int fired = ksession.fireAllRules();
    ksession.dispose();
    assertEquals(20, 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) 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)

Example 29 with Address

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

the class StatelessStressTest method testLotsOfStateless.

@Ignore
@Test
public void testLotsOfStateless() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "thread_class_test.drl");
    int numThreads = 100;
    Thread[] ts = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        Runnable run = () -> {
            long start = 0;
            long previous = 0;
            while (true) {
                start = System.currentTimeMillis();
                StatelessKieSession sess = kbase.newStatelessKieSession();
                // StatelessKieSession is not serializable
                // try {
                // sess    = SerializationHelper.serializeObject(sess);
                // } catch (Exception ex) {
                // throw new RuntimeException(ex);
                // }
                Person p = new Person();
                p.setName("Michael");
                Address add1 = new Address();
                add1.setStreet("High");
                Address add2 = new Address();
                add2.setStreet("Low");
                List l = new ArrayList();
                l.add(add1);
                l.add(add2);
                p.setAddresses(l);
                sess.execute(p);
                long current = System.currentTimeMillis() - start;
                if (previous != 0) {
                    float f = current / previous;
                    if (f > 1) {
                        System.err.println("SLOWDOWN");
                    }
                }
                previous = current;
            }
        };
        Thread t = new Thread(run);
        t.start();
        ts[i] = t;
    }
    for (int i = 0; i < ts.length; i++) {
        ts[i].join();
    }
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Address (org.drools.mvel.compiler.Address)29 Person (org.drools.mvel.compiler.Person)27 Test (org.junit.Test)27 KieSession (org.kie.api.runtime.KieSession)26 KieBase (org.kie.api.KieBase)25 ArrayList (java.util.ArrayList)12 List (java.util.List)11 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)8 AlphaNode (org.drools.core.reteoo.AlphaNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4 MVELConstraint (org.drools.mvel.MVELConstraint)4 MVELObjectClassFieldReader (org.drools.mvel.extractors.MVELObjectClassFieldReader)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)2 Cheese (org.drools.mvel.compiler.Cheese)2 KiePackage (org.kie.api.definition.KiePackage)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2