use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class ExecutionFlowControlTest method testSalienceIntegerAndLoadOrder.
@Test(timeout = 10000)
public void testSalienceIntegerAndLoadOrder() throws Exception {
KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "test_salienceIntegerRule.drl");
KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
final PersonInterface person = new Person("Edson", "cheese");
ksession.insert(person);
ksession.fireAllRules();
assertEquals("Three rules should have been fired", 3, list.size());
assertEquals("Rule 4 should have been fired first", "Rule 4", list.get(0));
assertEquals("Rule 2 should have been fired second", "Rule 2", list.get(1));
assertEquals("Rule 3 should have been fired third", "Rule 3", list.get(2));
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovals.
@Test(timeout = 10000)
public void testDynamicRuleRemovals() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl", "test_Dynamic3.drl", "test_Dynamic4.drl");
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic2.drl").getKiePackages();
kbase.addPackages(kpkgs);
KieSession wm = kbase.newKieSession();
// AgendaEventListener ael = mock( AgendaEventListener.class );
// wm.addEventListener( ael );
final List<?> list = new ArrayList<Object>();
wm.setGlobal("list", list);
final PersonInterface bob = new Person("bob", "stilton");
bob.setStatus("Not evaluated");
FactHandle fh0 = wm.insert(bob);
final Cheese stilton1 = new Cheese("stilton", 5);
FactHandle fh1 = wm.insert(stilton1);
final Cheese stilton2 = new Cheese("stilton", 3);
FactHandle fh2 = wm.insert(stilton2);
final Cheese stilton3 = new Cheese("stilton", 1);
FactHandle fh3 = wm.insert(stilton3);
final Cheese cheddar = new Cheese("cheddar", 5);
FactHandle fh4 = wm.insert(cheddar);
wm.fireAllRules();
assertEquals(15, list.size());
list.clear();
kbase.removeRule("org.drools.mvel.compiler.test", "Who likes Stilton");
wm.update(fh0, bob);
wm.update(fh1, stilton1);
wm.update(fh2, stilton2);
wm.update(fh3, stilton3);
wm.update(fh4, cheddar);
wm.fireAllRules();
assertEquals(12, list.size());
list.clear();
kbase.removeRule("org.drools.mvel.compiler.test", "like cheese");
wm.update(fh0, bob);
wm.update(fh1, stilton1);
wm.update(fh2, stilton2);
wm.update(fh3, stilton3);
wm.update(fh4, cheddar);
wm.fireAllRules();
assertEquals(8, list.size());
list.clear();
final Cheese muzzarela = new Cheese("muzzarela", 5);
wm.insert(muzzarela);
wm.fireAllRules();
assertEquals(1, list.size());
list.clear();
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleAdditions.
@Test(timeout = 10000)
public void testDynamicRuleAdditions() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl");
KieSession workingMemory = kbase.newKieSession();
workingMemory.setGlobal("total", new Integer(0));
final List<?> list = new ArrayList<Object>();
workingMemory.setGlobal("list", list);
// Adding person in advance. There is no Person() object
// type node in memory yet, but the rule engine is supposed
// to handle that correctly
final PersonInterface bob = new Person("bob", "stilton");
bob.setStatus("Not evaluated");
workingMemory.insert(bob);
final Cheese stilton = new Cheese("stilton", 5);
workingMemory.insert(stilton);
final Cheese cheddar = new Cheese("cheddar", 5);
workingMemory.insert(cheddar);
workingMemory.fireAllRules();
assertEquals(1, list.size());
assertEquals("stilton", list.get(0));
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic2.drl").getKiePackages();
kbase.addPackages(kpkgs);
workingMemory.fireAllRules();
assertEquals(5, list.size());
assertEquals("stilton", list.get(0));
assertTrue("cheddar".equals(list.get(1)) || "cheddar".equals(list.get(2)));
assertTrue("stilton".equals(list.get(1)) || "stilton".equals(list.get(2)));
list.clear();
kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic3.drl").getKiePackages();
kbase.addPackages(kpkgs);
// Package 3 has a rule working on Person instances.
// As we added person instance in advance, rule should fire now
workingMemory.fireAllRules();
assertEquals("Rule from package 3 should have been fired", "match Person ok", bob.getStatus());
assertEquals(1, list.size());
assertEquals(bob, list.get(0));
kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic4.drl").getKiePackages();
kbase.addPackages(kpkgs);
workingMemory.fireAllRules();
assertEquals("Rule from package 4 should have been fired", "Who likes Stilton ok", bob.getStatus());
assertEquals(2, list.size());
assertEquals(bob, list.get(1));
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class DslTest method testWithExpanderMore.
@Test
public void testWithExpanderMore() throws Exception {
final Resource resource1 = KieServices.Factory.get().getResources().newClassPathResource("test_expander.dsl", getClass());
resource1.setSourcePath(TestConstants.TEST_RESOURCES_FOLDER + "test_expander.dsl");
final Resource resource2 = KieServices.Factory.get().getResources().newClassPathResource("rule_with_expander_dsl_more.dslr", getClass());
resource2.setSourcePath(TestConstants.TEST_RESOURCES_FOLDER + "rule_with_expander_dsl_more.dslr");
KieBuilder kieBuilder = KieUtil.getKieBuilderFromResources(kieBaseTestConfiguration, false, resource1, resource2);
List<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
assertTrue(errors.toString(), errors.isEmpty());
// the compiled package
final Collection<KiePackage> pkgs = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBuilder).getKiePackages();
assertEquals(2, pkgs.size());
KieBase kbase = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBuilder);
KieSession session = kbase.newKieSession();
session.insert(new Person("rage"));
session.insert(new Cheese("cheddar", 15));
final List messages = new ArrayList();
session.setGlobal("messages", messages);
// wm = SerializationHelper.serializeObject(wm);
session.fireAllRules();
// should have NONE, as both conditions should be false.
assertEquals(0, messages.size());
session.insert(new Person("fire"));
session.fireAllRules();
// still no firings
assertEquals(0, messages.size());
session.insert(new Cheese("brie", 15));
session.fireAllRules();
// YOUR FIRED
assertEquals(1, messages.size());
}
use of org.drools.mvel.compiler.Person in project drools by kiegroup.
the class DslTest method checkDSLExpanderTest.
private void checkDSLExpanderTest(KieBuilder kieBuilder) throws IOException, ClassNotFoundException {
List<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
assertTrue(errors.toString(), errors.isEmpty());
// the compiled package
final Collection<KiePackage> pkgs = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBuilder).getKiePackages();
assertEquals(2, pkgs.size());
KieBase kbase = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBuilder);
KieSession session = kbase.newKieSession();
session.insert(new Person("Bob", "http://foo.bar"));
session.insert(new Cheese("stilton", 42));
final List messages = new ArrayList();
session.setGlobal("messages", messages);
session.fireAllRules();
assertEquals(1, messages.size());
}
Aggregations