use of org.drools.compiler.Foo in project drools by kiegroup.
the class MatchTest method testGetObjectsAccumulateWithNestedExists.
@Test
public void testGetObjectsAccumulateWithNestedExists() {
// DROOLS-1474
String str = "import org.drools.compiler.Foo\n" + "import org.drools.compiler.Bar\n" + "global java.util.List list\n" + "rule R when\n" + " $b1 : Bar(id == \"roadster\")\n" + " accumulate(\n" + " $b2 : Bar(this != $b1) and exists Foo(bar == $b2);\n" + " $t : count($b2)\n" + " )\n" + "then\n" + " list.addAll(((org.drools.core.spi.Activation)kcontext.getMatch()).getObjectsDeep());\n" + "end\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
List<Object> list = new ArrayList<>();
ksession.setGlobal("list", list);
Bar roadsterType = new Bar("roadster");
ksession.insert(roadsterType);
Foo bmwZ4 = new Foo("BMW Z4", roadsterType);
ksession.insert(bmwZ4);
Foo lotusElise = new Foo("Lotus Elise", roadsterType);
ksession.insert(lotusElise);
Foo mazdaMx5 = new Foo("Mazda MX-5", roadsterType);
ksession.insert(mazdaMx5);
Bar miniVanType = new Bar("minivan");
ksession.insert(miniVanType);
Foo kiaCarnival = new Foo("Kia Carnival", miniVanType);
ksession.insert(kiaCarnival);
Foo renaultEspace = new Foo("Renault Espace", miniVanType);
ksession.insert(renaultEspace);
ksession.fireAllRules();
assertTrue(list.contains(roadsterType));
assertFalse(list.contains(bmwZ4));
assertFalse(list.contains(lotusElise));
assertFalse(list.contains(mazdaMx5));
assertTrue(list.contains(miniVanType));
assertTrue(list.contains(kiaCarnival));
assertTrue(list.contains(renaultEspace));
ksession.dispose();
}
use of org.drools.compiler.Foo in project drools by kiegroup.
the class ExecutionFlowControlTest method testEnabledExpressionWithOr.
@Test
public void testEnabledExpressionWithOr() throws Exception {
String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + Foo.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "rule r1 salience(f1.field2) enabled(f1.field2 >= 20)\n" + "when\n" + " foo: Foo()\n" + " ( Pet() and f1 : FactA( field1 == 'f1') ) or \n" + " f1 : FactA(field1 == 'f2') \n" + "then\n" + " list.add( f1 );\n" + " foo.setId( 'xxx' );\n" + "end\n" + "\n";
KieBase kbase = loadKnowledgeBaseFromString(text);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Foo(null, null));
ksession.insert(new Pet(null));
FactA fact1 = new FactA();
fact1.setField1("f1");
fact1.setField2(10);
FactA fact2 = new FactA();
fact2.setField1("f1");
fact2.setField2(30);
FactA fact3 = new FactA();
fact3.setField1("f2");
fact3.setField2(20);
ksession.insert(fact1);
ksession.insert(fact2);
ksession.insert(fact3);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals(fact2, list.get(0));
assertEquals(fact3, list.get(1));
}
use of org.drools.compiler.Foo in project drools by kiegroup.
the class MatchTest method testGetObjectsExists.
@Test
public void testGetObjectsExists() {
// DROOLS-1474
String str = "import org.drools.compiler.Foo\n" + "import org.drools.compiler.Bar\n" + "global java.util.List list\n" + "rule R when\n" + " $b : Bar(id == \"roadster\")\n" + " exists Foo(bar == $b)\n" + "then\n" + " list.addAll(((org.drools.core.spi.Activation)kcontext.getMatch()).getObjectsDeep());\n" + "end\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
List<Object> list = new ArrayList<>();
ksession.setGlobal("list", list);
Bar roadsterType = new Bar("roadster");
ksession.insert(roadsterType);
Foo bmwZ4 = new Foo("BMW Z4", roadsterType);
ksession.insert(bmwZ4);
Foo lotusElise = new Foo("Lotus Elise", roadsterType);
ksession.insert(lotusElise);
Foo mazdaMx5 = new Foo("Mazda MX-5", roadsterType);
ksession.insert(mazdaMx5);
Bar miniVanType = new Bar("minivan");
ksession.insert(miniVanType);
Foo kiaCarnival = new Foo("Kia Carnival", miniVanType);
ksession.insert(kiaCarnival);
Foo renaultEspace = new Foo("Renault Espace", miniVanType);
ksession.insert(renaultEspace);
ksession.fireAllRules();
assertTrue(list.contains(roadsterType));
assertTrue(list.contains(bmwZ4));
assertTrue(list.contains(lotusElise));
assertTrue(list.contains(mazdaMx5));
assertFalse(list.contains(miniVanType));
assertFalse(list.contains(kiaCarnival));
assertFalse(list.contains(renaultEspace));
ksession.dispose();
}
use of org.drools.compiler.Foo in project drools by kiegroup.
the class MatchTest method testGetObjectsTwoPatterns.
@Test
public void testGetObjectsTwoPatterns() {
// DROOLS-1470
String str = "import org.drools.compiler.Foo\n" + "import org.drools.compiler.Bar\n" + "global java.util.List list\n" + "rule R when\n" + " $b : Bar(id == \"minivan\")\n" + " Foo(bar == $b)\n" + "then\n" + " list.addAll(kcontext.getMatch().getObjects());\n" + "end\n";
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
List<Object> list = new ArrayList<>();
ksession.setGlobal("list", list);
Bar roadsterType = new Bar("roadster");
ksession.insert(roadsterType);
Foo bmwZ4 = new Foo("BMW Z4", roadsterType);
ksession.insert(bmwZ4);
Foo lotusElise = new Foo("Lotus Elise", roadsterType);
ksession.insert(lotusElise);
Foo mazdaMx5 = new Foo("Mazda MX-5", roadsterType);
ksession.insert(mazdaMx5);
Bar miniVanType = new Bar("minivan");
ksession.insert(miniVanType);
Foo kieCarnival = new Foo("Kia Carnival", miniVanType);
ksession.insert(kieCarnival);
Foo renaultEspace = new Foo("Renault Espace", miniVanType);
ksession.insert(renaultEspace);
ksession.fireAllRules();
assertTrue(list.contains(miniVanType));
assertTrue(list.contains(kieCarnival));
assertTrue(list.contains(renaultEspace));
ksession.dispose();
}
use of org.drools.compiler.Foo in project drools by kiegroup.
the class EnabledTest method testEnabledExpression2.
@Test
public void testEnabledExpression2() {
final String drl = "import " + Foo.class.getName() + ";\n" + "rule R1\n" + " enabled( rule.name == $f.id )" + "when\n" + " $f : Foo()\n" + "then end\n" + "rule R2\n" + "when\n" + " Foo( id == \"R2\" )\n" + "then end\n";
final KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-enabled", "1.0.0");
final KieModule km = createAndDeployJar(ks, releaseId1, drl);
// Create a session and fire rules
final KieContainer kc = ks.newKieContainer(km.getReleaseId());
AgendaEventListener ael = mock(AgendaEventListener.class);
KieSession ksession = kc.newKieSession();
ksession.addEventListener(ael);
ksession.insert(new Foo("R1", null));
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
ArgumentCaptor<AfterMatchFiredEvent> event = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael).afterMatchFired(event.capture());
assertEquals("R1", event.getValue().getMatch().getRule().getName());
ael = mock(AgendaEventListener.class);
ksession = kc.newKieSession();
ksession.addEventListener(ael);
ksession.insert(new Foo("R2", null));
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
event = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael).afterMatchFired(event.capture());
assertEquals("R2", event.getValue().getMatch().getRule().getName());
}
Aggregations