Java Code Examples for org.kie.api.KieBaseConfiguration#setOption()

The following examples show how to use org.kie.api.KieBaseConfiguration#setOption() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FireUntilHaltAccumulateTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( ResourceFactory.newByteArrayResource( drl.getBytes() ), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors().toString());
    }
    final KieBaseConfiguration config =
            KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption( EventProcessingOption.STREAM);

    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config);
    kbase.addPackages(kbuilder.getKnowledgePackages());

    final KieSessionConfiguration sessionConfig =
            KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ));

    this.statefulSession = kbase.newKieSession(sessionConfig, null);
    this.stockFactory = new StockFactory(kbase);
}
 
Example 2
Source File: StatefulSessionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFactHandleEqualityBehavior() throws Exception {
    final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbc.setOption(EqualityBehaviorOption.EQUALITY);
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc));
    final KieSession ksession = createKnowledgeSession(kbase);

    final CheeseEqual cheese = new CheeseEqual("stilton", 10);
    ksession.insert(cheese);
    final FactHandle fh = ksession.getFactHandle(new CheeseEqual("stilton", 10));
    assertNotNull(fh);
}
 
Example 3
Source File: DeclarativeAgendaTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleBlockingUsingForall() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) salience -100 \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule2 salience 200\n";
    str += "when \n";        
    str += "     $s : String( this == 'go1' ) \n";
    str += "     exists  Match( department == 'sales' ) \n";  
    str += "     forall ( $a : Match( department == 'sales' ) Match( this == $a, active == false ) ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption( DeclarativeAgendaOption.ENABLED );
    KieBase kbase = loadKnowledgeBaseFromString( kconf, str );
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal( "list",
                        list );
    ksession.insert( "go1" );
    ksession.fireAllRules();

    assertEquals( 2,
                  list.size() );
    assertTrue( list.contains("rule1:go1") );
    assertTrue( list.contains("rule2:go1") );

    ksession.dispose();
}
 
Example 4
Source File: DeclarativeAgendaTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public KieSession getStatefulKnowledgeSession() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";

    str += "rule rule1 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";

    str += "rule blockerAllSalesRules @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption( DeclarativeAgendaOption.ENABLED );
    KieBase kbase = loadKnowledgeBaseFromString( kconf, str );
    KieSession ksession = createKnowledgeSession(kbase);

    return ksession;
}
 
Example 5
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarshallWithNot() throws Exception {
    String str =
            "import " + getClass().getCanonicalName() + ".*\n" +
                    "rule one\n" +
                    "when\n" +
                    "   A()\n" +
                    "   not(B())\n" +
                    "then\n" +
                    "System.out.println(\"a\");\n" +
                    "end\n" +
                    "\n" +
                    "rule two\n" +
                    "when\n" +
                    "   A()\n" +
                    "then\n" +
                    "System.out.println(\"b\");\n" +
                    "end\n";

    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption( EventProcessingOption.STREAM );

    KieBase kBase = loadKnowledgeBaseFromString(config, str);
    KieSession ksession = kBase.newKieSession();
    ksession.insert( new A() );
    MarshallerFactory.newMarshaller( kBase ).marshall( new ByteArrayOutputStream(), ksession );
}
 
Example 6
Source File: KieHelper.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public KieBase build(KieBaseOption... options) {
    KieContainer kieContainer = getKieContainer();
    if (options == null || options.length == 0) {
        return kieContainer.getKieBase();
    }
    KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
    for (KieBaseOption option : options) {
        kieBaseConf.setOption(option);
    }

    return kieContainer.newKieBase(kieBaseConf);
}
 
Example 7
Source File: PseudoClockEventsTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private int processStocks(int stockCount, AgendaEventListener agendaEventListener, String drlContentString)
        throws Exception {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString);

    KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
    ksessionConfig.setProperty("keep.reference", "true");
    final KieSession ksession = kbase.newKieSession(ksessionConfig, null);
    ksession.addEventListener(agendaEventListener);

    PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();

    Thread fireUntilHaltThread = new Thread(ksession::fireUntilHalt, "Engine's thread");
    fireUntilHaltThread.start();
    try {
        Thread.currentThread().setName("Feeding thread");

        for (int stIndex = 1; stIndex <= stockCount; stIndex++) {
            clock.advanceTime(20, TimeUnit.SECONDS);
            Thread.sleep( 100 );
            final StockTickInterface st = new StockTick(stIndex,
                                                        "RHT",
                                                        100 * stIndex,
                                                        100 * stIndex);
            ksession.insert(st);
            Thread.sleep( 100 );
        }

        Thread.sleep(100);
    } finally {
        ksession.halt();
        ksession.dispose();
    }
    
    fireUntilHaltThread.join(5000);

    return stockCount;
}
 
