Java Code Examples for org.drools.core.impl.InternalKnowledgeBase#removeRule()

The following examples show how to use org.drools.core.impl.InternalKnowledgeBase#removeRule() . 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: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicRuleRemovalsUnusedWorkingMemorySubNetwork() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase( "test_DynamicRulesWithSubnetwork1.drl",
                                                                             "test_DynamicRulesWithSubnetwork2.drl",
                                                                             "test_DynamicRulesWithSubnetwork.drl" );

    assertEquals( 2, kbase.getKiePackages().size() );
    assertEquals( 4,
                  kbase.getPackagesMap().get("org.drools.compiler").getRules().size() );

    kbase.removeRule( "org.drools.compiler", "Apply Discount on all books" );
    assertEquals( 3,
                  kbase.getPackagesMap().get("org.drools.compiler").getRules().size() );

    kbase.removeRule( "org.drools.compiler", "like book" );
    assertEquals( 2,
                  kbase.getPackagesMap().get("org.drools.compiler").getRules().size() );

    kbase.removeKiePackage( "org.drools.compiler" );
    assertEquals( 1,
                  kbase.getKiePackages().size() );
}
 
Example 2
Source File: FailureOnRemovalTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private void runTest(boolean shareBetaNodes) throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) createKnowledgeBase( shareBetaNodes );
    Collection<KiePackage> rule1 = compileRule( RULE_1 );
    kbase.addPackages( rule1 );

    // we need to add at least two rules. Test will not fail with only one rule.
    Collection<KiePackage> rule2 = compileRule( RULE_2 );
    kbase.addPackages( rule2 );

    kbase.removeRule( PACKAGE,
                      RULE_1 );
    
    KieSession ksession = createKnowledgeSession(kbase);
    int fired = ksession.fireAllRules();
    ksession.dispose();
    
    assertEquals( 1, fired );

    Collection<KiePackage> rule3 = compileRule( RULE_3 );
    kbase.addPackages( rule3 );
}
 
Example 3
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPathMemorySizeAfterSegmentMergeNonInitialized() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;) B(1;)\n" );
    kbase1.addPackages( buildKnowledgePackage("r2", "   A(1;)\n") );

    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());

    RuleTerminalNode rtn1 = getRtn( "org.kie.r1", kbase1 );
    RuleTerminalNode rtn2 = getRtn( "org.kie.r2", kbase1 );

    assertEquals( 1, wm.getNodeMemory(rtn1).getSegmentMemories().length );
    assertEquals( 1, wm.getNodeMemory(rtn2).getSegmentMemories().length );

    kbase1.removeRule("org.kie", "r2");
    assertEquals( 1, wm.getNodeMemory(rtn1).getSegmentMemories().length );
}
 
Example 4
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testPathMemorySizeAfterSegmentMerge() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;) B(1;)\n" );
    kbase1.addPackages( buildKnowledgePackage("r2", "   A(1;)\n") );
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    // trigger segment initialization
    wm.insert(new A(1));
    wm.insert(new B(1));
    wm.fireAllRules();

    RuleTerminalNode rtn1 = getRtn( "org.kie.r1", kbase1 );
    RuleTerminalNode rtn2 = getRtn( "org.kie.r2", kbase1 );

    assertEquals( 1, wm.getNodeMemory(rtn1).getSegmentMemories().length );
    assertEquals( 1, wm.getNodeMemory(rtn2).getSegmentMemories().length );

    kbase1.removeRule("org.kie", "r2");
    assertEquals( 1, wm.getNodeMemory(rtn1).getSegmentMemories().length );
}
 
Example 5
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPopulatedSingleRuleNoSharingWithSubnetworkAtStart() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase.newKieSession());

    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new D(1));
    wm.insert(new E(1));

    wm.insert(new C(2));
    wm.fireAllRules();


    kbase.addPackages( buildKnowledgePackage("r1", "   A() not( B() and C() ) D() E()\n") );
    List list = new ArrayList();
    wm.setGlobal("list", list);

    wm.fireAllRules();
    assertEquals(2, list.size() );
    assertEquals( "r1", ((Match)list.get(0)).getRule().getName() );
    assertEquals( "r1", ((Match)list.get(1)).getRule().getName() );

    kbase.removeRule("org.kie", "r1");
    wm.insert(new A(1));
    wm.fireAllRules();
    assertEquals(2, list.size() );
}
 
