Search in sources :

Example 1 with ToysStore

use of org.drools.modelcompiler.domain.ToysStore in project drools by kiegroup.

the class FromTest method testFromOr.

@Test
public void testFromOr() {
    String str = "package org.drools.compiler.test  \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + Toy.class.getCanonicalName() + "\n" + "import " + ToysStore.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule R\n" + "when\n" + "    Person($age : age)\n" + "    Address($c : city)\n" + "    $store : ToysStore(cityName == $c)\n" + "    (or Toy( targetAge == $age ) from $store.firstFloorToys \n" + "               Toy( targetAge == $age ) from $store.secondFloorToys\n" + "        )\n" + "then\n" + "    list.add($store.getStoreName());\n" + "end \n";
    KieSession ksession = getKieSession(str);
    List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.insert(new Person("Leonardo", 3));
    ksession.insert(new Address("Milan"));
    Toy car = new Toy("Car", 3);
    Toy bicycle = new Toy("Bicycle", 3);
    Toy computer = new Toy("Computer", 7);
    ksession.insert(car);
    ksession.insert(bicycle);
    ksession.insert(computer);
    ToysStore ts = new ToysStore("Milan", "Toystore1");
    ts.getFirstFloorToys().add(car);
    ts.getSecondFloorToys().addAll(Arrays.asList(bicycle, computer));
    ksession.insert(ts);
    ksession.fireAllRules();
    Assertions.assertThat(list).contains("Toystore1");
}
Also used : ToysStore(org.drools.modelcompiler.domain.ToysStore) Address(org.drools.modelcompiler.domain.Address) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) PetPerson(org.drools.modelcompiler.domain.PetPerson) Toy(org.drools.modelcompiler.domain.Toy) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Address (org.drools.modelcompiler.domain.Address)1 Person (org.drools.modelcompiler.domain.Person)1 PetPerson (org.drools.modelcompiler.domain.PetPerson)1 Toy (org.drools.modelcompiler.domain.Toy)1 ToysStore (org.drools.modelcompiler.domain.ToysStore)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1