org.kie.internal.utils.KieHelper Java Examples

The following examples show how to use org.kie.internal.utils.KieHelper. 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: 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 #2
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testRTNodeWithConstraintsNoPropertySpecific() {
    String rule = "package org.drools.compiler.integrationtests\n" +
                  "import " + Person.class.getCanonicalName() + "\n" +
                  "rule r1\n" +
                  "when\n" +
                  "   Person( name == 'bobba')\n" +
                  "then\n" +
                  "end\n";
    
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(rule, ResourceType.DRL).build();
    
    ObjectTypeNode otn = getObjectTypeNode(kbase, "Person" );
    assertNotNull( otn );

    AlphaNode alphaNode = ( AlphaNode ) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals( AllSetBitMask.get(), alphaNode.getDeclaredMask() );
    assertEquals( AllSetBitMask.get(), alphaNode.getInferredMask() );
    
    
    LeftInputAdapterNode liaNode = ( LeftInputAdapterNode ) alphaNode.getObjectSinkPropagator().getSinks()[0];
    
    RuleTerminalNode rtNode = ( RuleTerminalNode ) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals( AllSetBitMask.get(), rtNode.getDeclaredMask() );
    assertEquals( AllSetBitMask.get(), rtNode.getInferredMask() );
}
 
Example #3
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 #4
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 #5
Source File: ParallelEvaluationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleParallelKieSessionsWithDeletes() throws InterruptedException, ExecutionException, TimeoutException {
    final int NUMBER_OF_PARALLEL_SESSIONS = 5;

    /* Create KIE base */
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 1; i < 11; i++) {
        sb.append(getRule(i, "delete( $i );\n"));
    }
    for (int i = 1; i < 11; i++) {
        sb.append(getNotRule(i));
    }
    KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL)
            .build(MultithreadEvaluationOption.YES);

    List<Callable<Void>> tasks = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_PARALLEL_SESSIONS; i++) {
        tasks.add(getMultipleParallelKieSessionsWithDeletesCallable(kbase));
    }

    /* Run tasks in parallel */
    runTasksInParallel(tasks);
}
 
Example #6
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerics() throws Exception {
    // DROOLS-4939
    String drl =
            "package org.drools.compiler\n" +
                    "import java.util.List\n" +
                    "declare Node\n" +
                    "    values: List<String>\n" +
                    "end\n" +
                    "rule R1 when\n" +
                    "   $node: Node( values.get(0).length == 4 )\n" +
                    "then\n" +
                    "   System.out.println( $node );\n" +
                    "end";

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

    FactType nodeType = kbase.getFactType( "org.drools.compiler", "Node" );
    Object parent = nodeType.newInstance();
    nodeType.set( parent, "values", Arrays.asList("test") );
    ksession.insert( parent );

    int rules = ksession.fireAllRules();
    assertEquals( 1, rules );
}
 
Example #7
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testBetaNodeWithConstraintsNoPropertySpecific() {
    String rule = "package org.drools.compiler.integrationtests\n" +
                  "import " + Person.class.getCanonicalName() + "\n" +
                  "import " + Cheese.class.getCanonicalName() + "\n" +
                  "rule r1\n" +
                  "when\n" +
                  "   Person()\n" +
                  "   Cheese( type == 'brie' )\n" +
                  "then\n" +
                  "end\n";
    
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(rule, ResourceType.DRL).build();
    
    ObjectTypeNode otn = getObjectTypeNode(kbase, "Cheese" );
    assertNotNull( otn );

    AlphaNode alphaNode = ( AlphaNode ) otn.getObjectSinkPropagator().getSinks()[0];
    assertEquals( AllSetBitMask.get(), alphaNode.getDeclaredMask() );
    assertEquals( AllSetBitMask.get(), alphaNode.getInferredMask() );
    
    BetaNode betaNode = ( BetaNode ) alphaNode.getObjectSinkPropagator().getSinks()[0];
    
    assertEquals( AllSetBitMask.get(), betaNode.getRightDeclaredMask() );
    assertEquals( AllSetBitMask.get(), betaNode.getRightInferredMask() );
}
 
