|  | @@ -0,0 +1,262 @@
 | 
	
		
			
				|  |  | +package com.miniframe.modo.temp;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.miniframe.core.ext.UtilTools;
 | 
	
		
			
				|  |  | +import com.miniframe.mdo.service.LogService;
 | 
	
		
			
				|  |  | +import com.miniframe.model.mdo.*;
 | 
	
		
			
				|  |  | +import com.miniframe.model.mdo.dao.*;
 | 
	
		
			
				|  |  | +import com.miniframe.modo.temp.problem.*;
 | 
	
		
			
				|  |  | +import freemarker.template.Configuration;
 | 
	
		
			
				|  |  | +import freemarker.template.Template;
 | 
	
		
			
				|  |  | +import freemarker.template.TemplateException;
 | 
	
		
			
				|  |  | +import tk.mybatis.mapper.util.StringUtil;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.io.FileWriter;
 | 
	
		
			
				|  |  | +import java.io.IOException;
 | 
	
		
			
				|  |  | +import java.io.StringWriter;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +public class TemplateGenerator {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 创建 执行文件
 | 
	
		
			
				|  |  | +     * @param pid
 | 
	
		
			
				|  |  | +     * @throws IOException
 | 
	
		
			
				|  |  | +     * @throws TemplateException
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static void createRunpy(String pid) throws IOException, TemplateException {
 | 
	
		
			
				|  |  | +        Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
 | 
	
		
			
				|  |  | +        // 设置模板所在目录
 | 
	
		
			
				|  |  | +        cfg.setClassForTemplateLoading(TemplateGenerator.class, "/templates");
 | 
	
		
			
				|  |  | +        // 获取模板对象
 | 
	
		
			
				|  |  | +        Template template = cfg.getTemplate("run.ftl");
 | 
	
		
			
				|  |  | +        // 定义数据模型(Map)
 | 
	
		
			
				|  |  | +        Map<String, Object> dataModel = new HashMap<>();
 | 
	
		
			
				|  |  | +        dataModel.put("problemPath",LogService.BPATH + "/" + pid + "/in/problem.xml");
 | 
	
		
			
				|  |  | +        // 将数据模型传入模板进行处理
 | 
	
		
			
				|  |  | +        StringWriter writer = new StringWriter();
 | 
	
		
			
				|  |  | +        template.process(dataModel, writer);
 | 
	
		
			
				|  |  | +        FileWriter fileWriter = new FileWriter(LogService.BPATH + "/" + pid + "/in/run.py");
 | 
	
		
			
				|  |  | +        fileWriter.write(writer.toString());
 | 
	
		
			
				|  |  | +        fileWriter.close();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 创建problem.xml
 | 
	
		
			
				|  |  | +     * @param pid
 | 
	
		
			
				|  |  | +     * @throws IOException
 | 
	
		
			
				|  |  | +     * @throws TemplateException
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static void createProblemXml(String pid) throws IOException, TemplateException {
 | 
	
		
			
				|  |  | +        //Problem.xml 写入
 | 
	
		
			
				|  |  | +        Configuration cfg = new Configuration(Configuration.VERSION_2_3_0);
 | 
	
		
			
				|  |  | +        // 设置模板所在目录
 | 
	
		
			
				|  |  | +        cfg.setClassForTemplateLoading(TemplateGenerator.class, "/templates");
 | 
	
		
			
				|  |  | +        // 获取模板对象
 | 
	
		
			
				|  |  | +        Template template = cfg.getTemplate("problem.ftl");
 | 
	
		
			
				|  |  | +        // 定义数据模型(Map)
 | 
	
		
			
				|  |  | +        Map<String, Object> dataModel = new HashMap<>();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        ProblemVo vo = new ProblemVo();
 | 
	
		
			
				|  |  | +        HeaderVo header =new HeaderVo();
 | 
	
		
			
				|  |  | +        header.setName(pid);
 | 
	
		
			
				|  |  | +        header.setTimestamp(new Date());
 | 
	
		
			
				|  |  | +        vo.setHeader(header);
 | 
	
		
			
				|  |  | +        List<DesignVariable> designVariables=findDesignVariables(pid);
 | 
	
		
			
				|  |  | +        vo.setDesignVariables(designVariables);
 | 
	
		
			
				|  |  | +        List<Objective> objectives = findObjectives(pid);
 | 
	
		
			
				|  |  | +        vo.setObjectives(objectives);
 | 
	
		
			
				|  |  | +        List<Constraint> constraints = findConstraints(pid);
 | 
	
		
			
				|  |  | +        vo.setConstraints(constraints);
 | 
	
		
			
				|  |  | +        MdoProEvolution evolution = findEvolution(pid);
 | 
	
		
			
				|  |  | +        vo.setEvolution(evolution);
 | 
	
		
			
				|  |  | +        MdoProSurro surro = findSurro(pid);
 | 
	
		
			
				|  |  | +        vo.setSurro(surro);
 | 
	
		
			
				|  |  | +        MdoProGrad grad = findGrad(pid);
 | 
	
		
			
				|  |  | +        vo.setGrad(grad);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        List<Solver> solvers = findSolvers(pid);
 | 
	
		
			
				|  |  | +        vo.setSolvers(solvers);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        dataModel.put("vo",vo);
 | 
	
		
			
				|  |  | +//        dataModel.put("geoFilePath", BPATH + "/" + aid + "/" + jid + "/Geometry");
 | 
	
		
			
				|  |  | +        // 将数据模型传入模板进行处理
 | 
	
		
			
				|  |  | +        StringWriter writer = new StringWriter();
 | 
	
		
			
				|  |  | +        template.process(dataModel, writer);
 | 
	
		
			
				|  |  | +        FileWriter fileWriter = new FileWriter(LogService.BPATH + "/" + pid + "/in/problem.xml");
 | 
	
		
			
				|  |  | +        fileWriter.write(writer.toString());
 | 
	
		
			
				|  |  | +        fileWriter.close();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 获取 求解器配置
 | 
	
		
			
				|  |  | +     * @param pid
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     * @throws IOException
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static List<Solver> findSolvers(String pid) throws IOException {
 | 
	
		
			
				|  |  | +        //TODO 目前只有函数求解器
 | 
	
		
			
				|  |  | +        List<Solver> solvers =new ArrayList<>();
 | 
	
		
			
				|  |  | +        //ptyon py.py 文件 写入
 | 
	
		
			
				|  |  | +        String ppPath=writePython(pid);
 | 
	
		
			
				|  |  | +        if(!StringUtil.isEmpty(ppPath)){
 | 
	
		
			
				|  |  | +            Solver solver =new Solver();
 | 
	
		
			
				|  |  | +            solver.setPythonPath(ppPath);
 | 
	
		
			
				|  |  | +            solvers.add(solver);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return solvers;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static MdoProGrad findGrad(String pid) {
 | 
	
		
			
				|  |  | +        MdoProGrad grad = null;
 | 
	
		
			
				|  |  | +        MdoProGradMapper edao = UtilTools.getBean(MdoProGradMapper.class);
 | 
	
		
			
				|  |  | +        MdoProGradSQLBuilder esb =new MdoProGradSQLBuilder();
 | 
	
		
			
				|  |  | +        MdoProGradSQLBuilder.Criteria esc = esb.createCriteria();
 | 
	
		
			
				|  |  | +        esc.andPidEqualTo(pid);
 | 
	
		
			
				|  |  | +        esc.andCheckedEqualTo((short) 1);
 | 
	
		
			
				|  |  | +        List<MdoProGrad> eList = edao.selectByExample(esb);
 | 
	
		
			
				|  |  | +        if(eList==null || eList.isEmpty()){
 | 
	
		
			
				|  |  | +            grad=null;
 | 
	
		
			
				|  |  | +        }else{
 | 
	
		
			
				|  |  | +            grad = eList.get(0);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return grad;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static MdoProSurro findSurro(String pid) {
 | 
	
		
			
				|  |  | +        MdoProSurro surro = null;
 | 
	
		
			
				|  |  | +        MdoProSurroMapper edao = UtilTools.getBean(MdoProSurroMapper.class);
 | 
	
		
			
				|  |  | +        MdoProSurroSQLBuilder esb =new MdoProSurroSQLBuilder();
 | 
	
		
			
				|  |  | +        MdoProSurroSQLBuilder.Criteria esc = esb.createCriteria();
 | 
	
		
			
				|  |  | +        esc.andPidEqualTo(pid);
 | 
	
		
			
				|  |  | +        esc.andCheckedEqualTo((short) 1);
 | 
	
		
			
				|  |  | +        List<MdoProSurro> eList = edao.selectByExample(esb);
 | 
	
		
			
				|  |  | +        if(eList==null || eList.isEmpty()){
 | 
	
		
			
				|  |  | +            surro=null;
 | 
	
		
			
				|  |  | +        }else{
 | 
	
		
			
				|  |  | +            surro = eList.get(0);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return surro;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static MdoProEvolution findEvolution(String pid) {
 | 
	
		
			
				|  |  | +        MdoProEvolution evolution = null;
 | 
	
		
			
				|  |  | +        MdoProEvolutionMapper edao = UtilTools.getBean(MdoProEvolutionMapper.class);
 | 
	
		
			
				|  |  | +        MdoProEvolutionSQLBuilder esb =new MdoProEvolutionSQLBuilder();
 | 
	
		
			
				|  |  | +        MdoProEvolutionSQLBuilder.Criteria esc = esb.createCriteria();
 | 
	
		
			
				|  |  | +        esc.andPidEqualTo(pid);
 | 
	
		
			
				|  |  | +        esc.andCheckedEqualTo((short) 1);
 | 
	
		
			
				|  |  | +        List<MdoProEvolution> eList = edao.selectByExample(esb);
 | 
	
		
			
				|  |  | +        if(eList==null || eList.isEmpty()){
 | 
	
		
			
				|  |  | +            evolution=null;
 | 
	
		
			
				|  |  | +        }else{
 | 
	
		
			
				|  |  | +            evolution = eList.get(0);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return evolution;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static List<Constraint> findConstraints(String pid) {
 | 
	
		
			
				|  |  | +        List<Constraint> constraints = new ArrayList<>();
 | 
	
		
			
				|  |  | +        MdoProjectConstraintMapper cdao = UtilTools.getBean(MdoProjectConstraintMapper.class);
 | 
	
		
			
				|  |  | +        MdoProjectConstraintSQLBuilder csb = new MdoProjectConstraintSQLBuilder();
 | 
	
		
			
				|  |  | +        csb.createCriteria().andPidEqualTo(pid);
 | 
	
		
			
				|  |  | +        List<MdoProjectConstraint> cList = cdao.selectByExample(csb);
 | 
	
		
			
				|  |  | +        for (MdoProjectConstraint c:cList) {
 | 
	
		
			
				|  |  | +            Constraint vc =new Constraint();
 | 
	
		
			
				|  |  | +            vc.setuID(c.getId());
 | 
	
		
			
				|  |  | +            vc.setName(c.getName());
 | 
	
		
			
				|  |  | +            vc.setValue(c.getReference());
 | 
	
		
			
				|  |  | +            vc.setLower(c.getLower());
 | 
	
		
			
				|  |  | +            vc.setUpper(c.getUpper());
 | 
	
		
			
				|  |  | +            constraints.add(vc);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return constraints;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static List<Objective> findObjectives(String pid) {
 | 
	
		
			
				|  |  | +        List<Objective> objectives = new ArrayList<>();
 | 
	
		
			
				|  |  | +        MdoProjectObjfunMapper objdao = UtilTools.getBean(MdoProjectObjfunMapper.class);
 | 
	
		
			
				|  |  | +        MdoProjectObjfunSQLBuilder objsb = new MdoProjectObjfunSQLBuilder();
 | 
	
		
			
				|  |  | +        objsb.createCriteria().andPidEqualTo(pid);
 | 
	
		
			
				|  |  | +        List<MdoProjectObjfun> objList =objdao.selectByExample(objsb);
 | 
	
		
			
				|  |  | +        for (MdoProjectObjfun obj:objList) {
 | 
	
		
			
				|  |  | +            Objective  vObj =new Objective();
 | 
	
		
			
				|  |  | +            vObj.setuID(obj.getId());
 | 
	
		
			
				|  |  | +            vObj.setName(obj.getName());
 | 
	
		
			
				|  |  | +            vObj.setValue(obj.getReference());
 | 
	
		
			
				|  |  | +            vObj.setWeight(obj.getWeight());
 | 
	
		
			
				|  |  | +            if("-1".equals(obj.getOptdir())){ //-1 最大化 1 最小化
 | 
	
		
			
				|  |  | +                vObj.setFlag("max");
 | 
	
		
			
				|  |  | +            }else{
 | 
	
		
			
				|  |  | +                vObj.setFlag("min");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            objectives.add(vObj);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return objectives;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 查询设计变量
 | 
	
		
			
				|  |  | +     * @param pid
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static  List<DesignVariable> findDesignVariables(String pid) {
 | 
	
		
			
				|  |  | +        List<DesignVariable> designVariables =new ArrayList<>();
 | 
	
		
			
				|  |  | +        MdoProjectVariateMapper vDao =UtilTools.getBean(MdoProjectVariateMapper.class);
 | 
	
		
			
				|  |  | +        MdoProjectVariateSQLBuilder vsb = new MdoProjectVariateSQLBuilder();
 | 
	
		
			
				|  |  | +        vsb.createCriteria().andPidEqualTo(pid);
 | 
	
		
			
				|  |  | +        List<MdoProjectVariate> vList =vDao.selectByExample(vsb);
 | 
	
		
			
				|  |  | +        Map<String,List<MdoProjectVariate>> vMap=new HashMap<>();
 | 
	
		
			
				|  |  | +        for (MdoProjectVariate v:vList) {
 | 
	
		
			
				|  |  | +            String vName =v.getName();
 | 
	
		
			
				|  |  | +            if(vMap.get(vName)==null||vMap.get(vName).isEmpty()){
 | 
	
		
			
				|  |  | +                vMap.put(vName,new ArrayList<>());
 | 
	
		
			
				|  |  | +                vMap.get(vName).add(v);
 | 
	
		
			
				|  |  | +            }else{
 | 
	
		
			
				|  |  | +                vMap.get(vName).add(v);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Set<String> vkeys =vMap.keySet();
 | 
	
		
			
				|  |  | +        for (String key : vkeys) {
 | 
	
		
			
				|  |  | +            DesignVariable  dv = new DesignVariable();
 | 
	
		
			
				|  |  | +            dv.setName(key);
 | 
	
		
			
				|  |  | +            List<MdoProjectVariate> tVlist =vMap.get(key);
 | 
	
		
			
				|  |  | +            List<String> values = new ArrayList<>();
 | 
	
		
			
				|  |  | +            List<String> lowerbounds = new ArrayList<>();
 | 
	
		
			
				|  |  | +            List<String> upperbounds = new ArrayList<>();
 | 
	
		
			
				|  |  | +            for (MdoProjectVariate tv:tVlist) {
 | 
	
		
			
				|  |  | +                values.add(tv.getReference());
 | 
	
		
			
				|  |  | +                lowerbounds.add(tv.getLower());
 | 
	
		
			
				|  |  | +                upperbounds.add(tv.getUpper());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            dv.setValue(values);
 | 
	
		
			
				|  |  | +            dv.setLowerbound(lowerbounds);
 | 
	
		
			
				|  |  | +            dv.setUpperbound(upperbounds);
 | 
	
		
			
				|  |  | +            dv.setuID("");//TODO 目前结构不清
 | 
	
		
			
				|  |  | +            designVariables.add(dv);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return designVariables;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * python 控件写入
 | 
	
		
			
				|  |  | +     * @param pid
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     * @throws IOException
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static String writePython(String pid) throws IOException {
 | 
	
		
			
				|  |  | +        MdoProPythonMapper ppDao = UtilTools.getBean(MdoProPythonMapper.class);
 | 
	
		
			
				|  |  | +        MdoProPythonSQLBuilder ppsb = new MdoProPythonSQLBuilder();
 | 
	
		
			
				|  |  | +        ppsb.createCriteria().andPidEqualTo(pid);
 | 
	
		
			
				|  |  | +        List<MdoProPython> ppList = ppDao.selectByExample(ppsb);
 | 
	
		
			
				|  |  | +        if(!ppList.isEmpty()){
 | 
	
		
			
				|  |  | +            String ppPath =LogService.BPATH+"/"+ pid+"/in/py.py";
 | 
	
		
			
				|  |  | +            MdoProPython pp =ppList.get(0);
 | 
	
		
			
				|  |  | +            FileWriter writer =new FileWriter(ppPath);
 | 
	
		
			
				|  |  | +            writer.write(pp.getPtython());
 | 
	
		
			
				|  |  | +            writer.close();
 | 
	
		
			
				|  |  | +            return  ppPath;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return "";
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |