Java Code Examples for org.kie.api.runtime.KieSession#getEntryPoint()

The following examples show how to use org.kie.api.runtime.KieSession#getEntryPoint() . 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: GetEntryPointCommand.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public EntryPoint execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint ep = ksession.getEntryPoint(name);
    if (ep == null) {
        return null;
    }

    final EntryPointCreator epCreator = (EntryPointCreator)context.get(EntryPointCreator.class.getName());
    final EntryPoint entryPoint = epCreator != null ? epCreator.getEntryPoint(name) : ep;

    if ( this.outIdentifier != null ) {
        ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier,
                                                                                  entryPoint);
    }

    return entryPoint;
}
 
Example 2
Source File: InsertElementsCommand.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public Collection<FactHandle> execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    List<FactHandle> handles = new ArrayList<FactHandle>();
    
    EntryPoint wmep;
    if ( StringUtils.isEmpty( this.entryPoint ) ) {
        wmep = ksession;
    } else {
        wmep = ksession.getEntryPoint( this.entryPoint );
    }

    for ( Object object : objects ) {
        handles.add( wmep.insert( object ) );
    }

    if ( outIdentifier != null ) {
        if ( this.returnObject ) {
            ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, objects );
        }
        ((RegistryContext) context).lookup( ExecutionResultImpl.class ).getFactHandles().put( this.outIdentifier, handles );
    }
    return handles;
}
 
Example 3
Source File: DescrBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromEntryPoint() throws InstantiationException,
                                        IllegalAccessException {
    PackageDescr pkg = DescrFactory
            .newPackage().name("org.drools")
            .newRule().name("from rule")
                .lhs()
                    .pattern("String").id("s", false).from().entryPoint("EventStream").end()
                .end()
            .rhs("//System.out.println(s);")
            .end().getDescr();

    KiePackage kpkg = compilePkgDescr( pkg, "org.drools" );
    assertEquals( "org.drools",
                  kpkg.getName() );
    
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages( Collections.singletonList( kpkg ) );
    
    KieSession ksession = createKnowledgeSession(kbase);
    EntryPoint ep = ksession.getEntryPoint( "EventStream" );
    ep.insert( "Hello World!" );
    int rules = ksession.fireAllRules();
    assertEquals( 1, rules );

}
 
Example 4
Source File: GetFactHandleInEntryPointCommand.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public FactHandle execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint ep = ksession.getEntryPoint(entryPoint);
    InternalFactHandle factHandle = (InternalFactHandle) ep.getFactHandle( object );
    if ( factHandle != null ){
        InternalFactHandle handle = factHandle.clone();
        if ( disconnected ) {
            handle.disconnect();
        }

        if ( this.outIdentifier != null ) {
            ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier,
                                                                                      handle);
        }

        return handle;
    }
    return null;
}
 
Example 5
Source File: GetObjectsInEntryPointCommand.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public Collection execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint ep = ksession.getEntryPoint(entryPoint);

    Collection col = null;

    if ( filter != null ) {

        col =  ep.getObjects( this.filter );
    } else {
        col =  ep.getObjects( );
    }

    if ( this.outIdentifier != null ) {
        List objects = new ArrayList( col );

        ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, objects );
    }

    return col;
}
 
Example 6
Source File: InsertObjectInEntryPointCommand.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public FactHandle execute(Context context) {

        KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
        EntryPoint ep = ksession.getEntryPoint(entryPoint);
        FactHandle factHandle = ep.insert(object);

        DefaultFactHandle disconnectedHandle = ((DefaultFactHandle) factHandle).clone();
        disconnectedHandle.disconnect();

        if (outIdentifier != null) {
            if (this.returnObject) {
                ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult(this.outIdentifier, object);
            }
            ((RegistryContext) context).lookup( ExecutionResultImpl.class ).getFactHandles().put(this.outIdentifier, disconnectedHandle);
        }

        return disconnectedHandle;
    }
 
