From 0d7bc202bd673db07c453967786be62fecdcb054 Mon Sep 17 00:00:00 2001 From: hspcadmin Date: Wed, 9 Aug 2017 09:41:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9update=EF=BC=8C=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81lamdba?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../function/DataSetUpdateFunction.java | 127 +++++---- .../tinyscript/dataset/util/DataSetUtil.java | 259 ++++++++++-------- 2 files changed, 218 insertions(+), 168 deletions(-) diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUpdateFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUpdateFunction.java index 7596db9..bb8636a 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUpdateFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUpdateFunction.java @@ -13,101 +13,124 @@ import org.tinygroup.tinyscript.dataset.GroupDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; import org.tinygroup.tinyscript.impl.DefaultScriptContext; +import org.tinygroup.tinyscript.interpret.LambdaFunction; +import org.tinygroup.tinyscript.interpret.ScriptResult; +import org.tinygroup.tinyscript.interpret.call.FunctionCallExpressionParameter; public class DataSetUpdateFunction extends AbstractScriptFunction { public String getNames() { return "update"; } - + public String getBindingTypes() { return DataSet.class.getName(); } - - public boolean enableExpressionParameter(){ + + public boolean enableExpressionParameter() { return true; } - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - try{ + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + try { if (parameters == null || parameters.length == 0) { throw new ScriptException("update函数的参数为空!"); - }else if( checkParameters(parameters, 3) ){ + } else if (checkParameters(parameters, 3)) { AbstractDataSet dataSet = (AbstractDataSet) getValue(parameters[0]); - String colName = (String) getValue(parameters[1]); - return updateExpression(dataSet,colName,getExpression(parameters[2]),context); - }else{ + String colName = (String) getValue(parameters[1]); + + return updateExpression(dataSet, colName, parameters[2], context); + } else { throw new ScriptException("update函数的参数格式不正确!"); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { throw new ScriptException("update函数的参数格式不正确!", e); } } - - private DataSet updateExpression(AbstractDataSet dataSet,String colName,String expression,ScriptContext context) throws ScriptException { + + private DataSet updateExpression(AbstractDataSet dataSet, String colName, Object parameter, ScriptContext context) + throws ScriptException { + String expression = null; + try { + expression = getExpression(parameter); int col = DataSetUtil.getFieldIndex(dataSet, colName); - if(col<0){ - throw new ScriptException(String.format("根据字段%s没有在数据集找到匹配列", colName)); + if (col < 0) { + throw new ScriptException(String.format("根据字段%s没有在数据集找到匹配列", colName)); } - - //获取需要数组处理的字段 - Set columns = DataSetUtil.getFieldArray(dataSet, expression); - - //转换表达式为脚本可以执行的语法片段 + + Set columns = null; + + // 获取需要数组处理的字段 + columns = DataSetUtil.getFieldArray(dataSet, expression); + + // 转换表达式为脚本可以执行的语法片段 expression = DataSetUtil.convertExpression(expression); - - - if(dataSet instanceof GroupDataSet){ + + Object function = null; + if (expression.startsWith("(")) + function = ((FunctionCallExpressionParameter) (parameter)).eval(); + else + function = expression; + + if (dataSet instanceof GroupDataSet) { GroupDataSet groupDataSet = (GroupDataSet) dataSet; - for(DynamicDataSet subDs:groupDataSet.getGroups()){ - updateRowWithExpression(subDs,col,expression,columns,context); + for (DynamicDataSet subDs : groupDataSet.getGroups()) { + updateRowWithExpression(subDs, col, function, columns, context); } - }else{ - updateRowWithExpression(dataSet,col,expression,columns,context); + } else { + updateRowWithExpression(dataSet, col, function, columns, context); } - - return dataSet; - + + return dataSet; + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("执行update函数发生异常",e); + throw new ScriptException("执行update函数发生异常", e); } } - - //参数为实际下标 - private void updateRowWithExpression(AbstractDataSet dataSet,int col,String expression,Set columns,ScriptContext context) throws Exception{ - for(int i=0;i columns, + ScriptContext context) throws Exception { + for (int i = 0; i < dataSet.getRows(); i++) { ScriptContext subContext = new DefaultScriptContext(); subContext.setParent(context); subContext.put("$currentRow", dataSet.getShowIndex(i)); - setRowValue(subContext,dataSet,columns,i); + setRowValue(subContext, dataSet, columns, i); Object value; - try{ - value = executeDynamicObject(expression,subContext); - dataSet.setData(dataSet.getShowIndex(i), dataSet.getShowIndex(col),value); - }catch(ScriptException e){ - //忽略脚本异常 + try { + if (expression instanceof LambdaFunction) { + value = ((LambdaFunction) expression).execute(subContext, i + 1); + dataSet.setData(dataSet.getShowIndex(i), dataSet.getShowIndex(col), + ((ScriptResult) value).getResult()); + } else { + value = executeDynamicObject((String) expression, subContext); + dataSet.setData(dataSet.getShowIndex(i), dataSet.getShowIndex(col), value); + } + + } catch (ScriptException e) { + // 忽略脚本异常 } - + } } - - private void setRowValue(ScriptContext context,AbstractDataSet dataSet,Set columns,int row) throws Exception{ - for(int j=0;j columns, int row) + throws Exception { + for (int j = 0; j < dataSet.getColumns(); j++) { Field field = dataSet.getFields().get(j); - if(columns!=null && columns.contains(j)){ - //存在字段数组形式 - context.put(field.getName(),DataSetUtil.createDataSetColumn(dataSet, dataSet.getShowIndex(j))); - }else{ - //直接赋值 - context.put(field.getName(), dataSet.getData(dataSet.getShowIndex(row), dataSet.getShowIndex(j))); //设置行参数上下文 + if (columns != null && columns.contains(j)) { + // 存在字段数组形式 + context.put(field.getName(), DataSetUtil.createDataSetColumn(dataSet, dataSet.getShowIndex(j))); + } else { + // 直接赋值 + context.put(field.getName(), dataSet.getData(dataSet.getShowIndex(row), dataSet.getShowIndex(j))); // 设置行参数上下文 } - + } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/util/DataSetUtil.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/util/DataSetUtil.java index ae5221d..c09f9a8 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/util/DataSetUtil.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/util/DataSetUtil.java @@ -25,6 +25,7 @@ import org.tinygroup.tinyscript.expression.ExpressionUtil; /** * 结果集工具类 + * * @author yancheng11334 * */ @@ -32,348 +33,374 @@ public final class DataSetUtil { private static final Pattern EXCEL_ITEM = Pattern.compile("[a-zA-Z]{1,2}[0-9]{1,5}"); private static final Pattern EXCEL_CHAR = Pattern.compile("[a-zA-Z]{1,2}"); + /** * 构建动态数据集 + * * @param fields * @param dataArray * @return * @throws Exception */ - public static DynamicDataSet createDynamicDataSet(List fields,Object[][] dataArray) throws Exception{ - return new SimpleDataSet(fields,dataArray); + public static DynamicDataSet createDynamicDataSet(List fields, Object[][] dataArray) throws Exception { + return new SimpleDataSet(fields, dataArray); } - + /** * 构建动态数据集 + * * @param fields * @param dataArray * @param tag * @return * @throws Exception */ - public static DynamicDataSet createDynamicDataSet(List fields,Object[][] dataArray,boolean tag) throws Exception{ - return new SimpleDataSet(fields,dataArray,tag); + public static DynamicDataSet createDynamicDataSet(List fields, Object[][] dataArray, boolean tag) + throws Exception { + return new SimpleDataSet(fields, dataArray, tag); } - + /** * 构建动态数据集 + * * @param fields * @param dataSetRows * @return * @throws Exception */ - public static DynamicDataSet createDynamicDataSet(List fields,List dataSetRows) throws Exception{ + public static DynamicDataSet createDynamicDataSet(List fields, List dataSetRows) + throws Exception { Object[][] dataArray = new Object[dataSetRows.size()][]; - for(int i=0;i fields = new ArrayList(); - for(Field field:dataSet.getFields()){ + for (Field field : dataSet.getFields()) { fields.add(field); } - DynamicDataSet result = new SimpleDataSet(fields,dataArray); + DynamicDataSet result = new SimpleDataSet(fields, dataArray); result.setIndexFromOne(dataSet.isIndexFromOne()); return result; } - + /** * 构建动态数据集(保持父数据集副本,关联修改) + * * @param dataSet * @param rows * @return * @throws Exception */ - public static DynamicDataSet createDynamicDataSet(DataSet dataSet,List rows) throws Exception{ + public static DynamicDataSet createDynamicDataSet(DataSet dataSet, List rows) throws Exception { SimpleDataSet localDataSet = getSimpleDataSet(dataSet); - - if(localDataSet==null){ - throw new ScriptException(String.format("%s类型的数据集无法保持副本,建议转换成SimpleDataSet类型", dataSet.getClass().getName())); + + if (localDataSet == null) { + throw new ScriptException( + String.format("%s类型的数据集无法保持副本,建议转换成SimpleDataSet类型", dataSet.getClass().getName())); } - + Object[][] fatherArray = localDataSet.getDataArray(); - - if(rows==null){ - return new SimpleDataSet(dataSet.getFields(),fatherArray,dataSet.isIndexFromOne()); - }else{ + + if (rows == null) { + return new SimpleDataSet(dataSet.getFields(), fatherArray, dataSet.isIndexFromOne()); + } else { Object[][] childArray = new Object[rows.size()][]; - for(int i=0;i 0 ? beginIndex : 0; - endIndex = endIndex< dataSet.getRows() ? endIndex : dataSet.getRows() -1 ; - - SimpleDataSet localDataSet = getSimpleDataSet(dataSet); - - if(localDataSet==null){ - throw new ScriptException(String.format("%s类型的数据集无法保持副本,建议转换成SimpleDataSet类型", dataSet.getClass().getName())); + endIndex = endIndex < dataSet.getRows() ? endIndex : dataSet.getRows() - 1; + + SimpleDataSet localDataSet = getSimpleDataSet(dataSet); + + if (localDataSet == null) { + throw new ScriptException( + String.format("%s类型的数据集无法保持副本,建议转换成SimpleDataSet类型", dataSet.getClass().getName())); } - + Object[][] fatherArray = localDataSet.getDataArray(); - Object[][] childArray = new Object[endIndex-beginIndex+1][]; - for(int i=beginIndex;i<=endIndex;i++){ - childArray[i-beginIndex] = fatherArray[i]; + Object[][] childArray = new Object[endIndex - beginIndex + 1][]; + for (int i = beginIndex; i <= endIndex; i++) { + childArray[i - beginIndex] = fatherArray[i]; } - return new SimpleDataSet(dataSet.getFields(),childArray,dataSet.isIndexFromOne()); + return new SimpleDataSet(dataSet.getFields(), childArray, dataSet.isIndexFromOne()); } - + private static SimpleDataSet getSimpleDataSet(DataSet dataSet) throws Exception { SimpleDataSet localDataSet = null; - if(dataSet instanceof SimpleDataSet){ + if (dataSet instanceof SimpleDataSet) { localDataSet = (SimpleDataSet) dataSet; - }else if(dataSet instanceof VariableDataSet){ + } else if (dataSet instanceof VariableDataSet) { VariableDataSet variableDataSet = (VariableDataSet) dataSet; - if(variableDataSet.getVariableDataSet() instanceof SimpleDataSet){ - localDataSet = (SimpleDataSet) variableDataSet.getVariableDataSet(); + if (variableDataSet.getVariableDataSet() instanceof SimpleDataSet) { + localDataSet = (SimpleDataSet) variableDataSet.getVariableDataSet(); } } return localDataSet; } - + /** * 构建数据集的行对象 + * * @param dataSet * @param row * @return * @throws Exception */ - public static DataSetRow createDataSetRow(DataSet dataSet,int row) throws Exception{ - return new DefaultDataSetRow(dataSet,row); + public static DataSetRow createDataSetRow(DataSet dataSet, int row) throws Exception { + return new DefaultDataSetRow(dataSet, row); } - + /** * 构建数据集的列对象 + * * @param dataSet * @param col * @return * @throws Exception */ - public static DataSetColumn createDataSetColumn(DataSet dataSet,int col) throws Exception{ - return new DefaultDataSetColumn(dataSet,col); + public static DataSetColumn createDataSetColumn(DataSet dataSet, int col) throws Exception { + return new DefaultDataSetColumn(dataSet, col); } - + /** * 构建数据集的列对象 + * * @param dataSet * @param colName * @return * @throws Exception */ - public static DataSetColumn createDataSetColumn(DataSet dataSet,String colName) throws Exception{ - return new DefaultDataSetColumn(dataSet,colName); + public static DataSetColumn createDataSetColumn(DataSet dataSet, String colName) throws Exception { + return new DefaultDataSetColumn(dataSet, colName); } - + /** * 返回索引字段对应的列 + * * @param dataSet * @param colName * @return * @throws Exception */ - public static int getFieldIndex(DataSet dataSet,String colName) throws Exception{ - for(int i=0;i result = groupDataSet.getAggregateResultList(); - for(int i=0;i getFieldArray(DataSet dataSet,String expression) throws Exception{ + public static Set getFieldArray(DataSet dataSet, String expression) throws Exception { Set columns = new HashSet(); - for(int i=0;i