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

相关推荐
渔舟小调27 分钟前
针对vue 、 kotlin 、java 这三个语言AI辅助开发的提示词
java·vue.js·kotlin
巨可爱熊1 小时前
C++基础算法(插入排序)
java·c++·算法
深鱼~1 小时前
本地飞牛NAS快速部署WordPress个人网站并一键上线公网远程访问
java·开发语言
大萌神Nagato1 小时前
力扣刷题724. 寻找数组的中心下标
java·算法·leetcode
Arbori_262151 小时前
使用idea开发spark程序
java·ide·intellij-idea
欣然~1 小时前
Java康威生命游戏(Conway‘s Game of Life)
java
开开心心就好1 小时前
开启智能生活新篇:免费 APP 实现家电万能操控
java·windows·python·微信·pdf·生活·软件需求
希忘auto1 小时前
理解Java数据库编程之JDBC
java·jdbc·idea
馨谙2 小时前
里氏替换原则
java·开发语言·里氏替换原则
你曾经是少年2 小时前
MyBatis-Plus 从入门到精通教学文档
java·开发语言·tomcat