use of org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy in project drools by kiegroup.
the class DynamicRulesTest method testDynamicNotNode.
@Test(timeout = 10000)
public void testDynamicNotNode() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_CollectDynamicRules1.drl");
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 = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicNotNode.drl").getKiePackages();
kbase.addPackages(kpkgs);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
results = (List) ksession.getGlobal("results");
ksession.fireAllRules();
assertEquals(0, results.size());
kbase.removeKiePackage("org.drools.mvel.compiler");
ksession.retract(ksession.getFactHandle(b));
kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicNotNode.drl").getKiePackages();
kbase.addPackages(kpkgs);
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
results = (List<?>) ksession.getGlobal("results");
ksession.fireAllRules();
assertEquals(1, results.size());
}
use of org.drools.serialization.protobuf.marshalling.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" + "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" + " exists 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(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.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy in project drools by kiegroup.
the class AbstractCellTest method testCell.
@Test
public void testCell() throws IOException, ClassNotFoundException {
final String drl = "package evalmodify;\n" + "\n" + "import " + Cell.class.getCanonicalName() + "\n" + "import java.lang.Integer\n" + "\n" + "rule \"test eval\"\n" + " when\n" + " cell1 : Cell(value1:value != 0)\n" + " cell2 : Cell(value2:value < value1)\n" + " eval (true)\n" + " then\n" + " cell2.setValue(value2 + 1);\n" + " update(cell2);\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cell-test", kieBaseTestConfiguration, drl);
final Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_REALTIME.getKieSessionConfiguration(), env);
try {
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();
if (kieBaseTestConfiguration.isIdentity()) {
assertEquals(9, cell.getValue());
} else {
assertEquals(0, cell.getValue());
}
} finally {
session.dispose();
}
}
use of org.drools.serialization.protobuf.marshalling.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" + "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.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy in project drools by kiegroup.
the class ObjectMarshallingStrategyStoreTest method testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames.
@Test
public void testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames() throws IOException, ClassNotFoundException {
Environment env = EnvironmentFactory.newEnvironment();
final Thing entityOne = new Thing(1, "Object 1");
final Thing entityTwo = new Thing(2, "Object 2");
Collection srcItems = new ArrayList();
srcItems.add(entityOne);
srcItems.add(entityTwo);
ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy("entityOne", new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityOne.equals(object);
}
}, Collections.singletonMap(entityOne.id, (Object) entityOne)), new IdentityPlaceholderResolverStrategy("entityTwo", new ObjectMarshallingStrategyAcceptor() {
@Override
public boolean accept(Object object) {
return entityTwo.equals(object);
}
}, Collections.singletonMap(entityTwo.id, (Object) entityTwo)) };
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, strats);
KieSessionConfiguration ksc = SessionConfiguration.newInstance();
final KieBaseConfiguration kbconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kbconf));
KieSession ks = kbase.newKieSession(ksc, env);
ks.insert(entityOne);
ks.insert(entityTwo);
ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, strats);
// Serialize object
final byte[] b1;
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.marshall(bos, ks, System.currentTimeMillis());
b1 = bos.toByteArray();
bos.close();
}
// Deserialize object
StatefulKnowledgeSession ksession2;
{
ByteArrayInputStream bais = new ByteArrayInputStream(b1);
try {
ksession2 = marshaller.unmarshall(bais, ks.getSessionConfiguration(), ks.getEnvironment());
Collection items = ksession2.getFactHandles();
Assert.assertTrue(items.size() == 2);
for (Object item : items) {
FactHandle factHandle = (FactHandle) item;
Assert.assertTrue(srcItems.contains(((DefaultFactHandle) factHandle).getObject()));
}
} catch (RuntimeException npe) {
// Here ocurrs the bug that shows that NamedObjectMarshallingStrategies are required.
Assert.fail("This error only happens if identity ObjectMarshallingStrategy use old name");
} finally {
bais.close();
}
}
}
Aggregations