xyc
2024-08-02 4df461673ff8b350df032429c07bc973a3b83c7f
修改仿真后台
15个文件已修改
2个文件已添加
1 文件已重命名
713 ■■■■ 已修改文件
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/async/PythonLib.java 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/SimulatAssessController.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/TaskController.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/dao/OperatConditModelDao.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/dao/TaskPhaseDao.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/dao/TaskPhaseModelDao.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/FailureModel.java 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/ReliaSimLibResult.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/RepairModel.java 27 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/SimulatAssessTaskPhaseModel.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/TaskPhase.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/TaskPhaseModel.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/service/ModelLineService.java 128 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/service/SimulatAssessService.java 457 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/service/TaskService.java 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/resources/mapper/taskReliability/OperatConditModelDao.xml 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/resources/mapper/taskReliability/TaskPhaseDao.xml 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/resources/mapper/taskReliability/TaskPhaseModelDao.xml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/async/PythonLib.java
@@ -2,14 +2,13 @@
import com.alibaba.fastjson.JSONObject;
import com.zt.common.servlet.Result;
import com.zt.life.modules.mainPart.taskReliability.model.PythonResult;
import com.zt.life.modules.mainPart.taskReliability.model.ReliaSimLibResult;
import com.zt.life.modules.mainPart.taskReliability.model.SimulatAssess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
@@ -66,7 +65,7 @@
//            if (exitCode == 0) {
                logger.info("启动python计算成功");
                if (line != null) {
                    PythonResult rtn = JSONObject.parseObject(line, PythonResult.class);
                    ReliaSimLibResult rtn = JSONObject.parseObject(line, ReliaSimLibResult.class);
                    if ("0".equals(rtn.getCode())) {
                        result = Result.ok();
                    } else {
@@ -115,7 +114,6 @@
    private void setParamToRedis(SimulatAssess simulatAssess) {
        String key = simulatAssess.getId().toString() + taskType;
        logger.info("redis key:" + key);
/*
        String xmlfile = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<des name=\"General system\">\n" +
                "    <tasks>\n" +
@@ -244,7 +242,7 @@
                "        </repair_model>\n" +
                "    </repair_models>\n" +
                "</des>";
*/
/*
        String xmlfile = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<des name=\"General system\">\n" +
                "    <tasks>\n" +
@@ -345,6 +343,7 @@
                "        </repair_model>\n" +
                "    </repair_models>\n" +
                "</des>";
*/
        JSONObject jsonObject = new JSONObject();
/*
        Long simulate_times = 10000L;
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/SimulatAssessController.java
@@ -113,7 +113,8 @@
    @PostMapping("analyze")
    public Result<Long> analyze(@RequestBody SimulatAssess simulatAssess) {
        //效验数据
        Result result = pythonLib.callPython(simulatAssess);
//        Result result = pythonLib.callPython(simulatAssess);
        Result result = simulatAssessService.simulate(simulatAssess);
        return result;
    }
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/TaskController.java
@@ -66,8 +66,9 @@
    @GetMapping("checkTaskModel")
    @ApiOperation("任务模型完整性检查")
    public Result<List<TaskModelCheckResult>> checkTaskModel(Long productId){
        List<TaskModelCheckResult> checkResult = taskService.checkTaskModel(productId);
        return Result.ok(checkResult);
//        List<TaskModelCheckResult> checkResult = taskService.checkTaskModel(productId);
//        return Result.ok(checkResult);
        return Result.ok();
    }
    @PostMapping
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/dao/OperatConditModelDao.java
@@ -30,4 +30,5 @@
    void deleteByOperatConditId(Long operatConditId);
    List<OperatConditModel> getGKModelTrees(Long productId);
    List<OperatConditModel> getGKModelAssembled(Long gkId);
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/dao/TaskPhaseDao.java
@@ -23,4 +23,5 @@
    Integer getMaxSort(Long taskId);
    List<Long> getPhaseByTask(Long taskId);
    List<TaskPhase> getListByTaskId(Long taskId);
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/dao/TaskPhaseModelDao.java
@@ -1,6 +1,7 @@
package com.zt.life.modules.mainPart.taskReliability.dao;
import com.zt.common.dao.BaseDao;
import com.zt.life.modules.mainPart.taskReliability.model.TaskPhase;
import com.zt.life.modules.mainPart.taskReliability.model.TaskPhaseModel;
import org.apache.ibatis.annotations.Mapper;
@@ -20,4 +21,5 @@
    List<TaskPhaseModel> getList(Map<String, Object> params);
    List<TaskPhaseModel> getListByPhaseId(Long phaseId);
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/FailureModel.java
New file
@@ -0,0 +1,27 @@
package com.zt.life.modules.mainPart.taskReliability.model;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class FailureModel {
    public static final String TYPE_EXP = "exponential";
    public static final String TYPE_BIN = "binomial";
    public static final String TYPE_WBL = "weibull";
    public static final String TYPE_FIX = "fixed";
    @ApiModelProperty(value = "ID")
    private Long id;
    @ApiModelProperty(value = "type")
    private String type;
    @ApiModelProperty(value = "参数1")
    private double param1;
    @ApiModelProperty(value = "参数2")
    private double param2;
    @ApiModelProperty(value = "参数3")
    private double param3;
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/ReliaSimLibResult.java
File was renamed from modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/PythonResult.java
@@ -4,7 +4,7 @@
import lombok.Data;
@Data
public class PythonResult {
public class ReliaSimLibResult {
    @ApiModelProperty(value = "code")
    private String code;
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/RepairModel.java
New file
@@ -0,0 +1,27 @@
package com.zt.life.modules.mainPart.taskReliability.model;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class RepairModel {
    public static final String TYPE_EXP = "exponential";
    public static final String TYPE_BIN = "binomial";
    public static final String TYPE_WBL = "weibull";
    public static final String TYPE_FIX = "fixed";
    @ApiModelProperty(value = "ID")
    private Long id;
    @ApiModelProperty(value = "type")
    private String type;
    @ApiModelProperty(value = "参数1")
    private double param1;
    @ApiModelProperty(value = "参数2")
    private double param2;
    @ApiModelProperty(value = "参数3")
    private double param3;
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/SimulatAssessTaskPhaseModel.java
@@ -45,6 +45,9 @@
    @ApiModelProperty(value = "阶段时长")
    private Double phaseDuration;
    @ApiModelProperty(value = "阶段约束(不可维修设备类型列表,用逗号隔开)")
    private String phaseConstraint;
    @ApiModelProperty(value = "工况模型ID")
    private Long gkId;
@@ -57,6 +60,9 @@
    @ApiModelProperty(value = "工况模型时长")
    private Double gkDuration;
    @ApiModelProperty(value = "工况模型排序")
    private Integer sort;
    @ApiModelProperty(value = "备注")
    private String remark;
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/TaskPhase.java
@@ -46,4 +46,8 @@
    @TableField(exist = false)
    private Double operatConditDuration;
    @ApiModelProperty(value = "阶段时长")
    @TableField(exist = false)
    private Double phaseDuration;
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/model/TaskPhaseModel.java
@@ -48,5 +48,9 @@
    @TableField(exist = false)
    private String isCheck;
    @ApiModelProperty(value = "工况运行时长")
    @TableField(exist = false)
    private double operatConditDuration;
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/service/ModelLineService.java
@@ -189,134 +189,6 @@
        modelLineList.addAll(linesToAdd);
    }
    private boolean createIfXmlFromRbd(ModelRbd modelRbd,
                                       List<Algorithm> algorithmList,
                                       List<ModelNode> modelNodeAndVnodeList) {
        boolean result = true;
        try {
            Document document = DocumentHelper.createDocument();
            // 添加root节点
            Element root = document.addElement("DES");
            root.addAttribute("Name", "A System");
            // 添加terminal节点到root1
            Element terminal = root.addElement("Node");
            terminal.addAttribute("Name", "Terminal");
            terminal.addAttribute("Type", "NODE");
            // 将模型转换为DOM,添加到root
            Algorithm endAlgo = algorithmList.stream().filter(item ->
                    "end".equals(item.getAlgorithmType())).collect(Collectors.toList()).get(0);
            ModelNode computerNode = modelNodeAndVnodeList.stream().filter(item ->
                    endAlgo.getComputerList().equals(item.getId().toString())).collect(Collectors.toList()).get(0);
            node2DOM(algorithmList, modelNodeAndVnodeList, computerNode, root);
            // 添加start节点到root
            Element start = root.addElement("Node");
            start.addAttribute("Name", "Start");
            start.addAttribute("Type", "NODE");
            // 添加link(路径)到root
            Element link = root.addElement("Link");
            Element block = link.addElement("Block");
            block.addAttribute("Name", "Terminal");
            block = link.addElement("Block");
            block.addAttribute("Name", computerNode.getId().toString());
            block = link.addElement("Block");
            block.addAttribute("Name", "Start");
//            document.setXMLEncoding("UTF-8");
//            String xmlString = document.asXML();
            // 输出格式化xml
            XMLWriter xmlWriter = null;
            try {
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setEncoding("UTF-8");
                StringWriter writer = new StringWriter();
                xmlWriter = new XMLWriter(writer, format);
                xmlWriter.write(document);
                modelRbd.setPublishedXml(writer.toString());
            } finally {
                if (xmlWriter!=null) xmlWriter.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        return result;
    }
    // 递归函数
    private void node2DOM(List<Algorithm> algorithmList,
                          List<ModelNode> modelNodeAndVnodeList,
                          ModelNode node,
                          Element parent) {
        if ("node".equals(node.getNodeType())) {
            Long dataId = node.getDataId();
            XhProductModel xhProductModel = xhProductModelDao.getById(dataId);
            if (xhProductModel == null) return;
            if ("5".equals(xhProductModel.getProductType())) {
                // 设备
                ParamData paramData = paramDataDao.getParamData(dataId, "expect");
                if (paramData == null) return;
                Element element = parent.addElement("Node");
                element.addAttribute("Name", dataId.toString());
                element.addAttribute("Type", "NODE");
                Element failureTag = element.addElement("Failure");
                failureTag.addAttribute("Dist", "EXP"); // TODO:需设为具体的分布
                Element argsTag = failureTag.addElement("Args");
                Double value = new Double(paramData.getTaskMtbcfRegulate());
                value = 1.0/value;
                argsTag.addAttribute("value", value.toString());
                if (1==paramData.getRepairable()) {
                    Element repairTag = element.addElement("Repair");
                    repairTag.addAttribute("Dist", "EXP"); // TODO:需设为具体的分布
                    argsTag = repairTag.addElement("Args");
                    value = new Double(paramData.getRepairMttcr());
                    value = 1.0/value;
                    argsTag.addAttribute("value", value.toString());
                    Element repairTimeLimitTag = element.addElement("RepairTimeLimit");
                    repairTimeLimitTag.setText(String.valueOf(paramData.getRepairMttcr()));
                }
            } else if ("10".equals(xhProductModel.getProductType())) {
                // 虚单位
                ModelRbd rbdXDY = modelRbdDao.getDiagramOfXDY(dataId);
                if (rbdXDY == null) return;
                List<Algorithm> algorithmListXDY = algorithmDao.getListByModelId(rbdXDY.getId());
                List<ModelNode> modelNodeAndVnodeListXDY =modelNodeAlgorithmDao.getListByModelId(rbdXDY.getId());
                Algorithm endAlgo = algorithmListXDY.stream().filter(item ->
                        "end".equals(item.getAlgorithmType())).collect(Collectors.toList()).get(0);
                ModelNode computerNode = modelNodeAndVnodeListXDY.stream().filter(item ->
                        endAlgo.getComputerList().equals(item.getId().toString())).collect(Collectors.toList()).get(0);
                node2DOM(algorithmListXDY, modelNodeAndVnodeListXDY, computerNode, parent);
            }
        } else {
            // vnode(运算节点)
            Algorithm algo = algorithmList.stream().filter(item ->
                    node.getId().equals(item.getId())).collect(Collectors.toList()).get(0);
            Element element = parent.addElement("Logic");
            element.addAttribute("Name", algo.getId().toString());
            if ("series".equals(algo.getAlgorithmType())) {
                element.addAttribute("Type", "SERIES");
            } else if ("parallel".equals(algo.getAlgorithmType())) {
                element.addAttribute("Type", "PARALLEL");
            } else if ("vote".equals(algo.getAlgorithmType())) {
                element.addAttribute("Type", "VOTE");
                element.addAttribute("VoteValue", algo.getVoteNum().toString());
            } else if ("switch".equals(algo.getAlgorithmType())) {
                element.addAttribute("Type", "STANDBY");
            } else if ("bridge".equals(algo.getAlgorithmType())) {
                element.addAttribute("Type", "BRIDGE");
            }
            String[] computerNodeListStr = algo.getComputerList().split(",");
            for (String nodeStr : computerNodeListStr) {
                ModelNode nd = modelNodeAndVnodeList.stream().filter(item ->
                        nodeStr.equals(item.getId().toString())).collect(Collectors.toList()).get(0);
                node2DOM(algorithmList, modelNodeAndVnodeList, nd, element);
            }
        }
    }
    private boolean getAlgorithmFromRbd(ModelRbd modelRbd,
                                        List<ModelNode> modelNodeList,
                                        List<ModelLine> modelLineList,
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/service/SimulatAssessService.java
@@ -1,13 +1,37 @@
package com.zt.life.modules.mainPart.taskReliability.service;
import com.alibaba.fastjson.JSONObject;
import com.zt.common.service.BaseService;
import com.zt.life.modules.mainPart.taskReliability.dao.SimulatAssessDao;
import com.zt.common.servlet.Result;
import com.zt.common.utils.UUIDUtil;
import com.zt.life.modules.mainPart.basicInfo.dao.ParamDataDao;
import com.zt.life.modules.mainPart.basicInfo.dao.XhProductModelDao;
import com.zt.life.modules.mainPart.basicInfo.model.ParamData;
import com.zt.life.modules.mainPart.basicInfo.model.XhProductModel;
import com.zt.life.modules.mainPart.taskReliability.dao.*;
import com.zt.life.modules.mainPart.taskReliability.dto.ProductStatusDto;
import com.zt.life.modules.mainPart.taskReliability.model.SimulatAssess;
import com.zt.life.modules.mainPart.taskReliability.dto.TaskModelCheckResult;
import com.zt.life.modules.mainPart.taskReliability.model.*;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
@@ -18,8 +42,40 @@
 */
@Service
public class SimulatAssessService extends BaseService<SimulatAssessDao, SimulatAssess> {
    private static final Logger logger = LoggerFactory.getLogger(SimulatAssessService.class);
    private static final String RELIA_SIM_TASK_TYPE_SIMULATION = "calcreq";
    @Value("${spring.redis.host}")
    private String redisHost;
    @Value("${spring.redis.port}")
    private String redisPort;
    @Value("${data.reliaSimLib.mainPy}")
    private String reliaSimMain;
    @Value("${data.reliaSimLib.resultHome}")
    private String resultHome;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskPhaseService taskPhaseService;
    @Autowired
    private SimulatAssessTaskPhaseModelService simulatAssessTaskPhaseModelService;
    @Autowired
    private TaskPhaseDao taskPhaseDao;
    @Autowired
    private TaskPhaseModelDao taskPhaseModelDao;
    @Autowired
    private OperatConditModelDao operatConditModelDao;
    @Autowired
    private ModelNodeAlgorithmDao modelNodeAlgorithmDao;
    @Autowired
    private AlgorithmDao algorithmDao;
    @Autowired
    private XhProductModelDao xhProductModelDao;
    @Autowired
    private ParamDataDao paramDataDao;
    /**
     * 分页查询
@@ -55,4 +111,401 @@
    public List<ProductStatusDto> getChildren(Long productId, Long taskId) {
        return baseDao.getChildren(productId, taskId);
    }
    @Transactional(rollbackFor = Exception.class)
    public Result simulate(SimulatAssess simulatAssess) {
        Result result = null;
        // 1. 检查模型完整性
        List<TaskModelCheckResult> chkResult = taskService.checkTaskModel(simulatAssess.getProductId(),
                simulatAssess.getTaskModelId());
        if (chkResult.size() > 0) {
            result = Result.error("模型不完整,请检查模型定义及参数配置。");
            return result;
        }
        // 2. 组装供算法库仿真的模型xml
        assembleModelXml(simulatAssess);
        // 3. 调用算法库,进行仿真计算
        result = callReliaSimLib(simulatAssess);
        return result;
    }
    private Result callReliaSimLib(SimulatAssess simulatAssess) {
        Result result = null;
        InputStream is = null;
        BufferedReader br = null;
        try {
            setParamToRedis(simulatAssess);
            Process process = null;
            String command = "python " + reliaSimMain;
            command += " -ip " + redisHost + " -port " + redisPort;
            command += " -taskType " + RELIA_SIM_TASK_TYPE_SIMULATION + " -taskId " + simulatAssess.getId().toString();
            logger.info("cmd命令为:" + command);
            if(System.getProperty("os.name").toLowerCase().indexOf("windows") > -1){
                process = Runtime.getRuntime().exec(new String[]{"cmd", "/c", command});
            }else if(System.getProperty("os.name").toLowerCase().indexOf("linux") > -1){
                process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
            }else{
                throw new Exception("暂不支持该操作系统,进行启动算法库计算!");
            }
            is = process.getInputStream();
            // 以命令行方式调用算法库时,接口约定返回的结果是utf-8编码
            br = new BufferedReader(new InputStreamReader(is,"utf-8"));
            String line = br.readLine();
            logger.info("算法库返回结果:" + line);
//            int exitCode = process.waitFor(); // 异步方式,不等待算法库计算完毕,所以要注释掉该行
            if (line != null) {
                ReliaSimLibResult rtn = JSONObject.parseObject(line, ReliaSimLibResult.class);
                if ("0".equals(rtn.getCode())) {
                    logger.info("启动算法库成功。");
                    result = Result.ok();
                } else {
                    String errorMsg = rtn.getErrorMsg();
                    throw new RuntimeException("启动算法库失败: errorMsg=" + errorMsg);
                }
            }
        } catch (Exception e) {
            logger.error("启动算法库仿真时发生Exception:", e);
            e.printStackTrace();
            result = Result.error(e.getMessage());
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    private void setParamToRedis(SimulatAssess simulatAssess) {
        String key = simulatAssess.getId().toString() + RELIA_SIM_TASK_TYPE_SIMULATION;
        logger.info("redis key:" + key);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("xmlfile", simulatAssess.getXml());
        // 仿真次数
        jsonObject.put("simulate_times", simulatAssess.getSimulatFrequency());
        // 采样周期
        jsonObject.put("timeslice", simulatAssess.getSamplPeriod()/60.0);   // 单位统一换算为小时
        jsonObject.put("result_home", resultHome);
        jsonObject.put("seed", 1000);
        redisTemplate.opsForValue().set(key, jsonObject.toJSONString());
    }
    private void assembleModelXml(SimulatAssess simulatAssess) {
        Long productId = simulatAssess.getProductId();
        List<XhProductModel> productList = xhProductModelDao.getByShipId(productId);
        List<ParamData> paramDataList = paramDataDao.getDeviceParams(productId);
        Long taskId = simulatAssess.getTaskModelId();
        Task task = taskService.get(taskId);
        // 1. 计算各任务阶段的运行时长
        List<TaskPhase> taskPhases = calcTaskPhaseDuration(task);
        // 2. 计算各工况模型的运行时长
        List<TaskPhaseModel> taskPhaseModelAll = new ArrayList<>();
        for (TaskPhase taskPhase : taskPhases) {
            calcTaskPhaseModelDuration(taskPhase, taskPhaseModelAll);
        }
        // 3. 将各工况模型递归拆解为完整的可供算法包仿真计算的模型
        try {
            List<FailureModel> failureModels = new ArrayList<>();
            List<RepairModel> repairModels = new ArrayList<>();
            Document document = DocumentHelper.createDocument();
            // 添加root节点
            Element root = document.addElement("des");
            root.addAttribute("name", "General system");
            addTasksTag(taskPhaseModelAll, root);
            addModelsTag(productId,
                    productList,
                    paramDataList,
                    taskPhaseModelAll,
                    root,
                    failureModels, repairModels);
            addFailureModelsTag(failureModels, root);
            addRepairModelsTag(repairModels, root);
            saveSimulatAssessTaskPhaseModel(simulatAssess,
                    task,
                    taskPhases,
                    taskPhaseModelAll);
            // 输出格式化xml
            XMLWriter xmlWriter = null;
            try {
                OutputFormat format = OutputFormat.createPrettyPrint();
                format.setEncoding("UTF-8");
                StringWriter writer = new StringWriter();
                xmlWriter = new XMLWriter(writer, format);
                xmlWriter.write(document);
                simulatAssess.setXml(writer.toString());
            } finally {
                if (xmlWriter != null) xmlWriter.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("组装算法库仿真计算用模型XML失败: " + e.getMessage());
        }
    }
    private void saveSimulatAssessTaskPhaseModel(SimulatAssess simulatAssess,
                                                 Task task,
                                                 List<TaskPhase> taskPhases,
                                                 List<TaskPhaseModel> taskPhaseModelAll) {
        int sort = 1;
        for (TaskPhaseModel tpm : taskPhaseModelAll) {
            TaskPhase phase = taskPhases.stream().filter(item ->
                    tpm.getPhaseId().equals(item.getId())).collect(Collectors.toList()).get(0);
            SimulatAssessTaskPhaseModel satpm = new SimulatAssessTaskPhaseModel();
            satpm.setId(UUIDUtil.generateId());
            satpm.setFzId(simulatAssess.getId());
            satpm.setTaskId(task.getId());
            satpm.setTaskName(task.getTaskName());
            satpm.setTaskDuration(task.getTaskDuration());
            satpm.setPhaseId(tpm.getPhaseId());
            satpm.setPhaseName(phase.getPhaseName());
            satpm.setPhaseDurationRate(phase.getPhaseDurationRate());
            satpm.setPhaseDuration(phase.getPhaseDuration());
            satpm.setPhaseConstraint(null); // TODO
            satpm.setGkId(tpm.getOperatConditId());
            satpm.setGkName(tpm.getOperatConditName());
            satpm.setGkDurationRate(tpm.getOperatConditDurationRate());
            satpm.setGkDuration(tpm.getOperatConditDuration());
            satpm.setSort(sort++);
            simulatAssessTaskPhaseModelService.insert(satpm);
        }
    }
    private void addTasksTag(List<TaskPhaseModel> taskPhaseModelAll,
                             Element root) {
        Element tasksTag = root.addElement("tasks");
        for (TaskPhaseModel taskPhaseModel : taskPhaseModelAll) {
            Element taskTag = tasksTag.addElement("task");
            taskTag.addAttribute("duration", String.valueOf(taskPhaseModel.getOperatConditDuration()));
            taskTag.addAttribute("model", taskPhaseModel.getOperatConditId().toString());
        }
    }
    private void addFailureModelsTag(List<FailureModel> failureModels,
                                     Element root) {
        Element ele = null;
        Element failureModelsTag = root.addElement("failure_models");
        for (FailureModel failureModel : failureModels) {
            Element failureModelTag = failureModelsTag.addElement("failure_model");
            failureModelTag.addAttribute("name", failureModel.getId().toString());
            failureModelTag.addAttribute("type", failureModel.getType());
            switch (failureModel.getType()) {
                case FailureModel.TYPE_EXP:
                    ele = failureModelTag.addElement("failure-rate");
                    ele.addAttribute("value", String.valueOf(failureModel.getParam1()));
                    break;
                case FailureModel.TYPE_BIN:
                    ele = failureModelTag.addElement("reliability");
                    ele.addAttribute("value", String.valueOf(failureModel.getParam1()));
                    ele = failureModelTag.addElement("n");
                    ele.addAttribute("value", String.valueOf(failureModel.getParam2()));
                    ele = failureModelTag.addElement("k");
                    ele.addAttribute("value", String.valueOf(failureModel.getParam3()));
                    break;
                case FailureModel.TYPE_WBL:
                    ele = failureModelTag.addElement("scale");
                    ele.addAttribute("value", String.valueOf(failureModel.getParam2()));
                    ele = failureModelTag.addElement("shape");
                    ele.addAttribute("value", String.valueOf(failureModel.getParam3()));
                    break;
                case FailureModel.TYPE_FIX:
                    ele = failureModelTag.addElement("unreliability");
                    ele.addAttribute("value", String.valueOf(failureModel.getParam1()));
                    break;
                default:
                    break;
            }
        }
    }
    private void addRepairModelsTag(List<RepairModel> repairModels,
                                     Element root) {
        Element ele = null;
        Element repairModelsTag = root.addElement("repair_models");
        for (RepairModel repairModel : repairModels) {
            Element repairModelTag = repairModelsTag.addElement("repair_model");
            repairModelTag.addAttribute("name", repairModel.getId().toString());
            repairModelTag.addAttribute("type", repairModel.getType());
            switch (repairModel.getType()) {
                case RepairModel.TYPE_EXP:
                    ele = repairModelTag.addElement("repair-rate");
                    ele.addAttribute("value", String.valueOf(repairModel.getParam1()));
                    break;
                case RepairModel.TYPE_WBL:
                    ele = repairModelTag.addElement("scale");
                    ele.addAttribute("value", String.valueOf(repairModel.getParam2()));
                    ele = repairModelTag.addElement("shape");
                    ele.addAttribute("value", String.valueOf(repairModel.getParam3()));
                    break;
                default:
                    break;
            }
        }
    }
    private void calcTaskPhaseModelDuration(TaskPhase taskPhase,
                                                            List<TaskPhaseModel> taskPhaseModelAll) {
        List<TaskPhaseModel> taskPhaseModels = taskPhaseModelDao.getListByPhaseId(taskPhase.getId());
        double totalRate = taskPhaseModels.stream().mapToDouble(TaskPhaseModel::getOperatConditDurationRate).sum();
        double sum = 0.0;
        for (TaskPhaseModel taskPhaseModel : taskPhaseModels) {
            double duration = taskPhaseModel.getOperatConditDurationRate() * taskPhase.getPhaseDuration() / totalRate;
            taskPhaseModel.setOperatConditDuration(duration);
            sum += duration;
            taskPhaseModelAll.add(taskPhaseModel);
        }
        // 把零头补到最后一个工况模型
        taskPhaseModels.get(taskPhaseModels.size()-1).setOperatConditDuration(totalRate - sum);
    }
    private List<TaskPhase> calcTaskPhaseDuration(Task task) {
        List<TaskPhase> taskPhases = taskPhaseDao.getListByTaskId(task.getId());
        double totalRate = taskPhases.stream().mapToDouble(TaskPhase::getPhaseDurationRate).sum();
        double sum = 0.0;
        for (TaskPhase taskPhase : taskPhases) {
            double duration = task.getTaskDuration() * taskPhase.getPhaseDurationRate() / totalRate;
            taskPhase.setPhaseDuration(duration);
            sum += duration;
        }
        // 把零头补到最后一个阶段
        taskPhases.get(taskPhases.size()-1).setPhaseDuration(task.getTaskDuration() - sum);
        return taskPhases;
    }
    private void addModelsTag(Long productId,
                              List<XhProductModel> productList,
                              List<ParamData> paramDataList,
                              List<TaskPhaseModel> taskPhaseModelAll,
                              Element root,
                              List<FailureModel> failureModels,
                              List<RepairModel> repairModels) {
        Element modelsTag = root.addElement("models");
        for (TaskPhaseModel taskPhaseModel : taskPhaseModelAll) {
            Element modelTag = modelsTag.addElement("model");
            modelTag.addAttribute("name", taskPhaseModel.getOperatConditId().toString());
            List<OperatConditModel> gkModelsAssembled = operatConditModelDao.getGKModelAssembled(
                    taskPhaseModel.getOperatConditId());
            OperatConditModel gkModelTop = gkModelsAssembled.stream().filter(item ->
                    productId.equals(item.getProductId())).collect(Collectors.toList()).get(0);
            List<ModelNode> modelNodeAndVnodeList = modelNodeAlgorithmDao.getListByModelId(gkModelTop.getModelId());
            List<Algorithm> algorithmList = algorithmDao.getListByModelId(gkModelTop.getModelId());
            // 将模型转换为DOM,添加到model标签
            Algorithm endAlgo = algorithmList.stream().filter(item ->
                    "end".equals(item.getAlgorithmType())).collect(Collectors.toList()).get(0);
            ModelNode computerNode = modelNodeAndVnodeList.stream().filter(item ->
                    endAlgo.getComputerList().equals(item.getId().toString())).collect(Collectors.toList()).get(0);
            node2DOM(productList,
                    paramDataList,
                    gkModelsAssembled,
                    algorithmList,
                    modelNodeAndVnodeList,
                    computerNode, modelTag,
                    failureModels, repairModels);
        }
    }
    // 递归函数
    private void node2DOM(List<XhProductModel> productList,
                          List<ParamData> paramDataList,
                          List<OperatConditModel> gkModelsAssembled,
                          List<Algorithm> algorithmList,
                          List<ModelNode> modelNodeAndVnodeList,
                          ModelNode node,
                          Element parent,
                          List<FailureModel> failureModels,
                          List<RepairModel> repairModels) {
        if ("node".equals(node.getNodeType())) {
            Long dataId = node.getDataId();
            XhProductModel product = productList.stream().filter(item ->
                    dataId.equals(item.getId())).collect(Collectors.toList()).get(0);
            if ("3,4,10".equals(product.getProductType())) {
                // 系统、分系统、虚单元
                OperatConditModel gkModel = gkModelsAssembled.stream().filter(item ->
                        dataId.equals(item.getProductId())).collect(Collectors.toList()).get(0);
                List<Algorithm> algorithmListSub = algorithmDao.getListByModelId(gkModel.getModelId());
                List<ModelNode> modelNodeAndVnodeListSub = modelNodeAlgorithmDao.getListByModelId(gkModel.getModelId());
                Algorithm endAlgo = algorithmListSub.stream().filter(item ->
                        "end".equals(item.getAlgorithmType())).collect(Collectors.toList()).get(0);
                ModelNode computerNode = modelNodeAndVnodeListSub.stream().filter(item ->
                        endAlgo.getComputerList().equals(item.getId().toString())).collect(Collectors.toList()).get(0);
                node2DOM(productList,
                        paramDataList,
                        gkModelsAssembled,
                        algorithmListSub,
                        modelNodeAndVnodeListSub,
                        computerNode, parent,
                        failureModels, repairModels);
            } else if ("5".equals(product.getProductType())) {
                // 设备
                ParamData paramData = paramDataList.stream().filter(item ->
                        dataId.equals(item.getProductId())).collect(Collectors.toList()).get(0);
                Element nodeTag = parent.addElement("node");
                nodeTag.addAttribute("name", dataId.toString());
                nodeTag.addAttribute("type", "node");
                FailureModel failureModel = new FailureModel();
                failureModel.setId(UUIDUtil.generateId());
                failureModel.setType(FailureModel.TYPE_EXP);
                failureModel.setParam1(1.0 / paramData.getTaskMtbcfRegulate());
                failureModels.add(failureModel);
                Element failureTag = nodeTag.addElement("failure_model");
                failureTag.addAttribute("name", failureModel.getId().toString());
                if (1 == paramData.getRepairable()) {
                    RepairModel repairModel = new RepairModel();
                    repairModel.setId(UUIDUtil.generateId());
                    repairModel.setType(RepairModel.TYPE_EXP);
                    repairModel.setParam1(1.0 / paramData.getRepairMttcr());
                    repairModels.add(repairModel);
                    Element repairTag = nodeTag.addElement("repair_model");
                    repairTag.addAttribute("name", repairModel.getId().toString());
                }
            }
        } else {
            // vnode(运算节点)
            Algorithm algo = algorithmList.stream().filter(item ->
                    node.getId().equals(item.getId())).collect(Collectors.toList()).get(0);
            Element element = parent.addElement("logic");
            element.addAttribute("name", algo.getId().toString());
            if ("series".equals(algo.getAlgorithmType())) {
                element.addAttribute("type", "series");
            } else if ("parallel".equals(algo.getAlgorithmType())) {
                element.addAttribute("type", "parallel");
            } else if ("vote".equals(algo.getAlgorithmType())) {
                element.addAttribute("type", "vote");
                element.addAttribute("k", algo.getVoteNum().toString());
            } else if ("switch".equals(algo.getAlgorithmType())) {
                element.addAttribute("type", "standby");
            } else if ("bridge".equals(algo.getAlgorithmType())) {
                element.addAttribute("type", "bridge");
            }
            String[] computerNodeListStr = algo.getComputerList().split(",");
            for (String nodeStr : computerNodeListStr) {
                ModelNode md = modelNodeAndVnodeList.stream().filter(item ->
                        nodeStr.equals(item.getId().toString())).collect(Collectors.toList()).get(0);
                node2DOM(productList,
                        paramDataList,
                        gkModelsAssembled,
                        algorithmList,
                        modelNodeAndVnodeList,
                        md, element,
                        failureModels, repairModels);
            }
        }
    }
}
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/service/TaskService.java
@@ -76,13 +76,7 @@
     * @param productId 产品节点ID(一般为总体)
     * @return 检查出的错误的List,长度为0表示没有错误,不会返回null
     */
    public List<TaskModelCheckResult> checkTaskModel(Long productId) {
        List<TaskModelCheckResult> result = new ArrayList<>();
        result = doCheckTaskModel(productId, null);
        return result;
    }
    private List<TaskModelCheckResult> doCheckTaskModel(Long productId, Long taskModelId) {
    public List<TaskModelCheckResult> checkTaskModel(Long productId, Long taskModelId) {
        List<TaskModelCheckResult> checkResult = new ArrayList<>();
        List<XhProductModel> deviceUsedList = new ArrayList<>();    // 检查对象模型中使用到的所有设备
modules/mainPart/src/main/resources/mapper/taskReliability/OperatConditModelDao.xml
@@ -63,8 +63,18 @@
        a.is_delete = 0
        and b.is_delete = 0
        <if test="productId!=null">
            and b.product_id=${productId}
            and b.product_id=#{productId}
        </if>
        ORDER BY a.operat_condit_id, a.product_id
    </select>
    <select id="getGKModelAssembled" resultType="com.zt.life.modules.mainPart.taskReliability.model.OperatConditModel">
        select
            a.*
        FROM
            operat_condit_model a
        where
            a.is_delete = 0
        and a.is_disabled = 0
        and a.operat_condit_id=#{gkId}
    </select>
</mapper>
modules/mainPart/src/main/resources/mapper/taskReliability/TaskPhaseDao.xml
@@ -30,5 +30,14 @@
          and task_id =${taskId}
        order by phase_sort
    </select>
    <select id="getListByTaskId" resultType="com.zt.life.modules.mainPart.taskReliability.model.TaskPhase">
        SELECT
            a.*
        FROM
            task_phase a
        where a.is_delete = 0
          and a.task_id=#{taskId}
        ORDER BY a.phase_sort
    </select>
</mapper>
modules/mainPart/src/main/resources/mapper/taskReliability/TaskPhaseModelDao.xml
@@ -18,5 +18,13 @@
          and a.product_id = ${productId}
        order by a.CREATE_DATE
    </select>
    <select id="getListByPhaseId" resultType="com.zt.life.modules.mainPart.taskReliability.model.TaskPhaseModel">
        SELECT
            a.*
        FROM
            task_phase_model a
        where a.is_delete = 0
          and a.phase_id=#{phaseId}
    </select>
</mapper>