Stateless Vs Stateful Session behavior in drools
<p>I am new to the drools. I was trying to understand the difference  between Stateless and Stateful sessions provided by Drools. </p>

<p>As per my initial understanding,</p>

<p>In case of Stateless session, if fact is modified during action execution of any rule then it will not be re-submitted to inference engine to find out the new rules which matches the modified fact.</p>

<p>In case of Stateful session, if fact is modified during action execution of any rule then it will be re-submitted to inference engine to find out the new rules which matches the modified fact and then their corresponding action will be executed.</p>

<p>So when I tried to verify this behavior by writing a sample rule, I found that behavior is exactly same in both the cases. So now I am really confused regarding the difference between Stateful and Stateless sessions. </p>

<p>I would like to request everyone to help me in understand the correct behavior of Stateful and Stateless sessions. </p>

<p>For your reference I am pasting my sample code for Stateful and Stateless session along with their output and sample rule.</p>

<pre><code>licenseApplication.drl   (Rule file)

package com.idal.droolsapp

rule "Is of valid age"
    no-loop
when
    $a : Applicant( age &lt; 18 )
then
    System.out.println( "Not eligible for license" );
    System.out.println( "Setting Valid to false" );
    modify( $a ) { setValid( false ) };
end

rule "Is of valid false"
    salience 100
when
    $a : Applicant( valid == false )
then
    System.out.println( "Second rule fired" );
end
</code></pre>

<p>Input object (Fact) Applicant.java</p>

<pre><code>package com.idal.droolsapp;

public class Applicant {

    private String name;

    private int age;

    private boolean valid = true;

    public Applicant(String name, int age) {
        setName(name);
        setAge(age);
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setValid(boolean valid) {
        this.valid = valid;
    }

    public boolean isValid() {
        return valid;
    }

    @Override
    public String toString() {
        return "Applicant [name=" + name + ", age=" + age + ", valid=" + valid
                + "]";
    }


}
</code></pre>

<p>StatelessSessionExample.java  (Stateless Session test code)</p>

<pre><code>package com.idal.droolsapp;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;

public class StatelessSessionExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
                .newKnowledgeBuilder();

        kbuilder.add(ResourceFactory.newClassPathResource(
                "licenseApplication.drl", StatelessSessionExample.class),

        ResourceType.DRL);

        if (kbuilder.hasErrors()) {

            System.err.println(kbuilder.getErrors().toString());

        }

        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

        StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();

        Applicant applicant = new Applicant( "Mr John Smith", 16 );

        ksession.execute( applicant );

        System.out.println("Updated Applicant = " + applicant);
    }

}
</code></pre>

<p>Output of Stateless session test code:</p>

<blockquote>
<pre><code>Not eligible for license
Setting Valid to false
Second rule fired
Updated Applicant = Applicant [name=Mr John Smith, age=16, valid=false]
</code></pre>
</blockquote>

<p>StatefulSessionExample.java  (Stateless Session test code)</p>

<pre><code>package com.idal.droolsapp;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

public class StatefulSessionExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
                .newKnowledgeBuilder();

        kbuilder.add(ResourceFactory.newClassPathResource(
                "licenseApplication.drl", StatefulSessionExample.class),

        ResourceType.DRL);

        if (kbuilder.hasErrors()) {

            System.err.println(kbuilder.getErrors().toString());

        }

        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        Applicant applicant = new Applicant( "Mr John Smith", 16 );

        ksession.insert( applicant );

        ksession.fireAllRules();

        ksession.dispose();

        System.out.println("Updated Applicant = " + applicant);
    }

}
</code></pre>

<p>Output of Stateful session test code:</p>

<blockquote>
<pre><code>Not eligible for license
Setting Valid to false
Second rule fired
Updated Applicant = Applicant [name=Mr John Smith, age=16, valid=false]
</code></pre>
</blockquote>

<p>Once again requesting everyone to help me in understanding the correct behavior of Stateful and Stateless sessions. </p>

<p>Thanks in advance,
Manish Gandhi</p>