【仿真建模-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 小时前
基于Springboot的自习室预约系统的设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·毕业设计
秋书一叶2 小时前
SpringBoot项目打包为window安装包
java·spring boot·后端
碎梦归途2 小时前
23种设计模式-结构型模式之外观模式(Java版本)
java·开发语言·jvm·设计模式·intellij-idea·外观模式
极客先躯2 小时前
高级java每日一道面试题-2025年4月13日-微服务篇[Nacos篇]-Nacos如何处理网络分区情况下的服务可用性问题?
java·服务器·网络·微服务·nacos·高级面试
pwzs3 小时前
Spring MVC 执行流程全解析:从请求到响应的七步走
java·后端·spring·spring mvc
我该如何取个名字3 小时前
Mac配置Java的环境变量
java·开发语言·macos
kkkkatoq3 小时前
Java中的锁
java·开发语言
界面开发小八哥4 小时前
「Java EE开发指南」用MyEclipse开发EJB 3无状态会话Bean(二)
java·ide·java-ee·eclipse·myeclipse
LCY1334 小时前
spring security +kotlin 实现oauth2.0 认证
java·spring·kotlin
soulermax4 小时前
数字ic后端设计从入门到精通2(含fusion compiler, tcl教学)
java·linux·服务器