Example 8
Source File: DeclarativeAgendaTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasicBlockOnAnnotation() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule2 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule3 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption( DeclarativeAgendaOption.ENABLED );
    KieBase kbase = loadKnowledgeBaseFromString( kconf, str );
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal( "list",
                        list );
    ksession.insert( "go1" );
    FactHandle go2 = ksession.insert( "go2" );        
    ksession.fireAllRules();
    
    assertEquals( 3,
                  list.size() );
    assertTrue( list.contains( "rule1:go2" ) );
    assertTrue( list.contains( "rule2:go2" ) );
    assertTrue( list.contains( "rule3:go2" ) );

    list.clear();
    ksession.retract( go2 );
    ksession.fireAllRules();

    assertEquals( 3,
                  list.size() );
    assertTrue( list.contains( "rule1:go1" ) );
    assertTrue( list.contains( "rule2:go1" ) );
    assertTrue( list.contains( "rule3:go1" ) );

    ksession.dispose();
}
 
Example 9
Source File: TruthMaintenanceTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpdate() {
    String droolsSource =
            "package org.drools.tms.test; \n" +
            " declare Feat1\n" +
            "   context : String @key\n" +
            "   value : double @key\n" +
            "   missing : boolean  = false\n" +
            " \n" +
            " end\n" +
            " \n" +
            " \n" +
            "rule \"InitAsMissing_Test_MLP_Feat1\"\n" +
            "when\n" +
            "    not Feat1( context == null )\n" +
            "then\n" +
            "    insertLogical( new Feat1( \"Test_MLP\", 0.0, true ) );\n" +
            "end\n" +
            " \n" +
            "rule \"miningFieldMissing_Test_MLP_Feat1\"\n" +
            "when\n" +
            "     $x : Feat1( $m : missing == true, context == \"Test_MLP\" )\n" +
            "then\n" +
            "    modify ( $x ) {\n" +
            "        setValue( 3.95 ),\n" +
            "        setMissing( false );\n" +
            "    }\n" +
            "end\n" +
            " \n" +
            "rule \"OverrideInput_Feat1\"\n" +
            "when\n" +
            "    $old: Feat1( value != 4.33 )\n" +
            "then\n" +
            "    retract( $old );\n" +
            "end\n" +
            " \n" +
            " \n" +
            "rule \"Input_Feat1\"\n" +
            "when\n" +
            "    not Feat1(  context == null )\n" +
            "then\n" +
            "    insert( new Feat1( null, 4.33 ) );\n" +
            "end"
            ;

    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption( EqualityBehaviorOption.IDENTITY );
    KieBase kbase = loadKnowledgeBaseFromString( kieConf, droolsSource );
    KieSession session = kbase.newKieSession();
    try {
        try {
            session.fireAllRules();
            fail("Currently we cannot handle updates for a belief set that is mixed stated and justified" );
        } catch ( ConsequenceException e) {
            assertEquals(IllegalStateException.class, e.getCause().getClass());
        }
    } finally {
        session.dispose();
    }
}
 
