How can I read facts outside of the KieSession that were inserted inside of the KieSession­ (e.g. rules results)?

During my rules execution, I will be "inserting" new fact object in memory that I will need to read when the rules are done firing. How can I read those facts when outside the Rules Session?

I have tried to insert the fact with an outIdentifier, from outside the session (i.e. before the "fireAllRules()" method). However, because I may not know how many AccountingPeriod fact may get inserted during the rule session, or even if it will get inserted, this method does not seem suitable.

Accounting Period Fact:

package sample.package;

public class AccountingPeriod {

    private LocalDate accountingDate;
    private int personKey;

    public AccountingPeriod(LocalDate accountingDate, int personKey) {
        this.accountingDate = accountingDate;
        this.personKey = personKey;
    }

    public LocalDate getAccountingDate() { return accountingDate; }
    public LocalDate getPersonKey() { return personKey; }
}

Execution Code :

sample.package;
public static void main(String args[]) {
    StatelessKieSession ksession = [initialized KieSession]

    ksession.execute(Arrays.asList(Facts[]));
    [Code here to get the AccountingPeriod fact inserted in the rule session]
}

myRules.drl

rule
    when [some condition]
    then
        insert (new AccountingPeriod(LocalDate.of(year, month, day), 100));
end
Answer:1

I just found a way to get the facts from a stateless KieSession.

sample.package;
public static void main(String args[]) {
    StatelessKieSession ksession = [initialized KieSession]
    List<Command> cmds = new ArrayList<>();

    cmds.add([all required commands]);
    cmds.add(CommandFactory.newFireAllRules());
    cmds.add(CommandFactory.newGetObjects("facts"));
    ExecutionResults rulesResults = kSession.execute(CommandFactory.newBatchExecution(cmds));
    Collection<Object> results = (Collection<Object>) rulesResults.getValue("facts");
}

It turns out that by linking a command to an OutIdentifier ("facts", in this case) we can get its return value by using the getValue(outIdentifier) of the KieSession results.

Answer:2

I see several options.

1) Insert one more object to session from the very beginning and use it as result container.

Person person = new Person();
person.setAge(15);
List result = new ArrayList();
kieSession.execute(Arrays.asList(person,result));
assertThat(result.get(0)).isEqualTo("haha");


rule "Check person age"
    when
        $person : Person( age > 16 );
        $result : List ( );
    then
        insert(new IsCoder( $person ) );
        $result.add("haha");
    end

2) Instead of using StatelessKieSession you can use just KieSession. KieSession has getObjects method where you can find all objects inserted and iterate through them.

Answer:3

Im using CursorAdapter in my Listview for direct mapping of data to the database. my problem is when im creating a drag and drop listview. How to drag and drop items in Listview for reordering in ...

I am struggling with a specific problem, that I cannot think of correctly. The following is the problem I have a map with key value like the following, i just used strings here String key = "...

In my application, I use a JTable with a few resizable columns. At closing, I want the app to store the size of the columns displayed. My problem is that even if I manually resize the columns, the ...

I have a static matrixMult and static matrixAdd method and a static matrixDisplay(for print the result) and i wrote in Main function a example for test. My Goal: i want to multiply and add two matrix ...