use of org.drools.modelcompiler.domain.Pet in project drools by kiegroup.
the class FromTest method testGlobalInFromExpression.
@Test
public void testGlobalInFromExpression() {
// DROOLS-4999
String str = "package org.drools.compiler.test \n" + "import " + PetPerson.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "global String petName;\n" + "rule R\n" + "when\n" + " $p : PetPerson ( )\n" + " $pet : Pet ( type == Pet.PetType.dog ) from $p.getPet(petName)\n" + "then\n" + "end \n";
KieSession ksession = getKieSession(str);
ksession.setGlobal("petName", "Dog");
PetPerson petPerson = new PetPerson("me");
Map<String, Pet> petMap = new HashMap<>();
petMap.put("Dog", new Pet(Pet.PetType.dog));
petMap.put("Cat", new Pet(Pet.PetType.cat));
petPerson.setPets(petMap);
ksession.insert(petPerson);
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.modelcompiler.domain.Pet in project drools by kiegroup.
the class FromTest method testFromMapValues.
@Test
public void testFromMapValues() {
// DROOLS-3661
String str = "package org.drools.compiler.test \n" + "import " + PetPerson.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "rule R\n" + "when\n" + " $p : PetPerson ( )\n" + " $pet : Pet ( type == Pet.PetType.dog ) from $p.getPets().values()\n\n" + "then\n" + "end \n";
KieSession ksession = getKieSession(str);
PetPerson petPerson = new PetPerson("me");
Map<String, Pet> petMap = new HashMap<>();
petMap.put("Dog", new Pet(Pet.PetType.dog));
petMap.put("Cat", new Pet(Pet.PetType.cat));
petPerson.setPets(petMap);
ksession.insert(petPerson);
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.modelcompiler.domain.Pet in project drools by kiegroup.
the class PropertyReactivityTest method testNestedPropInRHS.
@Test
public void testNestedPropInRHS() throws Exception {
// Property Reactivity for "owner"
final String str = "package org.drools.test;\n" + "import " + Pet.class.getCanonicalName() + ";\n" + "rule R1\n" + "when \n" + " $pet : Pet(age == 3)\n" + "then\n" + " modify ($pet) { getOwner().setLikes(\"Cookie\") };\n" + "end\n" + "rule R2\n" + "when \n" + " Pet(owner.likes == \"Cookie\")\n" + "then\n" + "end";
final KieSession ksession = getKieSession(str);
Pet pet = new Pet(Pet.PetType.cat);
Person person = new Person("John");
person.setLikes("Meat");
pet.setOwner(person);
pet.setAge(3);
ksession.insert(pet);
assertEquals(2, ksession.fireAllRules());
}
use of org.drools.modelcompiler.domain.Pet in project drools by kiegroup.
the class PropertyReactivityTest method testDeeplyNestedPropInRHS.
@Test
public void testDeeplyNestedPropInRHS() throws Exception {
// Property Reactivity for "owner"
final String str = "package org.drools.test;\n" + "import " + Pet.class.getCanonicalName() + ";\n" + "rule R1\n" + "when \n" + " $pet : Pet(age == 3)\n" + "then\n" + " modify ($pet) { getOwner().getAddress().setStreet(\"XYZ street\") };\n" + "end\n" + "rule R2\n" + "when \n" + " Pet(owner.address.street == \"XYZ street\")\n" + "then\n" + "end";
final KieSession ksession = getKieSession(str);
Pet pet = new Pet(Pet.PetType.cat);
Person person = new Person("John");
person.setAddress(new Address("ABC street"));
pet.setOwner(person);
pet.setAge(3);
ksession.insert(pet);
assertEquals(2, ksession.fireAllRules());
}
use of org.drools.modelcompiler.domain.Pet in project drools by kiegroup.
the class CompilerTest method testMultipleModifyWithDifferentFacts.
@Test()
public void testMultipleModifyWithDifferentFacts() {
String str = "import " + Person.class.getCanonicalName() + ";" + "import " + Pet.class.getCanonicalName() + ";" + "rule R1 when\n" + " $person : Person(name == \"John\")\n" + " $pet : Pet(owner == $person)\n" + "then\n" + " modify($person) { setName(\"George\") }\n" + " modify($pet) { setAge($pet.getAge()+1) }\n" + "end\n" + "rule R2 when\n" + " $person : Person(name == \"George\")\n" + " $pet : Pet(owner == $person)\n" + "then\n" + " modify($pet) { setAge($pet.getAge()+1) }\n" + "end";
KieSession ksession = getKieSession(str);
Person person = new Person("John", 40);
Pet pet = new Pet(Pet.PetType.dog, 3);
pet.setOwner(person);
ksession.insert(person);
ksession.insert(pet);
ksession.fireAllRules();
assertEquals("George", person.getName());
assertEquals(5, pet.getAge());
}
Aggregations