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

The following examples show how to use org.kie.api.runtime.KieSession#getGlobal() . 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: DeleteTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testAssertRetract() throws Exception {
    // postponed while I sort out KnowledgeHelperFixer
    final KieBase kbase = loadKnowledgeBase("assert_retract.drl");
    final KieSession ksession = kbase.newKieSession();

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

    final PersonInterface person = new org.drools.compiler.Person("michael", "cheese");
    person.setStatus("start");
    ksession.insert(person);

    ksession.fireAllRules();

    final List<String> results = (List<String>) ksession.getGlobal("list");
    for (final String result : results) {
        logger.info(result);
    }
    assertEquals(5, results.size());
    assertTrue(results.contains("first"));
    assertTrue(results.contains("second"));
    assertTrue(results.contains("third"));
    assertTrue(results.contains("fourth"));
    assertTrue(results.contains("fifth"));
}
 
Example 2
Source File: VarargsTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testVarargs() throws Exception {
    KieBase kbase = loadKnowledgeBase("varargs2.drl");
    KieSession ksession = createKnowledgeSession(kbase);

    MySet mySet = new MySet( "one", "two" );
    ksession.insert(mySet);
    ksession.fireAllRules();

    assertTrue(mySet.contains("one"));
    assertTrue( mySet.contains("two") );
    assertTrue( mySet.contains("three") );
    assertTrue( mySet.contains("four") );
    assertTrue( mySet.contains("z") );

    mySet = (MySet) ksession.getGlobal("set");
    assertTrue( mySet.contains("x") );
    assertTrue( mySet.contains("y") );
    assertTrue( mySet.contains("three") );
    assertTrue( mySet.contains("four") );
    assertTrue( mySet.contains("z") );     }
 
Example 3
Source File: OOPathAccumulateTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private void testAccumulateCollection(final String accumulateFunction, final Integer... expectedResults) {
    final String drl =
            "import org.drools.compiler.oopath.model.*;\n" +
                    "global java.util.Collection<Integer> globalVar\n" +
                    "\n" +
                    "rule R when\n" +
                    "  accumulate ( Adult( $child: /children ) ; $accumulateResult: " + accumulateFunction + "($child.getAge()) )\n" +
                    "then\n" +
                    "  kcontext.getKieRuntime().setGlobal(\"globalVar\", $accumulateResult);\n" +
                    "end\n";

    final KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
            .build()
            .newKieSession();

    final Man bob = new Man( "Bob", 40 );
    bob.addChild( new Child( "Charles", 12 ) );
    bob.addChild( new Child( "Debbie", 8 ) );

    ksession.insert( bob );
    ksession.fireAllRules();

    final Collection<Integer> result = (Collection<Integer>) ksession.getGlobal("globalVar");
    assertThat(result).containsExactlyInAnyOrder(expectedResults);
}
 
Example 4
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarshalWithProtoBuf() throws Exception {
    KieBase kbase = loadKnowledgeBase( "../test_Serializable.drl" );
    KieSession ksession = kbase.newKieSession();

    ksession.setGlobal( "list",
                        new ArrayList() );
    final Person bob = new Person( "bob" );
    ksession.insert( bob );

    ksession = marsallStatefulKnowledgeSession( ksession );

    assertEquals( 1,
                  ksession.getFactCount() );
    assertEquals( bob,
                  ksession.getObjects().iterator().next() );

    int fired = ksession.fireAllRules();

    assertEquals( 3,
                  fired );

    List list = (List) ksession.getGlobal( "list" );

    assertEquals( 3,
                  list.size() );
    // because of agenda-groups
    assertEquals( new Integer( 4 ),
                  list.get( 0 ) );

    Collection<? extends Object> facts = ksession.getObjects();
    System.out.println( new ArrayList( facts ) );
    assertEquals( 2,
                  facts.size() );
}
 
Example 5
Source File: OOPathAccumulateTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private void testAccumulate(final String accumulateFunction, final Number expectedResult) {
    // DROOLS-1265
    final String drl =
            "import org.drools.compiler.oopath.model.*;\n" +
                    "global java.lang.Object globalVar\n" +
                    "\n" +
                    "rule R when\n" +
                    "  accumulate ( Adult( $child: /children ) ; $accumulateResult: " + accumulateFunction + "($child.getAge()) )\n" +
                    "then\n" +
                    "  kcontext.getKieRuntime().setGlobal(\"globalVar\", $accumulateResult);\n" +
                    "end\n";

    final KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
            .build()
            .newKieSession();

    final Man bob = new Man( "Bob", 40 );
    bob.addChild( new Child( "Charles", 12 ) );
    bob.addChild( new Child( "Debbie", 8 ) );

    ksession.insert( bob );
    ksession.fireAllRules();

    final Number result = (Number) ksession.getGlobal("globalVar");
    if (result instanceof Double) {
        assertThat(expectedResult.doubleValue()).isEqualTo(result.doubleValue());
    } else {
        assertThat(expectedResult.longValue()).isEqualTo(result.longValue());
    }
}
 