Example 6
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamicRuleRemovalsUnusedWorkingMemory() throws Exception {

    InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject( loadKnowledgeBase( "test_Dynamic1.drl", 
                                                                                  "test_Dynamic2.drl", 
                                                                                  "test_Dynamic3.drl", 
                                                                                  "test_Dynamic4.drl" ) );

    KieSession workingMemory = createKnowledgeSession( kbase );

    assertEquals( 2,
                  kbase.getKiePackages().size() );

    KiePackage knowledgePackage = null;
    for (KiePackage pkg : kbase.getKiePackages()) {
        if ( pkg.getName().equals( "org.drools.compiler.test" ) ) {
            knowledgePackage = pkg;
            break;
        }
    }

    assertEquals( 5,
                  knowledgePackage.getRules().size() );

    kbase.removeRule( "org.drools.compiler.test",
                      "Who likes Stilton" );
    assertEquals( 4,
                  knowledgePackage.getRules().size() );

    kbase.removeRule( "org.drools.compiler.test",
                      "like cheese" );
    assertEquals( 3,
                  knowledgePackage.getRules().size() );

    kbase.removeKiePackage( "org.drools.compiler.test" );
    assertEquals( 1,
                  kbase.getKiePackages().size() );
}
 
Example 7
Source File: PathEndNodeTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubNetworkSharing() throws Exception {
    String str =
            "package org.test \n" +
            "rule xxx \n" +
            "when \n" +
            "  $s : String()\n" +
            "  not( Integer() and Long() )\n" +
            "then \n" +
            "end  \n" +
            "rule yyy \n" +
            "when \n" +
            "  $s : String()\n" +
            "  not( Integer() and Long() )\n" +
            "then \n" +
            "end  \n";

    InternalKnowledgeBase kbase = (InternalKnowledgeBase) new KieHelper().addContent( str, ResourceType.DRL ).build();

    EntryPointNode epn = kbase.getRete().getEntryPointNode( EntryPointId.DEFAULT );
    ObjectTypeNode otn = epn.getObjectTypeNodes().get( new ClassObjectType( Long.class) );
    BetaNode beta1 = (BetaNode) otn.getObjectSinkPropagator().getSinks()[0];
    RightInputAdapterNode rian = (RightInputAdapterNode) beta1.getSinkPropagator().getSinks()[0];
    BetaNode beta2 = (BetaNode) rian.getObjectSinkPropagator().getSinks()[0];
    LeftTupleSink[] sinks = beta2.getSinkPropagator().getSinks();
    RuleTerminalNode rtn1 = (RuleTerminalNode) sinks[0];
    RuleTerminalNode rtn2 = (RuleTerminalNode) sinks[1];

    assertEquals(3, rian.getPathEndNodes().length);
    assertTrue( asList(rian.getPathEndNodes()).containsAll( asList(rtn1, rtn2, rian) ) );

    kbase.removeRule( "org.test", "xxx" );

    assertEquals(2, rian.getPathEndNodes().length);
    RuleTerminalNode remainingRTN = rtn1.getRule().getName().equals( "yyy" ) ? rtn1 : rtn2;
    assertTrue( asList(rian.getPathEndNodes()).containsAll( asList(remainingRTN, rian) ) );
}
 
Example 8
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testPopulatedSharedLiaNodeNoBeta() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    A a1 = new A(1);
    InternalFactHandle fh1 = (InternalFactHandle) wm.insert(a1);
    A a2 = new A(2);
    InternalFactHandle fh2 = (InternalFactHandle) wm.insert(a2);

    wm.fireAllRules();
    assertEquals( 2, list.size() );

    kbase1.addPackages( buildKnowledgePackage("r2", "   a : A()\n") );
    wm.fireAllRules();
    assertEquals( 4, list.size() );

    kbase1.removeRule("org.kie", "r1");
    kbase1.removeRule("org.kie", "r2");
    list.clear();

    assertNull( fh1.getFirstLeftTuple() );
    assertNull( fh2.getFirstLeftTuple() );
    wm.update( fh1,a1 );
    wm.update( fh2,a2 );
    wm.fireAllRules();

    assertEquals( 0, list.size() );
}
 
