【仿真建模-anylogic】EventCondition原理解析

复制代码
Author:赵志乾
Date:2024-06-15
Declaration:All Right Reserved!!!

1. 类图

2. 原理解析

2.1 核心函数

|-----------------------------|-----------------------------------|
| 函数 | 功能 |
| EventCondition(Agent owner) | 构造函数,入参设定事件的owner |
| void start() | 启动事件上的条件检测;如果启动时条件为true,则事件会被立即执行 |
| void reset() | 取消当前的事件调度 |
| void restart() | 重新在条件上启动检测 |
| void onChange() | 变更发生时执行 |
| boolean isMonitoring() | 判定当前是否在条件上做检测 |

2.2 代码解析

由于Anylogic内核做了代码混淆,以下代码为二次加工后的逻辑:

复制代码
//**************************核心字段************************
// 条件是否处于检测中
private boolean monitoring = false;

//**************************构造函数************************
//设定事件的owner
public EventCondition(Agent owner) {
	super(owner);
}

//*************************启动函数**************************
//在owner的start()函数中调用
public void start() {
    // 禁用超时事件--条件和超时拥有共同的基类
	this.disableTimeoutEvent();
    // 在引擎中注册该事件
	this.getAgent().getEngine().register(this);
    // 设置检测状态
	this.monitoring = true;
    // 首次判定,如果此刻条件满足,则立即执行
	if (this.getAgent().testConditionOf(this)) {
		this.enableTimeOutEvent(0.0); // 超时时间为0,即为立即开始执行
	}
}

//************************取消检测与重启检测*********************
//取消检测
public void reset() {
    // 禁用超时事件
	this.disableTimeoutEvent();
    // 设置检测状态为取消
	this.monitoring = false;
    // 引擎中移除事件
	this.getAgent().getEngine().unregister(this);
}
// 重启检测
public void restart() {
    // 禁用超时事件
	this.disableTimeoutEvent();
    // 设置检测状态为启用
	this.monitoring = true;
    // 引擎中注册事件
	this.getAgent().getEngine().register(this);
}


public boolean isMonitoring() {
	return this.monitoring;
}

//***************************变更**********************************
public void onChange() {
    // 未检测时不执行
	if (!this.monitoring) {
        return;
    }
    // 未触发检测条件时,再次禁用超时事件
	if (!this.getAgent().testConditionOf(this)) {
        this.disableTimeoutEvent();
        return;
    }
	// 如果事件未做超时规划,则规划0时长超时触发,即立刻执行
    if (!this.isActive()) {
		this.enableTimeOutEvent(0.0);
	}
}

//******************************执行逻辑****************************
void execute() {
	super.execute();
	Agent owner = this.getAgent();
    // 条件未满足则不执行
	if (!owner.testConditionOf(this)) {
		this.getAgent().nothingChanged();
        return;
	}
    // 设置检测状态为取消
	this.monitoring = false;
	try {
        // 执行自定义逻辑
		owner.executeActionOf(this);
	} finally {
        // 如果处于未检测状态则从引擎中移除事件,所以如果想再次启用检测需要调用restart方法
		if (!this.monitoring) {
			owner.getEngine().unregister(this);
		}
	}
}

3. 应用场景

图形编辑窗口拖拽Event组件,并选择Trigger Type为Condition时,Anylogic会自动生成EventCondition实例;当指定的检测条件为true时,自定义逻辑会被执行;

如果条件使用连续变化量,则数值引擎会持续检测条件是否发生;

如果是纯离散模型,则只会在变更发生时才会进行检测,即在onChange函数中检测;

相关推荐
lee_curry7 小时前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
九转成圣8 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
直奔標竿9 小时前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring
金銀銅鐵9 小时前
[java] 编译之后的记录类(Record Classes)长什么样子(上)
java·jvm·后端
野生技术架构师11 小时前
金三银四面试总结篇,汇总 Java 面试突击班后的面试小册
java·面试·职场和发展
小袁拒绝摆烂11 小时前
多表关联大平层转JSON树形结构
java·json
ja哇12 小时前
大厂面试高频八股
java·面试·职场和发展
yoyo_zzm12 小时前
Laravel6.x新特性全解析
java·spring boot·后端
Nick_zcy13 小时前
小说在线阅读网站和小说管理系统 · 功能全解析
java·后端·python·springboot·ruoyi