Example #8
Source File: PolymorphismTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testModifySubclassOverWindow() {
    // DROOLS-1501
    String drl = "declare Number @role( event ) end\n" +
                 "declare Integer @role( event ) end\n" +
                 "\n" +
                 "rule R1 no-loop when\n" +
                 "    $i: Integer()\n" +
                 "then\n" +
                 "    update($i);\n" +
                 "end\n" +
                 "rule R2 when\n" +
                 "    $n: Number() over window:length(1)\n" +
                 "then\n" +
                 "end";

    KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
                                         .build( EventProcessingOption.STREAM )
                                         .newKieSession();
    ksession.insert(1);
    ksession.fireAllRules();
}
 
Example #9
Source File: MarshallerTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@ParameterizedMarshallerTest
public void testFromWithFireAfterSerialization(Environment env) throws Exception {
    String str =
            "import java.util.Collection\n" +
                    "rule R1 when\n" +
                    "    String() from [ \"x\", \"y\", \"z\" ]\n" +
                    "then\n" +
                    "end\n";

    KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
    KieSession ksession = null;
    try {
        ksession = kbase.newKieSession(null, env);

        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);

        assertEquals(3, ksession.fireAllRules());
    } finally {
        if (ksession != null) {
            ksession.dispose();
        }
    }
}
 
Example #10
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryWithIncompatibleArgs() {
    String drl = "global java.util.List list; " +
                 "" +
                 "query foo( String $s, String $s, String $s ) end " +
                 "" +
                 "rule React \n" +
                 "when\n" +
                 "  $i : Integer() " +
                 "  foo( $i, $x, $i ; ) " +
                 "then\n" +
                 "end";

    KieHelper helper = new KieHelper();
    helper.addContent( drl, ResourceType.DRL );
    Results results = helper.verify();
    assertTrue( results.hasMessages( Message.Level.ERROR ) );
    assertEquals( 2, results.getMessages( Message.Level.ERROR ).size() );
}
 
Example #11
Source File: PropertySpecificTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testRTNodeNoConstraintsNoPropertySpecific() {
    String rule = "package org.drools.compiler.integrationtests\n" +
                  "import " + Person.class.getCanonicalName() + "\n" +
                  "rule r1\n" +
                  "when\n" +
                  "   Person()\n" +
                  "then\n" +
                  "end\n";
    
    KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(rule, ResourceType.DRL).build();
    
    ObjectTypeNode otn = getObjectTypeNode(kbase, "Person" );
    assertNotNull( otn );

    LeftInputAdapterNode liaNode = ( LeftInputAdapterNode ) otn.getObjectSinkPropagator().getSinks()[0];
    
    RuleTerminalNode rtNode = ( RuleTerminalNode ) liaNode.getSinkPropagator().getSinks()[0];
    assertEquals( AllSetBitMask.get(), rtNode.getDeclaredMask() );
    assertEquals( AllSetBitMask.get(), rtNode.getInferredMask() );
}
 
Example #12
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testRedeclareWithInterfaceExtensionAndOverride() {
    final String s1 = "package test;\n" +

                      "declare trait " + Ext.class.getCanonicalName() + " extends " + Base.class.getCanonicalName() + " " +
                      " fld : String " +
                      "end " +

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

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

    assertEquals( 0, kh.verify().getMessages( Message.Level.ERROR ).size() );
}
 
Example #13
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testModifyWithGetter() {
    final String rule1 =
            "package foo.bar\n" +
            "import " + Person.class.getName() + "\n" +
            "declare Person @propertyReactive end\n" +
            "rule x\n" +
            "    when\n" +
            "       $p : Person( address != null ) @watch(!address) \n" +
            "    then\n" +
            "       modify($p){getAddress().setStreet(\"foo\");}\n" +
            "end";

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

    final Person p = new Person();
    p.setAddress(new Address());
    ksession.insert(p);

    final int fired = ksession.fireAllRules(10);

    assertEquals(1, fired);
    assertEquals("foo", p.getAddress().getStreet());
}
 
