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

The following examples show how to use org.kie.internal.utils.KieHelper#addContent() . 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: 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 3
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 4
Source File: EnumTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnumsWithCompositeBuildingProcess() throws Exception {
    final String drl = "package org.test; " +
                 "" +
                 "declare enum DaysOfWeek " +
                 "    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;\n" +
                 "end\n" +

                 "declare Test " +
                 "  field: DaysOfWeek " +
                 "end";

    final KieHelper kieHelper = new KieHelper();
    kieHelper.addContent( drl, ResourceType.DRL );
    final Results res = kieHelper.verify();
    assertEquals( 0, res.getMessages().size() );
}
 
Example 5
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryWithWrongParamNumber() {
    String drl = "global java.util.List list; " +
                 "" +
                 "query foo( Integer $i ) end " +
                 "" +
                 "rule React \n" +
                 "when\n" +
                 "  $i : Integer() " +
                 "  $j : Integer() " +
                 "  foo( $i, $j ; ) " +
                "then\n" +
                 "end";

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    Results results = helper.verify();
    assertTrue( results.hasMessages( Message.Level.ERROR ) );
    assertEquals( 1, results.getMessages( Message.Level.ERROR ).size() );
}
 
Example 6
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryWithSyntaxError() {
    String drl = "global java.util.List list; " +
                 "" +
                 "query foo( Integer $i ) end " +
                 "" +
                 "rule React \n" +
                 "when\n" +
                 "  $i : Integer() " +
                 "  foo( $i ) " +   // missing ";" should result in 1 compilation error
                 "then\n" +
                 "end";

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    Results results = helper.verify();
    assertTrue( results.hasMessages( Message.Level.ERROR ) );
    assertEquals( 1, results.getMessages( Message.Level.ERROR ).size() );
}
 
Example 7
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test()
public void testTraitExtendPojo() {
    //DROOLS-697
    final String s1 = "package test;\n" +

                      "declare Poojo " +
                      "end " +

                      "declare trait Mask extends Poojo " +
                      "end " +
                      "";

    KieHelper kh = new KieHelper();
    kh.addContent( s1, ResourceType.DRL );

    assertEquals( 1, kh.verify().getMessages( Message.Level.ERROR ).size() );
}
 
Example 8
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 9
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryWithClassArg() {
    //DROOLS-590
    String drl = "global java.util.List list; " +
                 "" +
                 "declare Foo end " +
                 "" +
                 "query bar( Class $c ) " +
                 "  Class( this.getName() == $c.getName() ) " +
                 "end " +
                 "query bar2( Class $c ) " +
                 "  Class( this == $c ) " +
                 "end " +
                 "" +
                 "rule Init when then insert( Foo.class ); end " +
                 "" +
                 "rule React1 " +
                 "when " +
                 "  bar( Foo.class ; ) " +
                 "then " +
                 "  list.add( 'aa' ); " +
                 "end  " +

                 "rule React2 " +
                 "when\n" +
                 "  bar2( Foo.class ; ) " +
                 "then " +
                 "  list.add( 'bb' ); " +
                 "end";

    List list = new ArrayList(  );
    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    KieSession ks = helper.build(  ).newKieSession();
    ks.setGlobal( "list", list );
    ks.fireAllRules();

    assertEquals( Arrays.asList( "aa", "bb" ), list );
}
 
Example 10
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testRedeclareClassAsTrait() {
    final String s1 = "package test; " +
                      "global java.util.List list; " +

                      "declare trait " + SomeClass.class.getCanonicalName() + " end ";

    KieHelper kh = new KieHelper();
    kh.addContent( s1, ResourceType.DRL );

    assertEquals( 1, kh.verify().getMessages( Message.Level.ERROR ).size() );
}
 
Example 11
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 12
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryWithAccessorAsArgument() throws Exception {
    // DROOLS-414
    String str =
            "import org.drools.compiler.Person\n" +
            "global java.util.List persons;\n" +
            "\n" +
            "query contains(String $s, String $c)\n" +
            "    $s := String( this.contains( $c ) )\n" +
            "end\n" +
            "\n" +
            "rule R when\n" +
            "    $p : Person()\n" +
            "    contains( $p.name, \"a\"; )\n" +
            "then\n" +
            "    persons.add( $p );\n" +
            "end\n";

    KieHelper helper = new KieHelper();
    helper.addContent( str, ResourceType.DRL );
    KieSession ksession = helper.build().newKieSession();

    List<Person> personsWithA = new ArrayList<Person>();
    ksession.setGlobal("persons", personsWithA);

    ksession.insert("Mark");
    ksession.insert("Edson");
    ksession.insert("Mario");
    ksession.insert(new Person("Mark"));
    ksession.insert(new Person("Edson"));
    ksession.insert(new Person("Mario"));
    ksession.fireAllRules();

    assertEquals(2, personsWithA.size());
    for (Person p : personsWithA) {
        assertTrue( p.getName().equals( "Mark" ) || p.getName().equals( "Mario" ) );
    }
}
 