Example 9
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
     public void testPopulatedMultipleSharesRemoveFirst() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) D() E()\n" );
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    InternalFactHandle fh1 = ( InternalFactHandle ) wm.insert(new A(1));
    InternalFactHandle fh2 = ( InternalFactHandle ) wm.insert(new A(2));
    InternalFactHandle fh3 = ( InternalFactHandle ) wm.insert(new A(2));
    InternalFactHandle fh4 = ( InternalFactHandle ) wm.insert(new A(3));
    InternalFactHandle fh5 =  (InternalFactHandle) wm.insert(new B(1));
    InternalFactHandle fh6 =  (InternalFactHandle) wm.insert(new B(2));
    InternalFactHandle fh7 =  (InternalFactHandle) wm.insert(new C(1));
    InternalFactHandle fh8 =  (InternalFactHandle) wm.insert(new C(2));
    InternalFactHandle fh9 =  (InternalFactHandle) wm.insert(new D(1));
    InternalFactHandle fh10 =  (InternalFactHandle) wm.insert(new E(1));

    wm.fireAllRules();
    assertEquals( 2, list.size() );

    kbase1.addPackages( buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) D() E()\n") );
    kbase1.addPackages( buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) D() E()\n") );

    wm.fireAllRules();
    assertEquals( 5, list.size() );


    kbase1.removeRule("org.kie", "r1");
    list.clear();

    wm.update( fh1, fh1.getObject() );
    wm.update( fh2, fh2.getObject() );
    wm.update( fh3, fh3.getObject() );
    wm.update( fh4, fh4.getObject() );
    wm.update( fh5, fh5.getObject() );
    wm.update( fh6, fh6.getObject() );
    wm.update( fh7, fh7.getObject() );
    wm.update( fh8, fh8.getObject() );
    wm.update( fh9, fh9.getObject() );
    wm.update( fh10, fh10.getObject() );

    wm.fireAllRules();
    assertEquals( 3, list.size() );
}
 
Example 10
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopulatedMultipleSharesRemoveMid() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) D() E()\n" );
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    InternalFactHandle fh1 = ( InternalFactHandle ) wm.insert(new A(1));
    InternalFactHandle fh2 = ( InternalFactHandle ) wm.insert(new A(2));
    InternalFactHandle fh3 = ( InternalFactHandle ) wm.insert(new A(2));
    InternalFactHandle fh4 = ( InternalFactHandle ) wm.insert(new A(3));
    InternalFactHandle fh5 =  (InternalFactHandle) wm.insert(new B(1));
    InternalFactHandle fh6 =  (InternalFactHandle) wm.insert(new B(2));
    InternalFactHandle fh7 =  (InternalFactHandle) wm.insert(new C(1));
    InternalFactHandle fh8 =  (InternalFactHandle) wm.insert(new C(2));
    InternalFactHandle fh9 =  (InternalFactHandle) wm.insert(new D(1));
    InternalFactHandle fh10 =  (InternalFactHandle) wm.insert(new E(1));

    wm.fireAllRules();
    assertEquals( 2, list.size() );

    kbase1.addPackages( buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) D() E()\n") );
    kbase1.addPackages( buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) D() E()\n") );

    wm.fireAllRules();
    assertEquals( 5, list.size() );


    kbase1.removeRule("org.kie", "r2");
    list.clear();

    wm.update( fh1, fh1.getObject() );
    wm.update( fh2, fh2.getObject() );
    wm.update( fh3, fh3.getObject() );
    wm.update( fh4, fh4.getObject() );
    wm.update( fh5, fh5.getObject() );
    wm.update( fh6, fh6.getObject() );
    wm.update( fh7, fh7.getObject() );
    wm.update( fh8, fh8.getObject() );
    wm.update( fh9, fh9.getObject() );
    wm.update( fh10, fh10.getObject() );

    wm.fireAllRules();
    assertEquals( 3, list.size() );
}
 
