Search in sources :

Example 16 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class StatelessSessionTest method getSession2.

private StatelessKieSession getSession2(final Resource resource) throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    final StatelessKieSession session = kbase.newStatelessKieSession();
    session.setGlobal("list", this.list);
    session.setGlobal("cheesery", this.cheesery);
    return session;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 17 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class MarshallingTest method testSignedSerialization4.

/*
     *  A client environment configured to use signed serialization
     *  should refuse any non-signed serialized rulebase
     */
@Test
public void testSignedSerialization4() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    try {
        // set only the deserialisation properties, but not the serialization
        setPublicKeyProperties();
        kBase = SerializationHelper.serializeObject(kBase);
        fail("Should not deserialize an unsigned rulebase on an environment configured to work with signed rulebases.");
    } catch (Exception e) {
    // success
    } finally {
        unsetPublicKeyProperties();
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) IOException(java.io.IOException) Test(org.junit.Test)

Example 18 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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 19 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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 20 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase 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)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)368 Test (org.junit.Test)307 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)203 KieSession (org.kie.api.runtime.KieSession)193 ArrayList (java.util.ArrayList)163 List (java.util.List)99 KiePackage (org.kie.api.definition.KiePackage)79 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)45 FactHandle (org.kie.api.runtime.rule.FactHandle)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 InternalFactHandle (org.drools.core.common.InternalFactHandle)37 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)37 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)37 Cheese (org.drools.compiler.Cheese)36 Person (org.drools.compiler.Person)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 FactType (org.kie.api.definition.type.FactType)26 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)24 HashMap (java.util.HashMap)23