Example 6
Source File: GetGlobalCommand.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 );

    Object object = ksession.getGlobal( identifier );
    ExecutionResultImpl results = ((RegistryContext)context).lookup( ExecutionResultImpl.class );
    if ( results != null ) {
        results.getResults().put( (this.outIdentifier != null) ? this.outIdentifier : this.identifier,
                                  object );
    }
    return object;
}
 
Example 7
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSerializable() throws Exception {
    Collection<KiePackage>  kpkgs = loadKnowledgePackages("../test_Serializable.drl" );
    KiePackage kpkg = kpkgs.iterator().next();
    kpkg = SerializationHelper.serializeObject( kpkg );

    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    kbase.addPackages( Collections.singleton( kpkg ) );

    Map<String, InternalKnowledgeBase> map = new HashMap<String, InternalKnowledgeBase>();
    map.put( "x",
             kbase );
    map = SerializationHelper.serializeObject( map );
    kbase = map.get( "x" );

    final org.kie.api.definition.rule.Rule[] rules = kbase.getKiePackages().iterator().next().getRules().toArray( new org.kie.api.definition.rule.Rule[0] );
    assertEquals( 4,
                  rules.length );

    assertEquals( "match Person 1",
                  rules[0].getName() );
    assertEquals( "match Person 2",
                  rules[1].getName() );
    assertEquals( "match Person 3",
                  rules[2].getName() );
    assertEquals( "match Integer",
                  rules[3].getName() );

    KieSession ksession = kbase.newKieSession();

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

    final Person bob = new Person( "bob" );
    ksession.insert( bob );

    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession( ksession,
                                                                          true );

    assertEquals( 1,
                  ksession.getFactCount() );
    assertEquals( bob,
                  ksession.getObjects().iterator().next() );

    assertEquals( 2,
                  ((InternalAgenda) ksession.getAgenda()).agendaSize() );

    ksession.fireAllRules();

    List list = (List) ksession.getGlobal( "list" );

    assertEquals( 3,
                  list.size() );
    // because of agenda-groups
    assertEquals( new Integer( 4 ),
                  list.get( 0 ) );

    // need to create a new collection or otherwise the collection will be identity based
    List< ? > objects = new ArrayList<Object>( ksession.getObjects() );
    assertEquals( 2, objects.size() );
    assertTrue( objects.contains( bob ) );
    assertTrue( objects.contains( new Person( "help" ) ) );
}
 
Example 8
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testAccumulate() throws Exception {
    String rule = "package org.drools\n" +
            "import org.drools.compiler.Message\n" +
            "global java.util.List results\n" +
            "rule MyRule\n" +
            "  when\n" +
            "    $n : Number( intValue >= 2 ) from accumulate ( m: Message( ), count( m ) )\n" +
            "  then\n" +
            "    results.add($n);\n" +
            "end";

    KieBase kBase = loadKnowledgeBaseFromString( rule );
    KieSession ksession = getSerialisedStatefulKnowledgeSession( kBase.newKieSession(), true );

    kBase = SerializationHelper.serializeObject( kBase );
    ksession = getSerialisedStatefulKnowledgeSession( ksession, true );

    ksession.setGlobal( "results",
                       new ArrayList() );

    ksession = getSerialisedStatefulKnowledgeSession( ksession, true );
    ksession.insert( new Message() );
    ksession = getSerialisedStatefulKnowledgeSession( ksession, true );
    List results = (List) ksession.getGlobal( "results" );

    ksession.insert( new Message() );
    ksession.insert( new Message() );
    ksession.fireAllRules();
    assertEquals( 3,
                  ((Number) results.get( 0 )).intValue() );

    ksession = getSerialisedStatefulKnowledgeSession( ksession, true );

    ksession.insert( new Message() );
    ksession.insert( new Message() );
    ksession = getSerialisedStatefulKnowledgeSession( ksession, true );

    assertEquals( 1,
                  ((InternalAgenda) ksession.getAgenda()).agendaSize() );
    ksession.fireAllRules();
    assertEquals( 5,
                  ((Number) results.get( 1 )).intValue() );
}