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);
}
}
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"));
}
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);
}
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"));
}
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
}
Aggregations