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

The following examples show how to use org.drools.core.impl.InternalKnowledgeBase#newStatelessKieSession() . 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: SerializedPackageMergeTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private StatelessKieSession getSession(boolean serialize) throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

    for ( String drl : DRLs ) {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add( ResourceFactory.newInputStreamResource(getClass().getResourceAsStream(drl)),
                      ResourceType.DRL );
        
        assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString());
        
        Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();

        Collection<KiePackage> newCollection = null;
        if ( serialize ) {
            newCollection = new ArrayList<KiePackage>();
            for( KiePackage kpkg : kpkgs) {
                kpkg = SerializationHelper.serializeObject(kpkg);
                newCollection.add( kpkg );
            }
        } else {
            newCollection = kpkgs;
        }
        kbase.addPackages( newCollection );
    }
    return kbase.newStatelessKieSession();
}
 
Example 2
Source File: StatelessSessionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private StatelessKieSession getSession2(final Resource resource) throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add( resource, ResourceType.DRL );
    
    if (kbuilder.hasErrors() ) {
        System.out.println( kbuilder.getErrors() );
    }
    
    assertFalse( kbuilder.hasErrors() );
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();

    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    
   
    kbase.addPackages( pkgs );
    kbase    = SerializationHelper.serializeObject( kbase );
    final StatelessKieSession session = kbase.newStatelessKieSession();

    session.setGlobal( "list",
                       this.list );
    session.setGlobal( "cheesery",
                       this.cheesery );
    return session;
}
 
Example 3
Source File: SequentialTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testSequentialWithRulebaseUpdate() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase(kconf, "simpleSalience.drl");
    StatelessKieSession ksession = createStatelessKnowledgeSession( kbase );

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

    ksession.execute(new Person("pob"));

    kbase.addPackages(loadKnowledgePackagesFromString( new String( IoUtils.readBytesFromInputStream( DynamicRulesTest.class.getResource("test_Dynamic3.drl").openStream() ) ) ) );

    ksession = kbase.newStatelessKieSession();
    ksession.setGlobal( "list", list );
    Person person  = new Person("bop");
    ksession.execute(person);

    assertEquals( 7, list.size() );

    assertEquals( "rule 3", list.get( 0 ));
    assertEquals( "rule 2", list.get( 1 ));
    assertEquals( "rule 1", list.get( 2 ));
    assertEquals( "rule 3", list.get( 3 ) );
    assertEquals( "rule 2", list.get( 4 ) );
    assertEquals( "rule 1", list.get( 5 ) );
    assertEquals( person, list.get( 6 ));
}
 
Example 4
Source File: FireAllRulesCommandTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private StatelessKieSession getSession(String drl) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);

    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());

    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());

    return kbase.newStatelessKieSession();
}