Java Code Examples for org.kie.api.KieBase#newKieSession()

The following examples show how to use org.kie.api.KieBase#newKieSession() . 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: 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 2
Source File: AlphaNodeTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void test3Alpha() {
    String str =
            "import org.drools.compiler.Person\n" +
            "rule R1 when\n" +
            "  $p : Person(name == \"Mario\")\n" +
            "then\n" +
            "  modify($p) { setAge(2) }" +
            "  modify($p) { setAge(2) }" +
            "end\n" +
            "rule R3 when\n" +
            "  $p : Person(age > 1)\n" +
            "then\n" +
            "end\n";

    KieBase kbase = loadKnowledgeBaseFromString( str );
    KieSession ksession = kbase.newKieSession();

    ksession.insert( new Person( "Mario", 0 ) );
    assertEquals( 2, ksession.fireAllRules() );
}
 
Example 3
Source File: InlineCastTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testInlineCastOnRightOperand() throws Exception {
    String str = "import org.drools.compiler.*;\n" +
            "rule R1 when\n" +
            "   $person : Person( )\n" +
            "   String( this == $person.address#LongAddress.country )\n" +
            "then\n" +
            "end\n";

    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();

    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    ksession.insert("uk");

    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}
 
Example 4
Source File: AlphaNodeTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedAlpha() {
    String str =
            "import org.drools.compiler.Person\n" +
            "rule R1 when\n" +
            "  $p : Person(name == \"Mario\")\n" +
            "then\n" +
            "end\n" +
            "rule R2 when\n" +
            "  $p : Person(name == \"Mario\")\n" +
            "then\n" +
            "end\n";

    KieBase kbase = loadKnowledgeBaseFromString( str );
    KieSession ksession = kbase.newKieSession();

    ksession.insert( new Person( "Mario" ) );
    assertEquals( 2, ksession.fireAllRules() );
}
 
Example 5
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testAccumulate2() throws Exception {
    String str = "package org.drools\n" + "\n" +
                 "import org.drools.compiler.Message\n" + "\n" +
                 "rule MyRule\n" + "  when\n" +
                 "    Number( intValue >= 5 ) from accumulate ( m: Message( ), count( m ) )\n" +
                 "  then\n" +
                 "    System.out.println(\"Found messages\");\n" +
                 "end\n";

    KieBase kBase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kBase.newKieSession();

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

    assertEquals( 1,
                  ((InternalAgenda) ksession.getAgenda()).agendaSize()  );
}
 
Example 6
Source File: NestedAccessorsTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedAccessor() throws Exception {
    final String str = "import org.drools.compiler.*;\n" +
            "rule R1 when\n" +
            "   Person( name == \"mark\", cheese.(type == \"gorgonzola\", price == 10) )\n" +
            "then\n" +
            "end\n";

    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = kbase.newKieSession();

    final Person mark1 = new Person("mark");
    mark1.setCheese(new Cheese("gorgonzola", 10));
    ksession.insert(mark1);

    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}
 
Example 7
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositionalArguments() throws InstantiationException, IllegalAccessException {
    String drl = "package org.test;\n" +
                 "global java.util.List names;\n" +
                 "declare Person\n" +
                 "    name : String\n" +
                 "    age : int\n" +
                 "end\n" +
                 "rule R when \n" +
                 "    $p : Person( \"Mark\", 37; )\n" +
                 "then\n" +
                 "    names.add( $p.getName() );\n" +
                 "end\n";

    KieBuilder kieBuilder = build(drl);

    assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
    KieBase kieBase = KieServices.Factory.get().newKieContainer( kieBuilder.getKieModule().getReleaseId() ).getKieBase();

    FactType factType = kieBase.getFactType( "org.test", "Person" );
    Object instance = factType.newInstance();
    factType.set(instance, "name", "Mark");
    factType.set(instance, "age", 37);

    List<String> names = new ArrayList<String>();
    KieSession ksession = kieBase.newKieSession();
    ksession.setGlobal("names", names);

    ksession.insert(instance);
    ksession.fireAllRules();

    assertEquals( 1, names.size() );
    assertEquals( "Mark", names.get( 0 ) );
}
 