Example 11
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopulatedMultipleSharesRemoveLast() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;)  A(2;) B(1;) B(2;) C(1;) D() E()\n" );
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    InternalFactHandle fh1 = ( InternalFactHandle ) wm.insert(new A(1));
    InternalFactHandle fh2 = ( InternalFactHandle ) wm.insert(new A(2));
    InternalFactHandle fh3 = ( InternalFactHandle ) wm.insert(new A(2));
    InternalFactHandle fh4 = ( InternalFactHandle ) wm.insert(new A(3));
    InternalFactHandle fh5 =  (InternalFactHandle) wm.insert(new B(1));
    InternalFactHandle fh6 =  (InternalFactHandle) wm.insert(new B(2));
    InternalFactHandle fh7 =  (InternalFactHandle) wm.insert(new C(1));
    InternalFactHandle fh8 =  (InternalFactHandle) wm.insert(new C(2));
    InternalFactHandle fh9 =  (InternalFactHandle) wm.insert(new D(1));
    InternalFactHandle fh10 =  (InternalFactHandle) wm.insert(new E(1));

    wm.fireAllRules();
    assertEquals( 2, list.size() );

    kbase1.addPackages( buildKnowledgePackage("r2", "   A(1;)  A(2;) B(1;) B(2;) C(2;) D() E()\n") );
    kbase1.addPackages( buildKnowledgePackage("r3", "   A(1;)  A(3;) B(1;) B(2;) C(2;) D() E()\n") );

    wm.fireAllRules();
    assertEquals( 5, list.size() );


    kbase1.removeRule("org.kie", "r3");
    list.clear();

    wm.update( fh1, fh1.getObject() );
    wm.update( fh2, fh2.getObject() );
    wm.update( fh3, fh3.getObject() );
    wm.update( fh4, fh4.getObject() );
    wm.update( fh5, fh5.getObject() );
    wm.update( fh6, fh6.getObject() );
    wm.update( fh7, fh7.getObject() );
    wm.update( fh8, fh8.getObject() );
    wm.update( fh9, fh9.getObject() );
    wm.update( fh10, fh10.getObject() );

    wm.fireAllRules();
    assertEquals( 4, list.size() );
}
 
Example 12
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopulatedSharedToRtn() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C() D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));

    wm.fireAllRules();
    assertEquals( 2, list.size() );
    assertEquals( 7, countNodeMemories(wm.getNodeMemories()));

    kbase1.addPackages( buildKnowledgePackage("r2", "   A() B() C() D() E()\n") );
    wm.fireAllRules();
    assertEquals( 8, countNodeMemories(wm.getNodeMemories()));
    assertEquals(4, list.size() );

    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    PathMemory pmem1 = ( PathMemory ) wm.getNodeMemory(rtn1);
    PathMemory pmem2 = ( PathMemory ) wm.getNodeMemory(rtn2);

    SegmentMemory[] smems1 = pmem1.getSegmentMemories();
    SegmentMemory[] smems2 = pmem2.getSegmentMemories();
    assertEquals(2, smems1.length );
    assertEquals(2, smems2.length );

    assertSame( smems1[0], smems2[0] );
    assertNotSame( smems1[1], smems2[1] );

    SegmentMemory sm = smems1[0];
    assertEquals( smems1[1], sm.getFirst() );

    JoinNode eNode1 = ( JoinNode ) rtn1.getLeftTupleSource();
    JoinNode eNode2 = ( JoinNode ) rtn2.getLeftTupleSource();
    assertSame( eNode1, eNode2 );

    pmem1 = ( PathMemory ) wm.getNodeMemory(rtn1);
    kbase1.removeRule("org.kie", "r2");
    System.out.println( "---" );
    assertEquals( 7, countNodeMemories(wm.getNodeMemories()));
    assertNull( sm.getFirst() );

    pmem1 = ( PathMemory ) wm.getNodeMemory(rtn1);
    smems1 = pmem1.getSegmentMemories();
    assertEquals(1, smems1.length );
    assertSame( sm, smems1[0]);

    wm.insert(new A(1));
    wm.fireAllRules();

    assertEquals( "r1", ((Match)list.get(4)).getRule().getName() );
    assertEquals(5, list.size() ); // only one more added, as second rule as removed

}
 