Example #14
Source File: OOPathQueriesTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryFromCode() {
    final String drl =
            "import org.drools.compiler.oopath.model.Thing;\n" +
                    "query isContainedIn( Thing $x, Thing $y )\n" +
                    "    $y := /$x/children\n" +
                    "or\n" +
                    "    ( $z := /$x/children and isContainedIn( $z, $y; ) )\n" +
                    "end\n";

    final Thing smartphone = new Thing("smartphone");
    final List<String> itemList = Arrays.asList(new String[] { "display", "keyboard", "processor" });
    itemList.stream().map(item -> new Thing(item)).forEach((thing) -> smartphone.addChild(thing));

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

    final QueryResults queryResults = ksession.getQueryResults("isContainedIn", new Object[] { smartphone, Variable.v });
    final List<String> resultList = StreamSupport.stream(queryResults.spliterator(), false)
            .map(row -> ((Thing) row.get("$y")).getName()).collect(Collectors.toList());
    assertThat(resultList).as("Query does not contain all items").containsAll(itemList);

    ksession.dispose();
}
 
Example #15
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateOnNonExistingProperty() {
    // DROOLS-2170
    final String str1 =
            "import " + Klass.class.getCanonicalName() + "\n" +
            "rule R when\n" +
            "  Klass( b == 2 )\n" +
            "then\n" +
            "end\n";

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

    final Klass bean = new Klass( 1, 2, 3, 4, 5, 6 );
    final FactHandle fh = ksession.insert( bean );
    ksession.fireAllRules();

    try {
        ksession.update( fh, bean, "z" );
        fail("Trying to update not existing property must fail");
    } catch (Exception e) {
        assertTrue( e.getMessage().contains( Klass.class.getName() ) );
    }
}
 
Example #16
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPRAfterAccumulate() {
    // DROOLS-2427
    final String str1 =
            "import " + Order.class.getCanonicalName() + "\n" +
            "import " + OrderLine.class.getCanonicalName() + "\n" +
            "rule R when\n" +
            "        $o: Order($lines: orderLines)\n" +
            "        Number(intValue >= 15) from accumulate(\n" +
            "            OrderLine($q: quantity) from $lines\n" +
            "            , sum($q)\n" +
            "        )\n" +
            "    then\n" +
            "        modify($o) { setPrice(10) }\n" +
            "end\n";

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

    Order order = new Order( Arrays.asList(new OrderLine( 9 ), new OrderLine( 8 )), 12 );
    ksession.insert( order );
    ksession.fireAllRules();

    assertEquals( 10, order.getPrice() );
}
 
Example #17
Source File: KieSessionIterationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setup() {
    this.kieBase = new KieHelper().build();
    // create several KieSessions
    this.kieBase.newKieSession();
    this.kieBase.newKieSession();
    this.kieBase.newKieSession();
}
 
Example #18
Source File: PropertyReactivityTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testReassignment() {
    // DROOLS-4884
    final String str =
            "package com.example\n" +
                    "\n" +
                    "declare Counter\n" +
                    "    value1: int\n" +
                    "    value2: int\n" +
                    "end\n" +
                    "\n" +
                    "rule \"Init\" when\n" +
                    "    not Counter()\n" +
                    "then\n" +
                    "    drools.insert(new Counter(0, 0));\n" +
                    "end\n" +
                    "\n" +
                    "rule \"Loop\"\n" +
                    "when\n" +
                    "    $c: Counter( value1 == 0 )\n" +
                    "then\n" +
                    "    $c = new Counter(0, 0);\n" +
                    "    $c.setValue2(1);\n" +
                    "    update($c);\n" +
                    "end\n\n";

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

    assertEquals( 5, ksession.fireAllRules(5) );
}
 
