我有一个简单的检查用户 id 是否存在于 db
rule "check if user is already present"
agenda-group "dbcheck"
when
$blackListUserDto : BlackListUserDto( )
eval( BlackListServiceImpl.isUserBlacklisted($blackListUserDto) )
then
System.out.println("to be executed first");
System.out.println($blackListUserDto.isUserAlreadyBlacklisted());
end
方法isUserBlacklisted如下
public static boolean isUserBlacklisted(BlackListUserDto blackListUserDto)
{
try {
BlackListEntity blackListEntity = blackListRepository.findByUserId(blackListUserDto.getUserId());
if(blackListEntity!=null)
{
blackListUserDto.setUserAlreadyBlacklisted(true);
}
else
//do something else
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
可以看出,我正在通过 setUserAlreadyBlacklisted(true) 修改 fact(dto) blackListUserDto。
但是在我打印值时规则的“那么”部分
System.out.println($blackListUserDto.isUserAlreadyBlacklisted()); 输出仍然是错误的。
我还需要在另一个规则中共享这些数据,如下所示
rule "blacklist user"
agenda-group "blacklist"
when
(BlackListUserDto( userAlreadyBlacklisted == false ))
then
//do something else
end
到目前为止,我的理解是,当我编辑事实时,我们是否需要再次重新插入它们?如果是,那么我如何将它插入到同一个会话中,因为我创建此会话的另一种方法如下:-
public void blacklistUser(String userId) throws IOException
{
BlackListUserDto blackListUserDto=new BlackListUserDto();
blackListUserDto.setUserId(userId);
KieSession kieSession = kContainer.newKieSession();
Agenda agenda = kieSession.getAgenda();
agenda.getAgendaGroup( "blacklist" ).setFocus();
agenda.getAgendaGroup( "dbcheck" ).setFocus();
kieSession.insert(blackListUserDto);
kieSession.insert(queryTypeDto);
kieSession.fireAllRules();
kieSession.dispose();
}
为了确保事实得到更新并且更新的值反映在下一条规则中,需要进行哪些更改。