Search in sources :

Example 11 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class TimerAndCalendarTest method disposeAndReloadSession.

private KieSession disposeAndReloadSession(KieSession ksession, KieBase kbase) {
    long ksessionId = ksession.getIdentifier();
    ksession.dispose();
    final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    StatefulKnowledgeSession newksession = JPAKnowledgeService.loadStatefulKnowledgeSession(ksessionId, kbase, conf, createEnvironment(context));
    return newksession;
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Example 12 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class TimerAndCalendarTest method testTimerWithRemovingRule.

@Test
public void testTimerWithRemovingRule() throws Exception {
    // DROOLS-576
    // Only reproducible with RETEOO
    InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
    String str1 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R1\n" + "    timer ( int: 5s )\n" + "when\n" + "    $s : String( )\n" + "then\n" + "    list.add( $s );\n" + "end\n";
    Resource resource1 = ResourceFactory.newByteArrayResource(str1.getBytes());
    Collection<KiePackage> kpackages1 = buildKnowledgePackage(resource1, ResourceType.DRL);
    kbase1.addPackages(kpackages1);
    StatefulKnowledgeSession ksession1 = JPAKnowledgeService.newStatefulKnowledgeSession(kbase1, null, createEnvironment(context));
    long ksessionId = ksession1.getIdentifier();
    ArrayList<String> list = new ArrayList<String>();
    ksession1.setGlobal("list", list);
    ksession1.insert("hello");
    ksession1.fireAllRules();
    // dispose before firing
    ksession1.dispose();
    Assert.assertEquals(0, list.size());
    Thread.sleep(5000);
    // A new kbase without the timer's activated rule
    InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
    String str2 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R2\n" + "when\n" + "    $s : Integer( )\n" + "then\n" + "    list.add( $s );\n" + "end\n";
    Resource resource2 = ResourceFactory.newByteArrayResource(str2.getBytes());
    Collection<KiePackage> kpackages2 = buildKnowledgePackage(resource2, ResourceType.DRL);
    kbase2.addPackages(kpackages2);
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(ksessionId, kbase2, null, createEnvironment(context));
    ksession2.setGlobal("list", list);
    ksession2.fireAllRules();
    ksession2.dispose();
    Assert.assertEquals(0, list.size());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 13 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class TransactionTestCommand method execute.

public Void execute(Context context) {
    em.joinTransaction();
    em.persist(mainObject);
    if (subObject != null) {
        KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
        // THe following 3 lines are the important ones! (See below for an explanation)
        InternalKnowledgeBase cleanKBase = KnowledgeBaseFactory.newKnowledgeBase();
        cleanKBase.addPackages(ksession.getKieBase().getKiePackages());
        StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession(cleanKBase, null, initializeEnvironment());
        /**
         *  Here's what's going on:
         *  If the PersistableRunner (SSCS) & JtaTransactionManager (JTM) were _not_ aware of transactions,
         *  -> then inserting the mainObject _before_ having inserted the subObject
         *     would cause the operation/persist to fail and the transaction to fail.
         *     - This is because the mainObject contains a foreign key referring to the subObject.
         *
         *  However, the SSCS & JTM check whether or not they're owners of the transaction
         *  when starting and when committing the transaction they use.
         *  -> So that when we insert the mainObject here (via a _new_ CommandBasedStatefulKnowledgeSession),
         *     it does _not_ mess with the transaction state and the operation succeeds.
         */
        TransactionTestCommand transactionTestSubCommand = new TransactionTestCommand(this.subObject, getPersistenceEnvironment());
        commandKSession.execute(transactionTestSubCommand);
    }
    return null;
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 14 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class JpaPersistenceTraitTest method testMapBasedTraitsWithJPA.

@Test
public void testMapBasedTraitsWithJPA() {
    String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "" + "declare TBean2 \n" + "  @propertyReactive \n" + "  @Traitable \n" + "  fld : String \n" + "end \n " + "" + "declare trait Mask2 \n" + "  @propertyReactive \n" + "  fld : String \n" + "  xyz : int  \n" + "end \n" + "\n " + "declare trait Cloak2 \n" + "  @propertyReactive \n" + "  fld : String \n" + "  ijk : String  \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + "  insert( new TBean2(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + "  $b : TBean2( ) \n" + "then \n" + "  Mask2 m = don( $b, Mask2.class ); \n" + "  modify (m) { setXyz( 10 ); } \n" + "  list.add( m ); \n" + "  System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "rule Don2 \n" + "no-loop \n" + "when \n" + "  $b : TBean2( ) \n" + "then \n" + "  Cloak2 c = don( $b, Cloak2.class ); \n" + "  modify (c) { setIjk( \"ijklmn\" ); } \n" + "  list.add( c ); \n" + "  System.out.println( \"Don result : \" + c ); \n " + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    long id = ksession.getIdentifier();
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession2.fireAllRules();
    Collection x = ksession2.getObjects();
    assertEquals(3, x.size());
    TraitableBean core = null;
    for (Object o : x) {
        if (o instanceof TraitableBean) {
            core = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(core);
    assertEquals(2, core._getDynamicProperties().size());
    assertNotNull(core.getTrait("org.drools.trait.test.Mask2"));
    assertNotNull(core.getTrait("org.drools.trait.test.Cloak2"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 15 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project drools by kiegroup.

the class JpaPersistenceTraitTest method testTraitWithJPAOnFreshKieBase.

@Test
public void testTraitWithJPAOnFreshKieBase() {
    // DROOLS-904
    String str = "package org.drools.trait.test; " + "global java.util.List list; " + "declare TBean2  " + "  @propertyReactive  " + "  @Traitable  " + "end   " + "declare trait Mask " + "  @propertyReactive  " + "end  " + "query getTraits( Mask $m ) " + "  $m := Mask() " + "end " + "rule Init when then don( new TBean2(), Mask.class ); end " + "rule Trig when String() then don( new TBean2(), Mask.class ); end " + "rule Main when $m : Mask() then list.add( $m ); end ";
    List list = new ArrayList();
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    long id = ksession.getIdentifier();
    InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
    kbase2.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase2, null, env);
    ksession.setGlobal("list", list);
    ksession2.insert("go");
    ksession2.fireAllRules();
    assertEquals(2, list.size());
    Class<?> oldProxyClass = list.get(0).getClass();
    Class<?> newProxyClass = list.get(1).getClass();
    assertNotSame(oldProxyClass, newProxyClass);
    QueryResults qry = ksession2.getQueryResults("getTraits", Variable.v);
    assertEquals(2, qry.size());
    java.util.Iterator<QueryResultsRow> iter = qry.iterator();
    int j = 0;
    while (iter.hasNext()) {
        QueryResultsRow row = iter.next();
        Object entry = row.get("$m");
        assertNotNull(entry);
        assertSame(newProxyClass, entry.getClass());
        j++;
    }
    assertEquals(2, j);
    for (Object o : ksession2.getObjects()) {
        if (o.getClass().getName().contains("Mask")) {
            assertSame(newProxyClass, o.getClass());
        }
    }
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) QueryResults(org.kie.api.runtime.rule.QueryResults) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)21 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Collection (java.util.Collection)4 KieBase (org.kie.api.KieBase)4 Environment (org.kie.api.runtime.Environment)4 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 List (java.util.List)3 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)3 KieSession (org.kie.api.runtime.KieSession)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)2 RegistryContext (org.drools.core.command.impl.RegistryContext)2 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1