Example 13
Source File: AbductionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testAbductiveLogicNoConstructorFoundError() {
    String droolsSource =
            "package org.drools.abductive.test; \n" +
            "" +
            "import " + Abducible.class.getName() + "; \n" +
            "global java.util.List list; \n" +
            "" +
            "declare Foo \n" +
            "   @Abducible \n" +
            "   id : Integer @key \n" +
            "end \n" +

            "query foo( String $x ) \n" +
                // Foo does not have a String constructor
            "   @Abductive( target=Foo.class ) \n" +
            "end \n" +

            "rule R1 " +
            "when " +
            "   $x : foo( \"x\" ; ) " +
            "then " +
            "   list.add( $x ); " +
            "end \n" +
            "" +
            "";
    /////////////////////////////////////

    KieHelper kieHelper = new KieHelper();
    kieHelper.addContent( droolsSource, ResourceType.DRL );

    Results res = kieHelper.verify();
    assertEquals( 1, res.getMessages( Message.Level.ERROR ).size() );
}
 
Example 14
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclarationOfClassWithNonStandardSetterAndFulllName() {
    final String s1 = "package test; " +
                      "import " + BeanishClass.class.getCanonicalName() + "; " +

                      "declare " + BeanishClass.class.getName() + " @propertyReactive end " +

                      "rule Check when BeanishClass() @watch( foo ) then end ";

    KieHelper kh = new KieHelper();
    kh.addContent( s1, ResourceType.DRL );

    assertEquals( 0, kh.verify().getMessages( Message.Level.ERROR ).size() );
}
 
Example 15
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeclarationOfClassWithNonStandardSetterAndCanonicalName() {
    // DROOLS-815
    final String s1 = "package test; " +
                      "import " + BeanishClass.class.getCanonicalName() + "; " +

                      "declare " + BeanishClass.class.getCanonicalName() + " @propertyReactive end " +

                      "rule Check when BeanishClass() @Watch( foo ) then end ";

    KieHelper kh = new KieHelper();
    kh.addContent( s1, ResourceType.DRL );

    assertEquals( 0, kh.verify().getMessages( Message.Level.ERROR ).size() );
}
 
Example 16
Source File: AbductionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testAbductiveLogicWithNonExistingArgsMapping() {
    String droolsSource =
            "package org.drools.abductive.test; \n" +
            "" +
            "import " + Abducible.class.getName() + "; \n" +
            "global java.util.List list; \n" +
            "" +
            "declare Foo \n" +
            "   @Abducible \n" +
            "   id : String @key \n" +
            "   name : String @key \n" +
            "end \n" +

            "query foo( String $name ) \n" +
            "   @Abductive( target=Foo.class, args={ $missing, $name } ) \n" +
            "end \n" +

            "";
    /////////////////////////////////////

    KieHelper kieHelper = new KieHelper();
    kieHelper.addContent( droolsSource, ResourceType.DRL );

    Results res = kieHelper.verify();
    assertEquals( 1, res.getMessages( Message.Level.ERROR ).size() );
}
 
Example 17
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 18
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 19
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testIndexedNotWatchedProperty() {
    // DROOLS-569
    final String rule1 =
            "package com.sample;\n" +
            "import " + MyClass.class.getCanonicalName() + ";\n" +
            "global java.util.List list;\n" +
            "rule R1 when\n" +
            "    $s : String()\n" +
            "    $m : MyClass( data != null, value == $s ) @watch( !* )\n" +
            "then \n" +
            "    list.add($s);\n" +
            "    modify( $m ) { setValue(\"2\") };\n" +
            "end\n" +
            "\n" +
            "rule R2 when\n" +
            "    $i : Integer()\n" +
            "    $m : MyClass( value == $i.toString(), data == \"x\" ) @watch( !value )\n" +
            "then \n" +
            "    modify( $m ) { setValue(\"3\"), setData(\"y\") };\n" +
            "end";

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

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

    final MyClass myClass = new MyClass();
    myClass.setValue("1");
    myClass.setData("x");
    ksession.insert(myClass);
    ksession.insert("1");
    ksession.insert(2);
    ksession.fireAllRules();

    assertEquals(1, list.size());
    assertEquals("1", list.get(0));
    list.clear();

    ksession.insert("3");
    ksession.fireAllRules();

    assertEquals(1, list.size());
    assertEquals("3", list.get(0));
}
 
Example 20
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testQueryWithExpressionAsArgument() throws Exception {
    // DROOLS-414
    String str =
            "import org.drools.compiler.Person\n" +
            "global java.util.List persons;\n" +
            "\n" +
            "query checkLength(String $s, int $l)\n" +
            "    $s := String( length == $l )\n" +
            "end\n" +
            "\n" +
            "rule R when\n" +
            "    $i : Integer()\n" +
            "    $p : Person()\n" +
            "    checkLength( $p.name, 1 + $i + $p.age; )\n" +
            "then\n" +
            "    persons.add( $p );\n" +
            "end\n";

    KieHelper helper = new KieHelper();
    helper.addContent( str, ResourceType.DRL );
    KieBase kbase = SerializationHelper.serializeObject(helper.build());
    KieSession ksession = kbase.newKieSession();

    List<Person> list = new ArrayList<Person>();
    ksession.setGlobal("persons", list);

    ksession.insert(1);
    ksession.insert("Mark");
    ksession.insert("Edson");
    ksession.insert("Mario");
    ksession.insert(new Person("Mark", 2));
    ksession.insert(new Person("Edson", 3));
    ksession.insert(new Person("Mario", 4));
    ksession.fireAllRules();

    System.out.println(list);
    assertEquals(2, list.size());
    for (Person p : list) {
        assertTrue( p.getName().equals( "Mark" ) || p.getName().equals( "Edson" ) );
    }
}