Search in sources :

Example 71 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class TimerAndCalendarTest method testTimerRuleAfterCronReloadSession.

@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterCronReloadSession() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = createSession(kbase);
    // must advance time or it won't save.
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // if we do not call 'ksession.fireAllRules()', this test will run successfully.
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // build timer rule, if the rule is fired, the list size will increase every 300ms
    String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + "   timer (cron: * * * * * ?) \n" + "when \n" + "then \n" + "        list.add(list.size()); \n" + " end";
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    ksession.setGlobal("list", list);
    ksession.setGlobal("list", list);
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    Assert.assertEquals(1, list.size());
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(3, TimeUnit.SECONDS);
    Assert.assertEquals(4, list.size());
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(2, TimeUnit.SECONDS);
    // if the rule is fired, the list size will greater than one.
    Assert.assertEquals(6, list.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 72 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class InterpretedRuleUnit method internalCreateInstance.

@Override
public RuleUnitInstance<T> internalCreateInstance(T data) {
    KnowledgeBuilder kBuilder = new KnowledgeBuilderImpl();
    Class<? extends RuleUnitData> wmClass = data.getClass();
    String canonicalName = wmClass.getCanonicalName();
    // transform foo.bar.Baz to /foo/bar/Baz.drl
    // this currently only works for single files
    InputStream resourceAsStream = wmClass.getResourceAsStream(String.format("/%s.drl", canonicalName.replace('.', '/')));
    kBuilder.add(new InputStreamResource(resourceAsStream), ResourceType.DRL);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(kBuilder.getKnowledgePackages());
    ReteEvaluator reteEvaluator = new RuleUnitExecutorImpl(kBase);
    return new InterpretedRuleUnitInstance<>(this, data, reteEvaluator);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) RuleUnitExecutorImpl(org.drools.ruleunits.impl.sessions.RuleUnitExecutorImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) InputStream(java.io.InputStream) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InputStreamResource(org.drools.core.io.impl.InputStreamResource)

Example 73 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.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);
    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
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    session = 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));
    assertTrue(results.contains(bob2.getObject()));
    assertTrue(results.contains(bob1.getObject()));
    // serialize session
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("test_Dynamic1_2.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    session = 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));
    assertTrue(results.contains(addr2.getObject()));
    assertTrue(results.contains(addr1.getObject()));
    // serialize session
    session = 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));
    assertTrue(results.contains(addr4.getObject()));
    assertTrue(results.contains(addr3.getObject()));
    // serialize session
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.dispose();
}
Also used : Address(org.drools.mvel.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 74 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class MarshallingTest method testSerializeAddRemove_NoClassDefFoundError.

/*
     * I have tried both the scenarios
     * 1. Remove a rule from a pkg.
     * 2. Remove a pkg
     *
     * But both cases after inserting associated data points (i.e data points which are used to fire/activate the removed rule)
     * session.fireAllRules() is throwing NoClassDefFoundError
     */
@Test
public void testSerializeAddRemove_NoClassDefFoundError() 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);
    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
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    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(3, results.size());
    assertEquals(stilton2.getObject(), results.get(0));
    assertTrue(results.contains(bob1.getObject()));
    assertTrue(results.contains(bob2.getObject()));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    // CASE 1: remove rule
    kBase.removeRule("org.drools.compiler.test", "like stilton");
    InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(2, results.size());
    assertTrue(results.contains(bob3.getObject()));
    assertTrue(results.contains(bob4.getObject()));
    // deserialize the session and test it
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    // CASE 2: remove pkg
    kBase.removeKiePackage("org.drools.compiler.test");
    InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(2, results.size());
    assertTrue(results.contains(bob5.getObject()));
    assertTrue(results.contains(bob6.getObject()));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    results.clear();
    session.setGlobal("results", results);
    InternalFactHandle stilton5 = (InternalFactHandle) session.insert(new Cheese("stilton", 30));
    InternalFactHandle brie5 = (InternalFactHandle) session.insert(new Cheese("brie", 30));
    InternalFactHandle bob7 = (InternalFactHandle) session.insert(new Person("bob", 30));
    InternalFactHandle bob8 = (InternalFactHandle) session.insert(new Person("bob", 40));
    session.fireAllRules();
    assertEquals(2, results.size());
    assertTrue(results.contains(bob7.getObject()));
    assertTrue(results.contains(bob8.getObject()));
    session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 75 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase 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);
    } finally {
        unsetPrivateKeyProperties();
        unsetPublicKeyProperties();
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)268 Test (org.junit.Test)213 KieSession (org.kie.api.runtime.KieSession)136 ArrayList (java.util.ArrayList)99 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)98 KiePackage (org.kie.api.definition.KiePackage)82 List (java.util.List)55 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)39 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)34 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)33 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.mvel.compiler.Cheese)24 FactType (org.kie.api.definition.type.FactType)23 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)21 BetaMemory (org.drools.core.reteoo.BetaMemory)18 PackageDescr (org.drools.drl.ast.descr.PackageDescr)18 Resource (org.kie.api.io.Resource)18 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)17