Example 10
Source File: DeclarativeAgendaTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testExplicitUndercutWithDeclarativeAgenda() {

    String drl = "package org.drools.test;\n" +
                 "\n" +
                 "import org.kie.api.runtime.rule.Match;\n" +
                 "\n" +
                 "global java.util.List list;\n" +
                 "\n" +
                 "declare Foo\n" +
                 "    type  : String\n" +
                 "    value : double\n" +
                 "end\n" +
                 "\n" +
                 "declare Bar\n" +
                 "    type  : String\n" +
                 "    total : double\n" +
                 "end\n" +
                 "\n" +
                 "\n" +
                 "rule \"Init\"\n" +
                 "when\n" +
                 "then\n" +
                 "    insert( new Foo( \"first\", 10 ) );\n" +
                 "    insert( new Foo( \"first\", 11 ) );\n" +
                 "    insert( new Foo( \"second\", 20 ) );\n" +
                 "    insert( new Foo( \"second\", 22 ) );\n" +
                 "    insert( new Foo( \"third\", 30 ) );\n" +
                 "    insert( new Foo( \"third\", 40 ) );\n" +
                 "end\n" +
                 "\n" +
                 "rule \"Accumulate\"\n" +
                 "salience 100\n" +
                 "dialect \"mvel\"\n" +
                 "  when\n" +
                 "    $type : String() from [ \"first\", \"second\", \"third\" ]\n" +
                 "    accumulate ( Foo( type == $type, $value : value ),\n" +
                 "                 $total : sum( $value );\n" +
                 "                 $total > 0 )\n" +
                 "  then\n" +
                 "    insert(new Bar($type, $total));\n" +
                 "end\n" +
                 "\n" +
                 "rule \"handle all Bars of type first\"\n" +
                 "@Undercuts( others )\n" +
                 "  when\n" +
                 "    $bar : Bar( type == 'first', $total : total )\n" +
                 "  then\n" +
                 "    System.out.println( \"First bars \" + $total );\n" +
                 "    list.add( $total );\n" +
                 "end\n" +
                 "\n" +
                 "rule \"handle all Bars of type second\"\n" +
                 "@Undercuts( others )\n" +
                 "  when\n" +
                 "    $bar : Bar( type == 'second', $total : total )\n" +
                 "  then\n" +
                 "    System.out.println( \"Second bars \" + $total );\n" +
                 "    list.add( $total );\n" +
                 "end\n" +
                 "\n" +
                 "rule \"others\"\n" +
                 "  when\n" +
                 "    $bar : Bar( $total : total )\n" +
                 "  then\n" +
                 "    System.out.println( \"Other bars \" + $total );\n" +
                 "    list.add( $total );\n" +
                 "end\n" +
                 "\n" +
                 "\n" +
                 "rule \"Undercut\"\n" +
                 "@Direct \n" +
                 "when\n" +
                 "    $m : Match( $handles : factHandles )\n" +
                 "    $v : Match( rule.name == $m.Undercuts, factHandles == $handles )\n" +
                 "then\n" +
                 "    System.out.println( \"Activation of rule \" + $m.getRule().getName() + \" overrides \" + $v.getRule().getName() + \" for tuple \" + $handles );\n" +
                 "    kcontext.cancelMatch( $v );\n" +
                 "end\n" +
                 "\n" +
                 "\n";

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption( DeclarativeAgendaOption.ENABLED );
    KieBase kbase = loadKnowledgeBaseFromString( kconf, drl );
    KieSession ksession = createKnowledgeSession(kbase);

    List list = new ArrayList();
    ksession.setGlobal( "list", list );

    ksession.fireAllRules();

    assertEquals( Arrays.asList( 21.0, 42.0, 70.0 ), list );

    ksession.dispose();
}
 
Example 11
Source File: MVELTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testMVELTypeCoercion() {
    final String str = "package org.drools.compiler.test; \n" +
            "\n" +
            "global java.util.List list;" +
            "\n" +
            "declare Bean\n" +
            // NOTICE: THIS WORKS WHEN THE FIELD IS "LIST", BUT USED TO WORK WITH ARRAYLIST TOO
            "  field : java.util.ArrayList\n" +
            "end\n" +
            "\n" +
            "\n" +
            "rule \"Init\"\n" +
            "when  \n" +
            "then\n" +
            "  insert( new Bean( new java.util.ArrayList( java.util.Arrays.asList( \"x\" ) ) ) );\n" +
            "end\n" +
            "\n" +
            "rule \"Check\"\n" +
            "when\n" +
            "  $b : Bean( $fld : field == [\"x\"] )\n" +
            "then\n" +
            "  System.out.println( $fld );\n" +
            "  list.add( \"OK\" ); \n" +
            "end";

    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    final KieBaseConfiguration kbConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbConf.setOption(EqualityBehaviorOption.EQUALITY);
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbConf);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    final KieSession ksession = kbase.newKieSession();

    final List list = new ArrayList();
    ksession.setGlobal("list", list);

    ksession.fireAllRules();
    assertTrue(list.contains("OK"));

    ksession.dispose();
}
 
