0

我有一个简单的检查用户 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();
    }

为了确保事实得到更新并且更新的值反映在下一条规则中,需要进行哪些更改。

4

1 回答 1

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

该博客将有助于更多地了解在 drools 中修改的使用:https ://ilesteban.wordpress.com/2012/11/16/about-drools-and-infinite-execution-loops/

于 2018-08-08T17:28:51.700 回答