Search code examples
javaanylogic

can I would like to make a multiple iteration with a single loop Anylogic


I would like to make these multiple iterations in one single loop, but I don't know how it works. I'm reading the database of daily demand and sending it to the wholesaler agent if the demand is greater than the current stock of the retailer.

// read demand from DBs that send orders to wholesaler1
int today = 1;
retailers(0).dailyDemand = (int) selectFrom(ddr1)
    .where(ddr1.day.eq(today))
    .firstResult(ddr1.demand);
wholesaler1.retailersDemand = retailers(0).dailyDemand;     
retailers(1).dailyDemand = (int) selectFrom(ddr2)
    .where(ddr2.day.eq(today))
    .firstResult(ddr2.demand);
wholesaler1.retailersDemand = retailers(1).dailyDemand; 
retailers(2).dailyDemand = (int) selectFrom(ddr3)
    .where(ddr3.day.eq(today))
    .firstResult(ddr3.demand);
wholesaler1.retailersDemand = retailers(2).dailyDemand;
retailers(3).dailyDemand = (int) selectFrom(ddr4)
    .where(ddr4.day.eq(today))
    .firstResult(ddr4.demand);
wholesaler1.retailersDemand = retailers(3).dailyDemand; 
retailers(4).dailyDemand = (int) selectFrom(ddr5)
    .where(ddr5.day.eq(today))
    .firstResult(ddr5.demand);
wholesaler1.retailersDemand = retailers(4).dailyDemand; 
// read demand from DBs that send orders to wholesaler2
wholesaler2.retailersDemand = retailers(5).dailyDemand; 
retailers(5).dailyDemand = (int) selectFrom(ddr6)
    .where(ddr6.day.eq(today))
    .firstResult(ddr6.demand);
wholesaler2.retailersDemand = retailers(5).dailyDemand; 
today++;

the screenshot illustrates what I mean Code Agents List


Solution

  • You can do the following, mainting the name of the columns with the format you have

    Tuple t = selectFrom(daily_demand_retailers)
        .where(daily_demand_retailers.day.eq(today))
        .list().get(0);
            
    daily_demand_retailers.getColumns().forEach(col->{
        String colName = col.getMetadata().getName(); 
        if (!colName.equals("al_id")
                && !colName.equals("day")
                ) {
            int id=Integer.parseInt(colName.split("_r")[1]);
            
            retailers.get(id-1).dailyDemand=(int)t.get(col);
        }
    });