Example #19
Source File: OOPathTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDereferencedDeclarationOutsideOOPath() {
    // DROOLS-1411
    final String drl =
            "import org.drools.compiler.oopath.model.*;\n" +
            "global java.util.Set duplicateNames; \n" +
            "\n" +
            "rule DIFF_FILES_BUT_WITH_SAME_FILENAME when\n" +
            "  $dir1 : TMFileSet( $ic1 : /files )\n" +
            "  TMFileSet( this == $dir1, $ic2 : /files[this != $ic1], $ic2.name == $ic1.name )\n" +
            "then\n" +
            "  System.out.println( $dir1 + \".: \" + $ic1 + \" \" + $ic2 );\n" +
            "  duplicateNames.add( $ic1.getName() );\n" +
            "end\n";

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

    final Set<String> duplicateNames = new HashSet<>();
    ksession.setGlobal("duplicateNames", duplicateNames);

    final TMFileSet x = new TMFileSet("X");
    final TMFile file0 = new TMFile("File0", 47);
    final TMFile file1 = new TMFile("File1", 47);
    final TMFile file2 = new TMFile("File0", 47);
    x.getFiles().addAll(Arrays.asList(file0, file1, file2));

    ksession.insert(x);
    ksession.fireAllRules();

    assertTrue( duplicateNames.contains("File0") );
    assertFalse( duplicateNames.contains("File1") );
}
 
Example #20
Source File: SessionsPoolTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private KieHelper getKieHelper() {
    String drl =
            "global java.util.List list\n" +
                    "rule R1 when\n" +
                    "  $s: String()\n" +
                    "then\n" +
                    "  list.add($s);\n" +
                    "end\n";

    return new KieHelper().addContent( drl, ResourceType.DRL );
}
 
Example #21
Source File: TruthMaintenanceTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteLogicalAssertionFromRule() {
    // BZ-1317026
    String drl =
            "global java.util.List list;\n" +
            "rule R1 when\n" +
            "then\n" +
            "    insertLogical( \"test\" ); \n" +
            "end\n" +
            "rule R2 when\n" +
            "    $s : String()\n" +
            "then\n" +
            "    list.add( $s ); \n" +
            "    delete( $s ); \n" +
            "end\n";

    KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
                                         .build()
                                         .newKieSession();
    try {
        List<String> list = new ArrayList<String>();
        ksession.setGlobal( "list", list );

        ksession.fireAllRules();
        assertEquals(1, list.size());
        assertEquals("test", list.get(0));

        Collection<FactHandle> fhs = ksession.getFactHandles( new ClassObjectFilter( String.class ) );
        assertEquals(0, fhs.size());
    } finally {
        ksession.dispose();
    }
}
 
Example #22
Source File: TruthMaintenanceTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteLogicalAssertion() {
    // BZ-1317026
    String drl =
            "rule R1 when\n" +
            "then\n" +
            "    insertLogical( \"test\" ); \n" +
            "end\n";

    KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
                                         .build()
                                         .newKieSession();
    try {
        ksession.fireAllRules();
        Collection<FactHandle> fhs = ksession.getFactHandles( new ClassObjectFilter( String.class ) );
        assertEquals(1, fhs.size());

        for (FactHandle fh : fhs) {
            ksession.delete( fh );
        }

        fhs = ksession.getFactHandles( new ClassObjectFilter( String.class ) );
        assertEquals(0, fhs.size());
    } finally {
        ksession.dispose();
    }
}
 
