Java Code Examples for org.kie.api.runtime.StatelessKieSession#setGlobal()

The following examples show how to use org.kie.api.runtime.StatelessKieSession#setGlobal() . 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: SequentialTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasicOperation() throws Exception {
    KieBase kbase = loadKnowledgeBase(kconf, "simpleSequential.drl");
    StatelessKieSession ksession = createStatelessKnowledgeSession( kbase );
    final List list = new ArrayList();
    ksession.setGlobal( "list",
                       list );

    final Person p1 = new Person( "p1",
                                  "stilton" );
    final Person p2 = new Person( "p2",
                                  "cheddar" );
    final Person p3 = new Person( "p3",
                                  "stilton" );

    final Cheese stilton = new Cheese( "stilton",
                                       15 );
    final Cheese cheddar = new Cheese( "cheddar",
                                       15 );


    ksession.execute( CommandFactory.newInsertElements( Arrays.asList( new Object[]{p1, stilton, p2, cheddar, p3} ) ) );

    assertEquals( 3,
                  list.size() );
}
 
Example 2
Source File: SequentialTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSalience() throws Exception {
    KieBase kbase = loadKnowledgeBase(kconf, "simpleSalience.drl");
    StatelessKieSession ksession = createStatelessKnowledgeSession( kbase );

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

    ksession.execute( new Person( "pob")  );

    assertEquals( 3,
                  list.size() );

    assertEquals( "rule 3", list.get( 0 ));
    assertEquals( "rule 2", list.get( 1 ) );
    assertEquals( "rule 1", list.get( 2 ) );
}
 
Example 3
Source File: RulesBasedRoutingStrategy.java    From mdw with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public List<String> determineWorkgroups(TaskTemplate taskTemplate, TaskInstance taskInstance) throws StrategyException {
    KieBase knowledgeBase = getKnowledgeBase();

    StatelessKieSession knowledgeSession = knowledgeBase.newStatelessKieSession();

    List<Object> facts = new ArrayList<Object>();
    facts.add(getParameters());
    knowledgeSession.setGlobal("taskTemplate", taskTemplate);
    knowledgeSession.setGlobal("taskInstance", taskInstance);
    knowledgeSession.setGlobal("now", new Date());

    knowledgeSession.execute(CommandFactory.newInsertElements(facts));

    return taskInstance.getWorkgroups();
}
 
Example 4
Source File: RulesBasedPrioritizationStrategy.java    From mdw with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Date determineDueDate(TaskTemplate taskTemplate) throws StrategyException {

    TaskInstance taskInstance = new TaskInstance();  // for holding values

    // execute rules only once (results are stored in taskInstance)
    KieBase knowledgeBase = getKnowledgeBase();
    StatelessKieSession knowledgeSession = knowledgeBase.newStatelessKieSession();

    List<Object> facts = new ArrayList<>();
    facts.add(getParameters());
    knowledgeSession.setGlobal("taskTemplate", taskTemplate);
    knowledgeSession.setGlobal("taskInstance", taskInstance);

    knowledgeSession.execute(CommandFactory.newInsertElements(facts));

    return Date.from(taskInstance.getDue());
}
 
Example 5
Source File: SessionsPoolTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testStatelessKieSessionsPool() {
    KieContainerSessionsPool pool = getKieContainer().newKieSessionsPool( 1 );
    StatelessKieSession session = pool.newStatelessKieSession();

    List<String> list = new ArrayList<>();
    session.setGlobal( "list", list );
    session.execute( "test" );
    assertEquals(1, list.size());

    list.clear();
    session.execute( "test" );
    assertEquals(1, list.size());
}
 
Example 6
Source File: StatelessSessionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotInStatelessSession() throws Exception {
    final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbc.setOption(SequentialOption.YES);
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc, "test_NotInStatelessSession.drl"));
    final StatelessKieSession session = kbase.newStatelessKieSession();

    final List list = new ArrayList();
    session.setGlobal("list", list);
    session.execute("not integer");
    assertEquals("not integer", list.get(0));
}
 
