Search in sources :

Example 1 with IdentityPlaceholderResolverStrategy

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());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with IdentityPlaceholderResolverStrategy

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());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) Cheese(org.drools.mvel.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 3 with IdentityPlaceholderResolverStrategy

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();
    }
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Cell(org.drools.testcoverage.common.model.Cell) Test(org.junit.Test)

Example 4 with IdentityPlaceholderResolverStrategy

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());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) Cheese(org.drools.mvel.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 5 with IdentityPlaceholderResolverStrategy

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();
        }
    }
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) ByteArrayInputStream(java.io.ByteArrayInputStream) Environment(org.kie.api.runtime.Environment) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) ObjectMarshallingStrategyAcceptor(org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor) Test(org.junit.Test)

Aggregations

IdentityPlaceholderResolverStrategy (org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy)6 Test (org.junit.Test)6 Environment (org.kie.api.runtime.Environment)6 KieSession (org.kie.api.runtime.KieSession)6 ArrayList (java.util.ArrayList)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 Cheese (org.drools.mvel.compiler.Cheese)3 KieBase (org.kie.api.KieBase)3 Collection (java.util.Collection)2 List (java.util.List)2 Person (org.drools.mvel.compiler.Person)2 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)2 ObjectMarshallingStrategyAcceptor (org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 Cell (org.drools.testcoverage.common.model.Cell)1