Java Code Examples for org.kie.internal.utils.KieHelper#build()

The following examples show how to use org.kie.internal.utils.KieHelper#build() . 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: AbductionTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
protected KieSession getSessionFromString( String drlString, KieBaseConfiguration kbConf ) {
    KieHelper kieHelper = new KieHelper();
    kieHelper.addContent( drlString, ResourceType.DRL );

    Results res = kieHelper.verify();
    if ( res.hasMessages( Message.Level.ERROR ) ) {
        fail( res.getMessages( Message.Level.ERROR ).toString() );
    }

    if ( kbConf == null ) {
        kbConf = KieServices.Factory.get().newKieBaseConfiguration();
    }
    kbConf.setOption( EqualityBehaviorOption.EQUALITY );
    KieBase kieBase = kieHelper.build( kbConf );


    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType( BeliefSystemType.DEFEASIBLE );
    return kieBase.newKieSession( ksConf, null );
}
 
Example 2
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private void testEventsExpiredInThePast(final String drl) {
    final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption( ClockTypeOption.get( ClockType.PSEUDO_CLOCK.getId() ) );

    final KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    final KieBase kieBase = helper.build( EventProcessingOption.STREAM );
    final KieSession kieSession = kieBase.newKieSession( sessionConfig, null );

    PseudoClockScheduler clock = kieSession.getSessionClock();

    final long currentTime = clock.getCurrentTime();

    clock.advanceTime(100, TimeUnit.MILLISECONDS);

    kieSession.insert(new BasicEvent(new Date(currentTime + 20), 10L, "20ms-30ms"));
    clock.advanceTime(1, TimeUnit.MILLISECONDS);
    kieSession.insert(new BasicEvent(new Date(currentTime + 20), 20L, "20ms-40ms"));

    clock.advanceTime(100, TimeUnit.MILLISECONDS);

    assertThat(kieSession.fireAllRules()).isEqualTo(1);
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    assertThat(kieSession.getObjects()).isEmpty();
}
 
Example 3
Source File: AbstractConcurrentTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
protected synchronized KieBase getKieBase(final Parameters params, final String... drls) {
    final KieHelper kieHelper = new KieHelper();
    for (final String drl : drls) {
        kieHelper.addContent(drl, ResourceType.DRL);
    }

    KieBase kieBase;
    if (params.isEnforcedJitting()) {
        kieBase = kieHelper.build(ConstraintJittingThresholdOption.get(0));
    } else {
        kieBase = kieHelper.build();
    }
    if (params.isSerializeKieBase()) {
        kieBase = serializeAndDeserializeKieBase(kieBase);
    }
    return kieBase;
}
 
Example 4
Source File: JittingTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testJittingEnum() {
    final String drl = "import " + AnEnum.class.getCanonicalName() + ";\n" +
            " rule R1 \n" +
            " when \n" +
            "    $enumFact: AnEnum(this == AnEnum.FIRST)\n" +
            " then \n" +
            " end ";

    final KieHelper kieHelper = new KieHelper();
    kieHelper.addContent( drl, ResourceType.DRL );
    final KieBase kieBase = kieHelper.build(ConstraintJittingThresholdOption.get(0));
    final KieSession kieSession = kieBase.newKieSession();

    kieSession.insert(AnEnum.FIRST);
    assertThat(kieSession.fireAllRules()).isEqualTo(1);
}
 
Example 5
Source File: JittingTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testJittingEnumAttribute() {
    final String drl = "import " + AnEnum.class.getCanonicalName() + ";\n" +
            "import " + FactWithEnum.class.getCanonicalName() + ";\n" +
            " rule R1 \n" +
            " when \n" +
            "    $factWithEnum: FactWithEnum(enumValue == AnEnum.FIRST) \n" +
            " then \n" +
            " end ";

    final KieHelper kieHelper = new KieHelper();
    kieHelper.addContent( drl, ResourceType.DRL );
    final KieBase kieBase = kieHelper.build(ConstraintJittingThresholdOption.get(0));

    final KieSession kieSession = kieBase.newKieSession();
    kieSession.insert(new FactWithEnum(AnEnum.FIRST));
    assertThat(kieSession.fireAllRules()).isEqualTo(1);
}
 