Example 7
Source File: StatelessSessionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private StatelessKieSession getSession2(final Resource resource) throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( resource, ResourceType.DRL );
    
    if (kbuilder.hasErrors() ) {
        System.out.println( kbuilder.getErrors() );
    }
    
    assertFalse( kbuilder.hasErrors() );
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();

    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    
   
    kbase.addPackages( pkgs );
    kbase    = SerializationHelper.serializeObject( kbase );
    final StatelessKieSession session = kbase.newStatelessKieSession();

    session.setGlobal( "list",
                       this.list );
    session.setGlobal( "cheesery",
                       this.cheesery );
    return session;
}
 
Example 8
Source File: SerializedPackageMergeTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private void testRuleExecution(StatelessKieSession session) throws Exception {
    List<Object> list = new ArrayList<Object>();
    session.setGlobal( "list",
                       list );

    session.execute( getObject() );

    assertEquals( 2,
                  list.size() );
}
 
Example 9
Source File: SequentialTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testSequentialWithRulebaseUpdate() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase(kconf, "simpleSalience.drl");
    StatelessKieSession ksession = createStatelessKnowledgeSession( kbase );

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

    ksession.execute(new Person("pob"));

    kbase.addPackages(loadKnowledgePackagesFromString( new String( IoUtils.readBytesFromInputStream( DynamicRulesTest.class.getResource("test_Dynamic3.drl").openStream() ) ) ) );

    ksession = kbase.newStatelessKieSession();
    ksession.setGlobal( "list", list );
    Person person  = new Person("bop");
    ksession.execute(person);

    assertEquals( 7, list.size() );

    assertEquals( "rule 3", list.get( 0 ));
    assertEquals( "rule 2", list.get( 1 ));
    assertEquals( "rule 1", list.get( 2 ));
    assertEquals( "rule 3", list.get( 3 ) );
    assertEquals( "rule 2", list.get( 4 ) );
    assertEquals( "rule 1", list.get( 5 ) );
    assertEquals( person, list.get( 6 ));
}
 
Example 10
Source File: DroolsExecutor.java    From mdw with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object execute(Asset rulesAsset, Operand input) throws ExecutionException {

    ClassLoader packageClassLoader = null;
    if (input.getContext() != null && input.getContext().getPackage() != null)
        packageClassLoader = input.getContext().getPackage().getClassLoader();
    if (packageClassLoader == null) // fall back to rules asset pkg classloader
        packageClassLoader = PackageCache.getPackage(rulesAsset.getPackageName()).getClassLoader();

    KnowledgeBaseAsset kbAsset = DroolsKnowledgeBaseCache
            .getKnowledgeBaseAsset(rulesAsset.getName(), null, packageClassLoader);
    if (kbAsset == null) {
        throw new ExecutionException("Cannot load KnowledgeBase asset: "
                + rulesAsset.getPackageName() + "/" + rulesAsset.getLabel());
    }

    KieBase knowledgeBase = kbAsset.getKnowledgeBase();

    StatelessKieSession kSession = knowledgeBase.newStatelessKieSession();

    List<Object> facts = new ArrayList<Object>();
    if (input.getInput() != null) {
        facts.add(input.getInput()); // direct access
        if (input.getInput() instanceof Jsonable)
            facts.add(((Jsonable)input.getInput()).getJson());
    }

    kSession.setGlobal("operand", input);
    kSession.execute(CommandFactory.newInsertElements(facts));
    return input.getResult();
}
 
