Search in sources :

Example 81 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class MarshallingTest method testMarshallEntryPointsWithExpires.

@Test
public void testMarshallEntryPointsWithExpires() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10s )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10s )\n" + "end\n" + "" + "declare C\n" + " @role( event )\n" + " @expires( 15s )\n" + "end\n" + "" + "rule a1\n" + "when\n" + "   $a : A() from entry-point 'a-ep'\n" + "then\n" + "list.add( $a );" + "end\n" + "" + "rule b1\n" + "when\n" + "   $b : B() from entry-point 'b-ep'\n" + "then\n" + "list.add( $b );" + "end\n" + "" + "rule c1\n" + "when\n" + "   $c : C() from entry-point 'c-ep'\n" + "then\n" + "list.add( $c );" + "end\n";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kBase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = kBase.newKieSession(ksconf, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    EntryPoint bep = ksession.getEntryPoint("b-ep");
    bep.insert(new B());
    ksession = marsallStatefulKnowledgeSession(ksession);
    EntryPoint cep = ksession.getEntryPoint("c-ep");
    cep.insert(new C());
    ksession = marsallStatefulKnowledgeSession(ksession);
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(3, list.size());
    aep = ksession.getEntryPoint("a-ep");
    assertEquals(1, aep.getFactHandles().size());
    bep = ksession.getEntryPoint("b-ep");
    assertEquals(1, bep.getFactHandles().size());
    cep = ksession.getEntryPoint("c-ep");
    assertEquals(1, cep.getFactHandles().size());
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(11, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    assertEquals(0, aep.getFactHandles().size());
    bep = ksession.getEntryPoint("b-ep");
    assertEquals(0, bep.getFactHandles().size());
    cep = ksession.getEntryPoint("c-ep");
    assertEquals(1, cep.getFactHandles().size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactA(org.drools.compiler.FactA) FactB(org.drools.compiler.FactB) FactC(org.drools.compiler.FactC) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 82 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class Misc2Test method testMatchingEventsInStreamMode.

@Test
public void testMatchingEventsInStreamMode() {
    // DROOLS-338
    String drl = "import org.drools.compiler.integrationtests.Misc2Test.SimpleEvent\n" + "declare SimpleEvent\n" + "    @role(event)\n" + "end\n" + "\n" + "rule \"RuleA\"\n" + "salience 5\n" + "when\n" + "    $f : SimpleEvent( )\n" + "then\n" + "    delete ($f);\n" + "end\n" + "\n" + "rule \"RuleB\"\n" + "when\n" + "    $f : SimpleEvent( )\n" + "then\n" + "end\n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    final AtomicInteger i = new AtomicInteger(0);
    ksession.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            i.incrementAndGet();
        }

        public void matchCancelled(MatchCancelledEvent event) {
            i.decrementAndGet();
        }
    });
    ksession.insert(new SimpleEvent());
    ksession.fireAllRules();
    assertEquals(1, i.get());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Test(org.junit.Test)

Example 83 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class Misc2Test method testLegacySalienceResolver.

@Test
public void testLegacySalienceResolver() {
    // DROOLS-159
    String drl = "package org.drools.test; \n" + "" + "global java.util.List list; \n " + "" + "rule X salience 10 \n" + "then\n" + " list.add( 1 ); \n" + "end\n" + "" + "rule Y salience 5 \n" + "then\n" + " list.add( 2 ); \n" + "end\n" + "";
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    assertFalse(kb.hasErrors());
    KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    ((RuleBaseConfiguration) kbconf).setConflictResolver(SalienceConflictResolver.getInstance());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
    knowledgeBase.addPackages(kb.getKnowledgePackages());
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    List list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.fireAllRules();
    assertEquals(Arrays.asList(1, 2), list);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 84 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class NullTest method testBindingToNullFieldWithEquality.

@Test
public void testBindingToNullFieldWithEquality() {
    // JBRULES-3396
    final String str = "package org.drools.compiler.test; \n" + "\n" + "global java.util.List list;" + "\n" + "declare Bean\n" + "  id    : String @key\n" + "  field : String\n" + "end\n" + "\n" + "\n" + "rule \"Init\"\n" + "when  \n" + "then\n" + "  insert( new Bean( \"x\" ) );\n" + "end\n" + "\n" + "rule \"Check\"\n" + "when\n" + "  $b : Bean( $fld : field )\n" + "then\n" + "  System.out.println( $fld );\n" + "  list.add( \"OK\" ); \n" + "end";
    final KieBaseConfiguration kbConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbConf.setOption(EqualityBehaviorOption.EQUALITY);
    final KieBase kbase = loadKnowledgeBaseFromString(kbConf, str);
    final KieSession ksession = kbase.newKieSession();
    final java.util.List list = new java.util.ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertTrue(list.contains("OK"));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) List(java.util.List) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 85 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class TruthMaintenanceTest method testPrimeJustificationWithEqualityMode.

@Test(timeout = 10000)
public void testPrimeJustificationWithEqualityMode() {
    String droolsSource = "package org.drools.tms.test; \n" + "declare Bar end \n" + "" + "declare Holder x : Bar end \n" + "" + "" + "rule Init \n" + "when \n" + "then \n" + "   insert( new Holder( new Bar() ) ); \n" + "end \n" + "rule Justify \n" + "when \n" + " $s : Integer() \n" + " $h : Holder( $b : x ) \n" + "then \n" + " insertLogical( $b ); \n" + "end \n" + "rule React \n" + "when \n" + " $b : Bar(  ) \n" + "then \n" + " System.out.println( $b );  \n" + "end \n";
    // ///////////////////////////////////
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.EQUALITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    FactHandle handle1 = session.insert(10);
    FactHandle handle2 = session.insert(20);
    assertEquals(4, session.fireAllRules());
    session.delete(handle1);
    assertEquals(0, session.fireAllRules());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)124 Test (org.junit.Test)94 KieSession (org.kie.api.runtime.KieSession)77 KieBase (org.kie.api.KieBase)69 ArrayList (java.util.ArrayList)54 List (java.util.List)38 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)25 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 FactHandle (org.kie.api.runtime.rule.FactHandle)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)18 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 StockTick (org.drools.compiler.StockTick)11 KieContainer (org.kie.api.runtime.KieContainer)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 Match (org.kie.api.runtime.rule.Match)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7