Example #23
Source File: OOPathOnGraphTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testOOPathOnGraphWithNonReactiveContentModification() {
    String drl =
            "import org.drools.compiler.oopath.graph.*;\n" +
            "import " + Library.class.getCanonicalName() + ";\n" +
            "import " + Book.class.getCanonicalName() + ";\n" +
            "import " + Person.class.getCanonicalName() + ";\n" +
            "global java.util.List list\n" +
            "\n" +
            "rule R when\n" +
            "  Vertex( it instanceof Library, $v : /outVs/outVs[ it#Person.age > 25 ] )\n" +
            "then\n" +
            "  list.add( ((Person)$v.getIt()).getName() );\n" +
            "end\n";

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

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

    Vertex<Library> library = getGraph();
    ksession.insert( library );
    ksession.fireAllRules();

    assertEquals( 1, list.size() );
    assertTrue( list.contains( "Mario" ) );
    list.clear();

    Person raoul = (Person)library.getOutVs().get(0).getOutVs().get(0).getIt();
    assertEquals( "Raoul", raoul.getName() );
    raoul.setAge( raoul.getAge() + 1 );

    ksession.fireAllRules();
    assertEquals( 0, list.size() );
}
 
Example #24
Source File: OOPathTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testOOPathWithLocalInnerDeclaration() {
    // DROOLS-1411
    final String drl =
            "import org.drools.compiler.oopath.model.*;\n" +
            "import "+TMFileSetQuater.class.getCanonicalName()+";\n" +
            "global java.util.Set duplicateNames; \n" +
            "\n" +
            "rule DIFF_FILES_BUT_WITH_SAME_FILENAME when\n" +
            "  $ic1 : TMFileWithParentObj( $curName : name, $curId : id, \n" +
            "                               $ic2: /parent#TMFileSetQuater/files[name == $curName, id != $curId ] )\n" +
            "then\n" +
            "  System.out.println( $ic1 + \" \" + $ic2 );\n" +
            "  duplicateNames.add( $ic1.getName() );\n" +
            "end\n";

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

    final Set duplicateNames = new HashSet();
    ksession.setGlobal("duplicateNames", duplicateNames);

    final TMFileSetQuater x = new TMFileSetQuater("X");
    final TMFileWithParentObj file0 = new TMFileWithParentObj(0, "File0", 47, x);
    final TMFileWithParentObj file1 = new TMFileWithParentObj(1, "File1", 47, x);
    final TMFileWithParentObj file2 = new TMFileWithParentObj(2, "File0", 47, x);
    x.getFiles().addAll(Arrays.asList(file0, file1, file2));

    ksession.insert( x );
    ksession.insert( file0 );
    ksession.insert( file1 );
    ksession.insert( file2 );
    ksession.fireAllRules();

    assertTrue( duplicateNames.contains("File0") );
    assertFalse( duplicateNames.contains("File1") );
}
 
Example #25
Source File: MemoryLeakTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testStagedLeftTupleLeak() throws Exception {
    // BZ-1058874
    String str =
            "rule R1 when\n" +
            "    String( this == \"this\" )\n" +
            "    String( this == \"that\" )\n" +
            "then\n" +
            "end\n";

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

    for ( int i = 0; i < 10; i++ ) {
        FactHandle fh = ksession.insert( "this" );
        ksession.fireAllRules();
        ksession.delete( fh );
        ksession.fireAllRules();
    }

    Rete rete = ( (KnowledgeBaseImpl) kbase ).getRete();
    LeftInputAdapterNode liaNode = null;
    for ( ObjectTypeNode otn : rete.getObjectTypeNodes() ) {
        if ( String.class == otn.getObjectType().getValueType().getClassType() ) {
            AlphaNode alphaNode = (AlphaNode) otn.getObjectSinkPropagator().getSinks()[0];
            liaNode = (LeftInputAdapterNode) alphaNode.getObjectSinkPropagator().getSinks()[0];
            break;
        }
    }

    assertNotNull( liaNode );
    InternalWorkingMemory wm = (InternalWorkingMemory) ksession;
    LeftInputAdapterNode.LiaNodeMemory memory = (LeftInputAdapterNode.LiaNodeMemory) wm.getNodeMemory( liaNode );
    TupleSets<LeftTuple> stagedLeftTuples = memory.getSegmentMemory().getStagedLeftTuples();
    assertNull( stagedLeftTuples.getDeleteFirst() );
    assertNull( stagedLeftTuples.getInsertFirst() );
}
 