Example 12
Source File: TruthMaintenanceTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testLogicalThenStatedShadowSingleOccurance() {
    String droolsSource =
            "package org.drools.tms.test; \n" +

            "global java.util.List list; \n" +

            "rule Justify \n" +
            "when \n" +
            "    String( this == 'go1' ) " +
            "then \n" +
            "    insertLogical( 'f1' ); \n" +
            "end \n" +


            "rule StillHere \n" +
            "when \n" +
            "    String( this == 'go2' ) " +
            "    s : String( this == 'f1' ) " +
            "then \n" +
            "    list.add( s ); \n" +
            "end \n" +
            ""
            ;

    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption( EqualityBehaviorOption.IDENTITY );
    KieBase kbase = loadKnowledgeBaseFromString( kieConf, droolsSource );
    KieSession session = kbase.newKieSession();
    try {
        List list = new ArrayList();
        session.setGlobal("list", list);

        session.insert( "go1" );
        session.fireAllRules();

        TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl)session).getTruthMaintenanceSystem();
        InternalFactHandle jfh1 = tms.get( "f1" ).getLogicalFactHandle();
        assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());

        InternalFactHandle fh1 = (InternalFactHandle) session.insert( "f1" );
        InternalFactHandle fh2 = (InternalFactHandle) session.insert( "f2" );

        session.insert("go2");
        session.fireAllRules();

        assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
        assertSame( fh1.getEqualityKey(), jfh1.getEqualityKey() );
        assertNotSame( fh1, jfh1 );

        EqualityKey key = jfh1.getEqualityKey();
        assertSame( fh1.getEqualityKey(), key );
        assertNotSame( fh1, jfh1 );

        assertEquals(2, key.size());
        assertSame( jfh1,  key.getLogicalFactHandle() );

        // Make sure f1 only occurs once
        assertEquals(1, list.size());
        assertEquals("f1", list.get(0 ));
    } finally {
        session.dispose();
    }
}
 
Example 13
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test @Disabled("beta4 phreak")
public void testMarshallEntryPointsWithSlidingTimeWindow() throws Exception {
    String str =
            "package org.domain.test \n" +
                    "import " + getClass().getCanonicalName() + ".*\n" +
                    "import java.util.List\n" +
                    "global java.util.List list\n" +
                    "declare A\n" +
                    " @role( event )\n" +
                    " @expires( 10m )\n" +
                    "end\n" +
                    "declare B\n" +
                    "" +
                    " @role( event )\n" +
                    " @expires( 10m )\n" +
                    "end\n" +
                    "" +
                    "rule a1\n" +
                    "when\n" +
                    "   $l : List() from collect( A()  over window:time(30s) from entry-point 'a-ep') \n" +
                    "then\n" +
                    "   list.add( $l );" +
                    "end\n";

    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption( EventProcessingOption.STREAM );
    final KieBase kbase = loadKnowledgeBaseFromString( conf, str );

    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption( ClockTypeOption.get( "pseudo" ) );
    ksconf.setOption( TimerJobFactoryOption.get("trackable") );
    KieSession ksession = createKnowledgeSession(kbase, ksconf);

    List list = new ArrayList();
    ksession.setGlobal( "list", list );

    EntryPoint aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession( ksession );
    assertEquals( 2, ((List) list.get( 0 )).size() );

    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock();
    timeService.advanceTime( 15, TimeUnit.SECONDS );
    ksession = marsallStatefulKnowledgeSession( ksession );

    aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession( ksession );
    assertEquals( 4, ((List) list.get( 0 )).size() );

    timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock();
    timeService.advanceTime( 20, TimeUnit.SECONDS );
    ksession = marsallStatefulKnowledgeSession( ksession );

    list.clear();
    ksession.fireAllRules();
    assertEquals( 2, ((List) list.get( 0 )).size() );
}
 
