edgexfoundry

 找回密码
 立即注册
搜索
热搜: meta core rules
查看: 21043|回复: 2

edgexfoundry rulesengine 实战例子

[复制链接]

10

主题

10

帖子

620

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
620
发表于 2018-10-24 09:30:17 | 显示全部楼层 |阅读模式
本帖最后由 灰哥哥 于 2018-10-24 16:25 编辑

1、生成一个规则

postman工具提交数据

post application/json to  http://localhost:48075/api/v1/rule

{
    "name": "motortoofastsignal",
    "condition": {
        "device": "GS1-VariableSpeedMotor01",
        "checks": [{
            "parameter": "RPM",
            "operand1": "Integer.parseInt(value)",
            "operation": ">",
            "operand2": "2"
        }]
    },
    "action": {
        "device": "5bce72a0e4b055b4b7bf2d4f",
        "command": "5bce72a0e4b055b4b7bf2d4c",
        "body": "{\\\"RPM\\\":\\\"1\\\"}"
    },
    "log": "Patlite warning triggered for engine speed too high"
}


如下图:


上图提交内容说明:
  • condition 中的device:对应device集合中的name
  • check中的parameter:对应deviceprofile中定义了一个deviceresource
  • action中的device:对应device集合中的id
  • action中的command:先找到device对应的deviceprofile,command即是在deviceprofile中command的ID

此规则的含义
当support-rulesengine接收到export-distro发过来的event时,会判断此event如下:
设备名称为“GS1-VariableSpeedMotor01”,参数“RPM” 值大于2时,则会执行action。
执行action是指:调用设备ID为5bce72a0e4b055b4b7bf2d4f的ID为5bce72a0e4b055b4b7bf2d4c的command命令的put方法 ,put的内容为:{"RPM":"1"}

综述:当马达(RPM)转速高于2时,则将其设置为1


2、查看生成的规则
使用 docker exec -it support-rulesengine /bin/sh命令进入容器,在容器/edgex/edgex-support-rulesengine/rules目录下生成了motortoofastsignal.drl规则文件,查看过程如下:
  1. myEdgex@ubuntu:/var/lib/docker$ docker exec -it support-rulesengine /bin/sh
  2. /edgex/edgex-support-rulesengine # ls
  3. rules                    support-rulesengine.jar  templates
  4. /edgex/edgex-support-rulesengine # cd rules/
  5. /edgex/edgex-support-rulesengine/rules # ls
  6. motortoofastsignal.drl
  7. /edgex/edgex-support-rulesengine/rules # cat motortoofastsignal.drl
  8. package org.edgexfoundry.rules;
  9. global org.edgexfoundry.engine.CommandExecutor executor;
  10. global org.edgexfoundry.support.logging.client.EdgeXLogger logger;
  11. import org.edgexfoundry.domain.core.Event;
  12. import org.edgexfoundry.domain.core.Reading;
  13. import java.util.Map;
  14. rule "motortoofastsignal"
  15. when
  16.   $e:Event($rlist: readings && device=="GS1-VariableSpeedMotor01")
  17.   $r0:Reading(name=="RPM" && Integer.parseInt(value) > 2) from $rlist
  18. then
  19. executor.fireCommand("5bce72a0e4b055b4b7bf2d4f", "5bce72a0e4b055b4b7bf2d4c", "{"RPM":"1"}");
  20. logger.info("Patlite warning triggered for engine speed too high");
  21. end/edgex/edgex-support-rulesengine/rules #
复制代码

流程:
1、ZeroMQEventSubscriber类接收来自export-distro 的event消息(也可直接接收core-data的,可配置)后,调用RuleEngine的execute
2、RuleEngine的execute方法,执行规则判断(drools语句是按顺序执行的,每句通过再算规则满足)
3、规则满足,则执行CommandExecutor.fireCommand方法

drools语方




3、验证规则引擎


本例是通过device-virtual来验证,首先打开device-virtual的 H2数据库,看一下当前的RPM值,如下图:


上图可以看出RPM=3,是可以满足规则的,会触发执行executor.fireCommand("5bce72a0e4b055b4b7bf2d4f", "5bce72a0e4b055b4b7bf2d4c", "{\"RPM\":\"1\"}");

3.1 构造满足规则的场景
向command模块发一个命令
用postman工具 发送GET http://localhost:48082/api/v1/device/5bce72a0e4b055b4b7bf2d4f/command/5bce72a0e4b055b4b7bf2d4c
注意:上面红色部分与当时规则创建时的一致。上面请求会触发一个event


再查看device-vitrul H2:




查看rulesengine日志

  1. docker-compose -f docker-compose-registry-java.yml logs rulesengine
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

0

主题

1

帖子

26

积分

新手上路

Rank: 1

积分
26
发表于 2019-11-8 15:17:49 | 显示全部楼层
规则殷勤设置后一直不触发,查看日志也没有触发记录会是什么原因
回复

使用道具 举报

25

主题

34

帖子

1332

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1332
发表于 2019-12-2 22:25:13 | 显示全部楼层
july_wu 发表于 2019-11-8 15:17
规则殷勤设置后一直不触发,查看日志也没有触发记录会是什么原因

联系QQ:15599633
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ号:15599633|管理员邮箱:admin@edgexfoundry.net|Archiver|手机版|小黑屋|edgexfoundry ( 赣ICP备19008954号-1 )

GMT+8, 2023-3-27 11:04 , Processed in 0.040240 second(s), 23 queries .

© 2018-2029 edgexfoundry.net

快速回复 返回顶部 返回列表