Search in sources :

Example 11 with Address

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

the class OOPathTest method testOrConditionalElement.

@Test
public void testOrConditionalElement() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Employee( $address: /address[ city == 'Big City' ] )\n" + " or " + "  Employee( $address: /address[ city == 'Small City' ] )\n" + "then\n" + "  list.add( $address.getCity() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
Also used : Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) Address(org.drools.modelcompiler.domain.Address) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 12 with Address

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

the class OOPathTest method testOrConstraintNoBinding.

@Test
public void testOrConstraintNoBinding() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  $emp: Employee( /address[ street == 'Elm' || city == 'Big City' ] )\n" + "        Employee( this != $emp, /address[ street == 'Elm' || city == 'Big City' ] )\n" + "then\n" + "  list.add( $emp.getName() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Bruno", "Alice");
}
Also used : Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) Address(org.drools.modelcompiler.domain.Address) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 13 with Address

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

the class OOPathTest method testOrConstraintWithJoin.

@Test
public void testOrConstraintWithJoin() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  $emp: Employee( $address: /address[ street == 'Elm' || city == 'Big City' ] )\n" + "        Employee( this != $emp, /address[ street == $address.street || city == 'Big City' ] )\n" + "then\n" + "  list.add( $address.getCity() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
Also used : Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Employee(org.drools.modelcompiler.domain.Employee) Address(org.drools.modelcompiler.domain.Address) InternationalAddress(org.drools.modelcompiler.domain.InternationalAddress) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 14 with Address

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

the class OrTest method testOrConstraint.

@Test
public void testOrConstraint() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Employee( $address: address, ( address.city == 'Big City' || address.city == 'Small City' ) )\n" + "then\n" + "  list.add( $address.getCity() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
Also used : Employee(org.drools.modelcompiler.domain.Employee) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Address(org.drools.modelcompiler.domain.Address) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 15 with Address

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

the class OrTest method testOrConditional.

@Test
public void testOrConditional() {
    final String drl = "import " + Employee.class.getCanonicalName() + ";" + "import " + Address.class.getCanonicalName() + ";" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Employee( $address: address, address.city == 'Big City' )\n" + " or " + "  Employee( $address: address, address.city == 'Small City' )\n" + "then\n" + "  list.add( $address.getCity() );\n" + "end\n";
    KieSession kieSession = getKieSession(drl);
    List<String> results = new ArrayList<>();
    kieSession.setGlobal("list", results);
    final Employee bruno = createEmployee("Bruno", new Address("Elm", 10, "Small City"));
    kieSession.insert(bruno);
    final Employee alice = createEmployee("Alice", new Address("Elm", 10, "Big City"));
    kieSession.insert(alice);
    kieSession.fireAllRules();
    Assertions.assertThat(results).containsExactlyInAnyOrder("Big City", "Small City");
}
Also used : Employee(org.drools.modelcompiler.domain.Employee) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) Address(org.drools.modelcompiler.domain.Address) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

Address (org.drools.modelcompiler.domain.Address)17 Test (org.junit.Test)17 KieSession (org.kie.api.runtime.KieSession)17 ArrayList (java.util.ArrayList)10 Employee (org.drools.modelcompiler.domain.Employee)10 Employee.createEmployee (org.drools.modelcompiler.domain.Employee.createEmployee)10 InternationalAddress (org.drools.modelcompiler.domain.InternationalAddress)7 Person (org.drools.modelcompiler.domain.Person)7 Result (org.drools.modelcompiler.domain.Result)3 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 Assertions (org.assertj.core.api.Assertions)1