|
|
@@ -0,0 +1,902 @@
|
|
|
+package com.sagacloud.util.compute_engine.core;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.sagacloud.extend.dao_data.ImplDao_fjd_statinst;
|
|
|
+import com.sagacloud.parser.simulator.String2Grammar;
|
|
|
+import com.sagacloud.util.compute_engine.util.*;
|
|
|
+import com.sagacloud.util.math.MathUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class CoreComputeUtil {
|
|
|
+ static List<Date> splitDateList = new ArrayList<Date>();
|
|
|
+
|
|
|
+ static {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ try {
|
|
|
+ Date date_from = sdf.parse("20000101000000");
|
|
|
+ Date date_to = sdf.parse("21000101000000");
|
|
|
+ Date date_tmp = date_from;
|
|
|
+ while (date_tmp.compareTo(date_to) < 0) {
|
|
|
+ Date date_next = DateUtil.Instance().GetDate_offset(date_tmp, "1m", 1);
|
|
|
+ splitDateList.add(date_tmp);
|
|
|
+ date_tmp = date_next;
|
|
|
+ }
|
|
|
+ } catch (ParseException e) {
|
|
|
+ log.error(e.getMessage(),e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<String, List<TimeSpan>> ComputeByPart(String object_sign, String instance_name, List<CoreInput> inputList,
|
|
|
+ List<CoreOutput> outputList, long compute_round, Map<String, List<TimeSpan>> inputTimeSpanMap,
|
|
|
+ Map<String, List<TimeSpan>> outputdetailListMap, IDao_log dao_log) throws Exception {
|
|
|
+ SimpleDateFormat sdf_ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ for (int index_split = 0; index_split < splitDateList.size() - 1; index_split++) {
|
|
|
+ Date date_from = splitDateList.get(index_split);
|
|
|
+ Date date_to = splitDateList.get(index_split + 1);
|
|
|
+ Map<String, List<TimeSpan>> inputTimeSpanMapInner = new HashMap<String, List<TimeSpan>>();
|
|
|
+ for (CoreInput input : inputList) {
|
|
|
+ if (inputTimeSpanMap.containsKey(input.input_sign)) {
|
|
|
+ List<TimeSpan> timeSpanList = DateUtil.Instance().Clone(inputTimeSpanMap.get(input.input_sign));
|
|
|
+ DateUtil.Instance().Limit(timeSpanList, date_from, DateUtil.Instance().GetDate_offset(date_to, input.time_period, -1));
|
|
|
+ if (timeSpanList.size() > 0) {
|
|
|
+ inputTimeSpanMapInner.put(input.input_sign, timeSpanList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (inputTimeSpanMapInner.size() > 0) {
|
|
|
+ log.info("**************************************************************** ComputeByPartOnce " + object_sign + " "
|
|
|
+ + instance_name + "\t" + sdf_.format(date_from) + "\t" + sdf_.format(date_to) + "begin");
|
|
|
+ ComputeOnce(object_sign, instance_name, inputList, outputList, compute_round, inputTimeSpanMapInner, outputdetailListMap, dao_log,
|
|
|
+ false);
|
|
|
+ log.info("**************************************************************** ComputeByPartOnce " + object_sign + " "
|
|
|
+ + instance_name + "\t" + sdf_.format(date_from) + "\t" + sdf_.format(date_to) + "end");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, List<TimeSpan>> result = ComputeByPartAfter(object_sign, instance_name, inputList, outputList, compute_round, inputTimeSpanMap,
|
|
|
+ outputdetailListMap, dao_log);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Map<String, List<TimeSpan>> ComputeOnce(String object_sign, String instance_name, List<CoreInput> inputList,
|
|
|
+ List<CoreOutput> outputList, long compute_round, Map<String, List<TimeSpan>> inputTimeSpanMap,
|
|
|
+ Map<String, List<TimeSpan>> outputdetailListMap, IDao_log dao_log, boolean isAll) throws Exception {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ Map<String, CoreInput> inputMap = new HashMap<String, CoreInput>();
|
|
|
+ for (CoreInput input : inputList) {
|
|
|
+ inputMap.put(input.input_sign, input);
|
|
|
+ }
|
|
|
+ Map<String, CoreOutput> outputMap = new HashMap<String, CoreOutput>();
|
|
|
+ for (CoreOutput output : outputList) {
|
|
|
+ outputMap.put(output.output_sign, output);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确定相关输出需要计算的时间段,以及每个输入每种精度要取的数据的时间范围(去掉已经计算的output的时间段)
|
|
|
+ Map<String, Map<String, List<TimeSpan>>> inputIntervalTimeSpanMap = new HashMap<String, Map<String, List<TimeSpan>>>();
|
|
|
+ Map<String, List<TimeSpan>> outputTimeSpanMap = new HashMap<String, List<TimeSpan>>();
|
|
|
+ for (int i = 0; i < outputList.size(); i++) {
|
|
|
+ CoreOutput output = outputList.get(i);
|
|
|
+ List<TimeSpan> timeSpanList = new ArrayList<TimeSpan>();
|
|
|
+ if (output.dao_type.endsWith("_max") || output.dao_type.endsWith("_stat")) {
|
|
|
+ List<TimeSpan> timeSpanList_input = inputTimeSpanMap.get(output.output_sign);
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList_input, output.var_stepSpanMap.get(output.output_sign), output.time_period,
|
|
|
+ output.data_class, timeSpanList);
|
|
|
+ } else {
|
|
|
+ for (String input_sign : output.all_input.keySet()) {
|
|
|
+ CoreInput input = inputMap.get(input_sign);
|
|
|
+ if (inputTimeSpanMap.containsKey(input_sign)) {
|
|
|
+ List<TimeSpan> timeSpanList_input = inputTimeSpanMap.get(input_sign);
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList_input, output.var_stepSpanMap.get(input_sign), output.time_period,
|
|
|
+ input.data_class, timeSpanList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DateUtil.Instance().Limit(timeSpanList, output.time_begin, DateUtil.Instance().GetDate_offset(output.time_end, output.time_period, -1));
|
|
|
+ List<TimeSpan> timeSpanList_finish = outputdetailListMap.get(output.output_sign);
|
|
|
+ for (int ii = timeSpanList.size() - 1; ii >= 0; ii--) {
|
|
|
+ TimeSpan timeSpan = timeSpanList.get(ii);
|
|
|
+ boolean finish = false;
|
|
|
+ if (timeSpanList_finish != null) {
|
|
|
+ for (TimeSpan timeSpanInner : timeSpanList_finish) {
|
|
|
+ if (timeSpanInner.timefrom.getTime() == timeSpan.timefrom.getTime()) {
|
|
|
+ finish = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (finish) {
|
|
|
+ timeSpanList.remove(ii);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ outputTimeSpanMap.put(output.output_sign + "_" + sdf.format(output.time_begin), timeSpanList);
|
|
|
+
|
|
|
+ if (output.dao_type.endsWith("_max")) {
|
|
|
+ // TODO
|
|
|
+ } else if (output.dao_type.endsWith("_stat")) {
|
|
|
+ // TODO
|
|
|
+ } else {
|
|
|
+ for (String input_sign : output.all_input.keySet()) {
|
|
|
+ if (!inputIntervalTimeSpanMap.containsKey(input_sign)) {
|
|
|
+ inputIntervalTimeSpanMap.put(input_sign, new HashMap<String, List<TimeSpan>>());
|
|
|
+ }
|
|
|
+ Map<String, List<TimeSpan>> IntervalTimeSpanMap = inputIntervalTimeSpanMap.get(input_sign);
|
|
|
+ if (!IntervalTimeSpanMap.containsKey(output.time_period)) {
|
|
|
+ IntervalTimeSpanMap.put(output.time_period, new ArrayList<TimeSpan>());
|
|
|
+ }
|
|
|
+ List<TimeSpan> timeSpanList_input = IntervalTimeSpanMap.get(output.time_period);
|
|
|
+ DateUtil.Instance().MergeTo(timeSpanList, output.time_period, output.var_stepSpanMap.get(input_sign), timeSpanList_input);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DataCache dataCache = new DataCache();
|
|
|
+ // 读取数据
|
|
|
+ for (String input_sign : inputIntervalTimeSpanMap.keySet()) {
|
|
|
+ CoreInput input = inputMap.get(input_sign);
|
|
|
+ Map<String, List<TimeSpan>> timeSpanListMap = inputIntervalTimeSpanMap.get(input_sign);
|
|
|
+ for (String time_period : timeSpanListMap.keySet()) {
|
|
|
+ List<TimeSpan> timeSpanList = timeSpanListMap.get(time_period);
|
|
|
+ for (TimeSpan timeSpan : timeSpanList) {
|
|
|
+ dataCache.GetData(input_sign, input.dao_type, input.dao_key, time_period, timeSpan.timefrom, timeSpan.timeto);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 依次计算每个相关输出,计算同时填充DataCache
|
|
|
+ for (int i = 0; i < outputList.size(); i++) {
|
|
|
+ CoreOutput output = outputList.get(i);
|
|
|
+ String time_period = output.time_period;
|
|
|
+ List<TimeSpan> timeSpanList = outputTimeSpanMap.get(output.output_sign + "_" + sdf.format(output.time_begin));
|
|
|
+
|
|
|
+ if (output.dao_type.endsWith("_max")) {
|
|
|
+ CoreInput input = inputMap.get(output.output_sign);
|
|
|
+ double ratio = 1.0;
|
|
|
+ // 累计量计算max才需要折算功率
|
|
|
+ if ("consumption".equals(input.data_class)) {
|
|
|
+ ratio = 1.0 * (1000L * 60 * 60) / Parser_time_period.collect_cycle2Milliseconds(input.time_period);
|
|
|
+ }
|
|
|
+ List<String> biggerList = Parser_time_period.getBigger("1y", input.time_period);
|
|
|
+ for (String time_period_bigger : biggerList) {
|
|
|
+ List<TimeSpan> timeSpanList_new = new ArrayList<TimeSpan>();
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList, time_period_bigger, "consumption", timeSpanList_new);
|
|
|
+ for (TimeSpan timeSpan : timeSpanList_new) {
|
|
|
+ // 准备数据
|
|
|
+ List<DoubleTimeObject> dataList = Dao_dataManager.Instance().Get(input.dao_type).GetDataListGteLte(input.dao_key,
|
|
|
+ input.time_period, timeSpan.timefrom, DateUtil.Instance().GetDate_offset(timeSpan.timeto, time_period_bigger, 1),
|
|
|
+ false);
|
|
|
+
|
|
|
+ // 计算
|
|
|
+ List<DoubleTimeObject_max> outputDataList = new ArrayList<DoubleTimeObject_max>();
|
|
|
+ int tmp_data_index = 0;
|
|
|
+ Date tmpDate = timeSpan.timefrom;
|
|
|
+ while (tmpDate.getTime() <= timeSpan.timeto.getTime()) {
|
|
|
+ Date tmpDateNext = DateUtil.Instance().GetDate_offset(tmpDate, time_period_bigger, 1);
|
|
|
+
|
|
|
+ Double max_value = null;
|
|
|
+ Date max_time = null;
|
|
|
+ int data_count = 0;
|
|
|
+ for (int index_data = tmp_data_index; index_data < dataList.size(); index_data++) {
|
|
|
+ tmp_data_index = index_data;
|
|
|
+ DoubleTimeObject data = dataList.get(index_data);
|
|
|
+ if (data.data_time.getTime() < tmpDate.getTime()) {
|
|
|
+ } else if (data.data_time.getTime() < tmpDateNext.getTime()) {
|
|
|
+ if (max_value == null || max_value < data.data_value * ratio) {
|
|
|
+ max_value = data.data_value * ratio;
|
|
|
+ max_time = data.data_time;
|
|
|
+ }
|
|
|
+ data_count++;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (data_count > 0) {
|
|
|
+ DoubleTimeObject_max outputData = new DoubleTimeObject_max();
|
|
|
+ outputData.data_time = tmpDate;
|
|
|
+ outputData.data_value = max_value;
|
|
|
+ outputData.max_time = max_time;
|
|
|
+ outputDataList.add(outputData);
|
|
|
+ }
|
|
|
+
|
|
|
+ tmpDate = tmpDateNext;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除数据
|
|
|
+ Dao_data_maxManager.Instance().Get(output.dao_type).DeleteDataListGteLte(output.dao_key, time_period_bigger,
|
|
|
+ timeSpan.timefrom, timeSpan.timeto);
|
|
|
+ ConstantCE.SleepBetweenDeleteAndInsert();
|
|
|
+ // 保存数据
|
|
|
+ Dao_data_maxManager.Instance().Get(output.dao_type).InsertDataListGteLte(output.dao_key, time_period_bigger, outputDataList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (output.dao_type.endsWith("_stat")) {
|
|
|
+ CoreInput input = inputMap.get(output.output_sign);
|
|
|
+ if ("inststat".equals(output.data_class)) {
|
|
|
+ CoreComputeUtil.Stat_inststat(input.dao_type, output.dao_key, timeSpanList, input.time_period, "1y");
|
|
|
+ } else if ("consumption".equals(output.data_class)) {
|
|
|
+ CoreComputeUtil.Stat_consumption(input.dao_type, output.dao_key, timeSpanList, input.time_period, "1y");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ List<List<DoubleTimeObject>> outputDataListList = new ArrayList<List<DoubleTimeObject>>();
|
|
|
+ List<DoubleTimeObject> outputDataList_all = new ArrayList<DoubleTimeObject>();
|
|
|
+ for (TimeSpan timeSpan : timeSpanList) {
|
|
|
+ // 准备数据
|
|
|
+ Map<String, List<DoubleTimeObject>> inputDataListMap = new HashMap<String, List<DoubleTimeObject>>();
|
|
|
+ for (String input_sign : output.var_signMap.keySet()) {
|
|
|
+ String dao_type;
|
|
|
+ JSONObject dao_key;
|
|
|
+ if (inputMap.containsKey(input_sign)) {
|
|
|
+ CoreInput input = inputMap.get(input_sign);
|
|
|
+ dao_type = input.dao_type;
|
|
|
+ dao_key = input.dao_key;
|
|
|
+ } else {
|
|
|
+ CoreOutput input = outputMap.get(input_sign);
|
|
|
+ dao_type = input.dao_type;
|
|
|
+ dao_key = input.dao_key;
|
|
|
+ }
|
|
|
+ List<DoubleTimeObject> inputDataList = dataCache.GetData(input_sign, dao_type, dao_key, time_period,
|
|
|
+ DateUtil.Instance().GetDate_offset(timeSpan.timefrom, time_period, output.var_stepSpanMap.get(input_sign).backward),
|
|
|
+ DateUtil.Instance().GetDate_offset(timeSpan.timeto, time_period, output.var_stepSpanMap.get(input_sign).forward));
|
|
|
+ inputDataListMap.put(input_sign, inputDataList);
|
|
|
+ }
|
|
|
+ // 计算
|
|
|
+ List<DoubleTimeObject> outputDataList = new ArrayList<DoubleTimeObject>();
|
|
|
+ for (Date tmpDate = timeSpan.timefrom; tmpDate.compareTo(timeSpan.timeto) <= 0; tmpDate = DateUtil.Instance()
|
|
|
+ .GetDate_offset(tmpDate, time_period, 1)) {
|
|
|
+ Map<String, Double> dataMap = new HashMap<String, Double>();
|
|
|
+ int valid_data_count = 0;
|
|
|
+ for (String var_original : output.var_originalMap.keySet()) {
|
|
|
+ String var_sign;
|
|
|
+ int var_offset;
|
|
|
+ int index = var_original.indexOf('\'');
|
|
|
+ if (index != -1) {
|
|
|
+ var_sign = var_original.substring(0, index);
|
|
|
+ var_offset = Integer.parseInt(var_original.substring(index + 1));
|
|
|
+ } else {
|
|
|
+ var_sign = var_original;
|
|
|
+ var_offset = 0;
|
|
|
+ }
|
|
|
+ List<DoubleTimeObject> inputDataList = inputDataListMap.get(var_sign);
|
|
|
+ Date findDate = DateUtil.Instance().GetDate_offset(tmpDate, time_period, var_offset);
|
|
|
+ int findIndex = InsertUtil.Instance().FindIndex(inputDataList, findDate);
|
|
|
+ DoubleTimeObject input_data = null;
|
|
|
+ if (findIndex != -1) {
|
|
|
+ input_data = inputDataList.get(findIndex);
|
|
|
+ }
|
|
|
+ if (input_data == null) {
|
|
|
+ dataMap.put(var_original, null);
|
|
|
+ } else {
|
|
|
+ dataMap.put(var_original, input_data.data_value);
|
|
|
+ valid_data_count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String other_param : output.other_paramMap.keySet()) {
|
|
|
+ double opv = CoreComputeUtil_other.GetOtherValue(sdf.format(tmpDate), output.otherMap, other_param);
|
|
|
+ dataMap.put(other_param, opv);
|
|
|
+ }
|
|
|
+ if ((output.need_all_var_data && valid_data_count == output.var_originalMap.size())
|
|
|
+ || (!output.need_all_var_data && valid_data_count > 0)) {
|
|
|
+ DoubleTimeObject outputData = new DoubleTimeObject();
|
|
|
+ outputData.data_time = tmpDate;
|
|
|
+ if ("expression".equals(output.mode)) {
|
|
|
+ if (CoreComputeUtilUtil.CantCompute(output.root, dataMap)) {
|
|
|
+ outputData.data_value = null;
|
|
|
+ } else {
|
|
|
+ outputData.data_value = ComputeNode(output.root, dataMap);
|
|
|
+ }
|
|
|
+ } else if ("custom".equals(output.mode)) {
|
|
|
+ outputData.data_value = CustomFunctionManager.Instance().Compute(output.custom_function_name, output.custom_paramsMap,
|
|
|
+ dataMap);
|
|
|
+ } else {
|
|
|
+ throw new Exception("output.mode error:" + output.mode);
|
|
|
+ }
|
|
|
+ if (outputData.data_value != null) {
|
|
|
+ if (Double.isInfinite(outputData.data_value) || Double.isNaN(outputData.data_value)) {
|
|
|
+ outputData.data_value = 0.0;
|
|
|
+ }
|
|
|
+ if (ConstantCE.expression_negative_to_0 && outputData.data_value < 0.0) {
|
|
|
+ outputData.data_value = 0.0;
|
|
|
+ }
|
|
|
+ outputDataList.add(outputData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (output.successorList.size() > 0) {
|
|
|
+ dataCache.PutData(output.output_sign, time_period, timeSpan.timefrom, timeSpan.timeto, outputDataList);
|
|
|
+ }
|
|
|
+ outputDataListList.add(outputDataList);
|
|
|
+ outputDataList_all.addAll(outputDataList);
|
|
|
+
|
|
|
+ // 删除数据
|
|
|
+ Dao_dataManager.Instance().Get(output.dao_type).DeleteDataListGteLte(output.dao_key, time_period, timeSpan.timefrom,
|
|
|
+ timeSpan.timeto);
|
|
|
+ ConstantCE.SleepBetweenDeleteAndInsert();
|
|
|
+ // 保存数据
|
|
|
+ Dao_dataManager.Instance().Get(output.dao_type).InsertDataListGteLte(output.dao_key, time_period, outputDataList);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 统计
|
|
|
+ if ("consumption".equals(output.data_class)) {
|
|
|
+ Stat_consumption(output.dao_type, output.dao_key, timeSpanList, time_period, "1d");
|
|
|
+ } else if ("near".equals(output.data_class)) {
|
|
|
+ Stat_near(output.dao_type, output.dao_key, timeSpanList, time_period, outputDataList_all);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (output.other_type != null && timeSpanList.size() > 0) {
|
|
|
+ CoreComputeUtil_other.Compute_other(output.other_type, object_sign, instance_name, compute_round, output, time_period,
|
|
|
+ timeSpanList, outputDataListList, outputDataList_all, dao_log);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isAll) {
|
|
|
+ for (TimeSpan timeSpan : timeSpanList) {
|
|
|
+ // 保存日志
|
|
|
+ {
|
|
|
+ JSONObject InsertObject = new JSONObject();
|
|
|
+ InsertObject.put("object_sign", object_sign);
|
|
|
+ InsertObject.put("instance_name", instance_name);
|
|
|
+ InsertObject.put("compute_round", compute_round);
|
|
|
+ InsertObject.put("output_sign", output.output_sign);
|
|
|
+ InsertObject.put("valid_time_from", sdf.format(timeSpan.timefrom));
|
|
|
+ InsertObject.put("valid_time_to", sdf.format(timeSpan.timeto));
|
|
|
+ InsertObject.put("operate_time_to", sdf.format(new Date()));
|
|
|
+ dao_log.InsertData(InsertObject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return outputTimeSpanMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Map<String, List<TimeSpan>> ComputeByPartAfter(String object_sign, String instance_name, List<CoreInput> inputList,
|
|
|
+ List<CoreOutput> outputList, long compute_round, Map<String, List<TimeSpan>> inputTimeSpanMap,
|
|
|
+ Map<String, List<TimeSpan>> outputdetailListMap, IDao_log dao_log) throws Exception {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ Map<String, CoreInput> inputMap = new HashMap<String, CoreInput>();
|
|
|
+ for (CoreInput input : inputList) {
|
|
|
+ inputMap.put(input.input_sign, input);
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, List<TimeSpan>> outputTimeSpanMap = new HashMap<String, List<TimeSpan>>();
|
|
|
+ for (int i = 0; i < outputList.size(); i++) {
|
|
|
+ CoreOutput output = outputList.get(i);
|
|
|
+ List<TimeSpan> timeSpanList = new ArrayList<TimeSpan>();
|
|
|
+ if (output.dao_type.endsWith("_max") || output.dao_type.endsWith("_stat")) {
|
|
|
+ List<TimeSpan> timeSpanList_input = inputTimeSpanMap.get(output.output_sign);
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList_input, output.var_stepSpanMap.get(output.output_sign), output.time_period,
|
|
|
+ output.data_class, timeSpanList);
|
|
|
+ } else {
|
|
|
+ for (String input_sign : output.all_input.keySet()) {
|
|
|
+ CoreInput input = inputMap.get(input_sign);
|
|
|
+ if (inputTimeSpanMap.containsKey(input_sign)) {
|
|
|
+ List<TimeSpan> timeSpanList_input = inputTimeSpanMap.get(input_sign);
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList_input, output.var_stepSpanMap.get(input_sign), output.time_period,
|
|
|
+ input.data_class, timeSpanList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DateUtil.Instance().Limit(timeSpanList, output.time_begin, DateUtil.Instance().GetDate_offset(output.time_end, output.time_period, -1));
|
|
|
+ List<TimeSpan> timeSpanList_finish = outputdetailListMap.get(output.output_sign);
|
|
|
+ for (int ii = timeSpanList.size() - 1; ii >= 0; ii--) {
|
|
|
+ TimeSpan timeSpan = timeSpanList.get(ii);
|
|
|
+ boolean finish = false;
|
|
|
+ if (timeSpanList_finish != null) {
|
|
|
+ for (TimeSpan timeSpanInner : timeSpanList_finish) {
|
|
|
+ if (timeSpanInner.timefrom.getTime() == timeSpan.timefrom.getTime()) {
|
|
|
+ finish = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (finish) {
|
|
|
+ timeSpanList.remove(ii);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ outputTimeSpanMap.put(output.output_sign + "_" + sdf.format(output.time_begin), timeSpanList);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < outputList.size(); i++) {
|
|
|
+ CoreOutput output = outputList.get(i);
|
|
|
+ List<TimeSpan> timeSpanList = outputTimeSpanMap.get(output.output_sign + "_" + sdf.format(output.time_begin));
|
|
|
+ for (TimeSpan timeSpan : timeSpanList) {
|
|
|
+ // 保存日志
|
|
|
+ {
|
|
|
+ JSONObject InsertObject = new JSONObject();
|
|
|
+ InsertObject.put("object_sign", object_sign);
|
|
|
+ InsertObject.put("instance_name", instance_name);
|
|
|
+ InsertObject.put("compute_round", compute_round);
|
|
|
+ InsertObject.put("output_sign", output.output_sign);
|
|
|
+ InsertObject.put("valid_time_from", sdf.format(timeSpan.timefrom));
|
|
|
+ InsertObject.put("valid_time_to", sdf.format(timeSpan.timeto));
|
|
|
+ InsertObject.put("operate_time_to", sdf.format(new Date()));
|
|
|
+ dao_log.InsertData(InsertObject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return outputTimeSpanMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Stat_inststat(String dao_type, JSONObject dao_key, List<TimeSpan> timeSpanList, String time_period, String max_time_period)
|
|
|
+ throws Exception {
|
|
|
+ if (timeSpanList.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ ImplDao_fjd_statinst dao_fjd_statinst = (ImplDao_fjd_statinst) Dao_dataManager.Instance().Get("fjd_statinst");
|
|
|
+
|
|
|
+ List<String> biggerList = Parser_time_period.getBigger(max_time_period, time_period);
|
|
|
+ List<String> allList = new ArrayList<String>();
|
|
|
+ allList.add(time_period);
|
|
|
+ allList.addAll(biggerList);
|
|
|
+ for (int i = 1; i < allList.size(); i++) {
|
|
|
+ String time_period_base = allList.get(i - 1);
|
|
|
+ String time_period_bigger = allList.get(i);
|
|
|
+ List<TimeSpan> timeSpanList_new = new ArrayList<TimeSpan>();
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList, time_period_bigger, "inststat", timeSpanList_new);
|
|
|
+ for (TimeSpan timeSpan : timeSpanList_new) {
|
|
|
+ // 准备数据
|
|
|
+ List<DoubleTimeObject_statinst> dataList = dao_fjd_statinst.GetDataListGteLte_statinst(dao_key, time_period_base, timeSpan.timefrom,
|
|
|
+ DateUtil.Instance().GetDate_offset(timeSpan.timeto, time_period_bigger, 1), false);
|
|
|
+
|
|
|
+ // 计算
|
|
|
+ List<DoubleTimeObject_statinst> outputDataList = new ArrayList<DoubleTimeObject_statinst>();
|
|
|
+ int tmp_data_index = 0;
|
|
|
+ Date tmpDate = timeSpan.timefrom;
|
|
|
+ while (tmpDate.getTime() <= timeSpan.timeto.getTime()) {
|
|
|
+ Date tmpDateNext = DateUtil.Instance().GetDate_offset(tmpDate, time_period_bigger, 1);
|
|
|
+
|
|
|
+ DoubleTimeObject_statinst stat_data = new DoubleTimeObject_statinst();
|
|
|
+ double data_total = 0.0;
|
|
|
+ int data_count = 0;
|
|
|
+ for (int index_data = tmp_data_index; index_data < dataList.size(); index_data++) {
|
|
|
+ tmp_data_index = index_data;
|
|
|
+ DoubleTimeObject_statinst data = dataList.get(index_data);
|
|
|
+ if (data.data_time.getTime() < tmpDate.getTime()) {
|
|
|
+ } else if (data.data_time.getTime() < tmpDateNext.getTime()) {
|
|
|
+ if (stat_data.data_max == null) {
|
|
|
+ stat_data.time_max = data.time_max;
|
|
|
+ stat_data.data_max = data.data_max;
|
|
|
+ } else {
|
|
|
+ if (data.data_max != null) {
|
|
|
+ if (stat_data.data_max == null || stat_data.data_max < data.data_max) {
|
|
|
+ stat_data.time_max = data.time_max;
|
|
|
+ stat_data.data_max = data.data_max;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (stat_data.data_min == null) {
|
|
|
+ stat_data.time_min = data.time_min;
|
|
|
+ stat_data.data_min = data.data_min;
|
|
|
+ } else {
|
|
|
+ if (data.data_min != null) {
|
|
|
+ if (stat_data.data_min == null || stat_data.data_min > data.data_min) {
|
|
|
+ stat_data.time_min = data.time_min;
|
|
|
+ stat_data.data_min = data.data_min;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data_total += data.data_avg * data.data_count;
|
|
|
+ data_count += data.data_count;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (data_count > 0) {
|
|
|
+ stat_data.data_time = tmpDate;
|
|
|
+ stat_data.data_avg = data_total / data_count;
|
|
|
+ stat_data.data_count = (long) data_count;
|
|
|
+ outputDataList.add(stat_data);
|
|
|
+ }
|
|
|
+
|
|
|
+ tmpDate = tmpDateNext;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除数据
|
|
|
+ dao_fjd_statinst.DeleteDataListGteLte_statinst(dao_key, time_period_bigger, timeSpan.timefrom, timeSpan.timeto);
|
|
|
+ ConstantCE.SleepBetweenDeleteAndInsert();
|
|
|
+ // 保存数据
|
|
|
+ dao_fjd_statinst.InsertDataListGteLte_statinst(dao_key, time_period_bigger, outputDataList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Stat_consumption(String dao_type, JSONObject dao_key, List<TimeSpan> timeSpanList, String time_period, String max_time_period)
|
|
|
+ throws Exception {
|
|
|
+ if (timeSpanList.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> biggerList = Parser_time_period.getBigger(max_time_period, time_period);
|
|
|
+ List<List<TimeSpan>> biggerTimeSpanList = new ArrayList<List<TimeSpan>>();
|
|
|
+ for (String bigger : biggerList) {
|
|
|
+ List<TimeSpan> bts = new ArrayList<TimeSpan>();
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList, bigger, "consumption", bts);
|
|
|
+ biggerTimeSpanList.add(bts);
|
|
|
+ }
|
|
|
+ List<TimeSpan> biggestTimeSpanList = biggerTimeSpanList.get(biggerTimeSpanList.size() - 1);
|
|
|
+ String biggest = biggerList.get(biggerList.size() - 1);
|
|
|
+ Date stat_from = biggestTimeSpanList.get(0).timefrom;
|
|
|
+ Date stat_to = DateUtil.Instance().GetDate_offset(biggestTimeSpanList.get(biggestTimeSpanList.size() - 1).timeto, biggest, 1);
|
|
|
+ List<DoubleTimeObject> dataList = Dao_dataManager.Instance().Get(dao_type).GetDataListGteLte(dao_key, time_period, stat_from, stat_to, false);
|
|
|
+ for (int index = 0; index < biggerList.size(); index++) {
|
|
|
+ String bigger = biggerList.get(index);
|
|
|
+ List<TimeSpan> bts = biggerTimeSpanList.get(index);
|
|
|
+ for (TimeSpan biggerTimeSpan : bts) {
|
|
|
+ List<DoubleTimeObject> outputDataList = new ArrayList<DoubleTimeObject>();
|
|
|
+ int tmp_data_index = 0;
|
|
|
+ Date tmpDate = biggerTimeSpan.timefrom;
|
|
|
+ while (tmpDate.getTime() <= biggerTimeSpan.timeto.getTime()) {
|
|
|
+ Date tmpDateNext = DateUtil.Instance().GetDate_offset(tmpDate, bigger, 1);
|
|
|
+
|
|
|
+ double data_value = 0.0;
|
|
|
+ int data_count = 0;
|
|
|
+ for (int index_data = tmp_data_index; index_data < dataList.size(); index_data++) {
|
|
|
+ tmp_data_index = index_data;
|
|
|
+ DoubleTimeObject data = dataList.get(index_data);
|
|
|
+ if (data.data_time.getTime() < tmpDate.getTime()) {
|
|
|
+ } else if (data.data_time.getTime() < tmpDateNext.getTime()) {
|
|
|
+ data_value += data.data_value;
|
|
|
+ data_count++;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (data_count > 0) {
|
|
|
+ DoubleTimeObject outputData = new DoubleTimeObject();
|
|
|
+ outputData.data_time = tmpDate;
|
|
|
+ outputData.data_value = data_value;
|
|
|
+ outputDataList.add(outputData);
|
|
|
+ }
|
|
|
+
|
|
|
+ tmpDate = tmpDateNext;
|
|
|
+ }
|
|
|
+ // 删除数据
|
|
|
+ Dao_dataManager.Instance().Get(dao_type).DeleteDataListGteLte(dao_key, bigger, biggerTimeSpan.timefrom, biggerTimeSpan.timeto);
|
|
|
+ ConstantCE.SleepBetweenDeleteAndInsert();
|
|
|
+ // 保存数据
|
|
|
+ Dao_dataManager.Instance().Get(dao_type).InsertDataListGteLte(dao_key, bigger, outputDataList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Stat_near(String dao_type, JSONObject dao_key, List<TimeSpan> timeSpanList, String time_period,
|
|
|
+ List<DoubleTimeObject> dataList) throws Exception {
|
|
|
+ if (timeSpanList.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (dataList.size() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> biggerList = Parser_time_period.getBigger(ConstantCE.Stat_time_period_fjd_near, time_period);
|
|
|
+ List<List<TimeSpan>> biggerTimeSpanList = new ArrayList<List<TimeSpan>>();
|
|
|
+ for (String bigger : biggerList) {
|
|
|
+ List<TimeSpan> bts = new ArrayList<TimeSpan>();
|
|
|
+ DateUtil.Instance().ConvertAndMergeTo(timeSpanList, bigger, "near", bts);
|
|
|
+ biggerTimeSpanList.add(bts);
|
|
|
+ }
|
|
|
+ for (int index = 0; index < biggerList.size(); index++) {
|
|
|
+ String bigger = biggerList.get(index);
|
|
|
+ List<TimeSpan> bts = biggerTimeSpanList.get(index);
|
|
|
+ for (TimeSpan biggerTimeSpan : bts) {
|
|
|
+ List<DoubleTimeObject> outputDataList = new ArrayList<DoubleTimeObject>();
|
|
|
+ int tmp_data_index = 0;
|
|
|
+ Date tmpDate = biggerTimeSpan.timefrom;
|
|
|
+ while (tmpDate.getTime() <= biggerTimeSpan.timeto.getTime()) {
|
|
|
+ Date tmpDateNext = DateUtil.Instance().GetDate_offset(tmpDate, bigger, 1);
|
|
|
+
|
|
|
+ double data_value = 0.0;
|
|
|
+ int data_count = 0;
|
|
|
+ for (int index_data = tmp_data_index; index_data < dataList.size(); index_data++) {
|
|
|
+ tmp_data_index = index_data;
|
|
|
+ DoubleTimeObject data = dataList.get(index_data);
|
|
|
+ if (data.data_time.getTime() < tmpDate.getTime()) {
|
|
|
+ } else if (data.data_time.getTime() == tmpDate.getTime()) {
|
|
|
+ data_value = data.data_value;
|
|
|
+ data_count++;
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (data_count > 0) {
|
|
|
+ DoubleTimeObject outputData = new DoubleTimeObject();
|
|
|
+ outputData.data_time = tmpDate;
|
|
|
+ outputData.data_value = data_value;
|
|
|
+ outputDataList.add(outputData);
|
|
|
+ }
|
|
|
+
|
|
|
+ tmpDate = tmpDateNext;
|
|
|
+ }
|
|
|
+ // 删除数据
|
|
|
+ Dao_dataManager.Instance().Get(dao_type).DeleteDataListGteLte(dao_key, bigger, biggerTimeSpan.timefrom, biggerTimeSpan.timeto);
|
|
|
+ ConstantCE.SleepBetweenDeleteAndInsert();
|
|
|
+ // 保存数据
|
|
|
+ Dao_dataManager.Instance().Get(dao_type).InsertDataListGteLte(dao_key, bigger, outputDataList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 任务加入之前就需要PreCompute
|
|
|
+ public static void Refresh(CoreInstance instance) throws Exception {
|
|
|
+ if (instance.instance_name.startsWith("fjd_0_") || "servicedata".equals(instance.instance_name)
|
|
|
+ || "servicedataoftenant".equals(instance.instance_name) || instance.instance_name.endsWith("_max")
|
|
|
+ || instance.instance_name.endsWith("_stat")) {
|
|
|
+ for (CoreOutput output : instance.outputList) {
|
|
|
+ output.var_originalMap.put(output.expression, true);
|
|
|
+ output.var_signMap.put(output.expression, true);
|
|
|
+ output.var_stepSpanMap.put(output.expression, new StepSpan(0, 0));
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<CoreInput> inputList = instance.inputList;
|
|
|
+ List<CoreOutput> outputList = instance.outputList;
|
|
|
+ Map<String, CoreInput> inputMap = new HashMap<String, CoreInput>();
|
|
|
+ for (CoreInput input : inputList) {
|
|
|
+ inputMap.put(input.input_sign, input);
|
|
|
+ }
|
|
|
+ Map<String, CoreOutput> outputMap = new HashMap<String, CoreOutput>();
|
|
|
+ for (CoreOutput output : outputList) {
|
|
|
+ outputMap.put(output.output_sign, output);
|
|
|
+ }
|
|
|
+ Map<String, Boolean> all_var = new HashMap<String, Boolean>();
|
|
|
+ for (CoreOutput output : outputList) {
|
|
|
+ if ("expression".equals(output.mode)) {
|
|
|
+ if (output.expression != null) {
|
|
|
+ try {
|
|
|
+ output.root = String2Grammar.Generate(output.expression);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Error expression:" + instance.object_sign + "_" + instance.instance_name + "\t" + output.output_sign + "\t"
|
|
|
+ + output.expression);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ RefreshByNode(inputMap, outputMap, output, output.root);
|
|
|
+ } else if ("custom".equals(output.mode)) {
|
|
|
+ for (String custom_param : output.custom_paramsMap.keySet()) {
|
|
|
+ String var_original = output.custom_paramsMap.get(custom_param);
|
|
|
+ try {
|
|
|
+ Double.parseDouble(var_original);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ String var_sign;
|
|
|
+ int var_offset;
|
|
|
+ int index = var_original.indexOf('\'');
|
|
|
+ if (index != -1) {
|
|
|
+ var_sign = var_original.substring(0, index);
|
|
|
+ var_offset = Integer.parseInt(var_original.substring(index + 1));
|
|
|
+ } else {
|
|
|
+ var_sign = var_original;
|
|
|
+ var_offset = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!output.var_originalMap.containsKey(var_original)) {
|
|
|
+ output.var_originalMap.put(var_original, true);
|
|
|
+ }
|
|
|
+ if (!output.var_signMap.containsKey(var_sign)) {
|
|
|
+ output.var_signMap.put(var_sign, true);
|
|
|
+ }
|
|
|
+ if (!output.var_stepSpanMap.containsKey(var_sign)) {
|
|
|
+ output.var_stepSpanMap.put(var_sign, new StepSpan(var_offset, var_offset));
|
|
|
+ } else {
|
|
|
+ StepSpan stepSpan = output.var_stepSpanMap.get(var_sign);
|
|
|
+ if (stepSpan.backward > var_offset) {
|
|
|
+ stepSpan.backward = var_offset;
|
|
|
+ }
|
|
|
+ if (stepSpan.forward < var_offset) {
|
|
|
+ stepSpan.forward = var_offset;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (inputMap.containsKey(var_sign)) {
|
|
|
+ CoreInput input = inputMap.get(var_sign);
|
|
|
+ if (!output.time_period_intialized) {
|
|
|
+ if (DateUtil.Instance().compare(output.time_period, input.time_period) < 0) {
|
|
|
+ output.time_period = input.time_period;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new Exception("output.mode error:" + output.mode);
|
|
|
+ }
|
|
|
+ for (String input_sign : output.var_signMap.keySet()) {
|
|
|
+ if (!all_var.containsKey(input_sign)) {
|
|
|
+ all_var.put(input_sign, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Integer> removeList = new ArrayList<Integer>();
|
|
|
+ for (int i = 0; i < inputList.size(); i++) {
|
|
|
+ CoreInput input = inputList.get(i);
|
|
|
+ if (!all_var.containsKey(input.input_sign)) {
|
|
|
+ removeList.add(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = removeList.size() - 1; i >= 0; i--) {
|
|
|
+ inputList.remove(removeList.get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void RefreshByNode(Map<String, CoreInput> inputMap, Map<String, CoreOutput> outputMap, CoreOutput output, GrammarTreeNode node)
|
|
|
+ throws Exception {
|
|
|
+ if ("const".equals(node.type)) {
|
|
|
+ } else if ("var".equals(node.type)) {
|
|
|
+ if (!output.var_originalMap.containsKey(node.var_original)) {
|
|
|
+ output.var_originalMap.put(node.var_original, true);
|
|
|
+ }
|
|
|
+ if (!output.var_signMap.containsKey(node.var_sign)) {
|
|
|
+ output.var_signMap.put(node.var_sign, true);
|
|
|
+ }
|
|
|
+ if (!inputMap.containsKey(node.var_sign) && !outputMap.containsKey(node.var_sign)) {
|
|
|
+ throw new Exception("var not exist in input:" + node.var_sign);
|
|
|
+ }
|
|
|
+ if (!output.var_stepSpanMap.containsKey(node.var_sign)) {
|
|
|
+ output.var_stepSpanMap.put(node.var_sign, new StepSpan(node.var_offset, node.var_offset));
|
|
|
+ } else {
|
|
|
+ StepSpan stepSpan = output.var_stepSpanMap.get(node.var_sign);
|
|
|
+ if (stepSpan.backward > node.var_offset) {
|
|
|
+ stepSpan.backward = node.var_offset;
|
|
|
+ }
|
|
|
+ if (stepSpan.forward < node.var_offset) {
|
|
|
+ stepSpan.forward = node.var_offset;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (inputMap.containsKey(node.var_sign)) {
|
|
|
+ CoreInput input = inputMap.get(node.var_sign);
|
|
|
+ if (!output.time_period_intialized) {
|
|
|
+ if (DateUtil.Instance().compare(output.time_period, input.time_period) < 0) {
|
|
|
+ output.time_period = input.time_period;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if ("other".equals(node.type)) {
|
|
|
+ output.other_paramMap.put(node.other, true);
|
|
|
+ } else if ("evaluate".equals(node.type)) {
|
|
|
+ for (GrammarTreeNode nodeInner : node.evaluateItemList) {
|
|
|
+ RefreshByNode(inputMap, outputMap, output, nodeInner);
|
|
|
+ }
|
|
|
+ } else if ("compare".equals(node.type)) {
|
|
|
+ RefreshByCompare(inputMap, outputMap, output, node.compare);
|
|
|
+ RefreshByNode(inputMap, outputMap, output, node.compare_yes_node);
|
|
|
+ RefreshByNode(inputMap, outputMap, output, node.compare_no_node);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void RefreshByCompare(Map<String, CoreInput> inputMap, Map<String, CoreOutput> outputMap, CoreOutput output,
|
|
|
+ GrammarCompare compare) throws Exception {
|
|
|
+ if ("itom".equals(compare.type)) {
|
|
|
+ RefreshByNode(inputMap, outputMap, output, compare.itom_left);
|
|
|
+ RefreshByNode(inputMap, outputMap, output, compare.itom_right);
|
|
|
+ } else if ("group".equals(compare.type)) {
|
|
|
+ for (GrammarCompare compareInner : compare.groupItemList) {
|
|
|
+ RefreshByCompare(inputMap, outputMap, output, compareInner);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static double ComputeNode(GrammarTreeNode node, Map<String, Double> dataMap) throws Exception {
|
|
|
+ if ("const".equals(node.type)) {
|
|
|
+ return node.const_value;
|
|
|
+ } else if ("var".equals(node.type)) {
|
|
|
+ Double value = dataMap.get(node.var_original);
|
|
|
+ if (value != null) {
|
|
|
+ return value;
|
|
|
+ } else {
|
|
|
+ return 0.0;
|
|
|
+ }
|
|
|
+ } else if ("other".equals(node.type)) {
|
|
|
+ Double value = dataMap.get(node.other);
|
|
|
+ if (value != null) {
|
|
|
+ return value;
|
|
|
+ } else {
|
|
|
+ return 0.0;
|
|
|
+ }
|
|
|
+ } else if ("evaluate".equals(node.type)) {
|
|
|
+ List<Double> children = new ArrayList<Double>();
|
|
|
+ for (GrammarTreeNode nodeInner : node.evaluateItemList) {
|
|
|
+ double child = ComputeNode(nodeInner, dataMap);
|
|
|
+ children.add(child);
|
|
|
+ }
|
|
|
+ Double result = null;
|
|
|
+ if ("+".equals(node.evaluate)) {
|
|
|
+ result = 0.0;
|
|
|
+ for (Double child : children) {
|
|
|
+ result += child;
|
|
|
+ }
|
|
|
+ } else if ("-".equals(node.evaluate)) {
|
|
|
+ result = children.get(0) - children.get(1);
|
|
|
+ } else if ("*".equals(node.evaluate)) {
|
|
|
+ result = 1.0;
|
|
|
+ for (Double child : children) {
|
|
|
+ result *= child;
|
|
|
+ }
|
|
|
+ } else if ("/".equals(node.evaluate)) {
|
|
|
+ result = children.get(0) / children.get(1);
|
|
|
+ } else if ("max".equals(node.evaluate)) {
|
|
|
+ for (Double child : children) {
|
|
|
+ if (result == null) {
|
|
|
+ result = child;
|
|
|
+ } else {
|
|
|
+ if (result < child) {
|
|
|
+ result = child;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if ("min".equals(node.evaluate)) {
|
|
|
+ result = 1.0;
|
|
|
+ for (Double child : children) {
|
|
|
+ if (result == null) {
|
|
|
+ result = child;
|
|
|
+ } else {
|
|
|
+ if (result > child) {
|
|
|
+ result = child;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (MathUtil.Instance().diy_funname(MathUtil.Instance().fun0p, node.evaluate)) {
|
|
|
+ result = MathUtil.Instance().fun0p_value(node.evaluate);
|
|
|
+ } else if (MathUtil.Instance().diy_funname(MathUtil.Instance().fun1p, node.evaluate)) {
|
|
|
+ result = MathUtil.Instance().fun1p_value(node.evaluate, children.get(0));
|
|
|
+ } else if (MathUtil.Instance().diy_funname(MathUtil.Instance().fun2p, node.evaluate)) {
|
|
|
+ result = MathUtil.Instance().fun2p_value(node.evaluate, children.get(0), children.get(1));
|
|
|
+ } else {
|
|
|
+ throw new Exception("operator error:" + node.evaluate);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ } else if ("compare".equals(node.type)) {
|
|
|
+ Double result;
|
|
|
+ boolean com = Compute_compare(node.compare, dataMap);
|
|
|
+ if (com) {
|
|
|
+ result = ComputeNode(node.compare_yes_node, dataMap);
|
|
|
+ } else {
|
|
|
+ result = ComputeNode(node.compare_no_node, dataMap);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ throw new Exception("type error:" + node.type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean Compute_compare(GrammarCompare compare, Map<String, Double> dataMap) throws Exception {
|
|
|
+ if ("itom".equals(compare.type)) {
|
|
|
+ double left = ComputeNode(compare.itom_left, dataMap);
|
|
|
+ double right = ComputeNode(compare.itom_right, dataMap);
|
|
|
+ return MathUtil.Instance().cmp_value(compare.itom_op, left, right);
|
|
|
+ } else if ("group".equals(compare.type)) {
|
|
|
+ if ("&&".equals(compare.group_op)) {
|
|
|
+ boolean all_yes = true;
|
|
|
+ for (GrammarCompare compareInner : compare.groupItemList) {
|
|
|
+ if (!Compute_compare(compareInner, dataMap)) {
|
|
|
+ all_yes = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return all_yes;
|
|
|
+ } else if ("||".equals(compare.group_op)) {
|
|
|
+ boolean exist_yes = false;
|
|
|
+ for (GrammarCompare compareInner : compare.groupItemList) {
|
|
|
+ if (Compute_compare(compareInner, dataMap)) {
|
|
|
+ exist_yes = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return exist_yes;
|
|
|
+ } else if ("!".equals(compare.group_op)) {
|
|
|
+ return !Compute_compare(compare.groupItemList.get(0), dataMap);
|
|
|
+ } else {
|
|
|
+ throw new Exception("compare group_op error:" + compare.group_op);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new Exception("compare type error:" + compare.type);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|