《Drools7.0.0.Final规则引擎教程》第4章 Function函数

简介: 《Drools7.0.0.Final规则引擎教程》第4章 Function函数

Function函数

首先来看一下function函数的语法结构图:



函数是将语义代码放置在规则文件中的一种方式,就相当于java类中的方法一样。函数并不会比辅助类做更多的事情,实际上,编译器会在幕后生成助手类。使用函数的好处是可以将业务逻辑集中放置在一个地方,根据需要可以对函数进行修改。但它既有好处也有坏处。函数对于调用规则的后果部分操作是最有用处的,特别是只有参数变化但执行的操作完全相同时。这里的函数可以对照java中方法的抽取封装来理解。


典型的函数声明如下所示:


function String hello(String name) {

   return "Hello "+name+"!";

}

实例规则代码如下:


package com.rules
function String hello(String name){
    return "Hello " + name + "!";
}
rule helloSomeone
    agenda-group "function-group"
    when
        eval(true);
    then
        System.out.println(hello("Tom"));
    end

测试代码如下:

@Test
    public void testFunction(){
        KieSession kieSession = this.getKieSession("function-group");
        int count = kieSession.fireAllRules();
        kieSession.dispose();
        System.out.println("Fire " + count + " rule(s)!");
    }

执行结果:


Hello Tom!

Fire 1 rule(s)!

需要注意的是,function虽然不是java的一部分,但是依然可以在这里使用。函数的参数根据需要可以有一个,也可以有多个,也可以没有。返回结果的类型定义和正常的java语法没有区别。


前面我们已经讲过如何引入java中的静态方法,此处的function也可以用静态方法来代替,具体使用参考相关章节,这里就不再赘述。



目录
相关文章
|
2月前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
2月前
|
安全 JavaScript 应用服务中间件
【Azure Function App】如何修改Azure函数应用的默认页面呢?
【Azure Function App】如何修改Azure函数应用的默认页面呢?
|
2月前
|
C# C++ Python
【Azure 应用服务】Azure Durable Function(持久函数)在执行Activity Function时候,因为调用函数名称错误而导致长时间无响应问题
【Azure 应用服务】Azure Durable Function(持久函数)在执行Activity Function时候,因为调用函数名称错误而导致长时间无响应问题
|
2月前
|
SQL JavaScript 前端开发
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
|
2月前
|
JSON 数据格式 Python
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
|
2月前
|
C++ Python
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
|
2月前
【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题
【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题
|
2月前
|
消息中间件 Kafka 网络安全
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验
|
2月前
【Azure 应用服务】Function App中的函数(Functions)删除问题
【Azure 应用服务】Function App中的函数(Functions)删除问题
|
3月前
|
C++ 运维
开发与运维函数问题之使用std::function实现回调函数的示例如何解决
开发与运维函数问题之使用std::function实现回调函数的示例如何解决
33 7

热门文章

最新文章

下一篇
无影云桌面