Java Code Examples for org.kie.api.runtime.KieSession#getKieBase()

The following examples show how to use org.kie.api.runtime.KieSession#getKieBase() . 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: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private KieSession marsallStatefulKnowledgeSession(KieSession ksession) throws IOException,
                                                                                                   ClassNotFoundException {
    Globals globals = ksession.getGlobals();

    KieBase kbase = ksession.getKieBase();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    MarshallerFactory.newMarshaller( kbase ).marshall( out,
                                                       ksession );

    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption( TimerJobFactoryOption.get("trackable") );
    ksconf.setOption( ClockTypeOption.get( "pseudo" ) );

    Environment env = EnvironmentFactory.newEnvironment();
    env.set( EnvironmentName.GLOBALS, globals );
    ksession = MarshallerFactory.newMarshaller( kbase ).unmarshall( new ByteArrayInputStream( out.toByteArray() ), ksconf, env );

    return ksession;
}
 
Example 2
Source File: MemoryLeakTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
@Disabled("The checkReachability method is not totally reliable and can fall in an endless loop." +
        "We need to find a better way to check this.")
public void testLeakAfterSessionDispose() {
    // DROOLS-1655
    String drl =
            "import " + Person.class.getCanonicalName() + "\n" +
            "rule R when\n" +
            "    $p : Person()\n" +
            "then\n" +
            "end\n";

    KieContainer kContainer = new KieHelper().addContent( drl, ResourceType.DRL ).getKieContainer();
    KieSession ksession = kContainer.newKieSession();
    KieBase kBase = ksession.getKieBase();

    ksession.insert( new Person("Mario", 40) );
    ksession.fireAllRules();

    checkReachability( ksession, Person.class::isInstance, true );
    checkReachability( kBase, ksession::equals, true );
    checkReachability( kContainer, ksession::equals, true );

    ksession.dispose();

    checkReachability( kContainer, Person.class::isInstance, false );
    checkReachability( kBase, ksession::equals, false );
    checkReachability( kContainer, ksession::equals, false );
}
 
Example 3
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 4
Source File: GetKieBaseCommand.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public KieBase execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup( KieSession.class );
    return ksession.getKieBase();
}
 
Example 5
Source File: ReteMemoryChecker.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
public static void checkNodeMemories(KieSession session) {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase)session.getKieBase();
    for (EntryPointNode entryPointNode : kbase.getRete().getEntryPointNodes().values()) {
        checkNodeMemory( (InternalWorkingMemory) session, entryPointNode );
    }
}