use of org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy in project drools by kiegroup.
the class MarshallingTest method testNot.
@Test
public void testNot() throws Exception {
String header = "package org.drools.compiler.test;\n";
header += "import java.util.List;\n";
header += "import org.drools.compiler.Person\n";
header += "import org.drools.compiler.Cheese\n";
header += "global java.util.List list;\n";
String rule1 = "rule \"not rule test\"\n";
rule1 += "salience 10\n";
rule1 += "when\n";
rule1 += " Person()\n";
rule1 += " not Cheese( price >= 5 )\n";
rule1 += "then\n";
rule1 += " list.add( new Integer( 5 ) );\n";
rule1 += "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.core.marshalling.impl.IdentityPlaceholderResolverStrategy in project drools by kiegroup.
the class MarshallingTest method testExists.
@Test
public void testExists() throws Exception {
String header = "package org.drools.compiler.test;\n";
header += "import java.util.List;\n";
header += "import org.drools.compiler.Person\n";
header += "import org.drools.compiler.Cheese\n";
header += "global java.util.List list;\n";
String rule1 = "rule \"not rule test\"\n";
rule1 += "salience 10\n";
rule1 += "when\n";
rule1 += " Person()\n";
rule1 += " exists Cheese( price >= 5 )\n";
rule1 += "then\n";
rule1 += " list.add( new Integer( 5 ) );\n";
rule1 += "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(0, 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(0, 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(2, 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, no
ksession.retract(ksession.getFactHandle(brie));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// put one cheese back in, with one person should increase by one
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.insert(stilton);
ksession.fireAllRules();
assertEquals(5, list.size());
}
use of org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy in project drools by kiegroup.
the class CellTest method testCell.
@Test
public void testCell() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("evalmodify.drl"));
final Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession session = createKieSession(kbase, null, env);
final Cell cell1 = new Cell(9);
final Cell cell = new Cell(0);
session.insert(cell1);
session.insert(cell);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
assertEquals(9, cell.getValue());
}
use of org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy in project drools by kiegroup.
the class DynamicRulesTest method testDynamicNotNode.
@Test(timeout = 10000)
public void testDynamicNotNode() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_CollectDynamicRules1.drl");
kbase = SerializationHelper.serializeObject(kbase);
Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession ksession = kbase.newKieSession(null, env);
List<?> results = new ArrayList<Object>();
ksession.setGlobal("results", results);
final Cheese a = new Cheese("stilton", 10);
final Cheese b = new Cheese("stilton", 15);
final Cheese c = new Cheese("stilton", 20);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicNotNode.drl"));
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
results = (List) ksession.getGlobal("results");
ksession.fireAllRules();
assertEquals(0, results.size());
kbase.removeKiePackage("org.drools.compiler");
ksession.retract(ksession.getFactHandle(b));
kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicNotNode.drl"));
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
results = (List<?>) ksession.getGlobal("results");
ksession.fireAllRules();
assertEquals(1, results.size());
}
Aggregations