Example 13
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopulatedSharedLiaNode() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B(1;) C() D() E()\n");
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new B(2));
    wm.insert(new C(1));
    wm.insert(new D(1));
    wm.insert(new E(1));

    wm.fireAllRules();
    assertEquals( 3, list.size() );
    assertEquals( 7, countNodeMemories(wm.getNodeMemories()));

    kbase1.addPackages( buildKnowledgePackage("r2", "   a : A() B(2;) C() D() E()\n") );
    wm.fireAllRules();
    assertEquals( 17, countNodeMemories(wm.getNodeMemories()));

    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class );
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode b1Node = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode b2Node = (JoinNode) liaNode.getSinkPropagator().getLastLeftTupleSink();
    JoinNode c1Node = (JoinNode) b1Node.getSinkPropagator().getLastLeftTupleSink();

    LiaNodeMemory lm = ( LiaNodeMemory ) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();

    BetaMemory b1Mem = ( BetaMemory ) wm.getNodeMemory(b1Node);
    assertSame( sm.getFirst(), b1Mem.getSegmentMemory());
    assertEquals( 3, b1Mem.getLeftTupleMemory().size() );
    assertEquals( 1, b1Mem.getRightTupleMemory().size() );

    BetaMemory b2Mem = ( BetaMemory ) wm.getNodeMemory(b2Node);
    SegmentMemory b2Smem =  sm.getFirst().getNext();
    assertSame( b2Smem, b2Mem.getSegmentMemory());
    assertEquals( 3, b2Mem.getLeftTupleMemory().size() );
    assertEquals( 1, b2Mem.getRightTupleMemory().size() );
    assertEquals(6, list.size() );

    BetaMemory c1Mem = ( BetaMemory ) wm.getNodeMemory(c1Node);
    assertSame( b1Mem.getSegmentMemory(), c1Mem.getSegmentMemory() );
    assertNotSame(b1Mem.getSegmentMemory(), b2Mem.getSegmentMemory());

    wm.fireAllRules();
    assertEquals(6, list.size() );
    assertEquals( 17, countNodeMemories(wm.getNodeMemories()));

    kbase1.removeRule("org.kie", "r2");
    assertEquals( 12, countNodeMemories(wm.getNodeMemories()));

    assertSame( sm, b1Mem.getSegmentMemory());
    assertSame( sm, c1Mem.getSegmentMemory());
    assertNull(sm.getFirst());
    assertEquals( 3, b1Mem.getLeftTupleMemory().size() );
    assertEquals( 1, b1Mem.getRightTupleMemory().size() );

    //SegmentMemory b2Smem =  sm.getFirst().remove();
    assertSame( b2Smem, b2Mem.getSegmentMemory());

    wm.insert(new A(1));
    wm.fireAllRules();

    assertEquals( "r1", ((Match)list.get(6)).getRule().getName() );
    assertEquals(7, list.size() ); // only one more added, as second rule as removed
}
 
