如何在运行时将事实插入drowls中以在规则之间共享



我有一个简单的方法,可以检查数据库中是否存在用户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;
}

可以看出,我正在通过设置UserAlreadyBlacklisted(true(来修改事实(dto(blackListUserDto。

但在规则的"然后"部分,当我打印值时

System.out.println($blackListUserD到.isUserAlreadyBlacklisted(((;这个输出仍然为false。

我还需要在另一个规则中共享此数据,如下

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();
}

为了确保事实得到更新,更新后的值反映在下一个规则中,需要做哪些更改。

我找到了上述问题的解决方案,我正在共享解决上述用例的规则

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/

相关内容

  • 没有找到相关文章

最新更新