如何在Drools规则中使用Spring Service?

时间:2017-04-11 12:40:17

标签: spring dependency-injection drools kie mvel

我正在使用drools引擎构建警报系统。当满足条件时,我们需要执行Spring Framework对规则(RHS)的操作的 @Service instantiated 方法。

如何让Spring Framework创建的 @service实例由Drools规则的操作(RHS)使用?

我遵循了以下指示:

  1. 使用表单导入功能 Rule1.drl )。此解决方案不起作用,因为该类在drools中实例化,并且需要执行静态方法。
  2. 使用全局会话变量( Rule2.drl )。这个解决方案在"运行时"表明它不是同一类型。
  3. 关于如何使用它的任何想法?

    作为

    文件: Rule1.drl

    package com.mycompany.alerts.Alert;
    
    import function com.mycompany.alerts.service.SecurityService.notifyAlert;
    
    rule "Activate Alert Type"
    salience 9000
    when
      $alert: Alert(type == "TYPE1") 
    then
      System.out.println("Running action:" + drools.getRule().getName());
      $alert.setActive(Boolean.TRUE);
      notifyAlert($alert.getStatus(),$alert.getSmsNumber());    
      System.out.println("End action:" + drools.getRule().getName());
    end
    

    文件: Rule2.drl

    package com.mycompany.alerts.Alert;
    
    global com.mycompany.alerts.service.SecurityService securityService;
    
    rule "Activate Alert Type 2"
    salience 9000
    when
      $alert: Alert(type == "TYPE2") 
    then
      System.out.println("Running action:" + drools.getRule().getName());
      $alert.setActive(Boolean.TRUE);
      securityService.notifyAlert($alert.getStatus(),$alert.getSmsNumber());    
      System.out.println("End action:" + drools.getRule().getName());
    end
    

    文件: SecurityService.java

    package com.mycompany.alerts.service;
    
    import com.mycompany.alerts.service.UserRepository;
    
    @Service
    @Transactional
    public class SecurityService {
    
        private final Logger log = LoggerFactory.getLogger(SecurityService.class);
    
        private final UserRepository userRepository;
    
        public SecurityService(UserRepository userRepository) {
            this.userRepository = userRepository;
        }
    
        public void notifyAlert(String status, String sms) {
             System.out.println("Alert notify with:" + status + " sms:" + sms);
        }
    
    }
    

1 个答案:

答案 0 :(得分:2)

您可以将kieRuntime的setGlobal功能用作:

global SecurityService securityService.

然后您可以在drl文件中声明/使用此变量:

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <crypt.h>


int main(int argc, string argv[])
{
//check number of arguments
if( argc != 2 )
{
    printf("Usage: ./crack hash\n");   
}

char str[5];
char salt[] = "..";
strncpy(salt, argv[1], 2);
string hash = argv[1];
string password = "....";
char pass[5]; 


//brute force loop
for( int i = 65; i < 123; i++)
{
    str[0] = i;
    for( int j = 65; j < 123; j++)
    {
        str[1] = j;
        for( int k = 65; k < 123; k++)
        {
            str[2] = k;
            for( int l = 65; l < 123; l++)
            {
                str[3] = l;
                str[4] = '\0';

                strcpy(pass, str);

                password = crypt(pass, salt);
                if ( hash == password)
                {
                    printf("%s\n", password);
                    break;
                }

                printf("\r%s", pass);
                fflush(stdout);
            }   
        }   
    }
}
}

PS: - KieRuntime对象可以获得:KieRuntime kieRuntime =(KieRuntime)kieSssion;