如何在Anylogic中动态读取数据库列

问题描述

背景优先:我有以下代理类型:activitypathway路径是包含活动群体的代理。 活动包含给予其他代理(订单)的特定说明。 路径是这些指令的集合。 pathway 集合是通过数据库定义的,其中每行是 1 个通路代理,每列是 活动 的字符串 ID,单元格值为 0、1、2等等,取决于分配给该途径的每个活动数量

目标:我需要在代理启动时在 pathway 代理中创建一组 活动

我尝试过的: 我在 Main 中创建了一组 活动。每个路径都有一个activityList,其中包含所有活动的字符串ID。目的是当单元格值为 1 或以上时,代码将从 main 中选择相应的 activity 代理并将其放入 pathway 中的种群中。下面的代码,我从其他帖子中注意到索引用于遍历行,但这似乎不适用于列。

问题:我无法根据字符串 ID 列表动态搜索列的代码

activityIndex = 0;
// there are 115 activities in the list,the code would repeat for each one
while (activityIndex <= 114)
{
    activityName = activityList.get(activityIndex);
// search below returns the cell value from row with pathway ID = agAssess
// it searches the column with string ID matching activityName
// note: error returned as the database does not contain a column named "activityName"
// in this case "activityName" in the search below would need to be replaced with the parameter's value
// error message: activityName cannot be resolved or is not a field
    double value = selectFrom(activity_groups_assess).
        where(activity_groups_assess.code.eq(agAssess)).
        uniqueResult(activity_groups_assess.activityName);
// if the value > 0,create agent with string ID activityName
// for agents with value > 1,multiple agents with the same string ID would be created,hence while function
    while (value > 0)
    {
// find the agent with the same string ID
        Activity newActivity = findFirst(main.activities,p -> p.code == activityName);
// add agent to population activitiesAssess
// error when adding agent: Description: The method add_activitiesAssess() in the type ClinicalPathway is not applicable for the arguments (Activity).
        add_activitiesAssess( newActivity );
        value--;
    }
    activityIndex++;
}

解决方法

也许这有帮助:

您可以使用以下类型将数据库表的引用存储在变量中:

com.mysema.query.sql.RelationalPathBase

在特定列中选择 double(int、String 等)类型的值时,您可以通过索引调用 variable.getColumns().get(index) 来获取该列。然后您需要将其转换为相应的类型,如下所示:

List<Double> resultRows = selectFrom(variable).where( ( (com.mysema.query.types.path.NumberPath<Double>) variable.getColumns().get(1) ).eq(2.0)) .list(( (com.mysema.query.types.path.NumberPath<Double>) variable.getColumns().get(1) ));

,

我能不能先澄清一下,所以你有一个看起来像这样的表格:

路径ID 活动1 活动2 活动3
一个 0 3 2
两个 1 0 1
三个 1 1 0

并且您想在路径“一”代理中创建 3 个“活动 2”和 2 个“活动 3”代理。对吗?

如果是这样,恐怕没有一种“开箱即用”的干净方法。我建议一次阅读一行,然后调用“add_activity”。所以代码看起来像下面这样。

List<Tuple> records = selectFrom(testdbtable).list();
records.forEach(rec -> {
    String pathway = rec.get(testdbtable.pathway);
    testdbtable.getColumns().forEach(col -> {
        String activityName = col.getMetadata().getName(); // <-  this is the column name which in your case is Activity Id
        if (!activityName.equalsIgnoreCase(testdbtable.pathway.getMetadata().getName())) {
            int activityCount = (int)rec.get(col);
            // ... and below you add Acitivity agent to Pathway agent
            traceln("Pathway [%s],Activity [%s] -> count is %d",pathway,activityName,activityCount); 
        }
    });
});

以上代码产生以下输出:

Pathway [one],Activity [activity1] -> count is 0
Pathway [one],Activity [activity2] -> count is 3
Pathway [one],Activity [activity3] -> count is 2
Pathway [one],Activity [al_id] -> count is 0
Pathway [two],Activity [activity1] -> count is 1
Pathway [two],Activity [activity2] -> count is 0
Pathway [two],Activity [activity3] -> count is 1
Pathway [two],Activity [al_id] -> count is 1
Pathway [three],Activity [activity1] -> count is 1
Pathway [three],Activity [activity2] -> count is 1
Pathway [three],Activity [activity3] -> count is 0
Pathway [three],Activity [al_id] -> count is 2

因此,您只需在 traceln 语句之后添加用于创建适当活动代理的代码。希望有所帮助。