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

The following examples show how to use org.drools.core.impl.InternalKnowledgeBase#removeKiePackage() . 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 testRuleBaseAddRemove() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase( );

    //add and remove
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject( loadKnowledgePackages(  "test_Dynamic1.drl" ) );
    String pkgName = kpkgs.iterator().next().getName();
    kbase.addPackages( kpkgs );
    kbase.removeKiePackage( pkgName );
    kbase = SerializationHelper.serializeObject( kbase );

    //add and remove again
    kpkgs = SerializationHelper.serializeObject( loadKnowledgePackages(  "test_Dynamic1.drl" ) );
    pkgName = kpkgs.iterator().next().getName();
    kbase.addPackages( kpkgs );
    kbase.removeKiePackage( pkgName );
    kbase = SerializationHelper.serializeObject( kbase );
}
 
Example 2
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 3
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 4
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemovePackage() throws Exception {
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_RemovePackage.drl"));
    final String packageName = kpkgs.iterator().next().getName();
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase( );

    kbase.addPackages( kpkgs );
    kbase = SerializationHelper.serializeObject( kbase );

    KieSession session = createKnowledgeSession( kbase );

    session.insert( new Precondition( "genericcode",
                                      "genericvalue" ) );
    session.fireAllRules();

    InternalKnowledgeBase ruleBaseWM = (InternalKnowledgeBase) session.getKieBase();
    ruleBaseWM.removeKiePackage( packageName );
    
    kpkgs = SerializationHelper.serializeObject( loadKnowledgePackages(  "test_RemovePackage.drl" ) );
    ruleBaseWM.addPackages( kpkgs );
    ruleBaseWM = SerializationHelper.serializeObject( ruleBaseWM );

    session = SerializationHelper.getSerialisedStatefulKnowledgeSession( session, 
                                                                         true );
    session.fireAllRules();

    ruleBaseWM.removeKiePackage(packageName);
    ruleBaseWM.addPackages( SerializationHelper.serializeObject( kpkgs ) );

    ruleBaseWM.removeKiePackage( packageName );
    ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
}
 
Example 5
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testSharedLIANodeRemoval() throws Exception {
    // it's not a true share, but the liaNode will have two sinks, due to subnetwork.
    String str = "global java.util.List list;\n";
    str += "rule \"test\"\n";
    str += "when\n";
    str += "  exists(eval(true))\n";
    str += "then\n";
    str += " list.add(\"fired\");\n";
    str += "end\n";

    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase( );
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject( loadKnowledgePackagesFromString( str ) );

    // Add once ...
    kbase.addPackages( kpkgs );

    // This one works
    List list = new ArrayList();
    KieSession session = createKnowledgeSession( kbase );
    session.setGlobal( "list",
                       list );
    session.fireAllRules();
    assertEquals( 1,
                  list.size() );

    list.clear();
    // ... remove ...
    KiePackage kpkg = ( KiePackage ) kpkgs.toArray()[0];
    kbase.removeKiePackage( kpkg.getName() );
    kbase.addPackages( kpkgs );
    session = createKnowledgeSession( kbase );
    session.setGlobal( "list",
                       list );
    session.fireAllRules();
    assertEquals( 1,
                  list.size() );
}
 
Example 6
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testDynamicRulesAddRemove() {
    try {
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase( "test_DynamicRulesTom.drl" );
        KieSession session = createKnowledgeSession( kbase );

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

        InternalFactHandle h1 = (InternalFactHandle) session.insert( new Person( "tom",
                                                                                 1 ) );
        InternalFactHandle h2 = (InternalFactHandle) session.insert( new Person( "fred",
                                                                                 2 ) );
        InternalFactHandle h3 = (InternalFactHandle) session.insert( new Person( "harry",
                                                                                 3 ) );
        InternalFactHandle h4 = (InternalFactHandle) session.insert( new Person( "fred",
                                                                                 4 ) );
        InternalFactHandle h5 = (InternalFactHandle) session.insert( new Person( "ed",
                                                                                 5 ) );
        InternalFactHandle h6 = (InternalFactHandle) session.insert( new Person( "tom",
                                                                                 6 ) );
        InternalFactHandle h7 = (InternalFactHandle) session.insert( new Person( "sreeni",
                                                                                 7 ) );
        InternalFactHandle h8 = (InternalFactHandle) session.insert( new Person( "jill",
                                                                                 8 ) );
        InternalFactHandle h9 = (InternalFactHandle) session.insert( new Person( "ed",
                                                                                 9 ) );
        InternalFactHandle h10 = (InternalFactHandle) session.insert( new Person( "tom",
                                                                                  10 ) );

        session.fireAllRules();

        assertEquals( 3,
                      results.size() );
        assertTrue( results.contains( h1.getObject() ) );
        assertTrue( results.contains( h6.getObject() ) );
        assertTrue( results.contains( h10.getObject() ) );
        results.clear();

        kbase.addPackages( loadKnowledgePackages( "test_DynamicRulesFred.drl" ) );
        session.fireAllRules();

        assertEquals( 2,
                      results.size() );
        assertTrue( results.contains( h2.getObject() ) );
        assertTrue( results.contains( h4.getObject() ) );
        results.clear();

        kbase.removeKiePackage( "tom" );

        kbase.addPackages( loadKnowledgePackages( "test_DynamicRulesEd.drl" ) );
        session.fireAllRules();

        assertEquals( 2,
                      results.size() );
        assertTrue( results.contains( h5.getObject() ) );
        assertTrue( results.contains( h9.getObject() ) );
        results.clear();

        ((Person) h3.getObject()).setName( "ed" );
        session.update( h3,
                        h3.getObject() );
        session.fireAllRules();

        assertEquals( 1,
                      results.size() );
        assertTrue( results.contains( h3.getObject() ) );
    } catch ( Exception e ) {
        e.printStackTrace();
        fail( "Should not raise any exception: " + e.getMessage() );
    }
}
 
Example 7
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 8
Source File: DynamicRulesTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testDynamicRuleRemovalsSubNetworkAndNot() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_DynamicRulesWithNotSubnetwork.drl");
    KieSession ksession = createKnowledgeSession( kbase );


    final AgendaEventListener alistener = mock( AgendaEventListener.class );
    ksession.addEventListener( alistener );

    // pattern does not match, so do not activate
    ksession.insert( new Person( "toni" ) );
    ksession.fireAllRules();
    verify( alistener,
            never() ).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));

    // pattern matches, so create activation
    ksession.insert( new Person( "bob" ) );
    ksession.fireAllRules();
    verify( alistener,
            times( 1 ) ).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));

    // already active, so no new activation should be created
    ksession.insert( new Person( "mark" ) );
    ksession.fireAllRules();
    verify( alistener,
            times( 1 ) ).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));

    kbase.removeKiePackage( "org.drools.compiler" );

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

    // lets re-compile and add it again
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicRulesWithNotSubnetwork.drl");
    kbase.addPackages( kpkgs );
    ksession.fireAllRules();

            // rule should be reactivated, since data is still in the session
    verify( alistener,
            times( 2 ) ).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));

}