Example 8
Source File: ConcurrentBasesParallelTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@ParameterizedConcurrentBasesParallelTest
public void testFunctions(Parameters params) throws InterruptedException {
    final String rule = "import " + BeanA.class.getCanonicalName() + ";\n" +
            "global java.util.List list;" +
            "rule Rule " +
            "when " +
            "    BeanA() " +
            "then " +
            "    addToList(list);" +
            "end";

    final TestExecutor exec = counter -> {
        final String function = "import java.util.List;\n" +
                "function void addToList(List list) { \n" +
                "    list.add( \"" + counter + "\" );\n" +
                "}\n";

        final KieBase base = getKieBase(params, rule, function);
        final KieSession session = base.newKieSession();

        try {
            session.insert(new BeanA());
            final List<String> list = new ArrayList<>();
            session.setGlobal("list", list);
            final int rulesFired = session.fireAllRules();
            assertThat(list).hasSize(1);
            assertThat(list.get(0)).isEqualTo("" + counter);
            assertThat(rulesFired).isEqualTo(1);
            return true;
        } finally {
            session.dispose();
        }
    };

    parallelTest(NUMBER_OF_THREADS, exec);
}
 
Example 9
Source File: SerializedPackageMergeTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildAndSerializePackagesWithGlobalMethodInLHS() throws Exception {
    // DROOLS-2517
    String drl =   "package com.sample\n" +
            "import org.drools.compiler.Person\n" +
            "global org.drools.compiler.MyUtil myUtil\n" +
            "rule R1\n" +
            "when\n" +
            "  Person(myUtil.transform(name) == \"John-san\")\n" + // call global's method
            "then\n" +
            "end\n";

    KnowledgeBuilder builder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder1.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages = builder1.getKnowledgePackages();

    byte[] pkgBin = null;
    try (ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
            DroolsObjectOutputStream out = new DroolsObjectOutputStream(byteOutStream);) {
        out.writeObject(knowledgePackages);
        out.flush();
        pkgBin = byteOutStream.toByteArray();
    }

    KnowledgeBuilder builder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    builder2.add(ResourceFactory.newByteArrayResource(pkgBin), ResourceType.PKG);
    KieBase kbase = builder2.newKieBase();

    KieSession ksession = kbase.newKieSession();
    try {
        ksession.setGlobal("myUtil", new org.drools.compiler.MyUtil());
        ksession.insert(new org.drools.compiler.Person("John"));
        int fired = ksession.fireAllRules();
        assertEquals(1, fired);
    } finally {
        ksession.dispose();
    }
}
 
Example 10
Source File: ExecutionFlowControlTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testLockOnActive() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LockOnActive.drl");
    KieSession ksession = kbase.newKieSession();

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

    // AgendaGroup "group1" is not active, so should receive activation
    final Cheese brie12 = new Cheese( "brie", 12 );
    ksession.insert( brie12 );
    ((InternalWorkingMemory)ksession).flushPropagations();
    InternalAgenda agenda = ((InternalAgenda) ksession.getAgenda());
    final AgendaGroup group1 = agenda.getAgendaGroup( "group1" );
    assertEquals( 1, group1.size() );

    ksession.getAgenda().getAgendaGroup("group1").setFocus( );
    // AgendaqGroup "group1" is now active, so should not receive activations
    final Cheese brie10 = new Cheese( "brie", 10 );
    ksession.insert( brie10 );
    assertEquals( 1, group1.size() );

    final Cheese cheddar20 = new Cheese( "cheddar", 20 );
    ksession.insert( cheddar20 );
    final AgendaGroup group2 = agenda.getAgendaGroup( "group1" );
    assertEquals( 1, group2.size() );

    agenda.setFocus(group2);
    final Cheese cheddar17 = new Cheese( "cheddar", 17 );
    ksession.insert( cheddar17 );
    assertEquals( 1, group2.size() );
}
 
Example 11
Source File: ExistentialOperatorTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testForallAfterOr() throws Exception {
    // DROOLS-2710
    String str =
            "package redhat\n" +
            "declare Fact\n" +
            "    integer : int\n" +
            "    string1 : String\n" +
            "    string2 : String\n" +
            "end\n" +
            "rule \"Rule\"\n" +
            "when\n" +
            "Fact(string2 == \"Y\")\n" +
            "(\n" +
            "    exists (Fact(integer == 42)) or\n" +
            "    Fact(integer == 43)\n" +
            ")\n" +
            "forall (Fact(string1 == \"X\"))\n" +
            "then\n" +
            "end";

    KieBase kieBase = new KieHelper().addContent( str, ResourceType.DRL ).build();
    KieSession kieSession = kieBase.newKieSession();

    FactType factType = kieBase.getFactType("redhat", "Fact");

    Object fact = factType.newInstance();
    factType.set(fact, "string1", "X");
    factType.set(fact, "string2", "Y");
    factType.set(fact, "integer", 42);

    kieSession.insert(fact);

    int n = kieSession.fireAllRules();
    assertEquals(1, n);
}
 
