【仿真建模-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函数中检测;

相关推荐
老毛肚13 小时前
MyBatis体系结构与工作原理 上篇
java·mybatis
风流倜傥唐伯虎13 小时前
Spring Boot Jar包生产级启停脚本
java·运维·spring boot
Yvonne爱编码13 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
Re.不晚13 小时前
JAVA进阶之路——无奖问答挑战1
java·开发语言
你这个代码我看不懂13 小时前
@ConditionalOnProperty不直接使用松绑定规则
java·开发语言
fuquxiaoguang14 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析
琹箐14 小时前
最大堆和最小堆 实现思路
java·开发语言·算法
__WanG14 小时前
JavaTuples 库分析
java
坚持就完事了14 小时前
数据结构之树(Java实现)
java·算法
Monly2114 小时前
Java:修改打包配置文件
java·开发语言