|
@@ -0,0 +1,121 @@
|
|
|
+package com.gyee.gaia.meter.util;
|
|
|
+
|
|
|
+import groovy.lang.Binding;
|
|
|
+import groovy.lang.GroovyShell;
|
|
|
+import groovy.lang.Script;
|
|
|
+import org.codehaus.groovy.control.CompilerConfiguration;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import com.gyee.gaia.meter.entity.MeterPoint;
|
|
|
+
|
|
|
+public class TestScriptShell {
|
|
|
+
|
|
|
+ private static final GroovyShell shell;
|
|
|
+
|
|
|
+ private static ConcurrentHashMap<String, Script> cache = new ConcurrentHashMap<String, Script>();
|
|
|
+
|
|
|
+ static {
|
|
|
+ CompilerConfiguration cfg = new CompilerConfiguration();
|
|
|
+ cfg.setScriptBaseClass(AlarmScript.class.getName());
|
|
|
+
|
|
|
+ shell = new GroovyShell(cfg);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static final String entityIdName = "entityId";
|
|
|
+ public static final String variableName = "variable";
|
|
|
+ public static final String tsDataName = "tsData";
|
|
|
+
|
|
|
+ // private static ConcurrentHashMap<Long,ConcurrentHashMap<String, TsData>> tsDataCache = new ConcurrentHashMap<Long,ConcurrentHashMap<String, TsData>>();
|
|
|
+
|
|
|
+ public static Object parseExpr(String expr) {
|
|
|
+ Script s = getScriptFromCache(expr);
|
|
|
+ return s.run();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object parseExpr(String expr, Map<String, Object> map) {
|
|
|
+ Binding binding = new Binding(map);
|
|
|
+ Script script = getScriptFromCache(expr);
|
|
|
+ script.setBinding(binding);
|
|
|
+ return script.run();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Script getScriptFromCache(String expr) {
|
|
|
+ if (cache.containsKey(expr)) {
|
|
|
+ return cache.get(expr);
|
|
|
+ }
|
|
|
+ Script script = shell.parse(expr);
|
|
|
+ cache.put(expr, script);
|
|
|
+ return script;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ MeterPoint mp1 = new MeterPoint();
|
|
|
+ mp1.setNemCode("MHSF_ZHCYDL_P1_FJC");
|
|
|
+ MeterPoint mp2 = new MeterPoint();
|
|
|
+ mp2.setNemCode("MHSF_ZHCYDL_P2_FJC");
|
|
|
+ MeterPoint mp3 = new MeterPoint();
|
|
|
+ mp3.setNemCode("MHSF_ZHCYDL_P0_FJC");
|
|
|
+ mp3.setFormula("MHSF_ZHCYDL_P1_FJC + MHSF_ZHCYDL_P2_FJC");
|
|
|
+
|
|
|
+
|
|
|
+ Map<String, Object> row = new HashMap<String, Object>();
|
|
|
+ row.put(mp1.getNemCode(), getMeterpointValue(mp1.getMeterCode()));
|
|
|
+ row.put(mp2.getNemCode(), getMeterpointValue(mp2.getMeterCode()));
|
|
|
+
|
|
|
+ Object obj1 = parseExpr(mp3.getFormula(), row);
|
|
|
+ System.out.println("发电量=" + obj1);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private static double getMeterpointValue(String meterPointCode) {
|
|
|
+ return 19999;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main1(String[] args) {
|
|
|
+ Map<String, Object> row = new HashMap<String, Object>();
|
|
|
+ row.put("ai1", 13);
|
|
|
+ row.put("ai2", 2.1);
|
|
|
+ row.put("di1", false);
|
|
|
+
|
|
|
+// DoubleTsData tsd = new DoubleTsData(1,(short)1,335.98);
|
|
|
+// row.put("AI128", tsd);
|
|
|
+
|
|
|
+ //double a = Math.max()
|
|
|
+
|
|
|
+// Object obj = parseExpr("AI128",row);
|
|
|
+// System.out.println(obj);
|
|
|
+ Object obj1 = parseExpr("Math.max(ai1, ai2)", row);
|
|
|
+ System.out.println(obj1);
|
|
|
+ Object obj2 = parseExpr("ai1 > ai2 && di1", row);
|
|
|
+ System.out.println(obj2);
|
|
|
+ Object obj3 = parseExpr("dataSub(ai1,ai2)", row);
|
|
|
+ System.out.println(obj3);
|
|
|
+ Object obj4 = parseExpr("dataSub(ai2,ai1)", row);
|
|
|
+ System.out.println(obj4);
|
|
|
+
|
|
|
+ Map<String, Object> row1 = new HashMap<String, Object>();
|
|
|
+ row1.put("FDLI", 13000);
|
|
|
+ row1.put("FDLII", 5678);
|
|
|
+ //ZFDL = FDLI/1000 + FDLII*0.5
|
|
|
+ Object obj11 = parseExpr("FDLI/1000 + FDLII*0.5", row1);
|
|
|
+ System.out.println(obj11);
|
|
|
+ }
|
|
|
+
|
|
|
+ //eg1: fun1(var1)
|
|
|
+ //eg2: fun2(var1+var2*var3,const1)
|
|
|
+ //eg3: fun3(Math.fun(exp1, exp2...), exp)
|
|
|
+ //eg4: fun4(fun3, fun2,fun1)
|
|
|
+ //eg5: fun5(fun4, fun3)
|
|
|
+
|
|
|
+ //ar: varlist,funlist, scriptMap
|
|
|
+ // 函数内变量全部用引号括起
|
|
|
+ //wt: arlist, taglist
|
|
|
+ //tagInfo:
|
|
|
+
|
|
|
+}
|