Example 6
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlpha() {
    String drl = "import " + A.class.getCanonicalName() + "\n" +
                 "declare A @role( event ) @expires(11ms) end\n" +
                 "global java.util.concurrent.atomic.AtomicInteger counter;\n" +
                 "rule R0 when\n" +
                 "  $a: A( $Aid: id > 0 )\n" +
                 "then\n" +
                 "  System.out.println(\"[\" + $a + \"]\");" +
                 "  counter.incrementAndGet();\n" +
                 "end";

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

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieBase kbase = helper.build( EventProcessingOption.STREAM );
    KieSession ksession = kbase.newKieSession( sessionConfig, null );

    PseudoClockScheduler sessionClock = ksession.getSessionClock();

    AtomicInteger counter = new AtomicInteger( 0 );
    ksession.setGlobal( "counter", counter );

    ksession.insert( new A(1) );

    sessionClock.advanceTime( 10, TimeUnit.MILLISECONDS );
    ksession.insert( new A(2) );

    sessionClock.advanceTime( 10, TimeUnit.MILLISECONDS );
    ksession.fireAllRules();
    assertEquals(2, counter.get());
}
 
Example 7
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testBetaRightExpired() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" +
                 "import " + B.class.getCanonicalName() + "\n" +
                 "declare A @role( event ) @expires(11ms) end\n" +
                 "declare B @role( event ) @expires(11ms) end\n" +
                 "global java.util.concurrent.atomic.AtomicInteger counter;\n" +
                 "rule R0 when\n" +
                 "  $a: A( $Aid: id > 0 )\n" +
                 "  $b: B( id == $Aid )\n" +
                 "then\n" +
                 "  System.out.println(\"[\" + $a + \",\" + $b + \"]\");" +
                 "  counter.incrementAndGet();\n" +
                 "end";

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

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieBase kbase = helper.build( EventProcessingOption.STREAM );
    KieSession ksession = kbase.newKieSession( sessionConfig, null );

    PseudoClockScheduler sessionClock = ksession.getSessionClock();

    AtomicInteger counter = new AtomicInteger( 0 );
    ksession.setGlobal( "counter", counter );

    ksession.insert( new A(1) );

    sessionClock.advanceTime( 20, TimeUnit.MILLISECONDS );
    ksession.insert( new B(1) );

    ksession.fireAllRules();
    assertEquals(0, counter.get());
}
 
Example 8
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testBetaLeftExpired() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" +
                 "import " + B.class.getCanonicalName() + "\n" +
                 "declare A @role( event ) @expires(11ms) end\n" +
                 "declare B @role( event ) @expires(11ms) end\n" +
                 "global java.util.concurrent.atomic.AtomicInteger counter;\n" +
                 "rule R0 when\n" +
                 "  $a: A( $Aid: id > 0 )\n" +
                 "  $b: B( id == $Aid )\n" +
                 "then\n" +
                 "  System.out.println(\"[\" + $a + \",\" + $b + \"]\");" +
                 "  counter.incrementAndGet();\n" +
                 "end";

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

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieBase kbase = helper.build( EventProcessingOption.STREAM );
    KieSession ksession = kbase.newKieSession( sessionConfig, null );

    PseudoClockScheduler sessionClock = ksession.getSessionClock();

    AtomicInteger counter = new AtomicInteger( 0 );
    ksession.setGlobal( "counter", counter );

    ksession.insert( new B(1) );

    sessionClock.advanceTime( 20, TimeUnit.MILLISECONDS );
    ksession.insert( new A(1) );

    ksession.fireAllRules();
    assertEquals(0, counter.get());
}
 
