Search in sources :

Example 1 with Pet

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());
}
Also used : PetPerson(org.drools.modelcompiler.domain.PetPerson) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) Pet(org.drools.modelcompiler.domain.Pet) Test(org.junit.Test)

Example 2 with Pet

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());
}
Also used : PetPerson(org.drools.modelcompiler.domain.PetPerson) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) Pet(org.drools.modelcompiler.domain.Pet) Test(org.junit.Test)

Example 3 with Pet

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());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Pet(org.drools.modelcompiler.domain.Pet) Test(org.junit.Test)

Example 4 with Pet

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());
}
Also used : Address(org.drools.modelcompiler.domain.Address) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Pet(org.drools.modelcompiler.domain.Pet) Test(org.junit.Test)

Example 5 with Pet

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());
}
Also used : KieSession(org.kie.api.runtime.KieSession) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Person(org.drools.modelcompiler.domain.Person) Pet(org.drools.modelcompiler.domain.Pet) Test(org.junit.Test)

Aggregations

Pet (org.drools.modelcompiler.domain.Pet)5 Test (org.junit.Test)5 KieSession (org.kie.api.runtime.KieSession)5 Person (org.drools.modelcompiler.domain.Person)3 HashMap (java.util.HashMap)2 PetPerson (org.drools.modelcompiler.domain.PetPerson)2 Address (org.drools.modelcompiler.domain.Address)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1