如何在AnyLogic中映射Ped Source新Agent与数据库表并解决报错
我正在开发一个AnyLogic模型,需要通过Ped Source模块读取数据库表中的参数来生成Agent,相关设置如下:
- 数据库表详情:

- Patient类型Agent的Patients种群与主Agent参数映射:

- Ped Source添加行人至Patients种群的设置:

运行模型时出现如下错误:
Exception during discrete event execution:
root.pedSource:
Error when trying to initialize new agent
Caused by: Not unique database value!
java.lang.RuntimeException: root.pedSource:
Error when trying to initialize new agent
at com.anylogic.engine.Engine.error(Unknown Source)
at com.anylogic.engine.Agent.error(Unknown Source)
at com.anylogic.libraries.processmodeling.Source.initializeNewEntity(Unknown Source)
at com.anylogic.libraries.processmodeling.Enter.take(Unknown Source)
at com.anylogic.libraries.processmodeling.Source.inject(Unknown Source)
at com.anylogic.libraries.processmodeling.Source.executeActionOf(Unknown Source)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.l(Unknown Source)
Error during drawing animation frame:
java.lang.NullPointerException
at com.anylogic.engine.presentation.ShapeEmbeddedObjectPresentation.resetSVGState(Unknown Source)
at com.anylogic.engine.presentation.k.resetSVGState(Unknown Source)
at com.anylogic.engine.presentation.ShapeAgentPopulationGroup.resetSVGState(Unknown Source)
at com.anylogic.engine.presentation.ShapeGroup.resetSVGState(Unknown Source)
at com.anylogic.engine.gui.SVGFrameProducer.a(Unknown Source)
at com.anylogic.engine.gui.SVGFrameProducer.a(Unknown Source)
at com.anylogic.engine.gui.ExperimentHost$1.f(Unknown Source)
at com.anylogic.engine.gui.ExperimentHost$1.f(Unknown Source)
at com.anylogic.engine.gui.ExperimentHost$1.a(Unknown Source)
at com.anylogic.engine.gui.SVGFrameCollector.a(Unknown Source)
at com.anylogic.engine.gui.SVGFrameCollector.f(Unknown Source)
at java.base/java.lang.Thread.run(Thread.java:834)at com.anylogic.engine.Engine.nb(Unknown Source) at com.anylogic.engine.Engine$f.run(Unknown Source)Caused by: java.lang.RuntimeException: Not unique database value!
at com.anylogic.engine.database.SelectQuery.a(Unknown Source)
at com.anylogic.engine.database.SelectQuery.uniqueResult(Unknown Source)
at com.anylogic.engine.database.SelectQuery.uniqueResult(Unknown Source)
at medicalsupport.Main.setupParameters_patients_xjal(Main.java:1647)
at medicalsupport.Main$_patients_Population.callSetupParameters(Main.java:322)
at medicalsupport.Main$_patients_Population.callSetupParameters(Main.java:1)
at com.anylogic.engine.AgentList.callSetupParameters(Unknown Source)
at com.anylogic.engine.Agent.goToPopulation(Unknown Source)
... 8 more
1. 核心错误原因
报错Not unique database value!表明:在通过数据库查询初始化Agent参数时,AnyLogic调用uniqueResult()方法期望获取唯一结果,但实际查询返回了多条匹配数据,触发冲突。结合堆栈信息,问题出在种群参数映射的自动生成查询逻辑中。
2. 具体修复步骤
(1)修正数据库查询逻辑
- 打开主Agent的Population参数映射设置界面,找到自动生成的数据库查询代码。
- 默认映射逻辑未添加唯一筛选条件,需确保每次生成Agent时,查询语句能精确匹配单条记录。比如基于表的主键(如
id)筛选,或按顺序遍历数据。
(2)改用手动初始化Agent参数
放弃种群自动参数映射,在Ped Source的On create agent代码块中手动从数据库读取并赋值:
// 示例:按种群当前数量作为索引,读取对应数据库记录 int currentIdx = patients.size(); PatientData data = selectFrom(patientData) .where(patientData.id.eq(currentIdx + 1)) .singleResult(); // 给Agent参数赋值 agent.age = data.age; agent.gender = data.gender; // 其他参数依次赋值...
确保每次查询仅返回唯一一条记录,避免uniqueResult()抛出异常。
(3)确保数据库数据唯一性
检查数据库表,确认用于筛选的字段(如主键)无重复值,必要时给字段添加唯一约束或清理重复数据。
(4)空指针异常自动修复
空指针异常是Agent初始化失败导致的动画渲染错误,解决数据库查询唯一性问题后,该异常会自动消失。
3. 验证方法
- 运行模型前单独测试查询语句,确认每次调用仅返回单条记录。
- 逐步调试Agent初始化流程,检查参数赋值是否正常。
内容的提问来源于stack exchange,提问作者Jahidul Hasan Razib