Example 12
Source File: NamedConsequencesTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testNamedConsequenceAfterNotPattern() {
    // DROOLS-5
    String str = "import org.drools.compiler.Cheese;\n " +
            "global java.util.List results;\n" +
            "\n" +
            "rule R1 when\n" +
            "    $a: Cheese ( type == \"stilton\" )\n" +
            "    not Cheese ( type == \"brie\" )\n" +
            "    do[t1]\n" +
            "    $b: Cheese ( type == \"cheddar\" )\n" +
            "then\n" +
            "    results.add( $b.getType() );\n" +
            "then[t1]\n" +
            "    results.add( $a.getType() );\n" +
            "end\n";

    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();

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

    ksession.insert( new Cheese( "stilton", 5 ) );
    ksession.insert( new Cheese("cheddar", 7 ) );

    ksession.fireAllRules();

    assertTrue(results.contains("stilton"));
    assertTrue(results.contains("cheddar"));
}
 
Example 13
Source File: PositionalTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositionalWithNull() {
    // DROOLS-51
    String str =
            "declare Bean\n" +
            "  value : String\n" +
            "end\n" +
            "\n" +
            "rule \"Init\"\n" +
            "when\n" +
            "then\n" +
            "  insert( new Bean( null ) );\n" +
            "  insert( \"test\" );\n" +
            "end\n" +
            "\n" +
            "rule \"Bind\"\n" +
            "when\n" +
            "  $s : String(  )\n" +
            "  $b : Bean( null ; )\n" +
            "then\n" +
            "  modify ( $b ) { setValue( $s ); }\n" +
            "end";

    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    assertEquals(2, ksession.fireAllRules());
}
 
Example 14
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testComplexBetaSharedAlphaWithWatchesRemoveR2() {
    String rule1 = "$b : B( b == 15) @watch(i) A( a == 10, b == 15 ) @watch(c)";
    String rule2 = "$b : B( b == 15) @watch(j) A( a == 10, i == 20 ) @watch(s)";
    String rule3 = "$b : B( c == 15) @watch(k) A( a == 10, i == 20, b == 10 ) @watch(j)";
    KieBase kbase = getKnowledgeBase(rule1, rule2, rule3);
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase.newKieSession());
    
    kbase.removeRule( "org.drools.compiler.integrationtests", "r1" );
    
    ObjectTypeNode otn = getObjectTypeNode(kbase, "A" );
    assertNotNull( otn );
    Class classType = ((ClassObjectType) otn.getObjectType()).getClassType();

    List<String> sp = getSettableProperties(wm, otn);        

    AlphaNode alphaNode1 = ( AlphaNode ) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals( calculatePositiveMask(classType, list("a"), sp), alphaNode1.getDeclaredMask( ) );
    assertEquals( calculatePositiveMask(classType, list("a", "b", "c", "i", "j"), sp), alphaNode1.getInferredMask() );
            
    // first split
    AlphaNode alphaNode1_1 = ( AlphaNode ) alphaNode1.getObjectSinkPropagator().getSinks()[0];
    assertEquals( calculatePositiveMask(classType, list("b"), sp), alphaNode1_1.getDeclaredMask( ) );
    assertEquals( calculatePositiveMask(classType, list("a", "b", "c"), sp), alphaNode1_1.getInferredMask() );  
    
    BetaNode betaNode1 = ( BetaNode )  alphaNode1_1.getObjectSinkPropagator().getSinks()[0];
    assertEquals( calculatePositiveMask(classType, list("c"), sp), betaNode1.getRightDeclaredMask() );
    assertEquals( calculatePositiveMask(classType, list("a", "b", "c"), sp), betaNode1.getRightInferredMask() );
    
    assertEquals( calculatePositiveMask(classType, list("i"), sp), betaNode1.getLeftDeclaredMask() );
    assertEquals( calculatePositiveMask(classType, list("b", "i"), sp), betaNode1.getLeftInferredMask() );        
    
    // fist share, second alpha
    AlphaNode alphaNode1_2 = ( AlphaNode ) alphaNode1.getObjectSinkPropagator().getSinks()[1];
    assertEquals( calculatePositiveMask(classType, list("i"), sp), alphaNode1_2.getDeclaredMask( ) );
    assertEquals( calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), alphaNode1_2.getInferredMask() );  
    
    AlphaNode alphaNode1_3 = ( AlphaNode ) alphaNode1_2.getObjectSinkPropagator().getSinks()[0];
    assertEquals( calculatePositiveMask(classType, list("b"), sp), alphaNode1_3.getDeclaredMask( ) );
    assertEquals( calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), alphaNode1_3.getInferredMask() );         
    

    BetaNode betaNode2 = ( BetaNode )  alphaNode1_3.getObjectSinkPropagator().getSinks()[0];
    assertEquals( calculatePositiveMask(classType, list("j"), sp), betaNode2.getRightDeclaredMask() );
    assertEquals( calculatePositiveMask(classType, list("a", "i", "b", "j"), sp), betaNode2.getRightInferredMask() );
    
    assertEquals( calculatePositiveMask(classType, list("k"), sp), betaNode2.getLeftDeclaredMask() );
    assertEquals( calculatePositiveMask(classType, list("c", "k"), sp), betaNode2.getLeftInferredMask() );         
       
}
 
