Search in sources :

Example 36 with Person

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

the class Misc2Test method testReportFailingConstraintOnError.

@Test
public void testReportFailingConstraintOnError() {
    // DROOLS-1071
    String drl = "import " + Person.class.getCanonicalName() + "\n" + "rule R when\n" + "    Person( name.startsWith(\"A\") )\n" + "then\n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession kieSession = kbase.newKieSession();
    for (int i = 0; i < 100; i++) {
        kieSession.insert(new Person("A" + i));
    }
    kieSession.fireAllRules();
    kieSession.insert(new Person(null));
    try {
        kieSession.fireAllRules();
        fail("Evaluation with null must throw a NPE");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("name.startsWith(\"A\")"));
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Person(org.drools.mvel.compiler.Person) DroolsParserException(org.drools.drl.parser.DroolsParserException) Test(org.junit.Test)

Example 37 with Person

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

the class Misc2Test method testDeletedRightTupleInChangedBucket.

@Test
public void testDeletedRightTupleInChangedBucket() {
    // PLANNER-488
    String drl = "import " + Person.class.getCanonicalName() + "\n" + "rule R when\n" + "    Person( $name: name, $age: age )\n" + "    not Person( happy, name == $name, age == $age-1 )\n" + "then\n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession kieSession = kbase.newKieSession();
    Person p1 = new Person("C", 1, true);
    Person p2 = new Person("B", 1, true);
    Person p3 = new Person("B", 2, true);
    Person p4 = new Person("A", 2);
    FactHandle fh1 = kieSession.insert(p1);
    FactHandle fh2 = kieSession.insert(p2);
    FactHandle fh3 = kieSession.insert(p3);
    FactHandle fh4 = kieSession.insert(p4);
    kieSession.fireAllRules();
    p4.setName("B");
    p4.setHappy(true);
    kieSession.update(fh4, p4);
    kieSession.fireAllRules();
    p3.setName("A");
    p3.setHappy(false);
    kieSession.update(fh3, p3);
    p1.setName("B");
    kieSession.update(fh1, p1);
    p2.setName("C");
    kieSession.update(fh2, p2);
    kieSession.fireAllRules();
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 38 with Person

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

the class Misc2Test method testStringCoercionComparison.

@Test
public void testStringCoercionComparison() {
    // DROOLS-167
    String str = "import " + Person.class.getName() + ";\n" + "rule R1 when\n" + " $p : Person( name < \"90201304122000000000000017\" )\n" + "then end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Person("90201304122000000000000015", 38));
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 39 with Person

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

the class Misc2Test method testPhreakWith2Nots.

@Test
public void testPhreakWith2Nots() {
    // DROOLS-7
    String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule R when\n" + "  Person( $age : age, $name : name )\n" + "  not Person( name == $name, age == $age + 1 )\n" + "  not Person( name == $name, age == $age - 1 )\n" + "then\n" + "  list.add($age);\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    List<Integer> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    Person p1 = new Person("AAA", 31);
    Person p2 = new Person("AAA", 34);
    Person p3 = new Person("AAA", 33);
    FactHandle fh1 = ksession.insert(p1);
    FactHandle fh3 = ksession.insert(p3);
    FactHandle fh2 = ksession.insert(p2);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(31, (int) list.get(0));
    list.clear();
    p1.setAge(35);
    ksession.update(fh1, p1);
    p3.setAge(31);
    ksession.update(fh3, p3);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(31, (int) list.get(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 40 with Person

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

the class Misc2Test method producePeopleInCity.

private Address producePeopleInCity(final KieSession ksession, final String city, final int countOfPeople) {
    final Address address = new Address();
    address.setCity(city);
    ksession.insert(address);
    for (int i = 0; i < countOfPeople; i++) {
        final Person person = new Person();
        person.setName("Inhabitant " + i);
        person.setAddress(address);
        ksession.insert(person);
    }
    return address;
}
Also used : Address(org.drools.mvel.compiler.Address) Person(org.drools.mvel.compiler.Person)

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