Search in sources :

Example 91 with KiePackage

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

the class MarshallingTest method testDynamicEmptyRule.

@Test
public void testDynamicEmptyRule() throws Exception {
    String rule1 = "package org.drools.compiler.test;\n";
    rule1 += "global java.util.List list\n";
    rule1 += "rule \"Rule 1\"\n";
    rule1 += "when\n";
    rule1 += "then\n";
    rule1 += "    list.add( \"fired1\" );\n";
    rule1 += "end";
    String rule2 = "package org.drools.compiler.test;\n";
    rule2 += "global java.util.List list\n";
    rule2 += "rule \"Rule 2\"\n";
    rule2 += "when\n";
    rule2 += "then\n";
    rule2 += "    list.add( \"fired2\" );\n";
    rule2 += "end";
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) loadKnowledgeBaseFromString(rule1);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    // Make sure the rete node map is created correctly
    assertEquals(2, nodes.size());
    assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    KieSession session1 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, false);
    session1.fireAllRules();
    assertEquals(1, ((List) session1.getGlobal("list")).size());
    KieSession session2 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session1, kBase, false);
    session.dispose();
    session1.dispose();
    Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(rule2);
    kBase.addPackages(kpkgs);
    session2.fireAllRules();
    System.out.println(session2.getGlobal("list"));
    assertEquals(2, ((List) session2.getGlobal("list")).size());
    assertEquals("fired1", ((List) session2.getGlobal("list")).get(0));
    assertEquals("fired2", ((List) session2.getGlobal("list")).get(1));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KiePackage(org.kie.api.definition.KiePackage) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 92 with KiePackage

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

the class MarshallingTest method testSerializationOfIndexedWM.

@Test
public void testSerializationOfIndexedWM() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Serializable2.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    Map map = new HashMap();
    map.put("x", kBase);
    map = SerializationHelper.serializeObject(map);
    kBase = (InternalKnowledgeBase) map.get("x");
    org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler").getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
    assertEquals(3, rules.length);
    KieSession session = kBase.newKieSession();
    session.setGlobal("list", new ArrayList());
    final Primitives p = new Primitives();
    p.setBytePrimitive((byte) 1);
    p.setShortPrimitive((short) 2);
    p.setIntPrimitive((int) 3);
    session.insert(p);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    assertEquals(1, session.getObjects().size());
    assertEquals(p, IteratorToList.convert(session.getObjects().iterator()).get(0));
    assertEquals(3, ((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("1", list.get(0));
    assertEquals("2", list.get(1));
    assertEquals("3", list.get(2));
}
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) Primitives(org.drools.compiler.Primitives) Map(java.util.Map) HashMap(java.util.HashMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 93 with KiePackage

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

the class MarshallingTest method testSerializeWorkingMemoryAndRuleBase1.

@Test
public void testSerializeWorkingMemoryAndRuleBase1() throws Exception {
    // has the first newStatefulSession before the ruleBase is serialised
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Serializable.drl");
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    KieSession session = kBase.newKieSession();
    Map map = new HashMap();
    map.put("x", kBase);
    map = SerializationHelper.serializeObject(map);
    kBase = (InternalKnowledgeBase) map.get("x");
    kBase.addPackages(kpkgs);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    kBase = SerializationHelper.serializeObject(kBase);
    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 94 with KiePackage

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

the class MarshallingTest method testSignedSerialization1.

/*
     *  Testing the signature framework
     */
@Test
public void testSignedSerialization1() throws Exception {
    try {
        setPrivateKeyProperties();
        setPublicKeyProperties();
        // Compile a package, add it to kbase, serialize both
        Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
        kpkgs = SerializationHelper.serializeObject(kpkgs);
        InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
        kBase.addPackages(kpkgs);
        kBase = SerializationHelper.serializeObject(kBase);
    } finally {
        unsetPrivateKeyProperties();
        unsetPublicKeyProperties();
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 95 with KiePackage

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

the class MarshallingTest method testSerializeAdd2.

/*
     *  Here I am inserting data points which are not used by any rule (e.g Person).
     *  Later adding rule (e.g. Rule: 'match Person') for those data points.
     *
     *  Result: Pkg/Rule addition is failing with ClassCastException
     */
@Test
public void testSerializeAdd2() 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));
    session.insert(new Cheese("brie", 10));
    InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 10));
    // fire rules
    session.fireAllRules();
    // check the results are correct
    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);
    // dynamically add a new package
    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));
    session.insert(new Cheese("brie", 20));
    InternalFactHandle mark = (InternalFactHandle) session.insert(new Person("mark", 20));
    session.fireAllRules();
    results = (List) session.getGlobal("results");
    assertEquals(4, results.size());
    assertEquals(stilton2.getObject(), results.get(1));
    assertEquals(bob.getObject(), results.get(2));
    assertEquals(mark.getObject(), results.get(3));
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    // dispose session
    session.dispose();
}
Also used : 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)

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