Example 14
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopulatedRuleWithEvals() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   a:A() B() eval(1==1) eval(1==1) C(1;) \n");

    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();

    assertEquals( 7, countNodeMemories(wm.getNodeMemories()));

    kbase1.addPackages( buildKnowledgePackage("r2", "   a:A() B() eval(1==1) eval(1==1) C(2;) \n") );
    wm.fireAllRules();

    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class );
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();

    EvalConditionNode e1 = (EvalConditionNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    EvalConditionNode e2 = (EvalConditionNode) e1.getSinkPropagator().getFirstLeftTupleSink();

    JoinNode c1Node = (JoinNode) e2.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) e2.getSinkPropagator().getLastLeftTupleSink();

    LiaNodeMemory lm = ( LiaNodeMemory ) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();

    BetaMemory c1Mem = ( BetaMemory ) wm.getNodeMemory(c1Node);
    assertSame( sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals( 3, c1Mem.getLeftTupleMemory().size() );
    assertEquals( 1, c1Mem.getRightTupleMemory().size() );

    BetaMemory c2Mem = ( BetaMemory ) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem =  sm.getFirst().getNext();
    assertSame( c2Smem, c2Mem.getSegmentMemory());
    assertEquals( 3, c2Mem.getLeftTupleMemory().size() );
    assertEquals( 1, c2Mem.getRightTupleMemory().size() );
    assertEquals(6, list.size() );


    kbase1.removeRule("org.kie", "r2");
    assertEquals( 8, countNodeMemories(wm.getNodeMemories()));

    assertNull( sm.getFirst());

    assertSame( sm, c1Mem.getSegmentMemory()); // c1SMem repoints back to original Smem

    wm.insert(new A(1));
    wm.fireAllRules();

    assertEquals( "r1", ((Match)list.get(6)).getRule().getName() );
    assertEquals(7, list.size() ); // only one more added, as second rule as removed
}
 
Example 15
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
    public void testDynamicRuleRemovals() throws Exception {
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject( loadKnowledgeBase( "test_Dynamic1.drl", "test_Dynamic3.drl", "test_Dynamic4.drl" ) );

        Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic2.drl"));
        kbase.addPackages(kpkgs);

        KieSession wm = createKnowledgeSession( kbase );
//        AgendaEventListener ael = mock( AgendaEventListener.class );
//        wm.addEventListener( ael );

        final List< ? > list = new ArrayList<Object>();
        wm.setGlobal( "list",
                                 list );

        final PersonInterface bob = new Person( "bob",
                                                "stilton" );
        bob.setStatus( "Not evaluated" );
        FactHandle fh0 = wm.insert( bob );

        final Cheese stilton1 = new Cheese( "stilton",
                                            5 );
        FactHandle fh1 = wm.insert( stilton1 );

        final Cheese stilton2 = new Cheese( "stilton",
                                            3 );
        FactHandle fh2 = wm.insert( stilton2 );

        final Cheese stilton3 = new Cheese( "stilton",
                                            1 );
        FactHandle fh3 = wm.insert( stilton3 );

        final Cheese cheddar = new Cheese( "cheddar",
                                           5 );
        FactHandle fh4 = wm.insert( cheddar );

        wm.fireAllRules();
        assertEquals(15, list.size());
        list.clear();

        kbase.removeRule("org.drools.compiler.test",
                         "Who likes Stilton");

        wm.update(fh0, bob);
        wm.update(fh1, stilton1);
        wm.update(fh2, stilton2);
        wm.update(fh3, stilton3);
        wm.update(fh4, cheddar);
        wm.fireAllRules();
        assertEquals(12, list.size());
        list.clear();

        kbase.removeRule("org.drools.compiler.test",
                         "like cheese");

        wm.update( fh0, bob);
        wm.update(fh1, stilton1);
        wm.update(fh2, stilton2);
        wm.update(fh3, stilton3);
        wm.update(fh4, cheddar);
        wm.fireAllRules();
        assertEquals(8, list.size());
        list.clear();

        final Cheese muzzarela = new Cheese( "muzzarela",
                                             5 );
        wm.insert( muzzarela );
        wm.fireAllRules();

        assertEquals( 1, list.size() );
        list.clear();
    }
 
