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

相关推荐
麦兜*1 小时前
Spring Boot 整合量子密钥分发(QKD)实验方案
java·jvm·spring boot·后端·spring·spring cloud·maven
码破苍穹ovo2 小时前
堆----1.数组中的第K个最大元素
java·数据结构·算法·排序算法
2301_793086872 小时前
JVM 01 运行区域
java·开发语言
崎岖Qiu2 小时前
【JVM篇13】:兼顾吞吐量和低停顿的G1垃圾回收器
java·jvm·后端·面试
久念祈3 小时前
C++ - 仿 RabbitMQ 实现消息队列--服务端核心模块实现(五)
java·rabbitmq·java-rabbitmq
超级晒盐人5 小时前
用落霞归雁的思维框架推导少林寺用什么数据库?
java·python·系统架构·学习方法·教育电商
岁忧6 小时前
(LeetCode 面试经典 150 题) 138. 随机链表的复制 (哈希表)
java·c++·leetcode·链表·面试·go
鹦鹉0076 小时前
IO流中的字节流
java·开发语言·后端
你我约定有三6 小时前
分布式微服务--Nacos作为配置中心(二)
java·分布式·spring cloud·微服务·架构·wpf·负载均衡
qq_165706076 小时前
java实现运行SQL脚本完成数据迁移
java·sql