Example 9
Source File: PhreakInspector.java    From drools-workshop with Apache License 2.0 5 votes vote down vote up
private KieBase buildKieBase(Map<Resource, ResourceType> resources) {
    KieHelper kieHelper = new KieHelper();

    for (Map.Entry<Resource, ResourceType> entrySet : resources.entrySet()) {
        kieHelper.addResource(entrySet.getKey(), entrySet.getValue());
    }

    this.assertBuildResults(kieHelper.verify());

    return kieHelper.build();
}
 
Example 10
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testBeta() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" +
                 "import " + B.class.getCanonicalName() + "\n" +
                 "declare A @role( event ) @expires(11ms) end\n" +
                 "declare B @role( event ) @expires(11ms) end\n" +
                 "global java.util.concurrent.atomic.AtomicInteger counter;\n" +
                 "rule R0 when\n" +
                 "  $a: A( $Aid: id > 0 )\n" +
                 "  $b: B( ($Bid: id <= $Aid) && (id > ($Aid - 1 )))\n" +
                 "then\n" +
                 "  System.out.println(\"[\" + $a + \",\" + $b + \"]\");" +
                 "  counter.incrementAndGet();\n" +
                 "end";

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

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieBase kbase = helper.build( EventProcessingOption.STREAM );
    KieSession ksession = kbase.newKieSession( sessionConfig, null );

    PseudoClockScheduler sessionClock = ksession.getSessionClock();

    AtomicInteger counter = new AtomicInteger( 0 );
    ksession.setGlobal( "counter", counter );

    ksession.insert( new A(1) );
    ksession.insert( new B(1) );

    sessionClock.advanceTime( 10, TimeUnit.MILLISECONDS );
    ksession.insert( new A(2) );
    ksession.insert( new B(2) );

    sessionClock.advanceTime( 10, TimeUnit.MILLISECONDS );

    ksession.fireAllRules();
    assertEquals(2, counter.get());
}
 
Example 11
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testBetaLeftExpired2() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" +
                 "import " + B.class.getCanonicalName() + "\n" +
                 "import " + C.class.getCanonicalName() + "\n" +
                 "declare A @role( event ) @expires(31ms) end\n" +
                 "declare B @role( event ) @expires(11ms) end\n" +
                 "declare C @role( event ) @expires(31ms) end\n" +
                 "global java.util.concurrent.atomic.AtomicInteger counter;\n" +
                 "rule R0 when\n" +
                 "  $a: A( $Aid: id > 0 )\n" +
                 "  $b: B( $Bid: id == $Aid )\n" +
                 "  $c: C( id == $Bid )\n" +
                 "then\n" +
                 "  System.out.println(\"[\" + $a + \",\" + $b + \",\" + $c + \"]\");" +
                 "  counter.incrementAndGet();\n" +
                 "end";

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

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieBase kbase = helper.build( EventProcessingOption.STREAM );
    KieSession ksession = kbase.newKieSession( sessionConfig, null );

    PseudoClockScheduler sessionClock = ksession.getSessionClock();

    AtomicInteger counter = new AtomicInteger( 0 );
    ksession.setGlobal( "counter", counter );

    ksession.insert( new A(1) );
    ksession.insert( new B(1) );

    sessionClock.advanceTime( 20, TimeUnit.MILLISECONDS );
    ksession.insert( new C(1) );

    ksession.fireAllRules();
    assertEquals(0, counter.get());
}
 
