当前位置: 首页 > news >正文

同一类型,每条数据,执行不同逻辑

一、例子

设备表:

public class Equipment {private Long id;private Boolean state;//开关private String logic;//判断开关所要执行的逻辑
}

设备数据:

[{"id": 1,"state": true,"logic": "equipValue == 1"},{"id": 2,"state": false,"logic": "equipValue == 0"},{"id": 3,"state": true,"logic": "equipValue == 1 && humidity < 80"}
]

解决方法:


public class MainApp {public static void main(String[] args) {// 初始化 JEXL 引擎JexlEngine jexl = new JexlBuilder().create();// 创建设备对象Equipment equip1 = new Equipment(1L, true, "equipValue == 1 && temperature < 50");Equipment equip2 = new Equipment(2L, false, "equipValue == 0 && pressure > 10");Equipment equip3 = new Equipment(3L, true, "equipValue == 1 && humidity < 80");// 模拟变量输入Map<String, Object> variables1 = new HashMap<>();variables1.put("equipValue", 1);variables1.put("temperature", 45);Map<String, Object> variables2 = new HashMap<>();variables2.put("equipValue", 0);variables2.put("pressure", 15);Map<String, Object> variables3 = new HashMap<>();variables3.put("equipValue", 1);variables3.put("humidity", 75);// 判断设备状态System.out.println("Equip1 should be on? " + evaluateState(jexl, equip1, variables1));System.out.println("Equip2 should be on? " + evaluateState(jexl, equip2, variables2));System.out.println("Equip3 should be on? " + evaluateState(jexl, equip3, variables3));}/*** 使用 JEXL 表达式计算设备是否应为开启状态*/public static boolean evaluateState(JexlEngine jexl, Equipment equipment, Map<String, Object> variables) {try {// 构建上下文JexlContext context = new MapContext(variables);// 解析并执行表达式JexlExpression expression = jexl.createExpression(equipment.getLogic());Object result = expression.evaluate(context);// 返回布尔结果return Boolean.TRUE.equals(result);} catch (Exception e) {System.err.println("Error evaluating expression for equipment: " + equipment.getId() + " - " + e.getMessage());// 出错时返回原始 statereturn equipment.isState();}}
}

二、所用依赖

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-jexl3</artifactId><version>3.5</version> <!-- 可根据需求替换为最新版本 -->
</dependency>implementation 'org.apache.commons:commons-jexl3:3.5' // 可根据需求替换为最新版本

用法:

import org.apache.commons.jexl3.*;public class JexlExample {public static void main(String[] args) {JexlEngine engine = new JexlBuilder().create();String expression = "vars.temperature > 30 ? 'high' : 'normal'";JexlExpression jexlExpr = engine.createExpression(expression);JexlContext context = new MapContext();((MapContext) context).set("temperature", 35);Object result = jexlExpr.evaluate(context);System.out.println(result);  // 输出: high}
}

http://www.dtcms.com/a/271808.html

相关文章:

  • 偏振相机,偏振图像是怎么样的
  • WebGPU了解
  • 智能体决策机制深度剖析:ReAct、Plan-and-Execute与自适应策略
  • 云蝠智能VoiceAgent重构企业电话客服体系
  • PLC框架-1.3.2 报文750控制汇川伺服的转矩上下限
  • 【前缀和 BFS 并集查找】P3127 [USACO15OPEN] Trapped in the Haybales G|省选-
  • XSS(跨站脚本攻击)
  • RabbitMQ 消息队列:从入门到Spring Boot实战
  • Java 枚举详解:从基础到实战,掌握类型安全与优雅设计
  • 7-语言模型
  • CRT 不同会导致 fopen 地址不同
  • 技术演进中的开发沉思-30 MFC系列:五大机制
  • 删除k8s安装残留
  • Spring Boot:将应用部署到Kubernetes的完整指南
  • ACL协议:核心概念与配置要点解析
  • Docker 环境下 MySQL 主从复制集群、MGR 搭建及 Nginx 反向代理配置
  • SSRF10 各种限制绕过之30x跳转绕过协议限制
  • ip地址可以精确到什么级别?如何获取/更改ip地址
  • 配置双网卡Linux主机作为路由器(连接NAT网络和仅主机模式网络)
  • 在 Mac 上使用 Git 拉取项目:完整指南
  • 【算法笔记】6.LeetCode-Hot100-链表专项
  • selenium中find_element()用法进行元素定位
  • 在mac m1基于llama.cpp运行deepseek
  • Spring Boot 企业级动态权限全栈深度解决方案,设计思路,代码分析
  • C#基础:Winform桌面开发中窗体之间的数据传递
  • 【WEB】Polar靶场 Day8 详细笔记
  • 力扣 hot100 Day40
  • fastMCP基础(一)
  • imx6ull-裸机学习实验16——I2C 实验
  • 解锁localtime:使用技巧与避坑指南