Reputation: 111
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
Upvotes: 1
Views: 1711
Reputation: 3807
In my case simply ksession.execute(myPojoObject)
worked.
But make sure the package structure of myPojoObject
in application corresponds to that of deployed kJar
(via kie-workbench).
Upvotes: 0
Reputation: 2293
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.
Upvotes: 1
Reputation: 111
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.
Upvotes: 2