Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在DRL文件中传递规则条件的参数值?_Java_Spring Boot_Drools - Fatal编程技术网

Java 如何在DRL文件中传递规则条件的参数值?

Java 如何在DRL文件中传递规则条件的参数值?,java,spring-boot,drools,Java,Spring Boot,Drools,我是个流口水的新手。我想在rule.drl文件中传递规则条件的参数 目前我有如下代码:- rule "PREPAID TOP UP LIMIT" salience 1 when cardObject : Card(cardType=="prepaid" && loadCash > 25000); then cardObject.setTransactionResposne(false); cardObject.setMessage("Top up limit exceed

我是个流口水的新手。我想在rule.drl文件中传递规则条件的参数

目前我有如下代码:-

rule "PREPAID TOP UP LIMIT" salience 1

when
cardObject : Card(cardType=="prepaid" && loadCash > 25000);
then
cardObject.setTransactionResposne(false);
cardObject.setMessage("Top up limit exceeds ");
end;
正如您在代码中看到的,cardType和loadCash分别与“预付费”和25000与静态值进行比较。但是我想从java类中初始化这个静态值


那么,我如何传递这个参数呢。请分享一些解决方案。

要将参数传递到rule.drl文件,首先必须清楚如何在代码中初始化Drools会话。在非常基本的形式下,它可以如下所示

KieSession KieSession=new DroolsBeanFactory().getKieSession()

初始化会话后,我看到您正在评估卡对象的
cardType
&
loadCash
。这两个值基本上是接受两个值的卡对象构造函数的一部分,即“cardType”和“loadCash”。因此,首先确保已经在代码中创建了这样的bean或POJO,否则就创建它。之后,您需要做的是将这个“Card”对象作为一个整体传递到rule.drl文件。可以从以下方式开始:

class Card
{

private String cardType;
private int loadCash;

Card(String cardType,int loadCash)
{
this.cardType = cardType;
this.loadCash = loadCash;
}

public String getCardType()
{
return cardType;
}
public int getLoadCash()
{
return loadCash;
}

// Other methods as per your business-logic
}

public void setCardValue(Card card){    
        kieSession.insert(card);
        kieSession.fireAllRules();  // this will now execute all the rules in your drl file.

    }
您可以在这里查阅KieSession文档,并检查what if允许您完成的任务。
如果这对您有帮助,请向上投票。

要将参数传递到rule.drl文件,首先您必须清楚如何在代码中初始化Drools会话。在非常基本的形式下,它可以如下所示

KieSession KieSession=new DroolsBeanFactory().getKieSession()

初始化会话后,我看到您正在评估卡对象的
cardType
&
loadCash
。这两个值基本上是接受两个值的卡对象构造函数的一部分,即“cardType”和“loadCash”。因此,首先确保已经在代码中创建了这样的bean或POJO,否则就创建它。之后,您需要做的是将这个“Card”对象作为一个整体传递到rule.drl文件。可以从以下方式开始:

class Card
{

private String cardType;
private int loadCash;

Card(String cardType,int loadCash)
{
this.cardType = cardType;
this.loadCash = loadCash;
}

public String getCardType()
{
return cardType;
}
public int getLoadCash()
{
return loadCash;
}

// Other methods as per your business-logic
}

public void setCardValue(Card card){    
        kieSession.insert(card);
        kieSession.fireAllRules();  // this will now execute all the rules in your drl file.

    }
您可以在这里查阅KieSession文档,并检查what if允许您完成的任务。
如果这对你有帮助,请向上投票。你基本上有两个选择:

  • 将条件传递到工作记忆中
  • 将条件声明为全局变量
  • 全局变量通常是不受欢迎的,但为了完整性,我将包括这一点


    对于第一个选项,即将条件传递到工作内存,我建议创建一个简单的对象,该对象声明了这些条件以及适当的getter/setter

    例如:

    公共类信用卡限额{
    私有字符串类型;
    私有整数限制;
    //这里有接球手和接球手
    }
    
    您可以将
    cardlimites
    实例(或者如果您有多种类型的实例)与
    Card
    实例一起传递到工作内存中。那么您的规则可以如下所示:

    rule "PREPAID TOP UP LIMIT" 
    salience 1
    when
      CardLimits( $max: limit != null, $type: type == "prepaid" )
      cardObject : Card( cardType == $type && loadCash > $max);
    then
      cardObject.setTransactionResposne(false);
      cardObject.setMessage("Top up limit exceeds ");
    end
    
    (细微的挑剔:你在
    结束后的分号不应该在那里。而且你在右边的“response”拼写错误。)

    CardLimits实例将与您的卡实例一起传递到规则中,如下所示:

    rule "ANY TOP UP LIMIT" 
    salience 1
    when
      CardLimits( $max: limit != null, $type: type ) // no restriction on 'type'
      cardObject : Card( cardType == $type && loadCash > $max); 
    then
      cardObject.setTransactionResposne(false);
      cardObject.setMessage("Top up limit exceeds ");
    end
    
    KieSession KieSession=。。。;
    卡片=…;//你要传递给规则的卡片
    CardLimits限制=…;//极限
    插入(卡片);
    第二节插入(限额);
    kieSession.fireAllRules();
    
    请注意,对于此结构,可以为多种类型的限制使用一条规则

    例如,假设您有两种类型的卡“预付费”和“礼品”,不同的CardLimits实例描述了它们的限额。然后,您可以使用一条规则来处理这两个问题,如下所示:

    rule "ANY TOP UP LIMIT" 
    salience 1
    when
      CardLimits( $max: limit != null, $type: type ) // no restriction on 'type'
      cardObject : Card( cardType == $type && loadCash > $max); 
    then
      cardObject.setTransactionResposne(false);
      cardObject.setMessage("Top up limit exceeds ");
    end
    
    我还建议对卡片类型使用枚举而不是字符串

    通过传入对象,您还可以在将来自由添加其他条件。例如,假设如果卡在一定天数内过期,您希望拒绝补货,您可以根据需要将其添加到conditions对象中


    为了完整性起见,您的替代方法是使用globals。这通常是您想要避免的,出于同样的原因,我们尝试在大多数语言中避免全局变量,除非没有其他选择。当我第一次开始做口水(回到Drools 5)的时候,这是一个非常普遍的做法,但从那时起,它就不再受欢迎了

    global Integer max;
    global String type;
    
    rule "PREPAID TOP UP LIMIT" 
    salience 1
    when
      cardObject : Card( cardType == type && loadCash > max);
    then
      cardObject.setTransactionResposne(false);
      cardObject.setMessage("Top up limit exceeds ");
    end
    
    这将限制您仅使用单一类型和所有规则的最大值。所以你不能同时处理多张卡片

    在调用规则时设置全局变量:

    KieSession KieSession=。。。;
    卡片=…;//你要传递给规则的卡片
    //设置全局
    kieSession.setGlobal(“最大值”(整数)25000);
    kieSession.setGlobal(“类型”、“预付”);
    插入(卡片);
    kieSession.fireAllRules();
    

    我不建议为此使用Globals。我之所以包括这种可能性,是因为这是你可以做的事情。

    你基本上有两种选择:

  • 将条件传递到工作记忆中
  • 将条件声明为全局变量
  • 全局变量通常是不受欢迎的,但为了完整性,我将包括这一点


    对于第一个选项,即将条件传递到工作内存,我建议创建一个简单的对象,该对象声明了这些条件以及适当的getter/setter

    例如:

    公共类信用卡限额{
    私有字符串类型;
    私有整数限制;
    //这里有接球手和接球手
    }
    
    您可以将
    cardlimites
    实例(或者如果您有多种类型的实例)与
    Card
    实例一起传递到工作内存中。那么您的规则可以如下所示:

    rule "PREPAID TOP UP LIMIT" 
    salience 1
    when
      CardLimits( $max: limit != null, $type: type == "prepaid" )
      cardObject : Card( cardType == $type && loadCash > $max);
    then
      cardObject.setTransactionResposne(false);
      cardObject.setMessage("Top up limit exceeds ");
    end
    
    (细微的挑剔:你在
    结束后的分号不应该在那里。而且你在右边的“response”拼写错误。)