Example 16
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopulatedRuleMidwayShare() throws Exception {
    InternalKnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A() B() C(1;) D() E()\n");

    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase1.newKieSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);

    wm.insert(new A(1));
    wm.insert(new A(2));
    wm.insert(new A(3));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));
    wm.fireAllRules();

    assertEquals( 7, countNodeMemories(wm.getNodeMemories()));

    kbase1.addPackages( buildKnowledgePackage("r2", "   a : A() B() C(2;) D() E()\n") );
    wm.fireAllRules();

    ObjectTypeNode aotn = getObjectTypeNode(kbase1, A.class );
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
    JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getFirstLeftTupleSink();

    JoinNode c1Node = (JoinNode) bNode.getSinkPropagator().getFirstLeftTupleSink();
    JoinNode c2Node = (JoinNode) bNode.getSinkPropagator().getLastLeftTupleSink();

    LiaNodeMemory lm = ( LiaNodeMemory ) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();

    BetaMemory c1Mem = ( BetaMemory ) wm.getNodeMemory(c1Node);
    assertSame( sm.getFirst(), c1Mem.getSegmentMemory());
    assertEquals( 3, c1Mem.getLeftTupleMemory().size() );
    assertEquals( 1, c1Mem.getRightTupleMemory().size() );

    BetaMemory c2Mem = ( BetaMemory ) wm.getNodeMemory(c2Node);
    SegmentMemory c2Smem =  sm.getFirst().getNext();
    assertSame( c2Smem, c2Mem.getSegmentMemory());
    assertEquals( 3, c2Mem.getLeftTupleMemory().size() );
    assertEquals( 1, c2Mem.getRightTupleMemory().size() );
    assertEquals(6, list.size() );


    kbase1.removeRule("org.kie", "r2");
    assertEquals( 10, countNodeMemories(wm.getNodeMemories()));

    assertNull( sm.getFirst());

    assertSame( sm, c1Mem.getSegmentMemory()); // c1SMem repoints back to original Smem

    wm.insert(new A(1));
    wm.fireAllRules();

    assertEquals( "r1", ((Match)list.get(6)).getRule().getName() );
    assertEquals(7, list.size() ); // only one more added, as second rule as removed
}
 
Example 17
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testDynamicRuleRemovalsSubNetwork() throws Exception {
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject( loadKnowledgePackages( "test_DynamicRulesWithSubnetwork1.drl",
                                                                                                     "test_DynamicRulesWithSubnetwork.drl" ) );
    
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    kbase.addPackages( kpkgs );
    
    kpkgs = SerializationHelper.serializeObject( loadKnowledgePackages( "test_DynamicRulesWithSubnetwork2.drl" ) );
    kbase.addPackages( kpkgs );

    KieSession session = createKnowledgeSession( kbase );

    final List<?> list = new ArrayList<Object>();
    session.setGlobal( "results",
                       list );

    Order order = new Order();

    OrderItem item1 = new OrderItem( order,
                                     1,
                                     "Adventure Guide Brazil",
                                     OrderItem.TYPE_BOOK,
                                     24 );
    order.addItem( item1 );
    FactHandle item1Fh = session.insert( item1 );

    OrderItem item2 = new OrderItem( order,
                                     2,
                                     "Prehistoric Britain",
                                     OrderItem.TYPE_BOOK,
                                     15 );
    order.addItem( item2 );
    FactHandle item2Fh = session.insert( item2 );

    OrderItem item3 = new OrderItem( order,
                                     3,
                                     "Holiday Music",
                                     OrderItem.TYPE_CD,
                                     9 );
    order.addItem( item3 );
    FactHandle item3Fh = session.insert( item3 );

    OrderItem item4 = new OrderItem( order,
                                     4,
                                     "Very Best of Mick Jagger",
                                     OrderItem.TYPE_CD,
                                     11 );
    order.addItem( item4 );
    FactHandle item4Fh = session.insert( item4 );

    session.insert( order );
    session.fireAllRules();
    assertEquals( 11, list.size() );

    kbase.removeRule( "org.drools.compiler",
                      "Apply Discount on all books" );

    list.clear();
    session.update( item1Fh, item1 );
    session.update( item2Fh, item2 );
    session.update( item3Fh, item3 );
    session.update( item4Fh, item4 );
    session.fireAllRules();

    assertEquals( 10, list.size() );

    kbase.removeRule( "org.drools.compiler",
                      "like book" );
    list.clear();
    session.update( item1Fh, item1 );
    session.update( item2Fh, item2 );
    session.update( item3Fh, item3 );
    session.update( item4Fh, item4 );
    session.fireAllRules();

    assertEquals( 8, list.size() );

    final OrderItem item5 = new OrderItem( order, 5, "Sinatra : Vegas", OrderItem.TYPE_CD, 5 );
    FactHandle item5Fh = session.insert( item5 );

    session.fireAllRules();

    assertEquals( 10, list.size() );

    kbase.removeKiePackage( "org.drools.compiler" );
    list.clear();
    session.update( item1Fh, item1 );
    session.update( item2Fh, item2 );
    session.update( item3Fh, item3 );
    session.update( item4Fh, item4 );
    session.update( item5Fh, item5 );
    session.fireAllRules();

    assertEquals( 0, list.size() );
}
 
