Search in sources :

Example 11 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class MarshallingTest method testSerializeWorkingMemoryAndRuleBase3.

@Test
public void testSerializeWorkingMemoryAndRuleBase3() throws Exception {
    // has the first newStatefulSession after the ruleBase is serialised
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Serializable.drl");
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    KieSession session = kBase.newKieSession();
    kBase.addPackages(kpkgs);
    session.setGlobal("list", new ArrayList());
    final Person bob = new Person("bob");
    session.insert(bob);
    // serialise a hashmap with the RuleBase as a key, after WM population
    Map map = new HashMap();
    map.put("x", kBase);
    map = SerializationHelper.serializeObject(map);
    kBase = (InternalKnowledgeBase) map.get("x");
    // now try serialising with a fully populated wm from a serialised rulebase
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler.test").getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
    assertEquals(4, rules.length);
    assertEquals("match Person 1", rules[0].getName());
    assertEquals("match Person 2", rules[1].getName());
    assertEquals("match Person 3", rules[2].getName());
    assertEquals("match Integer", rules[3].getName());
    assertEquals(1, session.getObjects().size());
    assertEquals(bob, IteratorToList.convert(session.getObjects().iterator()).get(0));
    assertEquals(2, ((InternalAgenda) session.getAgenda()).agendaSize());
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    final List list = (List) session.getGlobal("list");
    assertEquals(3, list.size());
    // because of agenda-groups
    assertEquals(new Integer(4), list.get(0));
    assertEquals(2, session.getObjects().size());
    assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(bob));
    assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(new Person("help")));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Person(org.drools.compiler.Person) Map(java.util.Map) HashMap(java.util.HashMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 12 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class MarshallingTest method testSerializeAdd3.

/*
     *  Works Fine if both the scenarios mentioned above are skipped.
     */
@Test
public void testSerializeAdd3() throws Exception {
    // Create a rulebase, a session, and test it
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    List results = new ArrayList();
    KieSession session = kBase.newKieSession();
    session.setGlobal("results", results);
    InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(stilton1.getObject(), results.get(0));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(stilton2.getObject(), results.get(1));
    assertEquals(bob2.getObject(), results.get(2));
    assertEquals(bob1.getObject(), results.get(3));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("../test_Dynamic1_2.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 40));
    InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 40));
    InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 40));
    InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 40));
    InternalFactHandle addr1 = (InternalFactHandle) session.insert(new Address("bangalore"));
    InternalFactHandle addr2 = (InternalFactHandle) session.insert(new Address("India"));
    session.fireAllRules();
    assertEquals(9, results.size());
    assertEquals(stilton3.getObject(), results.get(4));
    assertEquals(bob4.getObject(), results.get(5));
    assertEquals(bob3.getObject(), results.get(6));
    assertEquals(addr2.getObject(), results.get(7));
    assertEquals(addr1.getObject(), results.get(8));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 50));
    InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 50));
    InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 50));
    InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 50));
    InternalFactHandle addr3 = (InternalFactHandle) session.insert(new Address("Tripura"));
    InternalFactHandle addr4 = (InternalFactHandle) session.insert(new Address("Agartala"));
    session.fireAllRules();
    assertEquals(14, results.size());
    assertEquals(stilton4.getObject(), results.get(9));
    assertEquals(bob6.getObject(), results.get(10));
    assertEquals(bob5.getObject(), results.get(11));
    assertEquals(addr4.getObject(), results.get(12));
    assertEquals(addr3.getObject(), results.get(13));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.dispose();
}
Also used : Address(org.drools.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 13 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class MarshallingTest method testSerializeWorkingMemoryAndRuleBase2.

@Test
public void testSerializeWorkingMemoryAndRuleBase2() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Serializable.drl");
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    // serialise a hashmap with the RuleBase as a key
    Map map = new HashMap();
    map.put("x", kBase);
    map = SerializationHelper.serializeObject(map);
    kBase = (InternalKnowledgeBase) map.get("x");
    KieSession session = kBase.newKieSession();
    // serialise the working memory before population
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    kBase.addPackages(kpkgs);
    session.setGlobal("list", new ArrayList());
    final Person bob = new Person("bob");
    session.insert(bob);
    ((InternalWorkingMemory) session).flushPropagations();
    org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler.test").getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
    assertEquals(4, rules.length);
    assertEquals("match Person 1", rules[0].getName());
    assertEquals("match Person 2", rules[1].getName());
    assertEquals("match Person 3", rules[2].getName());
    assertEquals("match Integer", rules[3].getName());
    assertEquals(1, session.getObjects().size());
    assertEquals(bob, IteratorToList.convert(session.getObjects().iterator()).get(0));
    assertEquals(2, ((InternalAgenda) session.getAgenda()).agendaSize());
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.fireAllRules();
    final List list = (List) session.getGlobal("list");
    assertEquals(3, list.size());
    // because of agenda-groups
    assertEquals(new Integer(4), list.get(0));
    assertEquals(2, session.getObjects().size());
    assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(bob));
    assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(new Person("help")));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Map(java.util.Map) HashMap(java.util.HashMap) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 14 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class MarshallingTest method testSignedSerialization3.

/*
     *  Deserializing a signed rulebase without the proper public key
     *  should fail.
     */
@Test
public void testSignedSerialization3() throws Exception {
    try {
        // set only the serialisation properties, but not the deserialization
        setPrivateKeyProperties();
        // create the kpkgs, but do not let them serialize
        Collection<KiePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
        InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
        kBase.addPackages(kpkgs);
        try {
            kBase = SerializationHelper.serializeObject(kBase);
            fail("Deserialisation should have failed.");
        } catch (Exception e) {
        // success
        }
    } finally {
        unsetPrivateKeyProperties();
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class AddRemoveRulesTest method loadRule.

private boolean loadRule(final String rule) {
    String prefix = getPrefix();
    prefix += rule;
    final KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder.add(ResourceFactory.newReaderResource(new StringReader(prefix)), ResourceType.DRL);
    final Collection<KiePackage> pkgs = this.buildKnowledge(builder);
    this.addKnowledgeToBase(pkgs);
    return true;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StringReader(java.io.StringReader)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7