Example 11
Source File: SequentialTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSequentialPlusPhreakOperationComplex() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str +="import " + A.class.getCanonicalName() + "\n";
    str +="global  " + List.class.getCanonicalName() + " list\n";


    // Focus is done as g1, g2, g1 to demonstrate that groups will not re-activate
    str +="rule r0 when\n";
    str +="then\n";
    str +="    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
    str +="    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g2' ).setFocus();\n";
    str +="    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
    str +="end\n";
    str +="rule r1 agenda-group 'g1' when\n";
    str +="    a : A( object > 0 )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="    modify(a) { setObject( 3 ) };\n";
    str +="end\n";

    // r1_x is here to show they do not react when g2.r9 changes A o=2,
    // i.e. checking that re-activating g1 won't cause it to pick up previous non evaluated rules.
    // this is mostly checking that the no linking doesn't interfere with the expected results.
    str +="rule r1_x agenda-group 'g1' when\n";
    str +="    a : A( object == 2 )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    // r1_y is here to show it does not react when A is changed to o=5 in r3
    str +="rule r1_y agenda-group 'g1' when\n";
    str +="    a : A( object == 5 )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    str +="rule r2 agenda-group 'g1' when\n";
    str +="    a : A( object < 3 )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";
    str +="rule r3 agenda-group 'g1' when\n";
    str +="    a : A(object >= 3  )\n";
    str +="then\n";
    str +="    modify(a) { setObject( 5 ) };\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    // Checks that itself, f3 and r1_y do not react as they are higher up
    str +="rule r4 agenda-group 'g1' when\n";
    str +="    a : A(object >= 2  )\n";
    str +="then\n";
    str +="    modify(a) { setObject( 5 ) };\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    // Checks that while this at one point matches, it does not match by the time g2 is entered
    // nor does it react when r9 changes a o=2
    str +="rule r6 agenda-group 'g2' when\n";
    str +="    a : A(object < 5  )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    str +="rule r7 agenda-group 'g2' when\n";
    str +="    a : A(object >= 3  )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";
    str +="rule r8 agenda-group 'g2' when\n";
    str +="    a : A(object >= 5  )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    // This changes A o=2 to check if g1.r1_x incorrect reacts when g1 is re-entered
    str +="rule r9 agenda-group 'g2' when\n";
    str +="    a : A(object >= 5  )\n";
    str +="then\n";
    str +="    modify(a) { setObject( 2 ) };\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    StatelessKieSession ksession = createStatelessKnowledgeSession( kbase );
    final List list = new ArrayList();
    ksession.setGlobal( "list",
                        list );

    ksession.execute( CommandFactory.newInsertElements(Arrays.asList( new Object[]{new A(1)} )) );

    assertEquals( 6, list.size() );
    assertEquals( "r1", list.get(0));
    assertEquals( "r3", list.get(1));
    assertEquals( "r4", list.get(2));
    assertEquals( "r7", list.get(3));
    assertEquals( "r8", list.get(4));
    assertEquals( "r9", list.get(5));
}
 
Example 12
Source File: SequentialTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSequentialPlusPhreakRevisitOriginallyEmptyGroup() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str +="import " + A.class.getCanonicalName() + "\n";
    str +="global  " + List.class.getCanonicalName() + " list\n";

    // Focus is done as g1, g2, g1 to demonstrate that groups will not re-activate
    str +="rule r0 when\n";
    str +="then\n";
    str +="    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
    str +="    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g2' ).setFocus();\n";
    str +="    drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
    str +="end\n";

    // r1_x is here to show they do not react when g2.r9 changes A o=2,
    // i.e. checking that re-activating g1 won't cause it to pick up previous non evaluated rules.
    // this is mostly checking that the none linking doesn't interfere with the expected results.
    // additional checks this works if g1 never had any Matches on the first visit
    str +="rule r1_x agenda-group 'g1' when\n";
    str +="    a : A( object == 2 )\n";
    str +="then\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    // This changes A o=2 to check if g1.r1_x incorrect reacts when g1 is re-entered
    str +="rule r9 agenda-group 'g2' when\n";
    str +="    a : A(object >= 5  )\n";
    str +="then\n";
    str +="    modify(a) { setObject( 2 ) };\n";
    str +="    list.add( drools.getRule().getName() );\n";
    str +="end\n";

    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    StatelessKieSession ksession = createStatelessKnowledgeSession( kbase );
    final List list = new ArrayList();
    ksession.setGlobal( "list",
                        list );

    ksession.execute( CommandFactory.newInsertElements(Arrays.asList( new Object[]{new A(5)} )) );

    assertEquals( 1, list.size() );
    assertEquals( "r9", list.get(0));
}
 
Example 13
Source File: DroolsActivity.java    From mdw with Apache License 2.0 4 votes vote down vote up
protected void setGlobalValues(StatelessKieSession knowledgeSession) throws ActivityException {
    knowledgeSession.setGlobal("activity", this); // TODO deprecate
    knowledgeSession.setGlobal("runtimeContext", getRuntimeContext());
    knowledgeSession.setGlobal("now", new Date());
}