Search in sources :

Example 11 with Address

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

the class MVELTest method testMapAccessorWithStaticFieldAccess.

@Test
public void testMapAccessorWithStaticFieldAccess() {
    final String str = "" + "package org.drools.mvel.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].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.getNamedAddresses().put(TestEnum.ONE, 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) TestEnum(org.drools.mvel.compiler.TestEnum) 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 12 with Address

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

the class MapConstraintTest method testMapNullConstraint.

@Test
public void testMapNullConstraint() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_mapNullConstraints.drl");
    KieSession ksession = kbase.newKieSession();
    final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    new WorkingMemoryConsoleLogger((WorkingMemory) ksession);
    final Map addresses = new HashMap();
    addresses.put("home", new Address("home street"));
    final Person bob = new Person("Bob");
    bob.setNamedAddresses(addresses);
    ksession.insert(bob);
    ksession.fireAllRules();
    final ArgumentCaptor<AfterMatchFiredEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(4)).afterMatchFired(arg.capture());
    org.kie.api.event.rule.AfterMatchFiredEvent aaf = arg.getAllValues().get(0);
    assertThat(aaf.getMatch().getRule().getName(), is("1. home != null"));
    aaf = arg.getAllValues().get(1);
    assertThat(aaf.getMatch().getRule().getName(), is("2. not home == null"));
    aaf = arg.getAllValues().get(2);
    assertThat(aaf.getMatch().getRule().getName(), is("7. work == null"));
    aaf = arg.getAllValues().get(3);
    assertThat(aaf.getMatch().getRule().getName(), is("8. not work != null"));
}
Also used : Address(org.drools.mvel.compiler.Address) HashMap(java.util.HashMap) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) WorkingMemoryConsoleLogger(org.drools.kiesession.audit.WorkingMemoryConsoleLogger) KieBase(org.kie.api.KieBase) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieSession(org.kie.api.runtime.KieSession) HashMap(java.util.HashMap) Map(java.util.Map) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 13 with Address

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

the class Misc2Test method testCorrectDeclarationsInConsequence.

@Test
public void testCorrectDeclarationsInConsequence() {
    // This showed up a bug where consequences declarations were being held onto and not replaced
    Address barcelonaCityCenter;
    Address cottageInMountains;
    Address cottageInDesert;
    Person johnFromBarcelona;
    Person elizabeth35Years;
    Person william25Years;
    Person peter70Years;
    Person mary33Years;
    barcelonaCityCenter = new Address("City Center", 1, "Barcelona");
    cottageInMountains = new Address("Mountains", 999, "Small Village");
    cottageInDesert = new Address("Sand Street", 1, "Desert Town");
    johnFromBarcelona = new Person("John", 18);
    peter70Years = new Person("Peter", 70);
    mary33Years = new Person("Mary", 33);
    johnFromBarcelona.setAddress(barcelonaCityCenter);
    elizabeth35Years = new Person("Elizabeth", 35);
    william25Years = new Person("William", 25);
    String str = "package org.drools.testcoverage.functional;\n" + "\n" + "import " + Address.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + "\n" + "import java.lang.Integer;" + "" + "rule \"Row 1 moveToBiggerCities\"\n" + "dialect \"mvel\"\n" + "  when\n" + "    $address : Address( $city : city)\n" + "    $inhabitantsCount : Integer( intValue() > 50000 , intValue() <= 100000 ) " + "           from accumulate ( $person : Person( address.city == $city ),\n" + "                             count($person)) \n" + "    $otherAddress : Address( $otherCity : city, this != $address )\n" + "    $person : Person( address == $otherAddress , likes == \"big city\" )\n" + "then\n" + "    $person.setAddress( $address );\n" + "end\n" + "";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final Address brno = producePeopleInCity(ksession, "Brno", 7000);
    final Address prague = producePeopleInCity(ksession, "Prague", 30000);
    final Address london = producePeopleInCity(ksession, "London", 60000);
    final Address smallCity = new Address();
    smallCity.setCity("city with just one person");
    peter70Years.setAddress(smallCity);
    peter70Years.setLikes("big city");
    william25Years.setAddress(brno);
    william25Years.setLikes("big city");
    mary33Years.setAddress(prague);
    mary33Years.setLikes("big city");
    elizabeth35Years.setAddress(london);
    elizabeth35Years.setLikes("big city");
    ksession.insert(smallCity);
    final FactHandle peter = ksession.insert(peter70Years);
    final FactHandle wiliam = ksession.insert(william25Years);
    final FactHandle mary = ksession.insert(mary33Years);
    final FactHandle elizabeth = ksession.insert(elizabeth35Years);
    Assertions.assertThat(ksession.fireAllRules()).isEqualTo(3);
}
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) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 14 with Address

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

the class QueryTest method testQueriesWithVariableUnificationOnNestedFields.

@Test
public void testQueriesWithVariableUnificationOnNestedFields() throws Exception {
    String str = "";
    str += "package org.drools.mvel.compiler.test  \n";
    str += "import org.drools.mvel.compiler.Person \n";
    str += "query peeps( String $name, String $likes, String $street) \n";
    str += "    $p : Person( $name := name, $likes := likes, $street := address.street ) \n";
    str += "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    Person p1 = new Person("darth", "stilton", 100);
    p1.setAddress(new Address("s1"));
    Person p2 = new Person("yoda", "stilton", 300);
    p2.setAddress(new Address("s2"));
    ksession.insert(p1);
    ksession.insert(p2);
    QueryResults results = getQueryResults(ksession, "peeps", new Object[] { Variable.v, Variable.v, Variable.v });
    assertEquals(2, results.size());
    List names = new ArrayList();
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        names.add(((Person) row.get("$p")).getName());
    }
    assertTrue(names.contains("yoda"));
    assertTrue(names.contains("darth"));
    results = getQueryResults(ksession, "peeps", new Object[] { Variable.v, Variable.v, "s1" });
    assertEquals(1, results.size());
    names = new ArrayList();
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        names.add(((Person) row.get("$p")).getName());
    }
    assertTrue(names.contains("darth"));
}
Also used : Address(org.drools.mvel.compiler.Address) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) DomainObject(org.drools.mvel.compiler.DomainObject) InsertedObject(org.drools.mvel.compiler.InsertedObject) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 15 with Address

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

the class JpaPersistentStatefulSessionTest method fromNodeWithModifiedCollection.

private void fromNodeWithModifiedCollection(final boolean withOOPath) {
    // DROOLS-376
    final String str = "package org.drools.test\n" + "import org.drools.mvel.compiler.Person\n" + "import org.drools.mvel.compiler.Address\n" + "rule rule1\n" + "when\n" + (withOOPath ? " $p: Person($list : addresses, /addresses[street == \"y\"])\n" : " $p: Person($list : addresses)\n" + " $a: Address(street == \"y\") from $list\n") + "then\n" + " $list.add( new Address(\"z\") );\n" + " $list.add( new Address(\"w\") );\n" + "end\n";
    final KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
    final Person p1 = new Person("John");
    p1.addAddress(new Address("x"));
    p1.addAddress(new Address("y"));
    ksession.insert(p1);
    ksession.fireAllRules();
    assertThat(p1.getAddresses()).hasSize(4);
    ksession.dispose();
// Should not fail here
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person)

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