use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class MarshallingTest method testNot.
@Test
public void testNot() throws Exception {
String header = "package org.drools.compiler.test;\n" + "import java.util.List;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "global java.util.List list;\n";
String rule1 = "rule \"not rule test\"\n" + "salience 10\n" + "when\n" + " Person()\n" + " not Cheese( price >= 5 )\n" + "then\n" + " list.add( new Integer( 5 ) );\n" + "end\n";
KieBase kBase = loadKnowledgeBaseFromString(header + rule1);
Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession ksession = kBase.newKieSession(null, env);
List list = new ArrayList();
ksession.setGlobal("list", list);
// add a person, no cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Person bobba = new Person("bobba fet", 50);
ksession.insert(bobba);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(1, list.size());
// add another person, no cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Person darth = new Person("darth vadar", 200);
ksession.insert(darth);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, list.size());
// add cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Cheese stilton = new Cheese("stilton", 5);
ksession.insert(stilton);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, list.size());
// remove cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.retract(ksession.getFactHandle(stilton));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// put 2 cheeses back in
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.insert(stilton);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Cheese brie = new Cheese("brie", 18);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(4, list.size());
// now remove a cheese, should be no change
ksession.retract(ksession.getFactHandle(stilton));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// now remove a person, should be no change
ksession.retract(ksession.getFactHandle(bobba));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// removal remaining cheese, should increase by one, as one person left
ksession.retract(ksession.getFactHandle(brie));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(5, list.size());
}
use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class MarshallingTest method testSingleRuleSingleJoinNodePattern.
@Test
public void testSingleRuleSingleJoinNodePattern() throws Exception {
String rule = "package org.drools.compiler.test;\n";
rule += "import " + Person.class.getCanonicalName() + "\n";
rule += "import " + Cheese.class.getCanonicalName() + "\n";
rule += "global java.util.List list\n";
rule += "rule \"Rule 1\"\n";
rule += "when\n";
rule += " $c : Cheese( ) \n";
rule += " $p : Person( cheese == $c ) \n";
rule += "then\n";
rule += " list.add( $p );\n";
rule += "end";
KieBase kBase = loadKnowledgeBaseFromString(rule);
// Make sure the rete node map is created correctly
Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
assertEquals(5, nodes.size());
assertEquals("Cheese", ((ClassObjectType) ((ObjectTypeNode) nodes.get(3)).getObjectType()).getClassType().getSimpleName());
assertEquals("Person", ((ClassObjectType) ((ObjectTypeNode) nodes.get(5)).getObjectType()).getClassType().getSimpleName());
assertTrue("Should end with JoinNode", nodes.get(6).getClass().getSimpleName().endsWith("JoinNode"));
assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(7)).getRule().getName());
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
Cheese stilton = new Cheese("stilton", 25);
Cheese brie = new Cheese("brie", 49);
Person bobba = new Person("bobba fet", 32);
bobba.setCheese(stilton);
Person vadar = new Person("darth vadar", 32);
session.insert(stilton);
session.insert(bobba);
session.insert(vadar);
session.insert(brie);
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
assertEquals(1, ((List) session.getGlobal("list")).size());
assertEquals(bobba, ((List) session.getGlobal("list")).get(0));
Person c3po = new Person("c3p0", 32);
c3po.setCheese(stilton);
session.insert(c3po);
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("list")).size());
assertEquals(c3po, ((List) session.getGlobal("list")).get(1));
Person r2d2 = new Person("r2d2", 32);
r2d2.setCheese(brie);
session.insert(r2d2);
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
assertEquals(3, ((List) session.getGlobal("list")).size());
assertEquals(r2d2, ((List) session.getGlobal("list")).get(2));
}
use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd.
@Test
public void testSerializeAdd() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Dynamic1.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle stilton = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
// now recreate the rulebase, deserialize the session and test it
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
list = (List) session.getGlobal("list");
assertNotNull(list);
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
kpkgs = loadKnowledgePackages("test_Dynamic3.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
kBase.addPackages(kpkgs);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(list.size(), 3);
assertEquals(bob.getObject(), list.get(2));
assertEquals("stilton", list.get(1));
session.dispose();
}
use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class MarshallingTest method testAccumulateSessionSerialization.
@Test
public void testAccumulateSessionSerialization() throws Exception {
KieBase kbase = loadKnowledgeBase("test_AccumulateSerialization.drl");
KieSession ksession = kbase.newKieSession();
final List<Number> results = new ArrayList<Number>();
ksession.setGlobal("results", results);
ksession.insert(new Cheese("stilton", 10));
ksession.insert(new Cheese("brie", 5));
ksession.insert(new Cheese("provolone", 150));
ksession.insert(new Cheese("brie", 20));
ksession.insert(new Person("Bob", "brie"));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(1, results.size());
assertEquals(25, results.get(0).intValue());
}
use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class QueryTest method testQueryRemoval.
@Test
public void testQueryRemoval() throws Exception {
KieBase kbase = loadKnowledgeBase("simple_query_test.drl");
KieSession session = createKieSession(kbase);
final Cheese stilton = new Cheese("stinky", 5);
session.insert(stilton);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
QueryResults results = session.getQueryResults("simple query");
assertEquals(1, results.size());
Rule rule = kbase.getKiePackage("org.drools.compiler.test").getRules().iterator().next();
assertEquals("simple query", rule.getName());
kbase.removeQuery("org.drools.compiler.test", "simple query");
assertTrue(kbase.getKiePackage("org.drools.compiler.test").getRules().isEmpty());
try {
results = session.getQueryResults("simple query");
} catch (Exception e) {
assertTrue(e.getMessage().endsWith("does not exist"));
}
}
Aggregations