Example 18
Source File: RemoveRuleTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testPopulatedSingleRuleNoSharing() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    InternalWorkingMemory wm = ((InternalWorkingMemory)kbase.newKieSession());

    wm.insert(new A(1));
    wm.insert(new B(1));
    wm.insert(new C(1));
    wm.insert(new C(2));
    wm.insert(new D(1));
    wm.insert(new E(1));

    wm.fireAllRules();


    kbase.addPackages( buildKnowledgePackage("r1", "   A() B() C(object == 2) D() E()\n") );
    List list = new ArrayList();
    wm.setGlobal("list", list);

    ObjectTypeNode aotn = getObjectTypeNode(kbase, A.class );
    LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];

    LiaNodeMemory lm = ( LiaNodeMemory ) wm.getNodeMemory(liaNode);
    SegmentMemory sm = lm.getSegmentMemory();
    assertNotNull(sm.getStagedLeftTuples().getInsertFirst());

    wm.fireAllRules();

    BetaMemory bMem = ( BetaMemory ) sm.getNodeMemories().get(1);
    assertEquals( 1, bMem.getLeftTupleMemory().size() );
    assertEquals( 1, bMem.getRightTupleMemory().size() );

    BetaMemory eMem = ( BetaMemory ) sm.getNodeMemories().get(4);
    assertEquals( 1, eMem.getLeftTupleMemory().size() );
    assertEquals( 1, eMem.getRightTupleMemory().size() );

    NodeMemories nms = wm.getNodeMemories();
    assertEquals( 12, countNodeMemories(nms));

    assertNull(sm.getStagedLeftTuples().getInsertFirst());
    assertEquals(1, list.size() );

    assertEquals( "r1", ((Match)list.get(0)).getRule().getName() );

    kbase.removeRule("org.kie", "r1");

    assertEquals( 6, countNodeMemories(nms)); // still has OTN
}
 
Example 19
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testSegmentMerging() {
    String drl1 = "global java.util.List list\n" +
                  "rule R1 when\n" +
                  "  $s : String()\n" +
                  "  $i : Integer( this == $s.length() )\n" +
                  "  $j : Integer( this == $i * 2 )\n" +
                  "then\n" +
                  "  list.add( $j );\n" +
                  "end\n";

    String drl2 = "global java.util.List list\n" +
                  "rule R2 when\n" +
                  "  $s : String()\n" +
                  "  $i : Integer( this == $s.length() )\n" +
                  "  $j : Integer( this == $i * 3 )\n" +
                  "then\n" +
                  "  list.add( $j );\n" +
                  "end\n";

    InternalKnowledgeBase kbase = (InternalKnowledgeBase)getKnowledgeBase( );

    kbase.addPackages(loadKnowledgePackagesFromString( drl1 ));

    KieSession ksession = createKnowledgeSession(kbase);
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);

    ksession.insert("test");
    ksession.insert(4);
    ksession.insert(8);
    ksession.insert(12);

    ksession.fireAllRules();
    assertEquals(8, (int)list.get(0));
    list.clear();

    kbase.addPackages(loadKnowledgePackagesFromString(drl2));

    kbase.removeRule("defaultpkg", "R1");

    ksession.fireAllRules();
    assertEquals(12, (int)list.get(0));
}
 
Example 20
Source File: DynamicRuleRemovalTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
private void removeRule(InternalKnowledgeBase kbase, String ruleName) {
    kbase.removeRule("org.kie.test", ruleName);
}