Example 15
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testRepeatedPatternWithPR() {
    // JBRULES-3705
    final String str1 =
            "package org.test;\n" +
            "global java.util.List list; \n" +
            "" +
            "declare SampleBean \n" +
            "@propertyReactive \n" +
            "x : java.math.BigDecimal \n" +
            "y : java.math.BigDecimal \n" +
            "id : Long @key\n" +
            "end \n" +
            "" +
            " rule \"calculate y\"\n" +
            " dialect \"mvel\"\n" +
            " when\n" +
            " $bean : SampleBean(id == 1L);\n" +
            " then\n" +
            " modify($bean){\n" +
            " y =5B;\n" +
            " }\n" +
            " list.add( $bean.y ); \n" +
            " end\n" +
            "\n" +
            " rule \"calculate x\"\n" +
            " dialect \"mvel\"\n" +
            " when\n" +
            " $bean : SampleBean(id == 1L);\n" +
            " then\n" +
            " modify($bean){\n" +
            " x =10B;\n" +
            " }\n" +
            " list.add( $bean.x ); \n" +
            " end\n" +
            "" +
            " rule Init \n" +
            " when\n" +
            " then\n" +
            " insert( new SampleBean( 1L ) ); \n" +
            " end";


    final KieBase kbase = loadKnowledgeBaseFromString( str1 );
    final KieSession ksession = kbase.newKieSession();
    final ArrayList list = new ArrayList();
    ksession.setGlobal( "list", list );
    ksession.fireAllRules();

    assertEquals( 2, list.size() );
    assertTrue( list.contains( new BigDecimal( 10 ) ) );
    assertTrue(list.contains(new BigDecimal(5)));
}
 
Example 16
Source File: CommonTestMethodBase.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
protected KieSession createKieSession(KieBase kbase, KieSessionConfiguration sessionConfiguration, Environment env) {
    return kbase.newKieSession(sessionConfiguration, env);
}
 
Example 17
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the use of PR on constraints involving 'virtual' properties
 * of a POJO: calculated properties without a setter.
 * getFullName doesn't have a setter and Klass3 doesn't state that setName()
 * nor setLastName() @Modifies fullName. We are explicitly using @watches
 * in the rule involving fullName to be aware of modifications in the name
 * and/or lastName of a Klass3 object.
 */
@Test
public void testPRConstraintOnAttributesWithoutSetterUsingWatches(){
    final String str =
            "package org.drools.test;\n" +
            "\n" +
            "import " + Klass3.class.getCanonicalName() + ";\n" +
            "\n" +
            "global java.util.List list;\n" +
            "\n" +
            "rule \"Init\"\n" +
            "when\n" +
            "then\n" +
            "  insert( new Klass3( \"XXX\", \"White\" ) );\n" +
            "end\n" +

            "rule \"Find Heisenberg\"\n" +
            "when\n" +
            "  $x : Klass3( fullName == 'Walter White' ) @watch( name, lastName)\n" +
            "then\n" +
            "  list.add( drools.getRule().getName() );\n" +
            "end\n" +

            "rule \"XXX -> Walter\"\n" +
            "when\n" +
            "  $x : Klass3( name == 'XXX' )\n" +
            "then\n" +
            "  modify($x){ setName('Walter') };\n" +
            "  list.add( drools.getRule().getName() );\n" +
            "end\n" +
            "\n";

    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = kbase.newKieSession();

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

    ksession.fireAllRules();

    assertEquals( 2, list.size() );
    assertEquals( Arrays.asList( "XXX -> Walter", "Find Heisenberg" ), list );
}
 