Example #26
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericsMap() throws Exception {
    // DROOLS-4939
    String drl =
            "package org.drools.compiler\n" +
                    "import " + ValuesProvider.class.getCanonicalName() + "\n" +
                    "import java.util.Map\n" +
                    "declare Node extends ValuesProvider\n" +
                    "    values: Map<String, String>\n" +
                    "end\n" +
                    "rule R1 when\n" +
                    "   $node: Node( values.get(\"value\").length == 4 )\n" +
                    "then\n" +
                    "   System.out.println( $node );\n" +
                    "end";

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

    FactType nodeType = kbase.getFactType( "org.drools.compiler", "Node" );
    Object parent = nodeType.newInstance();
    Map<String,String> map = new HashMap<>();
    map.put("value", "test");
    nodeType.set( parent, "values", map );
    ksession.insert( parent );

    int rules = ksession.fireAllRules();
    assertEquals( 1, rules );
}
 
Example #27
Source File: EnableAuditLogCommandTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnableAuditLogCommand() throws Exception {

    String str = "";
    str += "package org.drools.compiler.integrationtests \n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "rule StringRule \n";
    str += " when \n";
    str += " $c : Cheese() \n";
    str += " then \n";
    str += " System.out.println($c); \n";
    str += "end \n";

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

    List<Command> commands = new ArrayList<Command>();
    commands.add( CommandFactory.newEnableAuditLog( auditFileDir, auditFileName ) );
    commands.add( CommandFactory.newInsert( new Cheese() ) );
    commands.add( CommandFactory.newFireAllRules() );
    kSession.execute( CommandFactory.newBatchExecution( commands ) );
    kSession.dispose();

    File file = new File( auditFileDir + File.separator + auditFileName + ".log" );

    assertTrue( file.exists() );

}
 
Example #28
Source File: OOPathTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrimitives() {
    // DROOLS-1266
    final String drl =
            "import org.drools.compiler.oopath.model.*;\n" +
            "global java.util.List list\n" +
            "\n" +
            "rule R when\n" +
            "  Adult( $x: /children.age )\n" +
            "then\n" +
            "  list.add( $x );\n" +
            "end\n";

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

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

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

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

    assertThat(list).hasSize(2);
}
 
Example #29
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testQueryWithOptionalOr() {
    // DROOLS-1386
    String str =
            "package org.test\n" +
            "import " + Question.class.getCanonicalName() + "\n" +
            "import " + QuestionVisible.class.getCanonicalName() + "\n" +
            "query QuestionsKnowledge\n" +
            "    $question: Question()\n" +
            "    $visible: QuestionVisible(question == $question) or not QuestionVisible(question == $question)\n" +
            "end\n";

    KieSession ksession = new KieHelper().addContent( str, ResourceType.DRL ).build().newKieSession();
    Question question = new Question();
    ksession.insert( question );
    QueryResults results = ksession.getQueryResults("QuestionsKnowledge");
    assertEquals( 1, results.size() );
    QueryResultsRow row = results.iterator().next();
    assertSame( question, row.get( "$question" ) );

    QuestionVisible questionVisible = new QuestionVisible( question );
    ksession.insert( questionVisible );
    results = ksession.getQueryResults("QuestionsKnowledge");
    assertEquals( 1, results.size() );
    row = results.iterator().next();
    assertSame( question, row.get( "$question" ) );
    assertSame( questionVisible, row.get( "$visible" ) );
}
 
Example #30
Source File: OOPathTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithExists() {
    String header =
            "import org.drools.compiler.oopath.model.*;\n" +
            "global java.util.List list\n\n";

    String drl1 =
            "rule R1 when\n" +
            "  exists( Man( $m: /wife[age == 25] ) )\n" +
            "then\n" +
            "  list.add(\"Found\");\n" +
            "end\n\n";

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

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

    final Man bob = new Man("John", 25);
    bob.setWife( new Woman("Jane", 25) );

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

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