Example 7
Source File: UpdateInEntryPointCommand.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint ep = ksession.getEntryPoint( entryPoint );
    if (modifiedProperties != null) {
        ep.update( handle, object, modifiedProperties );
    } else {
        ep.update( handle, object );
    }
    return null;
}
 
Example 8
Source File: UpdateCommand.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint ep = ksession.getEntryPoint( handle.getEntryPointId() );
    if (modifiedProperties != null) {
        ep.update( handle, object, modifiedProperties );
    } else {
        ep.update( handle, object );
    }
    return null;
}
 
Example 9
Source File: DeleteObjectCommand.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint ep = ksession.getEntryPoint( entryPoint );
    if ( ep != null ) {
        FactHandle handle = ksession.getEntryPoint( entryPoint ).getFactHandle( object );
        ksession.delete( handle );
    }
    return null;
}
 
Example 10
Source File: GetObjectInEntryPointCommand.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Object execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint ep = ksession.getEntryPoint(entryPoint);

    Object object = ep.getObject( factHandle );

    if (this.outIdentifier != null) {
        ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, object );
    }

    return object;
}
 
Example 11
Source File: ModifyCommand.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public Object execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    EntryPoint wmep = ksession.getEntryPoint( factHandle.getEntryPointId() );

    Object object = wmep.getObject( this.factHandle );
    MVELSafeHelper.getEvaluator().eval( getMvelExpr(), object );

    wmep.update( factHandle,
                    object );
    return object;
}
 
Example 12
Source File: FireUntilHaltTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testFireUntilHaltFailingAcrossEntryPoints() throws Exception {
    String rule1 = "package org.drools.compiler\n";
    rule1 += "global java.util.List list\n";
    rule1 += "rule testFireUntilHalt\n";
    rule1 += "when\n";
    rule1 += "       Cheese()\n";
    rule1 += "  $p : Person() from entry-point \"testep2\"\n";
    rule1 += "then \n";
    rule1 += "  list.add( $p ) ;\n";
    rule1 += "end\n";

    final KieBase kbase = loadKnowledgeBaseFromString(rule1);
    final KieSession ksession = createKnowledgeSession(kbase);
    final EntryPoint ep = ksession.getEntryPoint("testep2");

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

    ksession.insert(new Cheese("cheddar"));
    ksession.fireAllRules();

    final Thread t1 = new Thread(ksession::fireUntilHalt);
    t1.start();

    Thread.sleep(500);
    ep.insert(new Person("darth"));
    Thread.sleep(500);
    ksession.halt();
    t1.join(5000);
    final boolean alive = t1.isAlive();
    if (alive) {
        t1.interrupt();
    }
    assertFalse(alive, "Thread should have died!");
    assertEquals(1, list.size());
}
 
Example 13
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamicRuleAdditionsWithEntryPoints() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicWithEntryPoint.drl" );
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();

    KieSession ksession = createKnowledgeSession( kbase );

    // now lets add some knowledge to the kbase
    kbase.addPackages( kpkgs );

    List<StockTick> results = new ArrayList<StockTick>();
    ksession.setGlobal( "results",
                        results );

    EntryPoint ep = ksession.getEntryPoint( "in-channel" );
    ep.insert( new StockTick( 1,
                              "RHT",
                              20,
                              10000 ) );
    ep.insert( new StockTick( 2,
                              "RHT",
                              21,
                              15000 ) );
    ep.insert( new StockTick( 3,
                              "RHT",
                              22,
                              20000 ) );

    ksession.fireAllRules();
    assertEquals( 3,
                  results.size() );

}
 
Example 14
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 15
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 16
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 17
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 18
Source File: PhreakConcurrencyTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public EPManipulator4(KieSession ksession, int index, CyclicBarrier barrier) {
    this.ksession = ksession;
    this.index = index;
    this.barrier = barrier;
    this.ep = ksession.getEntryPoint("EP" + index);
}