如何在流程建模库模块中区分Agent类型并执行对应计算?
区分Agent类型执行计算的解决方案
一、先排查Source模块代码未生效的核心问题
- 核对
TypeOfProduct选项列表的定义:确保选项里的A、B与代码中TypeOfProduct.A、TypeOfProduct.B的拼写、大小写完全一致。 - 确认Agent的
type参数赋值逻辑:必须在Source的Agent type设置中,给Product的type参数绑定customDistribution,而非仅在Agent参数里设默认值。操作步骤:- 打开Source属性面板,选中Agent type下的
Product。 - 点击
type参数旁的表达式按钮,选择customDistribution。 - 添加两项配置:值选
TypeOfProduct.A、权重0.3;值选TypeOfProduct.B、权重0.7。
- 打开Source属性面板,选中Agent type下的
- 检查Source的事件触发规则:若设置了批量生成,确认On exit是针对每个生成的Agent单独触发(默认是单Agent触发,若修改过需改回)。
二、各模块区分Agent类型的正确写法
1. Source模块(On exit事件)
因agent是通用类型,需强制转为Product后访问type参数,示例代码:
Product product = (Product) agent; if (product.type == TypeOfProduct.A) { // A类型Agent的计算逻辑,比如: product.processDuration = 5; traceln("处理A类产品"); } else { // B类型Agent的计算逻辑,比如: product.processDuration = 10; traceln("处理B类产品"); }
2. Resource模块(On seize/On release事件)
通过getOccupant()获取当前占用资源的Agent,示例代码:
Product product = (Product) getOccupant(); if (product.type == TypeOfProduct.A) { // 针对A类型调整资源工作参数,比如: setServiceTime(3); } else { setServiceTime(6); }
3. Service模块(On enter/On exit事件或Service time表达式)
- 事件中写法:
Product product = (Product) agent; if (product.type == TypeOfProduct.A) { setServiceTime(4); } else { setServiceTime(8); }
- 直接在Service time中写简洁表达式:
((Product)agent).type == TypeOfProduct.A ? 4 : 8
三、调试验证技巧
- 在代码中加入
traceln("当前Agent类型:" + product.type),运行模型后查看控制台输出,确认类型赋值是否正确。 - 在
ProductAgent的On create事件中打印type值,验证Agent生成时类型已初始化。
内容的提问来源于stack exchange,提问作者KaSher
相关产品推荐
相关产品推荐

