From 419b219d6c7bdf2ebcfcb756b7a17dfb39ae43ac Mon Sep 17 00:00:00 2001 From: zack Date: Sat, 24 Aug 2019 00:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B4=A8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/cal/CalculatorKit.java | 6 +- .../java/com/fanruan/api/cal/FormulaKit.java | 17 ++--- .../fanruan/api/function/shell/FineFunc.java | 48 ++++-------- .../fanruan/api/report/analy/AnalyKit.java | 66 +++++++++++++++- .../api/report/analy/AnalyKitHelper.java | 75 ------------------- .../com/fanruan/api/util/TransmissionKit.java | 4 +- .../util/{shell => trans}/BaseSmsBody.java | 2 +- .../util/{shell => trans}/BatchSmsBody.java | 2 +- .../api/util/{shell => trans}/EmailBody.java | 2 +- .../util/{shell => trans}/SingleSmsBody.java | 2 +- .../com/fanruan/api/cal/FormulaKitTest.java | 2 +- .../fanruan/api/function/FunctionKitTest.java | 2 +- .../fanruan/api/util/TransmissionKitTest.java | 4 +- 13 files changed, 96 insertions(+), 136 deletions(-) delete mode 100644 src/main/java/com/fanruan/api/report/analy/AnalyKitHelper.java rename src/main/java/com/fanruan/api/util/{shell => trans}/BaseSmsBody.java (92%) rename src/main/java/com/fanruan/api/util/{shell => trans}/BatchSmsBody.java (98%) rename src/main/java/com/fanruan/api/util/{shell => trans}/EmailBody.java (99%) rename src/main/java/com/fanruan/api/util/{shell => trans}/SingleSmsBody.java (99%) diff --git a/src/main/java/com/fanruan/api/cal/CalculatorKit.java b/src/main/java/com/fanruan/api/cal/CalculatorKit.java index fead792..e86e410 100644 --- a/src/main/java/com/fanruan/api/cal/CalculatorKit.java +++ b/src/main/java/com/fanruan/api/cal/CalculatorKit.java @@ -29,7 +29,7 @@ public class CalculatorKit { /** * 创建一个基础算子(基础算子仅支持函数计算.比如SUM()函数) * - * @return 算子 + * @return 算子对象 */ public static CalculatorProvider createCalculator() { return Calculator.createCalculator(); @@ -54,7 +54,7 @@ public class CalculatorKit { * * @param sessionID 模板的sessionID * @param paraMap 其他参数 - * @return + * @return 算子对象 */ public static CalculatorProvider createCalculator(@Nullable String sessionID, @Nullable Map paraMap) { ParameterMapNameSpace nameSpace = ParameterMapNameSpace.create(paraMap); @@ -80,7 +80,7 @@ public class CalculatorKit { /** * 返回服务器数据集的算子空间(可以通过调用calculator.pushNameSpace()将算子空间塞进算子实例,从而支持服务器数据集相关的函数计算) - * @return + * @return 算子空间对象 */ public static NameSpace getServerTableDataNameSpace(){ return TableDataNameSpace.getInstance(); diff --git a/src/main/java/com/fanruan/api/cal/FormulaKit.java b/src/main/java/com/fanruan/api/cal/FormulaKit.java index c32acc4..4e7c1ce 100644 --- a/src/main/java/com/fanruan/api/cal/FormulaKit.java +++ b/src/main/java/com/fanruan/api/cal/FormulaKit.java @@ -3,19 +3,16 @@ package com.fanruan.api.cal; import com.fanruan.api.err.KitError; import com.fr.base.BaseFormula; import com.fr.log.FineLoggerFactory; -import com.fr.parser.FRLexer; -import com.fr.parser.FRParser; import com.fr.script.Calculator; import com.fr.stable.FormulaProvider; import com.fr.stable.StringUtils; import com.fr.stable.UtilEvalError; import com.fr.stable.script.CalculatorProvider; import com.fr.stable.script.Expression; +import com.fr.third.antlr.ANTLRException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.StringReader; - /** * @author richie * @version 10.0 @@ -64,24 +61,20 @@ public class FormulaKit { /** * 检查公式内容合法性 * - * @param content 公式文本(公式的开头等号要先去掉,如果想校验等号开头的内容需要传Formula对象) + * @param content 公式文本 * @return 如果非空且不合法返回false, 反之返回true */ public static boolean checkFormulaContent(String content) { String formulaText = content.trim(); if (StringUtils.isNotEmpty(formulaText)) { - StringReader in = new StringReader(formulaText); - FRLexer lexer = new FRLexer(in); - FRParser parser = new FRParser(lexer); - + FormulaProvider formula = newFormula(formulaText); Expression expression = null; try { - expression = parser.parse(); - } catch (Exception e) { + expression = formula.parse(Calculator.createCalculator()); + } catch (ANTLRException e) { FineLoggerFactory.getLogger().error(e.getMessage(), e); } - return null != expression; } return true; diff --git a/src/main/java/com/fanruan/api/function/shell/FineFunc.java b/src/main/java/com/fanruan/api/function/shell/FineFunc.java index c66e17a..06f8ead 100644 --- a/src/main/java/com/fanruan/api/function/shell/FineFunc.java +++ b/src/main/java/com/fanruan/api/function/shell/FineFunc.java @@ -1,46 +1,24 @@ package com.fanruan.api.function.shell; -import com.fr.stable.StringUtils; +import com.fr.stable.script.FunctionDef; /** - * 自定义函数封装 + * ClassName FineFunc + * + * @Author zack + * @Date 2019/8/23 + * @Version 10.0 + * 自定义函数实体 */ -public class FineFunc { - private String name;//函数名 - private String description;//描述 - private String className;//完整类名 - - public FineFunc(String name, String description, String className) { - this.name = name; - this.description = description; - this.className = className; - } - - public FineFunc(String name, String className) { - this(name, StringUtils.EMPTY, className); +public class FineFunc extends FunctionDef { + public FineFunc() { } - public String getName() { - return name; + public FineFunc(String name, String descrption) { + super(name, descrption); } - public void setName(String name) { - this.name = name; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getClassName() { - return className; - } - - public void setClassName(String className) { - this.className = className; + public FineFunc(String name, String description, String className) { + super(name, description, className); } } \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/report/analy/AnalyKit.java b/src/main/java/com/fanruan/api/report/analy/AnalyKit.java index e1f4da8..266b375 100644 --- a/src/main/java/com/fanruan/api/report/analy/AnalyKit.java +++ b/src/main/java/com/fanruan/api/report/analy/AnalyKit.java @@ -1,9 +1,14 @@ package com.fanruan.api.report.analy; import com.fanruan.api.report.analy.data.TreeNode; +import com.fr.cache.list.IntList; +import com.fr.form.ui.Widget; import com.fr.main.workbook.ResultWorkBook; +import com.fr.report.cell.ResultCellElement; +import com.fr.report.cell.WidgetAttrElem; import com.fr.report.report.Report; import com.fr.report.report.ResultReport; +import com.fr.report.web.button.form.TreeNodeToggleButton; import com.fr.report.worksheet.AnalysisRWorkSheet; import com.fr.script.Calculator; import com.fr.web.core.TreeHTMLWriter; @@ -11,7 +16,10 @@ import com.fr.web.core.reportcase.WebElementReportCase; import com.fr.web.output.html.chwriter.ViewCellWriter; import com.fr.web.request.EmptyRepository; +import java.util.HashMap; +import java.util.Iterator; import java.util.Map; + /** * @ClassName AnalyKit * @Author zack @@ -42,6 +50,62 @@ public class AnalyKit { htmlWriter.writeReportToHtml(new WebElementReportCase(analysisRWorkSheet, new EmptyRepository()), 1, cellHtmlWriter, new EmptyRepository(), ""); cellHtmlWriter.dealWithAllTreeNodeRelation(c); - return AnalyKitHelper.generateNodeTree(analysisRWorkSheet); + return generateNodeTree(analysisRWorkSheet); + } + + private static Map generateNodeTree(AnalysisRWorkSheet resultWS) { + int rowSize = resultWS.getRowCount(); + Map nodeMap = new HashMap(); + + for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {//遍历行 + ResultCellElement treeNodeCell = findToggleCell(resultWS, rowIndex); + if (treeNodeCell != null) { + Widget widget = ((WidgetAttrElem) treeNodeCell).getWidget(); + IntList sonList = ((TreeNodeToggleButton) widget).getRelativeIndexList(); + if (sonList != null && sonList.size() > 1) { + if (sonList.get(0) == -1) { + //折叠行 + if (nodeMap.containsKey(treeNodeCell.getRow())) { + continue; + } + buildNodeMap(resultWS, treeNodeCell, nodeMap, -1); + } else { + //折叠列 暂不处理 + } + } + } + } + return nodeMap; + } + + private static ResultCellElement findToggleCell(AnalysisRWorkSheet reportCase, int rowIndex) { + Iterator cellIterator = reportCase.getRow(rowIndex); + while (cellIterator.hasNext()) { + ResultCellElement tmpCell = (ResultCellElement) cellIterator.next(); + if (tmpCell instanceof WidgetAttrElem) { + if (((WidgetAttrElem) tmpCell).getWidget() instanceof TreeNodeToggleButton) { + return tmpCell; + } + } + } + return null; + } + + private static void buildNodeMap(AnalysisRWorkSheet reportCase, ResultCellElement cellElment, Map nodeMap, int parent) { + if (cellElment == null) { + return; + } + TreeNodeToggleButton toggleButton = (TreeNodeToggleButton) ((WidgetAttrElem) cellElment).getWidget(); + int self = cellElment.getRow(); + IntList sonList = toggleButton.getRelativeIndexList(); + if (sonList != null && sonList.size() > 1) { + if (sonList.get(0) == -1) {//折叠行 + nodeMap.put(self, new TreeNode(self, parent, sonList)); + int size = sonList.size(); + for (int i = 0; i < size; i++) { + buildNodeMap(reportCase, findToggleCell(reportCase, sonList.get(i)), nodeMap, self); + } + } + } } } \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/report/analy/AnalyKitHelper.java b/src/main/java/com/fanruan/api/report/analy/AnalyKitHelper.java deleted file mode 100644 index 96f0f07..0000000 --- a/src/main/java/com/fanruan/api/report/analy/AnalyKitHelper.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.fanruan.api.report.analy; - -import com.fanruan.api.report.analy.data.TreeNode; -import com.fr.cache.list.IntList; -import com.fr.form.ui.Widget; -import com.fr.report.cell.ResultCellElement; -import com.fr.report.cell.WidgetAttrElem; -import com.fr.report.web.button.form.TreeNodeToggleButton; -import com.fr.report.worksheet.AnalysisRWorkSheet; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -/** - * 数据分析相关的业务类,此类是为了接盘AnalyKit中的庞杂的私有方法,增强AnalyKit的可读性 - */ -public class AnalyKitHelper { - - public static Map generateNodeTree(AnalysisRWorkSheet resultWS) { - int rowSize = resultWS.getRowCount(); - Map nodeMap = new HashMap(); - - for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {//遍历行 - ResultCellElement treeNodeCell = findToggleCell(resultWS, rowIndex); - if (treeNodeCell != null) { - Widget widget = ((WidgetAttrElem) treeNodeCell).getWidget(); - IntList sonList = ((TreeNodeToggleButton) widget).getRelativeIndexList(); - if (sonList != null && sonList.size() > 1) { - if (sonList.get(0) == -1) { - //折叠行 - if (nodeMap.containsKey(treeNodeCell.getRow())) { - continue; - } - buildNodeMap(resultWS, treeNodeCell, nodeMap, -1); - } else { - //折叠列 暂不处理 - } - } - } - } - return nodeMap; - } - - private static ResultCellElement findToggleCell(AnalysisRWorkSheet reportCase, int rowIndex) { - Iterator cellIterator = reportCase.getRow(rowIndex); - while (cellIterator.hasNext()) { - ResultCellElement tmpCell = (ResultCellElement) cellIterator.next(); - if (tmpCell instanceof WidgetAttrElem) { - if (((WidgetAttrElem) tmpCell).getWidget() instanceof TreeNodeToggleButton) { - return tmpCell; - } - } - } - return null; - } - - private static void buildNodeMap(AnalysisRWorkSheet reportCase, ResultCellElement cellElment, Map nodeMap, int parent) { - if (cellElment == null) { - return; - } - TreeNodeToggleButton toggleButton = (TreeNodeToggleButton) ((WidgetAttrElem) cellElment).getWidget(); - int self = cellElment.getRow(); - IntList sonList = toggleButton.getRelativeIndexList(); - if (sonList != null && sonList.size() > 1) { - if (sonList.get(0) == -1) {//折叠行 - nodeMap.put(self, new TreeNode(self, parent, sonList)); - int size = sonList.size(); - for (int i = 0; i < size; i++) { - buildNodeMap(reportCase, findToggleCell(reportCase, sonList.get(i)), nodeMap, self); - } - } - } - } -} \ No newline at end of file diff --git a/src/main/java/com/fanruan/api/util/TransmissionKit.java b/src/main/java/com/fanruan/api/util/TransmissionKit.java index f145198..6a310f0 100644 --- a/src/main/java/com/fanruan/api/util/TransmissionKit.java +++ b/src/main/java/com/fanruan/api/util/TransmissionKit.java @@ -1,7 +1,7 @@ package com.fanruan.api.util; -import com.fanruan.api.util.shell.BaseSmsBody; -import com.fanruan.api.util.shell.EmailBody; +import com.fanruan.api.util.trans.BaseSmsBody; +import com.fanruan.api.util.trans.EmailBody; import com.fr.base.EmailManager; import com.fr.base.sms.SMSManager; diff --git a/src/main/java/com/fanruan/api/util/shell/BaseSmsBody.java b/src/main/java/com/fanruan/api/util/trans/BaseSmsBody.java similarity index 92% rename from src/main/java/com/fanruan/api/util/shell/BaseSmsBody.java rename to src/main/java/com/fanruan/api/util/trans/BaseSmsBody.java index 863abb4..0ae2fd5 100644 --- a/src/main/java/com/fanruan/api/util/shell/BaseSmsBody.java +++ b/src/main/java/com/fanruan/api/util/trans/BaseSmsBody.java @@ -1,4 +1,4 @@ -package com.fanruan.api.util.shell; +package com.fanruan.api.util.trans; /** * 抽象短信体 diff --git a/src/main/java/com/fanruan/api/util/shell/BatchSmsBody.java b/src/main/java/com/fanruan/api/util/trans/BatchSmsBody.java similarity index 98% rename from src/main/java/com/fanruan/api/util/shell/BatchSmsBody.java rename to src/main/java/com/fanruan/api/util/trans/BatchSmsBody.java index 8c3437a..005e3aa 100644 --- a/src/main/java/com/fanruan/api/util/shell/BatchSmsBody.java +++ b/src/main/java/com/fanruan/api/util/trans/BatchSmsBody.java @@ -1,4 +1,4 @@ -package com.fanruan.api.util.shell; +package com.fanruan.api.util.trans; import com.fr.base.sms.SMSManager; import com.fr.json.JSONArray; diff --git a/src/main/java/com/fanruan/api/util/shell/EmailBody.java b/src/main/java/com/fanruan/api/util/trans/EmailBody.java similarity index 99% rename from src/main/java/com/fanruan/api/util/shell/EmailBody.java rename to src/main/java/com/fanruan/api/util/trans/EmailBody.java index dea730c..968bf3e 100644 --- a/src/main/java/com/fanruan/api/util/shell/EmailBody.java +++ b/src/main/java/com/fanruan/api/util/trans/EmailBody.java @@ -1,4 +1,4 @@ -package com.fanruan.api.util.shell; +package com.fanruan.api.util.trans; import com.fr.base.EmailAttachment; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/com/fanruan/api/util/shell/SingleSmsBody.java b/src/main/java/com/fanruan/api/util/trans/SingleSmsBody.java similarity index 99% rename from src/main/java/com/fanruan/api/util/shell/SingleSmsBody.java rename to src/main/java/com/fanruan/api/util/trans/SingleSmsBody.java index eae5aec..8c9b872 100644 --- a/src/main/java/com/fanruan/api/util/shell/SingleSmsBody.java +++ b/src/main/java/com/fanruan/api/util/trans/SingleSmsBody.java @@ -1,4 +1,4 @@ -package com.fanruan.api.util.shell; +package com.fanruan.api.util.trans; import com.fr.base.sms.SMSManager; import com.fr.json.JSONObject; diff --git a/src/test/java/com/fanruan/api/cal/FormulaKitTest.java b/src/test/java/com/fanruan/api/cal/FormulaKitTest.java index 997712c..c54ec7f 100644 --- a/src/test/java/com/fanruan/api/cal/FormulaKitTest.java +++ b/src/test/java/com/fanruan/api/cal/FormulaKitTest.java @@ -50,7 +50,7 @@ public class FormulaKitTest extends Prepare { assertEquals(true, FormulaKit.checkFormulaContent("")); - assertEquals(false, FormulaKit.checkFormulaContent(("=SUM(1,1)"))); + assertEquals(true, FormulaKit.checkFormulaContent(("=SUM(1,1)"))); assertEquals(true, FormulaKit.checkFormulaContent(("SUM(1,1)"))); diff --git a/src/test/java/com/fanruan/api/function/FunctionKitTest.java b/src/test/java/com/fanruan/api/function/FunctionKitTest.java index f49cb0c..515d81b 100644 --- a/src/test/java/com/fanruan/api/function/FunctionKitTest.java +++ b/src/test/java/com/fanruan/api/function/FunctionKitTest.java @@ -33,7 +33,7 @@ public class FunctionKitTest extends Prepare { public void testFuncRemove() { FunctionKit.removeFunc("com.fanruan.api.function.TestFunc"); FunctionKit.addFunc(new FineFunc("TestFunc", "com.fanruan.api.function.TestFunc")); - FunctionKit.removeFunc("com.fr.sdk.FuncTest"); + FunctionKit.removeFunc("com.fanruan.api.function.TestFunc"); String result = "ERROR"; try { result = Calculator.createCalculator().evalValue("TESTFUNC()").toString(); diff --git a/src/test/java/com/fanruan/api/util/TransmissionKitTest.java b/src/test/java/com/fanruan/api/util/TransmissionKitTest.java index 71db8f0..b0e0c24 100644 --- a/src/test/java/com/fanruan/api/util/TransmissionKitTest.java +++ b/src/test/java/com/fanruan/api/util/TransmissionKitTest.java @@ -1,7 +1,7 @@ package com.fanruan.api.util; -import com.fanruan.api.util.shell.BatchSmsBody; -import com.fanruan.api.util.shell.SingleSmsBody; +import com.fanruan.api.util.trans.BatchSmsBody; +import com.fanruan.api.util.trans.SingleSmsBody; import org.junit.Test; import static org.junit.Assert.assertTrue;