Search in sources :

Example 21 with Address

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

the class NullSafeDereferencingTest method testNullSafeNullComparison.

@Test
public void testNullSafeNullComparison() {
    String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + "   Person( address!.street == null ) \n" + "then\n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Person("Mario", 38));
    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street"));
    ksession.insert(mark);
    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 22 with Address

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

the class NullSafeDereferencingTest method testNullSafeNestedAccessors.

@Test
public void testNullSafeNestedAccessors() {
    String str = "package org.drools.test; " + "import " + Person.class.getName() + "; " + "global java.util.List list; " + "rule R1 when " + " $street : String() " + " Person( address!.( street == $street, $zip : zipCode ) ) " + "then " + " list.add( $zip ); " + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Person("Mario", 38));
    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street", "", "123456"));
    ksession.insert(mark);
    ksession.insert("Main Street");
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
    assertEquals(Arrays.asList("123456"), list);
}
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 23 with Address

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

the class Misc2Test method testMergeMVELDialect.

@Test
public void testMergeMVELDialect() {
    // DROOLS-1751
    String drl1 = "package com.sample\n" + "import org.drools.mvel.compiler.*;\n" + "rule rule1 \n" + "    when\n" + "        (PersonHolder($addresses : person.addresses))\n" + "            &&\n" + "        (Address (street == \"AAA\") from $addresses)\n" + "    then\n" + "end";
    Collection<KiePackage> knowledgePackages1 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, drl1).getKiePackages();
    String drl2 = "package com.sample\n" + "import org.drools.mvel.compiler.*;\n" + "rule rule2 \n" + "    when\n" + "        PersonHolder()\n" + "    then\n" + "end";
    Collection<KiePackage> knowledgePackages2 = KieBaseUtil.getKieBaseFromKieModuleFromDrl("tmp", kieBaseTestConfiguration, drl2).getKiePackages();
    InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> combinedPackages1 = new ArrayList<>();
    combinedPackages1.addAll(knowledgePackages1);
    combinedPackages1.addAll(knowledgePackages2);
    // Add once to make inUse=true
    kbase1.addPackages(combinedPackages1);
    InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> combinedPackages2 = new ArrayList<>();
    combinedPackages2.addAll(knowledgePackages1);
    combinedPackages2.addAll(knowledgePackages2);
    // this will cause package deepClone
    kbase2.addPackages(combinedPackages2);
    KieSession ksession = kbase2.newKieSession();
    PersonHolder personHolder = new PersonHolder();
    Person person = new Person("John");
    Address address = new Address("AAA", "BBB", "111");
    person.addAddress(address);
    personHolder.setPerson(person);
    ksession.insert(personHolder);
    int fired = ksession.fireAllRules();
    assertEquals(2, fired);
}
Also used : Address(org.drools.mvel.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) PersonHolder(org.drools.mvel.compiler.PersonHolder) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 24 with Address

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

the class Misc2Test method testUpdateWithNonEffectiveActivations.

@Test
public void testUpdateWithNonEffectiveActivations() throws Exception {
    // JBRULES-3604
    String str = "package inheritance\n" + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "\n" + "rule \"Parent\"\n" + "    enabled false\n" + "    when \n" + "        $a : Address(suburb == \"xyz\")\n" + "    then \n" + "        System.out.println( $a ); \n" + "end \n" + "rule \"Child\" extends \"Parent\" \n" + "    when \n" + "        $b : Address( this == $a, street == \"123\")\n" + "    then \n" + "        System.out.println( $b ); \n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    Address address = new Address();
    address.setSuburb("xyz");
    FactHandle addressHandle = ksession.insert(address);
    int rulesFired = ksession.fireAllRules();
    assertEquals(0, rulesFired);
    address.setStreet("123");
    ksession.update(addressHandle, address);
    rulesFired = ksession.fireAllRules();
    System.out.println(rulesFired);
    assertEquals(1, rulesFired);
    ksession.dispose();
}
Also used : Address(org.drools.mvel.compiler.Address) 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) Test(org.junit.Test)

Example 25 with Address

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

the class MVELTest method testArrayAccessorWithoutGenerics.

@Test
public void testArrayAccessorWithoutGenerics() {
    final String str = "" + "package org.drools.mvel.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( addressesNoGenerics[0].street == new Address('s1').street) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    KieBuilder kieBuilder = KieUtil.getKieBuilderFromDrls(kieBaseTestConfiguration, false, str);
    List<org.kie.api.builder.Message> errors = kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
    // This should fail as there are no generics for the List
    assertFalse("Should have an error", errors.isEmpty());
}
Also used : Address(org.drools.mvel.compiler.Address) Message(org.kie.api.builder.Message) KieBuilder(org.kie.api.builder.KieBuilder) 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