Example 14
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testMarshallEntryPointsWithSlidingLengthWindow() throws Exception {
    String str =
            "package org.domain.test \n" +
                    "import " + getClass().getCanonicalName() + ".*\n" +
                    "import java.util.List\n" +
                    "global java.util.List list\n" +
                    "declare A\n" +
                    " @role( event )\n" +
                    " @expires( 10m )\n" +
                    "end\n" +
                    "declare B\n" +
                    "" +
                    " @role( event )\n" +
                    " @expires( 10m )\n" +
                    "end\n" +
                    "" +
                    "rule a1\n" +
                    "when\n" +
                    "   $l : List() from collect( A()  over window:length(3) from entry-point 'a-ep') \n" +
                    "then\n" +
                    "   list.add( $l );" +
                    "end\n";

    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption( EventProcessingOption.STREAM );
    final KieBase kbase = loadKnowledgeBaseFromString( conf, str );

    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption( ClockTypeOption.get( "pseudo" ) );
    ksconf.setOption( TimerJobFactoryOption.get("trackable") );
    KieSession ksession = createKnowledgeSession(kbase, ksconf);

    List list = new ArrayList();
    ksession.setGlobal( "list", list );

    EntryPoint aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession( ksession );
    assertEquals( 2, ((List) list.get( 0 )).size() );

    aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    aep = ksession.getEntryPoint( "a-ep" );
    aep.insert( new A() );
    ksession = marsallStatefulKnowledgeSession( ksession );

    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession( ksession );
    assertEquals( 3, ((List) list.get( 0 )).size() );
}
 
Example 15
Source File: DeclarativeAgendaTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleBlockersWithUnblockAll() {
    // This test is a bit wierd as it recurses. Maybe unblockAll is not feasible...
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";

    str += "rule rule0 @Propagation(EAGER) @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go0' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";

    str += "rule blockerAllSalesRules1 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";

    str += "rule blockerAllSalesRules2 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";

    str += "rule blockerAllSalesRules3 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go3' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";

    str += "rule unblockAll @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go4' ) \n";
    str += "     $i : Match( department == 'sales', active == true ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.unblockAllMatches( $i ); \n";
    str += "end \n";

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption( DeclarativeAgendaOption.ENABLED );
    KieBase kbase = loadKnowledgeBaseFromString( kconf, str );
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal( "list",
                        list );
    FactHandle go0 = ksession.insert( "go0" );
    FactHandle go1 = ksession.insert( "go1" );
    FactHandle go2 = ksession.insert( "go2" );
    FactHandle go3 = ksession.insert( "go3" );

    ksession.fireAllRules();
    assertEquals( 3,
                  list.size() );
    System.out.println( list );
    assertTrue( list.contains( "blockerAllSalesRules1:rule0:go1" ) );
    assertTrue( list.contains( "blockerAllSalesRules2:rule0:go2" ) );
    assertTrue( list.contains( "blockerAllSalesRules3:rule0:go3" ) );

    list.clear();

    FactHandle go4 = ksession.insert( "go4" );
    ksession.fireAllRules();
    System.out.println( list );
    assertEquals( 5,
                  list.size() );

    assertTrue( list.contains( "unblockAll:rule0:go4" ) );
    assertTrue( list.contains( "rule0:go0" ) );
    assertTrue( list.contains( "blockerAllSalesRules1:rule0:go1" ) );
    assertTrue( list.contains( "blockerAllSalesRules2:rule0:go2" ) );
    assertTrue( list.contains( "blockerAllSalesRules3:rule0:go3" ) );
}
 
Example 16
Source File: MultithreadTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testRaceOnAccumulateNodeSimple() throws InterruptedException {

    final String drl = "package org.drools.integrationtests;\n" +
            "" +
            "import " + Server.class.getCanonicalName() + ";\n" +
            "import " + IntEvent.class.getCanonicalName() + ";\n" +
            "" +
            "declare IntEvent\n" +
            "  @role ( event )\n" +
            "  @expires( 15s )\n" +
            "end\n" +
            "\n" +
            "" +
            "rule \"average temperature\"\n" +
            "when\n" +
            "  $s : Server (hostname == \"hiwaesdk\")\n" +
            " $avg := Number( ) from accumulate ( " +
            "      IntEvent ( $temp : data ) over window:length(10) from entry-point ep01; " +
            "      average ($temp)\n" +
            "  )\n" +
            "then\n" +
            "  $s.avgTemp = $avg.intValue();\n" +
            "  System.out.println( $avg );\n" +
            "end\n" +
            "\n";

    final KieBaseConfiguration kbconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconfig.setOption(EventProcessingOption.STREAM);

    final KieBase kbase = loadKnowledgeBaseFromString(kbconfig, drl);

    final KieSession session = kbase.newKieSession();
    final EntryPoint ep01 = session.getEntryPoint("ep01");

    final Runner t = new Runner(session);
    t.start();
    try {
        Thread.sleep(1000);

        final Server hiwaesdk = new Server("hiwaesdk");
        session.insert(hiwaesdk);
        final long LIMIT = 20;

        for (long i = LIMIT; i > 0; i--) {
            ep01.insert(new IntEvent((int) i)); //Thread.sleep (0x1); }
            if (i % 1000 == 0) {
                System.out.println(i);
            }
        }
        Thread.sleep(1000);
    } finally {
        session.halt();
        session.dispose();
    }

    if (t.getError() != null) {
        Assertions.fail(t.getError().getMessage());
    }
}
 
Example 17
Source File: AbductionTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
@Disabled( "Not implemented yet" )
public void testBacktracking() {
    String droolsSource =
            "package org.drools.abductive.test; \n" +
            "import org.kie.api.runtime.rule.Match;\n" +
            "" +

            "declare Foo " +
            "@Abducible " +
            "   id : Integer @key " +
            "end " +

            "query bar( Integer $id ) " +
            "   @Abductive( target=Foo.class, backtracking=true ) " +
            "   $id := Integer() " +
            "end " +

            "rule Check " +
            "when " +
            "   bar( $i ; ) " +
            "then " +
            "   System.out.println( 'YES ' + $i ); " +
            "end " +

            "rule Check2 " +
            "when " +
            "   bar( $i ; ) " +
            "then " +
            "   System.out.println( 'HAH ' + $i ); " +
            "end " +

            "rule Init " +
            "when " +
            "then" +
            "   insert( new Integer( 1 ) ); " +
            "   insert( new Integer( 2 ) ); " +
            "end  " +

            "";

    /////////////////////////////////////

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption( DeclarativeAgendaOption.ENABLED );

    KieSession session = getSessionFromString( droolsSource, kconf );

    session.fireAllRules();

    for ( Object o : session.getObjects() ) {
        System.out.println( ">>> " + o );
    }


}
 
Example 18
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testMarshallEntryPointsWithNot() 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( 10m )\n" +
                    "end\n" +
                    "declare B\n" +
                    "" +
                    " @role( event )\n" +
                    " @expires( 10m )\n" +
                    "end\n" +
                    "" +
                    "rule a1\n" +
                    "when\n" +
                    "   $a : A() from entry-point 'a-ep'\n" +
                    "   not B( this after[0s, 10s] $a) from entry-point 'a-ep'\n" +
                    "then\n" +
                    "list.add( $a );" +
                    "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 );

    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock> getSessionClock();
    timeService.advanceTime( 3, TimeUnit.SECONDS );

    ksession = marsallStatefulKnowledgeSession( ksession );

    ksession.fireAllRules();

    ksession = marsallStatefulKnowledgeSession( ksession );

    assertEquals( 0,
                  list.size() );
}
 
Example 19
Source File: DeclarativeAgendaTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testActiveInActiveChanges() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule2 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule3 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule countActivateInActive @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $active : Number( this == 1 ) from accumulate( $a : Match( department == 'sales', active == true ), count( $a ) )\n";
    str += "     $inActive : Number( this == 2 ) from  accumulate( $a : Match( department == 'sales', active == false ), count( $a ) )\n";
    str += "then \n";
    str += "    list.add( $active + ':' + $inActive  ); \n";
    str += "    kcontext.halt( ); \n";
    str += "end \n";

    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption( DeclarativeAgendaOption.ENABLED );
    KieBase kbase = loadKnowledgeBaseFromString( kconf, str );
    KieSession ksession = createKnowledgeSession(kbase);

    List list = new ArrayList();
    ksession.setGlobal( "list",
                        list );
    ksession.insert( "go1" );
    FactHandle go2 = ksession.insert( "go2" );
    ksession.fireAllRules();

    assertEquals( 3,
                  list.size() );
    System.out.println( list );
    assertTrue( list.contains( "1:2" ) );
    assertTrue( list.contains( "rule1:go1" ) );
    assertTrue( list.contains( "rule2:go1" ) );

    ksession.dispose();
}
 
Example 20
Source File: NullTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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();
}