Example 18
Source File: ProcessStateTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
 @Disabled
 public void FIXMEtestDelayedStateConstraintPriorities1() {
 	KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
     Reader source = new StringReader(
         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
         "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" +
         "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
         "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" +
         "         type=\"RuleFlow\" name=\"flow\" id=\"org.drools.state\" package-name=\"org.drools\" version=\"1\" >\n" +
         "\n" +
         "  <header>\n" +
"    <imports>\n" +
"      <import name=\"org.jbpm.integrationtests.test.Person\" />\n" +
"    </imports>\n" +
"    <globals>\n" +
"      <global identifier=\"list\" type=\"java.util.List\" />\n" +
"    </globals>\n" +
         "  </header>\n" +
         "\n" +
         "  <nodes>\n" +
         "    <start id=\"1\" name=\"Start\" />\n" +
         "    <state id=\"2\" >\n" +
         "      <constraints>\n" +
         "        <constraint toNodeId=\"3\" name=\"one\" priority=\"1\" >\n" +
         "            Person( )" +
         "        </constraint>"+
          "       <constraint toNodeId=\"5\" name=\"two\" priority=\"2\" >\n" +
         "           Person( )" +
         "        </constraint>"+
         "      </constraints>\n" +
         "    </state>\n" +
"    <actionNode id=\"3\" name=\"ActionNode1\" >\n" +
"      <action type=\"expression\" dialect=\"java\" >list.add(\"1\");</action>\n" +
"    </actionNode>\n" +
         "    <end id=\"4\" name=\"End\" />\n" +
"    <actionNode id=\"5\" name=\"ActionNode2\" >\n" +
"      <action type=\"expression\" dialect=\"java\" >list.add(\"2\");</action>\n" +
"    </actionNode>\n" +
         "    <end id=\"6\" name=\"End\" />\n" +
         "  </nodes>\n" +
         "\n" +
         "  <connections>\n" +
         "    <connection from=\"1\" to=\"2\" />\n" +
         "    <connection from=\"2\" to=\"3\" />\n" +
         "    <connection from=\"3\" to=\"4\" />\n" +
         "    <connection from=\"2\" to=\"5\" />\n" +
         "    <connection from=\"5\" to=\"6\" />\n" +
         "  </connections>\n" +
         "\n" +
         "</process>");
     kbuilder.add( ResourceFactory.newReaderResource(source), ResourceType.DRF );
     KieBase kbase = kbuilder.newKieBase();
     KieSession ksession = kbase.newKieSession();
     List<String> list = new ArrayList<String>();
     ksession.setGlobal("list", list);
     ProcessInstance processInstance = ksession.startProcess("org.drools.state");
     assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
     assertTrue(list.isEmpty());
     Person person = new Person("John Doe", 30);
     ksession.insert(person);
     assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
     assertEquals(1, list.size());
     assertEquals("1", list.get(0));
 }
 
Example 19
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithDeclaredTypeAndTraitInDifferentPackages() {
    // DROOLS-91
    final String str1 =
            "package org.pkg1;\n" +
            "declare trait Trait " +
            "    @propertyReactive\n" +
            "    a : int\n" +
            "end";

    final String str2 =
            "package org.pkg2;\n" +
            "declare Bean " +
            "    @propertyReactive\n" +
            "    @Traitable\n" +
            "    a : int\n" +
            "    b : int\n" +
            "end";

    final String str3 =
            "package org.pkg3;\n" +
            "import org.pkg1.Trait;\n" +
            "import org.pkg2.Bean;\n" +
            "rule Init\n" +
            "when\n" +
            "then\n" +
            "    insert(new Bean(1, 2));\n" +
            "end\n" +
            "rule R\n" +
            "when\n" +
            "   $b : Bean( b == 2)" +
            "then\n" +
            "   Trait t = don( $b, Trait.class, true );\n" +
            "   modify(t) { setA(2) };\n" +
            "end";

    final KieBase kbase = loadKnowledgeBaseFromString(str1, str2, str3);
    final KieSession ksession = kbase.newKieSession();

    ksession.fireAllRules();
}
 
Example 20
Source File: WindowTest.java    From kogito-runtimes with Apache License 2.0 3 votes vote down vote up
@BeforeEach
public void initialization() {
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();

    kfs.write("src/main/resources/kbase1/window_test.drl", drl);

    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);

    kbuilder.buildAll();

    List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);

    assertEquals(0, res.size(), res.toString());

    KieBaseConfiguration kbconf = KnowledgeBaseFactory
            .newKnowledgeBaseConfiguration();

    kbconf.setOption(EventProcessingOption.STREAM);

    KieBase kbase = KieServices.Factory.get()
                               .newKieContainer(kbuilder.getKieModule().getReleaseId())
                               .newKieBase(kbconf);

    KieSessionConfiguration ksconfig = KnowledgeBaseFactory
            .newKnowledgeSessionConfiguration();
    ksconfig.setOption(ClockTypeOption.get("pseudo"));

    ksession = kbase.newKieSession(ksconfig, null);

    clock = ksession.getSessionClock();
}