我有一个简单的方法,可以检查db中是否存在用户ID
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)修改事实(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();
}
要进行什么更改以确保事实得到更新并且更新的值会反映在下一条规则中。
答案 0 :(得分:0)
我找到了上述解决方案,并分享了解决上述用例的规则
rule "check if user is already blacklisted 1"
agenda-group "dbcheck"
when
(QueryTypeDto( queryType == "blacklist" ))
$blackListUser : BlackListUserDto( )
not ControlFact( blackListUserDto == $blackListUser )
$blackListUserDto : BlackListUserDto( )
eval( BlackListServiceImpl.isUserBlacklisted($blackListUser) == false )
$queryTypeDto : QueryTypeDto()
then
System.out.println("to be executed first");
System.out.println($blackListUser.isBlackListFraudUser());
modify($blackListUser){
setBlackListFraudUser(true)
}
insert (new ControlFact($blackListUser));
//$queryTypeDto.setUserBlackListed(false);
end
此博客将帮助您更多地了解在口水中使用修改:https://ilesteban.wordpress.com/2012/11/16/about-drools-and-infinite-execution-loops/