|
@@ -0,0 +1,729 @@
|
|
|
+package com.persagy.service;
|
|
|
+
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.antlr.runtime.ANTLRInputStream;
|
|
|
+import org.antlr.runtime.CommonTokenStream;
|
|
|
+import org.antlr.runtime.tree.CommonTree;
|
|
|
+import org.antlr.runtime.tree.CommonTreeNodeStream;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.pbsage.parser.emsiv.EMSLexer;
|
|
|
+import com.pbsage.parser.emsiv.EMSParser;
|
|
|
+import com.pbsage.parser.emsiv.EMSScanner;
|
|
|
+import com.persagy.entity.WSEntity_point;
|
|
|
+import com.persagy.util.ConstantWeb;
|
|
|
+import com.persagy.wsentity2.TBranch;
|
|
|
+import com.persagy.wsentity2.TCalculateUnit;
|
|
|
+import com.persagy.wsentity2.TCalculateUnit2EnergyItem;
|
|
|
+import com.persagy.wsentity2.TConfigUse;
|
|
|
+import com.persagy.wsentity2.TConfigUseItem;
|
|
|
+import com.persagy.wsentity2.TEnergyItem;
|
|
|
+import com.persagy.wsentity2.TEnergyModel;
|
|
|
+import com.persagy.wsentity2.TMeter;
|
|
|
+import com.persagy.wsentity2.TSurvey;
|
|
|
+import com.persagy.wsentity2.TSurveyResult;
|
|
|
+import com.sagacloud.parser.simulator.ComputeEngineLexer;
|
|
|
+import com.sagacloud.parser.simulator.ComputeEngineParser;
|
|
|
+import com.sagacloud.parser.simulator.ComputeEngineScanner;
|
|
|
+import com.sagacloud.util.compute_engine.entity.Entity_ci_instance;
|
|
|
+import com.sagacloud.util.compute_engine.entity.Entity_ci_instanceinput;
|
|
|
+import com.sagacloud.util.compute_engine.entity.Entity_ci_instanceoutput;
|
|
|
+
|
|
|
+public class SurveyUtil2 {
|
|
|
+ public static TSurveyResult Check(TSurvey survey) {
|
|
|
+ TSurveyResult result = new TSurveyResult();
|
|
|
+ result.setProjectSign(survey.getProjectSign());
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Entity_ci_instance Generate_servicedata(String collectBuildingSign, List<WSEntity_point> pointList, Map<String, Double> InputCTMap)
|
|
|
+ throws Exception {
|
|
|
+ String instance_name = "servicedata";
|
|
|
+ Entity_ci_instance result = Generate_servicedata(collectBuildingSign, instance_name, pointList, InputCTMap);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Entity_ci_instance Generate_servicedata(String collectBuildingSign, String instance_name, List<WSEntity_point> pointList,
|
|
|
+ Map<String, Double> InputCTMap) throws Exception {
|
|
|
+ String object_sign = ConstantWeb.old_saas_parallel ? collectBuildingSign + "002" : collectBuildingSign;
|
|
|
+ Entity_ci_instance instance = new Entity_ci_instance();
|
|
|
+ instance.setObject_sign(object_sign);
|
|
|
+ instance.setInstance_name(instance_name);
|
|
|
+ instance.setFlag_valid(1L);
|
|
|
+ instance.setCompute_interval_seconds(ConstantWeb.name2interval.get(instance_name));
|
|
|
+
|
|
|
+ String building = collectBuildingSign;
|
|
|
+ List<Entity_ci_instanceinput> inputList = new ArrayList<Entity_ci_instanceinput>();
|
|
|
+ List<Entity_ci_instanceoutput> outputList = new ArrayList<Entity_ci_instanceoutput>();
|
|
|
+ for (WSEntity_point meter : pointList) {
|
|
|
+ if (!meter.getData_type().equals("Acc") && !meter.getData_type().equals("InAc")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String detail_meter = meter.getMeter();
|
|
|
+ long detail_funcid = meter.getFuncid();
|
|
|
+ {
|
|
|
+ Entity_ci_instanceinput input = new Entity_ci_instanceinput();
|
|
|
+ input.setObject_sign(object_sign);
|
|
|
+ input.setInstance_name(instance_name);
|
|
|
+ input.setInput_sign("meterdata_" + detail_meter + "_" + detail_funcid);
|
|
|
+ input.setTime_period(meter.getCollect_cycle());
|
|
|
+ input.setData_class("near");
|
|
|
+ input.setDao_type("fjd_near");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", building);
|
|
|
+ dao_key.put("meter", detail_meter);
|
|
|
+ dao_key.put("funcid", detail_funcid);
|
|
|
+ input.setDao_key(dao_key.toJSONString());
|
|
|
+ input.setSource_object_sign(building);
|
|
|
+ input.setSource_instance_name("fjd");
|
|
|
+ input.setSource_output_sign(detail_meter + "_" + detail_funcid);
|
|
|
+ inputList.add(input);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ Entity_ci_instanceoutput output = new Entity_ci_instanceoutput();
|
|
|
+ output.setObject_sign(object_sign);
|
|
|
+ output.setInstance_name(instance_name);
|
|
|
+ output.setOutput_sign("servicedata_" + detail_meter + "_" + detail_funcid);
|
|
|
+ output.setTime_begin("19700101000000");
|
|
|
+ output.setTime_end("99990101000000");
|
|
|
+ output.setData_class("consumption");
|
|
|
+ output.setDao_type("servicedata");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", building);
|
|
|
+ dao_key.put("meter", detail_meter);
|
|
|
+ dao_key.put("funcid", detail_funcid);
|
|
|
+ output.setDao_key(dao_key.toJSONString());
|
|
|
+ output.setNeed_all_var_data(true);
|
|
|
+ output.setTime_period_intialized(true);
|
|
|
+ output.setTime_period(meter.getCollect_cycle());
|
|
|
+ output.setMode("expression");
|
|
|
+ String expression;
|
|
|
+ if (InputCTMap.containsKey(detail_meter + "_" + detail_funcid)) {
|
|
|
+ expression = "(meterdata_" + detail_meter + "_" + detail_funcid + "'+1-" + "meterdata_" + detail_meter + "_" + detail_funcid
|
|
|
+ + ")*" + InputCTMap.get(detail_meter + "_" + detail_funcid);
|
|
|
+ } else {
|
|
|
+ expression = "meterdata_" + detail_meter + "_" + detail_funcid + "'+1-" + "meterdata_" + detail_meter + "_" + detail_funcid;
|
|
|
+ }
|
|
|
+ output.setExpression(expression);
|
|
|
+ outputList.add(output);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ instance.setInputs(inputList.toArray(new Entity_ci_instanceinput[0]));
|
|
|
+ instance.setOutputs(outputList.toArray(new Entity_ci_instanceoutput[0]));
|
|
|
+
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Entity_ci_instance Generate_EMS(TSurvey survey, Map<String, Long> defaultMap, Map<String, Boolean> InputMap) throws Exception {
|
|
|
+ String time_period = "15min";
|
|
|
+
|
|
|
+ String source_object_sign = ConstantWeb.old_saas_parallel ? survey.getProjectSign() + "002" : survey.getProjectSign();
|
|
|
+ String object_sign = ConstantWeb.old_saas_parallel ? survey.getProjectSign() + "003" : survey.getProjectSign() + "-" + "001";
|
|
|
+ String instance_name = "EMS";
|
|
|
+ String building = survey.getProjectSign();
|
|
|
+
|
|
|
+ Map<String, TMeter> reportMeterMap = new HashMap<String, TMeter>();
|
|
|
+ for (TMeter meter : survey.getReportMeterArray()) {
|
|
|
+ reportMeterMap.put(meter.getSign(), meter);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Entity_ci_instanceinput> inputList = new ArrayList<Entity_ci_instanceinput>();
|
|
|
+ List<Entity_ci_instanceoutput> outputList = new ArrayList<Entity_ci_instanceoutput>();
|
|
|
+
|
|
|
+ Map<String, TBranch> branchMap = new HashMap<String, TBranch>();
|
|
|
+ TBranch[] branchArray = survey.getBranchArray();
|
|
|
+ for (TBranch branch : branchArray) {
|
|
|
+ branchMap.put(branch.getSign(), branch);
|
|
|
+ }
|
|
|
+ // 统计输入
|
|
|
+ for (String input_sign : InputMap.keySet()) {
|
|
|
+ int index_ = input_sign.lastIndexOf('_');
|
|
|
+ String meter = input_sign.substring(0, index_);
|
|
|
+ long funcid = Long.parseLong(input_sign.substring(index_ + 1));
|
|
|
+
|
|
|
+ TMeter exist = null;
|
|
|
+ for (TMeter pm : survey.getPointArray()) {
|
|
|
+ String ppp = pm.getSign() + "_" + pm.getFuncid();
|
|
|
+ if (ppp.equals(input_sign)) {
|
|
|
+ exist = pm;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (exist != null) {
|
|
|
+ Entity_ci_instanceinput input = new Entity_ci_instanceinput();
|
|
|
+ input.setObject_sign(object_sign);
|
|
|
+ input.setInstance_name(instance_name);
|
|
|
+ input.setInput_sign(exist.getDataType().equals("Acc") ? "servicedata_" + input_sign : "meterdata_" + input_sign);
|
|
|
+ input.setTime_period(exist.getTimeType());
|
|
|
+ input.setData_class(exist.getDataType().equals("Acc") ? "consumption" : "near");
|
|
|
+ input.setDao_type(exist.getDataType().equals("Acc") ? "servicedata" : "fjd_near");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", building);
|
|
|
+ dao_key.put("meter", meter);
|
|
|
+ dao_key.put("funcid", funcid);
|
|
|
+ input.setDao_key(dao_key.toJSONString());
|
|
|
+ input.setSource_object_sign(source_object_sign);
|
|
|
+ input.setSource_instance_name(exist.getDataType().equals("Acc") ? "servicedata" : "fjd");
|
|
|
+ input.setSource_output_sign(exist.getDataType().equals("Acc") ? "servicedata_" + input_sign : input_sign);
|
|
|
+ inputList.add(input);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Entity_ci_instanceinput input = new Entity_ci_instanceinput();
|
|
|
+ input.setObject_sign(object_sign);
|
|
|
+ input.setInstance_name(instance_name);
|
|
|
+ input.setInput_sign("servicedata_" + input_sign);
|
|
|
+ if (reportMeterMap.containsKey(meter)) {
|
|
|
+ TMeter TMeter = reportMeterMap.get(meter);
|
|
|
+ if (ConstantWeb.reportpoint_fixed_period_enable) {
|
|
|
+ input.setTime_period(ConstantWeb.reportpoint_fixed_period_value);
|
|
|
+ } else {
|
|
|
+ input.setTime_period(TMeter.getTimeType());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ input.setTime_period(time_period);
|
|
|
+ }
|
|
|
+ input.setData_class("consumption");
|
|
|
+ input.setDao_type("servicedata");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", building);
|
|
|
+ dao_key.put("meter", meter);
|
|
|
+ dao_key.put("funcid", funcid);
|
|
|
+ input.setDao_key(dao_key.toJSONString());
|
|
|
+ input.setSource_object_sign(source_object_sign);
|
|
|
+ input.setSource_instance_name("servicedata");
|
|
|
+ input.setSource_output_sign("servicedata_" + input_sign);
|
|
|
+ inputList.add(input);
|
|
|
+ }
|
|
|
+ {
|
|
|
+ String virtualpoint_meter = "virtualpoint";
|
|
|
+ long virtualpoint_funcid = 99999L;
|
|
|
+ Entity_ci_instanceinput input = new Entity_ci_instanceinput();
|
|
|
+ input.setObject_sign(object_sign);
|
|
|
+ input.setInstance_name(instance_name);
|
|
|
+ input.setInput_sign("servicedata_" + virtualpoint_meter + "_" + virtualpoint_funcid);
|
|
|
+ {
|
|
|
+ input.setTime_period(time_period);
|
|
|
+ }
|
|
|
+ input.setData_class("consumption");
|
|
|
+ input.setDao_type("servicedata");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", building);
|
|
|
+ dao_key.put("meter", virtualpoint_meter);
|
|
|
+ dao_key.put("funcid", virtualpoint_funcid);
|
|
|
+ input.setDao_key(dao_key.toJSONString());
|
|
|
+ input.setSource_object_sign(source_object_sign);
|
|
|
+ input.setSource_instance_name("servicedata");
|
|
|
+ input.setSource_output_sign("servicedata_" + virtualpoint_meter + "_" + virtualpoint_funcid);
|
|
|
+ inputList.add(input);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据时间段生成output
|
|
|
+ Map<String, Boolean> modelSignMap = new HashMap<String, Boolean>();
|
|
|
+ for (TConfigUse configUse : survey.getConfigUseArray()) {
|
|
|
+ Long defaultFunctionId = defaultMap.get(configUse.getEnergyType());
|
|
|
+ // 初始化分项节点
|
|
|
+ List<EnergyModel_tmp> emList = new ArrayList<EnergyModel_tmp>();
|
|
|
+ for (TEnergyModel energyModel : survey.getEnergyModelArray()) {
|
|
|
+ if (!energyModel.getEnergyType().equals(configUse.getEnergyType())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!modelSignMap.containsKey(energyModel.getSign())) {
|
|
|
+ modelSignMap.put(energyModel.getSign(), true);
|
|
|
+ }
|
|
|
+
|
|
|
+ EnergyModel_tmp em = new EnergyModel_tmp();
|
|
|
+ em.sign = energyModel.getSign();
|
|
|
+ em.name = energyModel.getName();
|
|
|
+ em.building = energyModel.getBuildingSign();
|
|
|
+ em.energyType = energyModel.getEnergyType();
|
|
|
+ Map<String, EnergyItem_tmp> eiMap = new HashMap<String, EnergyItem_tmp>();
|
|
|
+ for (TEnergyItem energyItem : energyModel.getEnergyItemArray()) {
|
|
|
+ EnergyItem_tmp ei = new EnergyItem_tmp();
|
|
|
+ ei.sign = energyItem.getSign();
|
|
|
+ ei.name = energyItem.getName();
|
|
|
+ ei.parentSign = energyItem.getParentSign();
|
|
|
+ if (energyItem.getFormula() != null) {
|
|
|
+ ei.isExpression = true;
|
|
|
+ ei.initialized = true;
|
|
|
+ ei.formula = energyItem.getFormula();
|
|
|
+ }
|
|
|
+ em.itemList.add(ei);
|
|
|
+ eiMap.put(ei.sign, ei);
|
|
|
+ }
|
|
|
+ for (EnergyItem_tmp ei : em.itemList) {
|
|
|
+ if (ei.parentSign != null) {
|
|
|
+ EnergyItem_tmp parent = eiMap.get(ei.parentSign);
|
|
|
+ parent.children.add(ei);
|
|
|
+ ei.parent = parent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ emList.add(em);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 初始化支路计算单元节点,并链接到分项
|
|
|
+ Map<String, CalculateUnit_tmp> cuMap = new HashMap<String, CalculateUnit_tmp>();
|
|
|
+ for (TConfigUseItem configUseItem : configUse.getItemArray()) {
|
|
|
+ if (configUseItem.getFormula() == null && configUseItem.getMeterSign() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String formula;
|
|
|
+ if (configUseItem.getFormula() != null) {
|
|
|
+ formula = configUseItem.getFormula();
|
|
|
+ } else {
|
|
|
+ formula = "servicedata_" + configUseItem.getMeterSign() + "_" + defaultFunctionId;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理计算单元
|
|
|
+ if (configUseItem.getCalculateUnitSign() != null) {
|
|
|
+ String cu_sign = configUseItem.getCalculateUnitSign();
|
|
|
+ if (!cuMap.containsKey(cu_sign)) {
|
|
|
+ CalculateUnit_tmp cuInner = new CalculateUnit_tmp();
|
|
|
+ cuInner.sign = cu_sign;
|
|
|
+ cuMap.put(cu_sign, cuInner);
|
|
|
+
|
|
|
+ TCalculateUnit cu_exist = null;
|
|
|
+ for (TCalculateUnit cu_tmp : survey.getCalculateUnitArray()) {
|
|
|
+ if (cu_tmp.getSign().equals(cu_sign)) {
|
|
|
+ cu_exist = cu_tmp;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (TCalculateUnit2EnergyItem cu2ei : cu_exist.getCalculateUnit2EnergyItemArray()) {
|
|
|
+ String energyModelSign = cu2ei.getEnergyModelSign();
|
|
|
+ String energyItemSign = cu2ei.getEnergyItemSign();
|
|
|
+ EnergyItem_tmp ei = getEnergyItem_tmp(emList, energyModelSign, energyItemSign);
|
|
|
+ if (!ei.initialized) {
|
|
|
+ ei.cuList.add("objectdata" + "_" + cu_sign);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ CalculateUnit_tmp cu = cuMap.get(cu_sign);
|
|
|
+ cu.formulaList.add(formula);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理支路
|
|
|
+ {
|
|
|
+ String cu_sign = configUseItem.getBranchSign();
|
|
|
+ if (!cuMap.containsKey(cu_sign)) {
|
|
|
+ CalculateUnit_tmp cuInner = new CalculateUnit_tmp();
|
|
|
+ cuInner.sign = cu_sign;
|
|
|
+ cuMap.put(cu_sign, cuInner);
|
|
|
+
|
|
|
+ if (configUseItem.getEnergyModelSign() != null && configUseItem.getEnergyItemSign() != null) {
|
|
|
+ String energyModelSign = configUseItem.getEnergyModelSign();
|
|
|
+ String energyItemSign = configUseItem.getEnergyItemSign();
|
|
|
+ EnergyItem_tmp ei = getEnergyItem_tmp(emList, energyModelSign, energyItemSign);
|
|
|
+ if (!ei.initialized) {
|
|
|
+ ei.cuList.add("objectdata" + "_" + cu_sign);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ CalculateUnit_tmp cu = cuMap.get(cu_sign);
|
|
|
+ cu.formulaList.add(formula);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // children
|
|
|
+ Map<String, String> parentMap = new HashMap<String, String>();
|
|
|
+ for (TConfigUseItem configUseItem : configUse.getItemArray()) {
|
|
|
+ if (configUseItem.getMeterSign() != null) {
|
|
|
+ parentMap.put(configUseItem.getBranchSign(), configUseItem.getMeterSign());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Map<String, List<String>> childrenMap = new HashMap<String, List<String>>();
|
|
|
+ for (TConfigUseItem configUseItem : configUse.getItemArray()) {
|
|
|
+ if (configUseItem.getParentSign() != null && parentMap.containsKey(configUseItem.getParentSign())
|
|
|
+ && configUseItem.getMeterSign() != null) {
|
|
|
+ String parentMeter = parentMap.get(configUseItem.getParentSign());
|
|
|
+ String childMeter = configUseItem.getMeterSign();
|
|
|
+ String serviceType = null;
|
|
|
+ if (branchMap.containsKey(configUseItem.getBranchSign())) {
|
|
|
+ serviceType = branchMap.get(configUseItem.getBranchSign()).getServiceType();
|
|
|
+ }
|
|
|
+ if (serviceType != null && ConstantWeb.zongzhi_ignore_serviceTypeMap.containsKey(serviceType)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!childrenMap.containsKey(parentMeter)) {
|
|
|
+ childrenMap.put(parentMeter, new ArrayList<String>());
|
|
|
+ }
|
|
|
+ childrenMap.get(parentMeter).add(childMeter);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ {
|
|
|
+ Iterator<String> keyIter = childrenMap.keySet().iterator();
|
|
|
+ while (keyIter.hasNext()) {
|
|
|
+ String parentMeter = keyIter.next();
|
|
|
+ List<String> children = childrenMap.get(parentMeter);
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (String meter_child : children) {
|
|
|
+ String MeterSign = meter_child;
|
|
|
+ if (!InputMap.containsKey(MeterSign + "_" + defaultFunctionId)) {
|
|
|
+ InputMap.put(MeterSign + "_" + defaultFunctionId, true);
|
|
|
+ }
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ sb.append("+");
|
|
|
+ }
|
|
|
+ sb.append("servicedata_" + MeterSign + "_" + defaultFunctionId);
|
|
|
+ }
|
|
|
+ String expression = sb.toString();
|
|
|
+ Entity_ci_instanceoutput output = new Entity_ci_instanceoutput();
|
|
|
+ output.setObject_sign(object_sign);
|
|
|
+ output.setInstance_name(instance_name);
|
|
|
+ output.setOutput_sign("servicedata_children_" + parentMeter);
|
|
|
+ output.setTime_begin(configUse.getStartDate());
|
|
|
+ output.setTime_end(configUse.getEndDate());
|
|
|
+ output.setData_class("consumption");
|
|
|
+ output.setDao_type("servicedata");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", building);
|
|
|
+ dao_key.put("meter", "children_" + parentMeter);
|
|
|
+ dao_key.put("funcid", defaultFunctionId);
|
|
|
+ output.setDao_key(dao_key.toJSONString());
|
|
|
+ output.setNeed_all_var_data(false);
|
|
|
+ output.setTime_period_intialized(true);
|
|
|
+ output.setTime_period(time_period);
|
|
|
+ output.setMode("expression");
|
|
|
+ output.setExpression(expression);
|
|
|
+ outputList.add(output);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 分项汇总
|
|
|
+ while (true) {
|
|
|
+ boolean has_work = false;
|
|
|
+ for (EnergyModel_tmp em : emList) {
|
|
|
+ for (EnergyItem_tmp ei : em.itemList) {
|
|
|
+ if (ei.initialized) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean children_ready = true;
|
|
|
+ for (EnergyItem_tmp ei_tmp : ei.children) {
|
|
|
+ if (!ei_tmp.initialized) {
|
|
|
+ children_ready = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (children_ready) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (EnergyItem_tmp ei_tmp : ei.children) {
|
|
|
+ if (ei_tmp.formula != null) {
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ sb.append("+");
|
|
|
+ }
|
|
|
+ if (ei_tmp.isExpression) {
|
|
|
+ sb.append("energydata_" + em.building + "_" + em.sign + "_" + ei_tmp.sign);
|
|
|
+ } else {
|
|
|
+ sb.append("(" + ei_tmp.formula + ")");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String cu : ei.cuList) {
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ sb.append("+");
|
|
|
+ }
|
|
|
+ sb.append(cu);
|
|
|
+ }
|
|
|
+ ei.initialized = true;
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ ei.formula = sb.toString();
|
|
|
+ }
|
|
|
+ has_work = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!has_work) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构造输出
|
|
|
+ for (EnergyModel_tmp em : emList) {
|
|
|
+ for (EnergyItem_tmp ei : em.itemList) {
|
|
|
+ if (!ei.initialized) {
|
|
|
+ throw new Exception("energyItem not initialized:" + ei.sign);
|
|
|
+ }
|
|
|
+ if (ei.formula == null) {
|
|
|
+ ei.formula = "servicedata_virtualpoint_99999" + "-" + "servicedata_virtualpoint_99999";
|
|
|
+ }
|
|
|
+
|
|
|
+ Entity_ci_instanceoutput output = new Entity_ci_instanceoutput();
|
|
|
+ output.setObject_sign(object_sign);
|
|
|
+ output.setInstance_name(instance_name);
|
|
|
+ output.setOutput_sign("energydata_" + em.building + "_" + em.sign + "_" + ei.sign);
|
|
|
+ output.setTime_begin(configUse.getStartDate());
|
|
|
+ output.setTime_end(configUse.getEndDate());
|
|
|
+ output.setData_class("consumption");
|
|
|
+ output.setDao_type("energydata");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", em.building);
|
|
|
+ dao_key.put("energyModelSign", em.sign);
|
|
|
+ dao_key.put("energyModelNodeSign", ei.sign);
|
|
|
+ output.setDao_key(dao_key.toJSONString());
|
|
|
+ output.setNeed_all_var_data(false);
|
|
|
+ output.setTime_period_intialized(false);
|
|
|
+ output.setTime_period(time_period);
|
|
|
+ output.setMode("expression");
|
|
|
+ output.setExpression(ei.formula);
|
|
|
+ outputList.add(output);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (CalculateUnit_tmp cu : cuMap.values()) {
|
|
|
+ Entity_ci_instanceoutput output = new Entity_ci_instanceoutput();
|
|
|
+ output.setObject_sign(object_sign);
|
|
|
+ output.setInstance_name(instance_name);
|
|
|
+ output.setOutput_sign("objectdata_" + cu.sign);
|
|
|
+ output.setTime_begin(configUse.getStartDate());
|
|
|
+ output.setTime_end(configUse.getEndDate());
|
|
|
+ output.setData_class("consumption");
|
|
|
+ output.setDao_type("objectdata");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", building);
|
|
|
+ dao_key.put("objectSign", cu.sign);
|
|
|
+ output.setDao_key(dao_key.toJSONString());
|
|
|
+ output.setNeed_all_var_data(false);
|
|
|
+ output.setTime_period_intialized(false);
|
|
|
+ output.setTime_period(time_period);
|
|
|
+ output.setMode("expression");
|
|
|
+ if (cu.formulaList.size() == 0) {
|
|
|
+ throw new Exception("CalculateUnit no formula:" + cu.sign);
|
|
|
+ }
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ for (String formula : cu.formulaList) {
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ sb.append("+");
|
|
|
+ }
|
|
|
+ sb.append("(" + formula + ")");
|
|
|
+ }
|
|
|
+ output.setExpression(sb.toString());
|
|
|
+ outputList.add(output);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 没有调研的分项模型
|
|
|
+ for (TEnergyModel energyModel : survey.getEnergyModelArray()) {
|
|
|
+ if (modelSignMap.containsKey(energyModel.getSign())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ EnergyModel_tmp em = new EnergyModel_tmp();
|
|
|
+ em.sign = energyModel.getSign();
|
|
|
+ em.name = energyModel.getName();
|
|
|
+ em.building = energyModel.getBuildingSign();
|
|
|
+ em.energyType = energyModel.getEnergyType();
|
|
|
+ Map<String, EnergyItem_tmp> eiMap = new HashMap<String, EnergyItem_tmp>();
|
|
|
+ for (TEnergyItem energyItem : energyModel.getEnergyItemArray()) {
|
|
|
+ EnergyItem_tmp ei = new EnergyItem_tmp();
|
|
|
+ ei.sign = energyItem.getSign();
|
|
|
+ ei.name = energyItem.getName();
|
|
|
+ ei.parentSign = energyItem.getParentSign();
|
|
|
+ if (energyItem.getFormula() != null) {
|
|
|
+ ei.isExpression = true;
|
|
|
+ ei.initialized = true;
|
|
|
+ ei.formula = energyItem.getFormula();
|
|
|
+ }
|
|
|
+ em.itemList.add(ei);
|
|
|
+ eiMap.put(ei.sign, ei);
|
|
|
+ }
|
|
|
+ for (EnergyItem_tmp ei : em.itemList) {
|
|
|
+ if (ei.parentSign != null) {
|
|
|
+ EnergyItem_tmp parent = eiMap.get(ei.parentSign);
|
|
|
+ parent.children.add(ei);
|
|
|
+ ei.parent = parent;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (EnergyItem_tmp ei : em.itemList) {
|
|
|
+ if (!ei.initialized) {
|
|
|
+ ei.formula = "servicedata_virtualpoint_99999" + "-" + "servicedata_virtualpoint_99999";
|
|
|
+ }
|
|
|
+ Entity_ci_instanceoutput output = new Entity_ci_instanceoutput();
|
|
|
+ output.setObject_sign(object_sign);
|
|
|
+ output.setInstance_name(instance_name);
|
|
|
+ output.setOutput_sign("energydata_" + em.building + "_" + em.sign + "_" + ei.sign);
|
|
|
+ output.setTime_begin("19700101000000");
|
|
|
+ output.setTime_end("99990101000000");
|
|
|
+ output.setData_class("consumption");
|
|
|
+ output.setDao_type("energydata");
|
|
|
+ JSONObject dao_key = new JSONObject();
|
|
|
+ dao_key.put("building", em.building);
|
|
|
+ dao_key.put("energyModelSign", em.sign);
|
|
|
+ dao_key.put("energyModelNodeSign", ei.sign);
|
|
|
+ output.setDao_key(dao_key.toJSONString());
|
|
|
+ output.setNeed_all_var_data(false);
|
|
|
+ output.setTime_period_intialized(false);
|
|
|
+ output.setTime_period(time_period);
|
|
|
+ output.setMode("expression");
|
|
|
+ output.setExpression(ei.formula);
|
|
|
+ outputList.add(output);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Entity_ci_instance instance = new Entity_ci_instance();
|
|
|
+ instance.setObject_sign(object_sign);
|
|
|
+ instance.setInstance_name(instance_name);
|
|
|
+ instance.setFlag_valid(1L);
|
|
|
+ instance.setCompute_interval_seconds(1L);
|
|
|
+ instance.setInputs(inputList.toArray(new Entity_ci_instanceinput[0]));
|
|
|
+ instance.setOutputs(outputList.toArray(new Entity_ci_instanceoutput[0]));
|
|
|
+
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static EnergyItem_tmp getEnergyItem_tmp(List<EnergyModel_tmp> emList, String energyModelSign, String energyItemSign) {
|
|
|
+ EnergyModel_tmp em_exist = null;
|
|
|
+ for (EnergyModel_tmp em : emList) {
|
|
|
+ if (em.sign.equals(energyModelSign)) {
|
|
|
+ em_exist = em;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ EnergyItem_tmp ei_exist = null;
|
|
|
+ for (EnergyItem_tmp ei : em_exist.itemList) {
|
|
|
+ if (ei.sign.equals(energyItemSign)) {
|
|
|
+ ei_exist = ei;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ei_exist;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 扫描公式,整理公式
|
|
|
+ public static void refresh(TSurvey survey, Map<String, Long> defaultMap, Map<String, Boolean> InputMap, Map<String, Double> InputCTMap)
|
|
|
+ throws Exception {
|
|
|
+ for (TEnergyModel energyModel : survey.getEnergyModelArray()) {
|
|
|
+ Long defaultFunctionId = defaultMap.get(energyModel.getEnergyType());
|
|
|
+ for (TEnergyItem energyItem : energyModel.getEnergyItemArray()) {
|
|
|
+ if (energyItem.getFormula() != null) {
|
|
|
+ String new_formula = formula_pre(energyItem.getFormula(), defaultFunctionId, InputMap);
|
|
|
+ energyItem.setFormula(new_formula);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (TConfigUse configUse : survey.getConfigUseArray()) {
|
|
|
+ Long defaultFunctionId = defaultMap.get(configUse.getEnergyType());
|
|
|
+ for (TConfigUseItem configUseItem : configUse.getItemArray()) {
|
|
|
+ if (configUseItem.getFormula() != null) {
|
|
|
+ String new_formula = formula_pre(configUseItem.getFormula(), defaultFunctionId, InputMap);
|
|
|
+ configUseItem.setFormula(new_formula);
|
|
|
+ } else {
|
|
|
+ String MeterSign = configUseItem.getMeterSign();
|
|
|
+ long FunctionTypeId = defaultFunctionId;
|
|
|
+ InputMap.put(MeterSign + "_" + FunctionTypeId, true);
|
|
|
+ if (configUseItem.getMeterCT() != null && configUseItem.getMeterCT() != 1.0) {
|
|
|
+ if (InputCTMap.containsKey(MeterSign + "_" + FunctionTypeId)) {
|
|
|
+ double exist_value = InputCTMap.get(MeterSign + "_" + FunctionTypeId).doubleValue();
|
|
|
+ double value_new = configUseItem.getMeterCT().doubleValue();
|
|
|
+ if (exist_value != value_new) {
|
|
|
+ throw new Exception("CT has multi value:" + MeterSign + "_" + FunctionTypeId + "\t" + exist_value + "\t" + value_new);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ InputCTMap.put(MeterSign + "_" + FunctionTypeId, configUseItem.getMeterCT());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 公式预处理,加上默认功能号,[1001.1]改成servicedata_1001_0
|
|
|
+ private static String formula_pre(String formula, Long defaultFunctionId, Map<String, Boolean> InputMap) throws Exception {
|
|
|
+ try {
|
|
|
+ CommonTreeNodeStream nodes = GenerateParser_ComputeEngine(formula);
|
|
|
+ ComputeEngineScanner scanner = new ComputeEngineScanner(nodes);
|
|
|
+ scanner.prog();
|
|
|
+ String[] vars = scanner.varDict.keySet().toArray(new String[0]);
|
|
|
+ for (int index_var = 0; index_var < vars.length; index_var++) {
|
|
|
+ String var = vars[index_var];
|
|
|
+ if (var.startsWith("meterdata_")) {
|
|
|
+ InputMap.put(var.substring("meterdata_".length()), true);
|
|
|
+ } else if (var.startsWith("servicedata_")) {
|
|
|
+ InputMap.put(var.substring("servicedata_".length()), true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return formula;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // log.error(e.getMessage(),e);
|
|
|
+ String result = formula_EMS(formula, defaultFunctionId, InputMap);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String formula_EMS(String formula, Long defaultFunctionId, Map<String, Boolean> InputMap) throws Exception {
|
|
|
+ CommonTreeNodeStream nodes = GenerateParser_EMS(formula);
|
|
|
+ EMSScanner scanner = new EMSScanner(nodes);
|
|
|
+ scanner.prog();
|
|
|
+ String[] vars = scanner.GetVarDict().keySet().toArray(new String[0]);
|
|
|
+ Map<String, String> old2newMap = new HashMap<String, String>();
|
|
|
+ String new_expression = formula;
|
|
|
+ for (int index_var = 0; index_var < vars.length; index_var++) {
|
|
|
+ String var = vars[index_var];
|
|
|
+ String MeterSign;
|
|
|
+ long FunctionTypeId;
|
|
|
+ String varPart = var;
|
|
|
+ int index_dot = varPart.indexOf('.');
|
|
|
+ if (index_dot == -1) {
|
|
|
+ MeterSign = varPart;
|
|
|
+ FunctionTypeId = defaultFunctionId.longValue();
|
|
|
+ } else {
|
|
|
+ MeterSign = varPart.substring(0, index_dot);
|
|
|
+ FunctionTypeId = Integer.parseInt(varPart.substring(index_dot + 1));
|
|
|
+ }
|
|
|
+ if (ConstantWeb.funcid_10101_to_0) {
|
|
|
+ if (FunctionTypeId == 10101) {
|
|
|
+ FunctionTypeId = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!InputMap.containsKey(MeterSign + "_" + FunctionTypeId)) {
|
|
|
+ InputMap.put(MeterSign + "_" + FunctionTypeId, true);
|
|
|
+ }
|
|
|
+ old2newMap.put(var, "servicedata_" + MeterSign + "_" + FunctionTypeId);
|
|
|
+ }
|
|
|
+ for (int index_var = 0; index_var < vars.length; index_var++) {
|
|
|
+ String var = vars[index_var];
|
|
|
+ int index_dot = var.indexOf('.');
|
|
|
+ if (index_dot == -1) {
|
|
|
+ new_expression = new_expression.replaceAll("\\[" + var + "\\]", old2newMap.get(var));
|
|
|
+ } else {
|
|
|
+ new_expression = new_expression.replaceAll("\\[" + var.substring(0, index_dot) + "\\." + var.substring(index_dot + 1) + "\\]",
|
|
|
+ old2newMap.get(var));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new_expression;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CommonTreeNodeStream GenerateParser_EMS(String expression) throws Exception {
|
|
|
+ ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream((expression + "$").getBytes()));
|
|
|
+ EMSLexer lexer = new EMSLexer(input);
|
|
|
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
|
+ EMSParser parser = new EMSParser(tokens);
|
|
|
+ EMSParser.prog_return r = parser.prog();
|
|
|
+
|
|
|
+ CommonTree t = (CommonTree) r.getTree();
|
|
|
+ CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
|
|
|
+ nodes.setTokenStream(tokens);
|
|
|
+
|
|
|
+ return nodes;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CommonTreeNodeStream GenerateParser_ComputeEngine(String expression) throws Exception {
|
|
|
+ ANTLRInputStream input = new ANTLRInputStream(new ByteArrayInputStream((expression + "$").getBytes()));
|
|
|
+ ComputeEngineLexer lexer = new ComputeEngineLexer(input);
|
|
|
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
|
+ ComputeEngineParser parser = new ComputeEngineParser(tokens);
|
|
|
+ ComputeEngineParser.prog_return r = parser.prog();
|
|
|
+
|
|
|
+ CommonTree t = (CommonTree) r.getTree();
|
|
|
+ CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
|
|
|
+ nodes.setTokenStream(tokens);
|
|
|
+
|
|
|
+ return nodes;
|
|
|
+ }
|
|
|
+}
|