Example 12
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSoftExpiration() {
    // DROOLS-1483
    String drl = "import " + ExpiringEventA.class.getCanonicalName() + "\n" +
                 "import " + ExpiringEventB.class.getCanonicalName() + "\n" +
                 "import " + ExpiringEventC.class.getCanonicalName() + "\n" +
                 "rule Ra when\n" +
                 "  $e : ExpiringEventA() over window:time(20s)\n" +
                 "then end\n " +
                 "rule Rb when\n" +
                 "  $e : ExpiringEventB() over window:time(20s)\n" +
                 "then end\n " +
                 "rule Rc when\n" +
                 "  $e : ExpiringEventC()\n" +
                 "then end\n";

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

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieBase kbase = helper.build( EventProcessingOption.STREAM );
    KieSession ksession = kbase.newKieSession( sessionConfig, null );

    PseudoClockScheduler clock = ksession.getSessionClock();

    ksession.insert( new ExpiringEventA() );
    ksession.insert( new ExpiringEventB() );
    ksession.insert( new ExpiringEventC() );
    ksession.fireAllRules();

    clock.advanceTime( 5, TimeUnit.SECONDS );
    ksession.fireAllRules();

    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( ExpiringEventA.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( ExpiringEventB.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( ExpiringEventC.class ) ).size() );

    clock.advanceTime( 10, TimeUnit.SECONDS );
    ksession.fireAllRules();

    // t=15 -> hard expiration of A
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( ExpiringEventA.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( ExpiringEventB.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( ExpiringEventC.class ) ).size() );

    clock.advanceTime( 10, TimeUnit.SECONDS );
    ksession.fireAllRules();

    // t=25 -> implicit expiration of B
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( ExpiringEventA.class ) ).size() );
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( ExpiringEventB.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( ExpiringEventC.class ) ).size() );

    clock.advanceTime( 10, TimeUnit.SECONDS );
    ksession.fireAllRules();

    // t=35 -> soft expiration of C
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( ExpiringEventA.class ) ).size() );
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( ExpiringEventB.class ) ).size() );
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( ExpiringEventC.class ) ).size() );
}
 
Example 13
Source File: ExpirationTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSoftExpirationWithDeclaration() {
    // DROOLS-1483
    String drl = "import " + A.class.getCanonicalName() + "\n" +
                 "import " + B.class.getCanonicalName() + "\n" +
                 "import " + C.class.getCanonicalName() + "\n" +
                 "declare A @role( event ) @expires(10s) end\n" +
                 "declare B @role( event ) @expires(value = 30s, policy = TIME_SOFT) end\n" +
                 "declare C @role( event ) @expires(value = 30s, policy = TIME_SOFT) end\n" +
                 "rule Ra when\n" +
                 "  $e : A() over window:time(20s)\n" +
                 "then end\n " +
                 "rule Rb when\n" +
                 "  $e : B() over window:time(20s)\n" +
                 "then end\n " +
                 "rule Rc when\n" +
                 "  $e : C()\n" +
                 "then end\n";

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

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieBase kbase = helper.build( EventProcessingOption.STREAM );
    KieSession ksession = kbase.newKieSession( sessionConfig, null );

    PseudoClockScheduler clock = ksession.getSessionClock();

    ksession.insert( new A(1) );
    ksession.insert( new B(2) );
    ksession.insert( new C(3) );
    ksession.fireAllRules();

    clock.advanceTime( 5, TimeUnit.SECONDS );
    ksession.fireAllRules();

    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( A.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( B.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( C.class ) ).size() );

    clock.advanceTime( 10, TimeUnit.SECONDS );
    ksession.fireAllRules();

    // t=15 -> hard expiration of A
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( A.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( B.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( C.class ) ).size() );

    clock.advanceTime( 10, TimeUnit.SECONDS );
    ksession.fireAllRules();

    // t=25 -> implicit expiration of B
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( A.class ) ).size() );
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( B.class ) ).size() );
    assertEquals( 1, ksession.getObjects( new ClassObjectFilter( C.class ) ).size() );

    clock.advanceTime( 10, TimeUnit.SECONDS );
    ksession.fireAllRules();

    // t=35 -> soft expiration of C
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( A.class ) ).size() );
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( B.class ) ).size() );
    assertEquals( 0, ksession.getObjects( new ClassObjectFilter( C.class ) ).size() );
}