You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将TimeFold.ai FoodPackaging数据源从DemoDataGenerator改为JSON读取

将TimeFold.ai FoodPackaging示例切换为JSON数据源

1. 匹配实体定义JSON结构

对照DemoDataGenerator生成的核心实体(FoodType、PackagingType、Demand、FoodPackagingSolution),定义对应的JSON结构,确保字段名与Java实体属性一致(或用@JsonProperty注解映射)。示例JSON如下:

{
  "foodTypes": [
    {
      "id": "FOOD_0",
      "name": "Fresh Apple",
      "minProtectionDays": 7,
      "maxProtectionDays": 30
    }
  ],
  "packagingTypes": [
    {
      "id": "PACK_0",
      "name": "Plastic Wrap",
      "protectionDays": 10,
      "costPerUnit": 0.5
    }
  ],
  "demands": [
    {
      "id": "DEMAND_0",
      "foodTypeId": "FOOD_0",
      "quantity": 1000,
      "dueDate": "2024-10-01"
    }
  ]
}

2. 添加JSON解析依赖

如果用Maven,在pom.xml中添加Jackson依赖以处理JSON序列化/反序列化:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.15.2</version>
</dependency>

Gradle项目则添加:

implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2'

3. 编写JSON数据读取类

创建工具类负责加载JSON文件并转换为FoodPackagingSolution对象:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class JsonDataLoader {
    private static final ObjectMapper objectMapper = new ObjectMapper()
            .registerModule(new JavaTimeModule()); // 处理Java 8日期类型

    public static FoodPackagingSolution loadFromJson(String filePath) throws IOException {
        JsonSolutionWrapper wrapper = objectMapper.readValue(new File(filePath), JsonSolutionWrapper.class);
        FoodPackagingSolution solution = new FoodPackagingSolution();
        
        // 映射基础数据
        solution.setFoodTypeList(wrapper.getFoodTypes());
        solution.setPackagingTypeList(wrapper.getPackagingTypes());
        solution.setDemandList(wrapper.getDemands());
        
        // 初始化规划实体Allocation(与DemoDataGenerator逻辑对齐)
        wrapper.getDemands().forEach(demand -> {
            Allocation allocation = new Allocation();
            allocation.setDemand(demand);
            solution.getAllocationList().add(allocation);
        });
        return solution;
    }

    // 内部包装类,对应JSON根结构
    private static class JsonSolutionWrapper {
        private List<FoodType> foodTypes;
        private List<PackagingType> packagingTypes;
        private List<Demand> demands;

        // 生成getter和setter方法
        public List<FoodType> getFoodTypes() { return foodTypes; }
        public void setFoodTypes(List<FoodType> foodTypes) { this.foodTypes = foodTypes; }
        public List<PackagingType> getPackagingTypes() { return packagingTypes; }
        public void setPackagingTypes(List<PackagingType> packagingTypes) { this.packagingTypes = packagingTypes; }
        public List<Demand> getDemands() { return demands; }
        public void setDemands(List<Demand> demands) { this.demands = demands; }
    }
}

4. 修改主程序入口

替换原来调用DemoDataGenerator.generateData()的逻辑,改为加载JSON数据:

public class FoodPackagingApp {
    public static void main(String[] args) {
        SolverFactory<FoodPackagingSolution> solverFactory = SolverFactory.create(
                SolverConfig.createFromXmlResource("foodPackagingSolverConfig.xml"));

        // 替换Demo数据生成逻辑
        FoodPackagingSolution solution;
        try {
            solution = JsonDataLoader.loadFromJson("src/main/resources/foodPackagingData.json");
        } catch (IOException e) {
            throw new RuntimeException("Failed to load JSON data", e);
        }

        Solver<FoodPackagingSolution> solver = solverFactory.buildSolver();
        FoodPackagingSolution bestSolution = solver.solve(solution);

        // 输出结果逻辑保持不变
        printSolution(bestSolution);
    }
}

5. 验证数据与求解逻辑

  • 检查JSON字段值是否符合业务规则(如minProtectionDays ≤ maxProtectionDays)
  • 确认Allocation等规划实体初始化正确,保证TimeFold能识别规划变量与实体
  • 运行程序,对比切换前后的求解结果,验证逻辑一致性

内容的提问来源于stack exchange,提问作者EdoZp12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:17:06