[英]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 (ie before the "fireAllRules()" method). 我尝试从会话外部(即,在“ fireAllRules()”方法之前)使用outIdentifier插入事实。 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.
但是,由于我可能不知道在规则会话期间可能插入了多少AccountingPeriod事实,或者即使将其插入,因此该方法似乎不合适。
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 myRules.drl
rule
when [some condition]
then
insert (new AccountingPeriod(LocalDate.of(year, month, day), 100));
end
I see several options. 我看到几个选择。
1) Insert one more object to session from the very beginning and use it as result container. 1)从一开始就在会话中再插入一个对象,并将其用作结果容器。
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
. 2)除了使用
StatelessKieSession
您还可以仅使用KieSession
。 KieSession
has getObjects
method where you can find all objects inserted and iterate through them. KieSession
具有getObjects
方法,您可以在其中找到所有插入的对象并对其进行遍历。
I just found a way to get the facts from a stateless KieSession. 我只是找到了一种从无状态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. 事实证明,通过将命令链接到OutIdentifier(在本例中为
"facts"
),我们可以使用KieSession结果的getValue(outIdentifier)
来获取其返回值。
In my case simply ksession.execute(myPojoObject)
worked. 就我而言,简单地使用
ksession.execute(myPojoObject)
。
But make sure the package structure of myPojoObject
in application corresponds to that of deployed kJar
(via kie-workbench). 但是,请确保应用程序中的
myPojoObject
的包结构与myPojoObject
部署的kJar
的包结构相对应(通过kie-workbench)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.