diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/AbstractDataSet.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/AbstractDataSet.java index 366e485728e7e237de0b86e97e0d58ca852b3141..1ddf3ae7fb72933f75419aa5db64b965017e4ad0 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/AbstractDataSet.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/AbstractDataSet.java @@ -7,100 +7,103 @@ import java.util.Map; import org.tinygroup.logger.Logger; import org.tinygroup.logger.LoggerFactory; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** - * 抽象数据集 - * Created by luoguo on 2014/7/4. + * 抽象数据集 Created by luoguo on 2014/7/4. */ public abstract class AbstractDataSet implements DataSet { - /** - * 字段 - */ - protected List fields=new ArrayList(); - private transient Map columnIndex = new HashMap(); - private String name; - protected Logger logger=LoggerFactory.getLogger(AbstractDataSet.class); - private boolean indexFromOne; - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - protected void throwNotSupportMethod() throws Exception { - throw new Exception("本数据集为只读数据集,不可以修改数据!"); - } - - protected Integer getColumn(String fieldName) { - return columnIndex.get(fieldName.toUpperCase()); - } - - public List getFields() { - return fields; - } - - public void setFields(List fields) { - this.fields = fields; - columnIndex.clear(); - for (int i = 0; i < fields.size(); i++) { - columnIndex.put(fields.get(i).getName().toUpperCase(), i); - } - } - - public T getData(String fieldName) throws Exception { - Integer index = getColumn(fieldName); - if(index==null){ - throw new Exception(String.format("本数据集没有找到字段%s", fieldName)); - } - return getData(getShowIndex(index)); - } - - public void setData(String fieldName, T data) throws Exception { - Integer index = getColumn(fieldName); - if(index==null){ - throw new Exception(String.format("本数据集没有找到字段%s", fieldName)); - } - setData(getShowIndex(index), data); - } - - public void clean() { - if(fields!=null){ - fields.clear(); - columnIndex.clear(); - fields = null; - columnIndex=null; - } - } - - public DataSet cloneDataSet() throws CloneNotSupportedException{ - throw new CloneNotSupportedException("本数据集不支持clone操作"); - } - - public boolean isIndexFromOne() { + /** + * 字段 + */ + protected List fields = new ArrayList(); + private transient Map columnIndex = new HashMap(); + private String name; + protected Logger logger = LoggerFactory.getLogger(AbstractDataSet.class); + private boolean indexFromOne; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + protected void throwNotSupportMethod() throws Exception { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset.onlyread.error", "dataset.fields.notfound")); + } + + protected Integer getColumn(String fieldName) { + return columnIndex.get(fieldName.toUpperCase()); + } + + public List getFields() { + return fields; + } + + public void setFields(List fields) { + this.fields = fields; + columnIndex.clear(); + for (int i = 0; i < fields.size(); i++) { + columnIndex.put(fields.get(i).getName().toUpperCase(), i); + } + } + + public T getData(String fieldName) throws Exception { + Integer index = getColumn(fieldName); + if (index == null) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.notfound", fieldName)); + } + return getData(getShowIndex(index)); + } + + public void setData(String fieldName, T data) throws Exception { + Integer index = getColumn(fieldName); + if (index == null) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.notfound", fieldName)); + } + setData(getShowIndex(index), data); + } + + public void clean() { + if (fields != null) { + fields.clear(); + columnIndex.clear(); + fields = null; + columnIndex = null; + } + } + + public DataSet cloneDataSet() throws CloneNotSupportedException { + throw new CloneNotSupportedException("本数据集不支持clone操作"); + } + + public boolean isIndexFromOne() { return indexFromOne; } public void setIndexFromOne(boolean tag) { this.indexFromOne = tag; } - + /** * 根据实际索引获取显示下标(假设实际索引从0开始) + * * @param index * @return */ - public int getShowIndex(int index){ - return isIndexFromOne()?index+1:index; + public int getShowIndex(int index) { + return isIndexFromOne() ? index + 1 : index; } - + /** * 根据显示下标获取实际索引(假设实际索引从0开始) + * * @param index * @return */ - public int getActualIndex(int index){ - return isIndexFromOne()?index-1:index; + public int getActualIndex(int index) { + return isIndexFromOne() ? index - 1 : index; } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/RowComparator.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/RowComparator.java deleted file mode 100644 index 75fa0d8fee145f0acab72b92ecda0cb6fac76a41..0000000000000000000000000000000000000000 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/RowComparator.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.tinygroup.tinyscript.dataset; - -public interface RowComparator { - - public boolean isEqual(DataSetRow o1, DataSetRow o2); - - public int countHash(DataSetRow row); - -} diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractDataSetOperateFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractDataSetOperateFunction.java index 6d55a4fe141b3adf600bcd8834b5b66d283765c4..85ed6b5a5b073ec04a8a5ce9d164f2f347baace2 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractDataSetOperateFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractDataSetOperateFunction.java @@ -1,21 +1,19 @@ package org.tinygroup.tinyscript.dataset.function; import java.util.ArrayList; -import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; -import java.util.Set; +import java.util.Map; import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.dataset.AbstractDataSet; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.Field; -import org.tinygroup.tinyscript.dataset.RowComparator; import org.tinygroup.tinyscript.dataset.impl.DefaultDataSetRow; -import org.tinygroup.tinyscript.dataset.impl.LambdaRowComparator; -import org.tinygroup.tinyscript.dataset.impl.ListRowComparator; 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; public abstract class AbstractDataSetOperateFunction extends AbstractScriptFunction { @@ -25,7 +23,9 @@ public abstract class AbstractDataSetOperateFunction extends AbstractScriptFunct return DataSet.class.getName(); } - /**批量转换主键(string→int) + /** + * 批量转换主键(string→int) + * * @param dataSet * @param pks * @return @@ -50,36 +50,36 @@ public abstract class AbstractDataSetOperateFunction extends AbstractScriptFunct return pksIndex; } - /**创建行集合 - * @param dataSet - * @param pks - * @param context - * @return - * @throws Exception - */ - protected Set createDataSetRows(AbstractDataSet dataSet, Object pks, ScriptContext context) + + + protected Map createMapDataSetRows(AbstractDataSet dataSet, Object pks, ScriptContext context) throws Exception { - Set rows = new HashSet(); + Map result = new LinkedHashMap(); for (int i = 1; i <= dataSet.getRows(); i++) { - rows.add(new DefaultDataSetRow(dataSet, i, createRowComparator(dataSet, pks, context))); + String key = createRowKey(dataSet, pks, i, context); + result.put(key, new DefaultDataSetRow(dataSet, i)); } - return rows; + return result; } - /** - * 创建行比较器 - * @param dataSet - * @param pks - * @param context - * @return - * @throws Exception - */ - protected RowComparator createRowComparator(DataSet dataSet, Object pks, ScriptContext context) throws Exception { + protected String createRowKey(AbstractDataSet dataSet, Object pks, int row, ScriptContext context) + throws Exception { if (pks instanceof List || pks instanceof String) { List pksIndex = showPkIndex(dataSet, pks); - return new ListRowComparator(pksIndex); + StringBuilder builder = new StringBuilder(); + for (int col : pksIndex) { + builder.append(dataSet.getData(row, col)); + } + return builder.toString(); } else if (pks instanceof LambdaFunction) { - return new LambdaRowComparator((LambdaFunction) pks, context); + LambdaFunction keyFunction = (LambdaFunction) pks; + ScriptContext subContext = new DefaultScriptContext(); + subContext.setParent(context); + for (int i = 0; i < dataSet.getFields().size(); i++) { + subContext.put(dataSet.getFields().get(i).getName(), dataSet.getData(row, i + 1)); + } + return keyFunction.execute(subContext).getResult().toString(); + } return null; } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractVisitRowFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractVisitRowFunction.java index 828a378dec28f6470bf51a66336bbacafaedc55f..829a58d1311d907fbf70bd65d2d4d4d2a3a03d52 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractVisitRowFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/AbstractVisitRowFunction.java @@ -8,9 +8,11 @@ import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 抽象访问数据集行对象的函数 + * * @author yancheng11334 * */ @@ -20,57 +22,60 @@ public abstract class AbstractVisitRowFunction extends AbstractScriptFunction { return DynamicDataSet.class.getName(); } - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - try{ - if (parameters == null || parameters.length == 0) { - throw new ScriptException(String.format("%s函数的参数为空!",getNames())); - } else if(parameters.length==1 && parameters[0] instanceof DataSet){ - //默认index为0 - return visit((DynamicDataSet)parameters[0],getDefaultIndex()); - } else if(parameters.length==2 && parameters[0] instanceof DataSet){ - Object value = DataSetUtil.getValue(parameters[1], context); - if(value!=null && value instanceof Integer){ - int index = (Integer) value; - //用户输入的下标需要检查范围合法性 - checkIndex(index); - return visit((DynamicDataSet)parameters[0],index); - }else{ - throw new ScriptException(String.format("%s函数的参数格式不正确:下标%s非整型",getNames(),parameters[1])); - } - + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + try { + if (parameters == null || parameters.length == 0) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (parameters.length == 1 && parameters[0] instanceof DataSet) { + // 默认index为0 + return visit((DynamicDataSet) parameters[0], getDefaultIndex()); + } else if (parameters.length == 2 && parameters[0] instanceof DataSet) { + Object value = DataSetUtil.getValue(parameters[1], context); + if (value != null && value instanceof Integer) { + int index = (Integer) value; + // 用户输入的下标需要检查范围合法性 + checkIndex(index); + return visit((DynamicDataSet) parameters[0], index); + } else { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("dataset", + "dataset.parameter.interror", getNames(), parameters[1])); + } + } else { - throw new ScriptException(String.format("%s函数的参数格式不正确!",getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - - }catch (ScriptException e) { + + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException(String.format("%s函数的参数格式不正确!",getNames()), e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - + /** * 访问行对象 + * * @param index * @return * @throws ScriptException */ - protected abstract DataSetRow visit(DynamicDataSet dataSet,int index) throws ScriptException; - + protected abstract DataSetRow visit(DynamicDataSet dataSet, int index) throws ScriptException; + /** * 获得默认值 + * * @return */ - protected abstract int getDefaultIndex(); - + protected abstract int getDefaultIndex(); + /** * 根据业务场景判断下标是否合法 + * * @param index * @throws ScriptException */ - protected void checkIndex(int index) throws Exception{ - + protected void checkIndex(int index) throws Exception { + } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/CursorRowFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/CursorRowFunction.java index 41e520abd25b47dc66ff82f4de2dda7f5531f1be..0853cd146d3ffbe2afe32426d6a923314b1a53e0 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/CursorRowFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/CursorRowFunction.java @@ -4,24 +4,25 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * cursor函数 + * * @author yancheng11334 * */ -public class CursorRowFunction extends AbstractVisitRowFunction{ +public class CursorRowFunction extends AbstractVisitRowFunction { public String getNames() { return "cursorRow"; } - protected DataSetRow visit(DynamicDataSet dataSet, int index) - throws ScriptException { - try { - return DataSetUtil.createDataSetRow(dataSet, dataSet.getCurrentRow()+index); + protected DataSetRow visit(DynamicDataSet dataSet, int index) throws ScriptException { + try { + return DataSetUtil.createDataSetRow(dataSet, dataSet.getCurrentRow() + index); } catch (Exception e) { - throw new ScriptException(String.format("%s函数操作失败", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetAggregateFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetAggregateFunction.java index ce3ab7b50b79e24bf62c76f287138a971dba0860..59355195d30204d4311b483c1940111164109533 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetAggregateFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetAggregateFunction.java @@ -11,66 +11,67 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.expression.ExpressionUtil; import org.tinygroup.tinyscript.function.DynamicNameScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptContextUtil; /** * 数据集聚合函数 + * * @author yancheng11334 * */ -public class DataSetAggregateFunction extends DynamicNameScriptFunction{ +public class DataSetAggregateFunction extends DynamicNameScriptFunction { 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 { + + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { String functionName = ScriptContextUtil.getDynamicFunctionName(context); - try{ + try { if (parameters == null || parameters.length == 0) { - throw new ScriptException(String.format("%s聚合函数的参数为空!", functionName)); - }else if(checkParameters(parameters, 2)){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 2)) { AbstractDataSet dataSet = (AbstractDataSet) getValue(parameters[0]); String fieldName = (String) getValue(parameters[1]); - return executeDataSet(dataSet,fieldName,functionName); - }else{ - throw new ScriptException(String.format("%s聚合函数的参数格式不正确!", functionName)); + return executeDataSet(dataSet, fieldName, functionName); + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException(String.format("%s聚合函数的参数格式不正确!", functionName),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } public boolean exsitFunctionName(String name) { - return ExpressionUtil.getNumberCalculator(name)!=null; + return ExpressionUtil.getNumberCalculator(name) != null; } - - protected Object executeDataSet(AbstractDataSet dataSet,String fieldName,String functionName) throws Exception{ - int col = getColumn(dataSet,fieldName); + + protected Object executeDataSet(AbstractDataSet dataSet, String fieldName, String functionName) throws Exception { + int col = getColumn(dataSet, fieldName); int rowNum = dataSet.getRows(); List parameterList = new ArrayList(); - for(int i=0;i cols = new ArrayList(); - for(int i=1;i cols,Object... rules) throws Exception{ - for(int i=0;i cols, Object... rules) throws Exception { + for (int i = 0; i < dataSet.getRows(); i++) { + for (int col : cols) { Object v = dataSet.getData(dataSet.getShowIndex(i), dataSet.getShowIndex(col)); - if(v!=null){ - try{ - if(rules!=null){ - dataSet.setData(dataSet.getShowIndex(i), dataSet.getShowIndex(col), TypeConvertUtil.convert(type, v,rules)); - }else{ - dataSet.setData(dataSet.getShowIndex(i), dataSet.getShowIndex(col), TypeConvertUtil.convert(type, v)); - } - }catch(Exception e){ - throw new ScriptException(String.format("[%s]函数处理第[%d]行,第[%d]列,值[%s]的记录发生错误:", type,dataSet.getShowIndex(i), dataSet.getShowIndex(col),v),e); - } - + if (v != null) { + try { + if (rules != null) { + dataSet.setData(dataSet.getShowIndex(i), dataSet.getShowIndex(col), + TypeConvertUtil.convert(type, v, rules)); + } else { + dataSet.setData(dataSet.getShowIndex(i), dataSet.getShowIndex(col), + TypeConvertUtil.convert(type, v)); + } + } catch (Exception e) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.parameter.executeerror", type, + dataSet.getShowIndex(i), dataSet.getShowIndex(col), v), + e); + } + } } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetCopyFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetCopyFunction.java index 493e6abc1ecdf554fa0b8b0f429b05304d64886e..91875f998444b007f55ed29fa8f889f225998d99 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetCopyFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetCopyFunction.java @@ -11,62 +11,62 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; public class DataSetCopyFunction extends AbstractScriptFunction { public String getNames() { return "copy"; } - + public String getBindingTypes() { return DataSet.class.getName(); } - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { try { if (parameters == null || parameters.length == 0) { - throw new ScriptException("copy函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); } else if (checkParameters(parameters, 1)) { DataSet dataSet = (DataSet) parameters[0]; - return copy(dataSet); + return copy(dataSet); } else { - throw new ScriptException("copy函数的参数格式不正确!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("copy函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - private DataSet copy(DataSet dataSet) throws Exception{ - if(dataSet instanceof AbstractDataSet){ - AbstractDataSet abstractDataSet = (AbstractDataSet) dataSet; - try{ - return abstractDataSet.cloneDataSet(); - }catch(CloneNotSupportedException e){ - //某些实现不支持cloneDataSet方法 - } - } - //走循环遍历复制 - List newFields = new ArrayList(); - for(Field field:dataSet.getFields()){ + + private DataSet copy(DataSet dataSet) throws Exception { + if (dataSet instanceof AbstractDataSet) { + AbstractDataSet abstractDataSet = (AbstractDataSet) dataSet; + try { + return abstractDataSet.cloneDataSet(); + } catch (CloneNotSupportedException e) { + // 某些实现不支持cloneDataSet方法 + } + } + // 走循环遍历复制 + List newFields = new ArrayList(); + for (Field field : dataSet.getFields()) { newFields.add(field); } Object[][] dataArray = new Object[dataSet.getRows()][]; - for(int i=0;i(result.values()),dataSet.isIndexFromOne()); }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetGroupFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetGroupFunction.java index 0b1cd31d5cf646d46dc3988e698f8dd9a08ea3cf..7400febfa2fa3aa4eba6410e39ee505526a90175 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetGroupFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetGroupFunction.java @@ -16,118 +16,125 @@ import org.tinygroup.tinyscript.dataset.GroupDataSet; import org.tinygroup.tinyscript.dataset.impl.DefaultGroupDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 数据集分组聚合的算法 + * * @author yancheng11334 * */ public class DataSetGroupFunction extends AbstractScriptFunction { - + public String getNames() { return "group"; } - + 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("group函数的参数为空!"); - }else if(parameters.length >= 2){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (parameters.length >= 2) { DataSet dataSet = (DataSet) getValue(parameters[0]); - String[] fields = new String[parameters.length-1]; - for(int i=0;i result = new LinkedHashMap(); - try{ - int[] columns = getFieldIndexs(dataSet,fields); - int rowNum = dataSet.getRows(); - //记录数不足的数据集直接返回 - if(rowNum<=1){ - return new DefaultGroupDataSet(dataSet.getFields(),dataSet.isIndexFromOne()); + private GroupDataSet group(DataSet dataSet, String[] fields) throws Exception { + Map result = new LinkedHashMap(); + try { + int[] columns = getFieldIndexs(dataSet, fields); + int rowNum = dataSet.getRows(); + // 记录数不足的数据集直接返回 + if (rowNum <= 1) { + return new DefaultGroupDataSet(dataSet.getFields(), dataSet.isIndexFromOne()); } - + AbstractDataSet abstractDataSet = (AbstractDataSet) dataSet; - //逐条遍历记录 - for(int i=0;i newFields = new ArrayList(); - for(Field field:dataSet.getFields()){ + for (Field field : dataSet.getFields()) { newFields.add(field); } - return new DefaultGroupDataSet(newFields,new ArrayList(result.values()),dataSet.isIndexFromOne()); - - }catch(Exception e){ - throw new ScriptException("执行group函数发生异常",e); + return new DefaultGroupDataSet(newFields, new ArrayList(result.values()), + dataSet.isIndexFromOne()); + + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - private int[] getFieldIndexs(DataSet dataSet,String[] fields) throws Exception{ + + private int[] getFieldIndexs(DataSet dataSet, String[] fields) throws Exception { int[] indexs = new int[fields.length]; - for(int i=0;i1){ AbstractDataSet dataSet = (AbstractDataSet) getValue(parameters[0]); String[] expressions = new String[parameters.length-1]; @@ -51,12 +52,12 @@ public class DataSetGroupStagedFunction extends AbstractScriptFunction { } return groupStaged(dataSet,expressions,context); }else{ - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch(ScriptException e){ throw e; }catch(Exception e){ - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetIntersectionFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetIntersectionFunction.java index 73c996b072ff0a06777175864b994326f23774bf..d47ab95ddabf7830c6354be96972d11b8390e219 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetIntersectionFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetIntersectionFunction.java @@ -1,7 +1,7 @@ package org.tinygroup.tinyscript.dataset.function; -import java.util.HashSet; -import java.util.Set; +import java.util.LinkedHashMap; +import java.util.Map; import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; @@ -9,7 +9,6 @@ import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.dataset.AbstractDataSet; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DataSetRow; -import org.tinygroup.tinyscript.dataset.impl.DefaultDataSetRow; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; @@ -62,15 +61,15 @@ public class DataSetIntersectionFunction extends AbstractDataSetOperateFunction */ protected DataSet operate(AbstractDataSet dataSet1, AbstractDataSet dataSet2, Object pks, ScriptContext context) throws Exception { - Set newRows = new HashSet(); - Set set = createDataSetRows(dataSet1, pks, context); + Map map = createMapDataSetRows(dataSet1, pks, context); + Map newMap = new LinkedHashMap(); for (int i = 1; i <= dataSet2.getRows(); i++) { - DataSetRow row = new DefaultDataSetRow(dataSet2, i, createRowComparator(dataSet2, pks, context)); - if (set.contains(row)) { - newRows.add(row); + String key = createRowKey(dataSet2, pks, i, context); + if (map.containsKey(key)) { + newMap.put(key, map.get(key)); } } - return DataSetUtil.createDynamicDataSet(newRows); + return DataSetUtil.createDynamicDataSet(newMap); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetJoinFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetJoinFunction.java index 75037a31e052196b30f5f1800d3b1090a961e387..690ca11fe53b8bda588b85e574ca9821a460e94c 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetJoinFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetJoinFunction.java @@ -13,9 +13,11 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.impl.MatchDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * join函数 + * * @author yancheng11334 * */ @@ -24,39 +26,37 @@ public class DataSetJoinFunction extends AbstractScriptFunction { public String getNames() { return "join"; } - + 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("join函数的参数为空!"); - }else if(checkParameters(parameters, 3)){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 3)) { DataSet left = (DataSet) getValue(parameters[0]); DataSet right = (DataSet) getValue(parameters[1]); String expression = getExpression(parameters[2]); - return executeJoin(left,right,expression,context); - }else{ - throw new ScriptException("join函数的参数格式不正确!"); + return executeJoin(left, right, expression, context); + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("join函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - - + /** * 执行join逻辑 + * * @param left * @param right * @param fields @@ -64,70 +64,73 @@ public class DataSetJoinFunction extends AbstractScriptFunction { * @return * @throws ScriptException */ - protected DataSet executeJoin(DataSet left,DataSet right,String expression,ScriptContext context) throws ScriptException{ - try{ - String newExpression = (String)getScriptEngine().execute(convertExpression(expression), context); - if(newExpression==null){ - newExpression = expression; + protected DataSet executeJoin(DataSet left, DataSet right, String expression, ScriptContext context) + throws ScriptException { + try { + String newExpression = (String) getScriptEngine().execute(convertExpression(expression), context); + if (newExpression == null) { + newExpression = expression; } - //解析关联条件 + // 解析关联条件 String[] fields = getJoinField(newExpression); - + int leftColumn = DataSetUtil.getFieldIndex(left, fields[0]); - if(leftColumn<0){ - throw new ScriptException(String.format("左数据集查不到字段%s", fields[0])); + if (leftColumn < 0) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.leftnotfound", fields[0])); } - + int rightColumn = DataSetUtil.getFieldIndex(right, fields[1]); - if(rightColumn<0){ - throw new ScriptException(String.format("右数据集查不到字段%s", fields[1])); + if (rightColumn < 0) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.rightnotfound", fields[1])); } - - //记录关联行信息 - List joinList = new ArrayList(); - - //遍历右数据集 - Map values = new HashMap(); + + // 记录关联行信息 + List joinList = new ArrayList(); + + // 遍历右数据集 + Map values = new HashMap(); AbstractDataSet rightDs = (AbstractDataSet) right; int rrow = right.getRows(); - for(int row=0;row it = result.subList(1, result.size()).iterator(); diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetLimitFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetLimitFunction.java index 5a62471bbb9d36a1a0f6b1638bdd79287593f20c..b2b448a174d7d79a44ab0b9515f30f770cd63cf4 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetLimitFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetLimitFunction.java @@ -8,15 +8,14 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.GroupDataSet; -import org.tinygroup.tinyscript.dataset.impl.AggregateResult; import org.tinygroup.tinyscript.dataset.impl.DefaultGroupDataSet; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; public class DataSetLimitFunction extends AbstractScriptFunction { @Override public String getNames() { - // TODO Auto-generated method stub return "limit"; } @@ -27,22 +26,21 @@ public class DataSetLimitFunction extends AbstractScriptFunction { @Override public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { - // TODO Auto-generated method stub try { if (parameters == null || parameters.length == 0) { - throw new ScriptException("limit函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); } else if (checkParameters(parameters, 3)) { DefaultGroupDataSet dataSet = (DefaultGroupDataSet) parameters[0]; int begin = (Integer) parameters[1]; int end = (Integer) parameters[2]; return limit(dataSet, begin - 1, end - 1); } else { - throw new ScriptException("limit函数的参数格式不正确!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("limit函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetMatchFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetMatchFunction.java index e8ddd1038439a6930be26a042188dbe7ca362d0b..a05f173d1eb989f5effeee797a835effdbd5bee0 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetMatchFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetMatchFunction.java @@ -12,9 +12,11 @@ import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.dataset.impl.MatchDataSet; import org.tinygroup.tinyscript.function.AbstractScriptFunction; import org.tinygroup.tinyscript.impl.DefaultScriptContext; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 通过条件匹配左右数据集 + * * @author yancheng11334 * */ @@ -23,93 +25,95 @@ public class DataSetMatchFunction extends AbstractScriptFunction { public String getNames() { return "match"; } - + 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("match函数的参数为空!"); - }else if(checkParameters(parameters, 3)){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 3)) { DataSet left = (DataSet) getValue(parameters[0]); DataSet right = (DataSet) getValue(parameters[1]); String expression = getExpression(parameters[2]); - return executeMatch(left,right,expression,context); - }else{ - throw new ScriptException("match函数的参数格式不正确!"); + return executeMatch(left, right, expression, context); + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("match函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected DataSet executeMatch(DataSet left,DataSet right,String expression,ScriptContext context) throws ScriptException{ + + protected DataSet executeMatch(DataSet left, DataSet right, String expression, ScriptContext context) + throws ScriptException { String oldExpression = expression; - try{ - //转换表达式为脚本可以执行的语法片段 + try { + // 转换表达式为脚本可以执行的语法片段 expression = convertExpression(expression); - - //合并新的字段 - List newFields =new ArrayList(); + + // 合并新的字段 + List newFields = new ArrayList(); newFields.addAll(left.getFields()); newFields.addAll(right.getFields()); - - //匹配的左表、右表对应的行下标 - List matchList = new ArrayList(); - + + // 匹配的左表、右表对应的行下标 + List matchList = new ArrayList(); + int leftRows = left.getRows(); int rightRows = right.getRows(); - - for(int i=0;i=2 && parameters[0] instanceof DynamicDataSet){ - Object[] args = new Object[parameters.length-1]; - for(int i=0;i= 2 && parameters[0] instanceof DynamicDataSet) { + Object[] args = new Object[parameters.length - 1]; + for (int i = 0; i < args.length; i++) { + args[i] = parameters[i + 1]; } - return select((DynamicDataSet)parameters[0],args); + return select((DynamicDataSet) parameters[0], args); } else { - throw new ScriptException("select函数的参数格式不正确"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - - }catch (ScriptException e) { + + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("select函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - + /** * 执行选择字段 + * * @param dataSet * @param objects * @return */ - private DynamicDataSet select(DynamicDataSet dataSet,Object...objects) throws Exception{ - boolean[] columns = new boolean[dataSet.getColumns()]; - //判断哪些字段需要保留 - for(Object obj:objects){ - if(obj instanceof Integer){ - if(dataSet.isIndexFromOne()){ - columns[((Integer)obj)-1] = true; - }else{ - columns[(Integer)obj] = true; - } - - }else if(obj instanceof String){ - int index = DataSetUtil.getFieldIndex(dataSet, (String)obj); - if(index<0){ - throw new ScriptException(String.format("不能匹配的字段名%s", obj)); - } - columns[index] = true; - }else{ - throw new ScriptException(String.format("不能识别的字段对象%s", obj)); - } - } - //执行删除逻辑,自后往前删除 - for(int i=columns.length-1;i>=0;i--){ - if(columns[i]==false){ - dataSet.deleteColumn(dataSet.getShowIndex(i)); - } - } - return dataSet; - } + private DynamicDataSet select(DynamicDataSet dataSet, Object... objects) throws Exception { + boolean[] columns = new boolean[dataSet.getColumns()]; + // 判断哪些字段需要保留 + for (Object obj : objects) { + if (obj instanceof Integer) { + if (dataSet.isIndexFromOne()) { + columns[((Integer) obj) - 1] = true; + } else { + columns[(Integer) obj] = true; + } + + } else if (obj instanceof String) { + int index = DataSetUtil.getFieldIndex(dataSet, (String) obj); + if (index < 0) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.notfound", obj)); + } + columns[index] = true; + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.unsupport", + getNames(), obj.getClass())); + } + } + // 执行删除逻辑,自后往前删除 + for (int i = columns.length - 1; i >= 0; i--) { + if (columns[i] == false) { + dataSet.deleteColumn(dataSet.getShowIndex(i)); + } + } + return dataSet; + } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSortFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSortFunction.java index fcfa171859ef47ce3931e4c10c8e85956e44a8b7..ec4fb150c6d42fb2d350111e8f2b021db6dbc456 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSortFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSortFunction.java @@ -12,93 +12,97 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractSortFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; @SuppressWarnings("rawtypes") -public class DataSetSortFunction extends AbstractSortFunction{ - +public class DataSetSortFunction extends AbstractSortFunction { + public String getBindingTypes() { return DataSet.class.getName(); } - protected Object sort(ScriptSegment segment, ScriptContext context, - Object obj, String rule) throws ScriptException { - - try{ + protected Object sort(ScriptSegment segment, ScriptContext context, Object obj, String rule) + throws ScriptException { + + try { DynamicDataSet dataSet = (DynamicDataSet) obj; - Comparator c = getComparator(rule,segment,dataSet); - if(c==null){ - throw new ScriptException(String.format("解析排序规则[%s]失败", rule)); + Comparator c = getComparator(rule, segment, dataSet); + if (c == null) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.expression.error", getNames(), rule)); } dataSet.sort(c); return dataSet; - }catch(ScriptException e){ + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("序列按规则[%s]排序发生异常:", rule),e); + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("dataset", "dataset.sort.error", rule), e); } } @SuppressWarnings({ "unchecked" }) - protected Comparator createComparator(String rule, ScriptSegment segment, - Object source) throws Exception { + protected Comparator createComparator(String rule, ScriptSegment segment, Object source) throws Exception { List rules = new ArrayList(); - try{ - if(matchFieldRule(rule)){ + try { + if (matchFieldRule(rule)) { String[] ss = rule.trim().split(","); - for(String s:ss){ - FieldSortRule fieldSortRule = parse(s,(DataSet)source); - if(fieldSortRule!=null){ - rules.add(fieldSortRule); + for (String s : ss) { + FieldSortRule fieldSortRule = parse(s, (DataSet) source); + if (fieldSortRule != null) { + rules.add(fieldSortRule); } } FieldSortComparator comparator = new FieldSortComparator(); comparator.setFieldSortRuleList(rules); return comparator; } - }catch(ScriptException e){ + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("序列按规则[%s]排序发生异常:", rule),e); + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("dataset", "dataset.sort.error", rule), e); } return null; } - + /** * 解析字段信息 + * * @param s * @return - * @throws Exception + * @throws Exception */ - private FieldSortRule parse(String s,DataSet dataSet) throws Exception{ - FieldSortRule rule=null; - if(!StringUtil.isEmpty(s)){ - String [] ss = s.trim().split("\\s+"); - rule = new FieldSortRule(); - if(ss.length>=2 && ss[1].equalsIgnoreCase("desc")){ - rule.setAsc(false); - }else{ - rule.setAsc(true); - } - rule.setContextName(DEFAULT_CONTEXT_NAME); - int index = DataSetUtil.getFieldIndex(dataSet, ss[0]); - if(index<0){ - throw new ScriptException(String.format("根据字段%s匹配数据集列失败", ss[0])); - } - rule.setIndex(index); - rule.setSegment(createDataSetSegment(rule,dataSet)); - + private FieldSortRule parse(String s, DataSet dataSet) throws Exception { + FieldSortRule rule = null; + if (!StringUtil.isEmpty(s)) { + String[] ss = s.trim().split("\\s+"); + rule = new FieldSortRule(); + if (ss.length >= 2 && ss[1].equalsIgnoreCase("desc")) { + rule.setAsc(false); + } else { + rule.setAsc(true); + } + rule.setContextName(DEFAULT_CONTEXT_NAME); + int index = DataSetUtil.getFieldIndex(dataSet, ss[0]); + if (index < 0) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.notfound", ss[0])); + } + rule.setIndex(index); + rule.setSegment(createDataSetSegment(rule, dataSet)); + } - + return rule; } - + /** * return array[i]; + * * @param rule * @param dataSet * @return */ - private String createDataSetSegment(FieldSortRule rule,DataSet dataSet){ + private String createDataSetSegment(FieldSortRule rule, DataSet dataSet) { StringBuilder sb = new StringBuilder(); sb.append("return ").append(DEFAULT_CONTEXT_NAME); sb.append("[").append(rule.getIndex()).append("]"); @@ -106,8 +110,7 @@ public class DataSetSortFunction extends AbstractSortFunction{ return sb.toString(); } - protected Object sortByLambda(Object sortObject, Comparator c) - throws Exception { + protected Object sortByLambda(Object sortObject, Comparator c) throws Exception { DynamicDataSet dataSet = (DynamicDataSet) sortObject; return dataSet.sort(c); } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSubFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSubFunction.java index dfba70f478b3f581bd205f7f6f46e970ae251913..bdbd3202ed6bfa7c4b09e2e687bfe62e5c0f635e 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSubFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetSubFunction.java @@ -11,9 +11,11 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 拆分数据集 + * * @author yancheng11334 * */ @@ -22,61 +24,62 @@ public class DataSetSubFunction extends AbstractScriptFunction { public String getNames() { return "sub"; } - + public String getBindingTypes() { return DataSet.class.getName(); } - 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("sub函数的参数为空!"); - }else if(checkParameters(parameters, 2) && parameters[1] instanceof Integer){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 2) && parameters[1] instanceof Integer) { AbstractDataSet dataSet = (AbstractDataSet) parameters[0]; int beginIndex = (Integer) parameters[1]; - int endIndex = dataSet.getRows() -1; - if(getScriptEngine().isIndexFromOne()){ - return sub(dataSet,beginIndex-1,endIndex); - }else{ - return sub(dataSet,beginIndex,endIndex); + int endIndex = dataSet.getRows() - 1; + if (getScriptEngine().isIndexFromOne()) { + return sub(dataSet, beginIndex - 1, endIndex); + } else { + return sub(dataSet, beginIndex, endIndex); } - - }else if(checkParameters(parameters, 3) && parameters[1] instanceof Integer && parameters[2] instanceof Integer){ + + } else if (checkParameters(parameters, 3) && parameters[1] instanceof Integer + && parameters[2] instanceof Integer) { AbstractDataSet dataSet = (AbstractDataSet) parameters[0]; int beginIndex = (Integer) parameters[1]; - int endIndex =(Integer) parameters[2]; - if(getScriptEngine().isIndexFromOne()){ - return sub(dataSet,beginIndex-1,endIndex-1); - }else{ - return sub(dataSet,beginIndex,endIndex); + int endIndex = (Integer) parameters[2]; + if (getScriptEngine().isIndexFromOne()) { + return sub(dataSet, beginIndex - 1, endIndex - 1); + } else { + return sub(dataSet, beginIndex, endIndex); } - - }else{ - throw new ScriptException("sub函数的参数格式不正确!"); + + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("sub函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - //本参数是实际下标 - private DataSet sub(AbstractDataSet dataSet,int beginIndex,int endIndex) throws Exception{ - if(beginIndex<0 || endIndex>dataSet.getRows()-1 || beginIndex>endIndex){ - throw new ScriptException(String.format("sub函数越界:开始行下标%s,结束行下标%s", beginIndex,endIndex)); + + // 本参数是实际下标 + private DataSet sub(AbstractDataSet dataSet, int beginIndex, int endIndex) throws Exception { + if (beginIndex < 0 || endIndex > dataSet.getRows() - 1 || beginIndex > endIndex) { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("dataset", "dataset.row2.outofindex", + getNames(), beginIndex, endIndex)); } - List newFields =new ArrayList(); + List newFields = new ArrayList(); newFields.addAll(dataSet.getFields()); - Object[][] dataArray = new Object[endIndex-beginIndex+1][]; - for(int i=beginIndex;i<=endIndex;i++){ - dataArray[i-beginIndex] = new Object[newFields.size()]; - for(int j=0;j set = createDataSetRows(dataSet1, pks, context); + Map map = createMapDataSetRows(dataSet1, pks, context); for (int i = 1; i <= dataSet2.getRows(); i++) { - DataSetRow row = new DefaultDataSetRow(dataSet2, i, createRowComparator(dataSet2, pks, context)); - if (set.contains(row)) { - set.remove(row); + String key = createRowKey(dataSet2, pks, i, context); + if (map.containsKey(key)) { + map.remove(key); } } - return DataSetUtil.createDynamicDataSet(set); + return DataSetUtil.createDynamicDataSet(map); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUnionFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUnionFunction.java index 27ad841f2afc680f4516fd8478a4e86736018766..6fc5933ae0ba65748957f37eeaba6aecac944ab3 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUnionFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetUnionFunction.java @@ -1,6 +1,6 @@ package org.tinygroup.tinyscript.dataset.function; -import java.util.Set; +import java.util.Map; import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; @@ -51,12 +51,13 @@ public class DataSetUnionFunction extends AbstractDataSetOperateFunction { @Override protected DataSet operate(AbstractDataSet dataSet1, AbstractDataSet dataSet2, Object pks, ScriptContext context) throws Exception { - Set set1 = createDataSetRows(dataSet1, pks, context); + Map map = createMapDataSetRows(dataSet1, pks, context); for (int i = 1; i <= dataSet2.getRows(); i++) { - DataSetRow row = new DefaultDataSetRow(dataSet2, i, createRowComparator(dataSet2, pks, context)); - set1.add(row); + String key = createRowKey(dataSet2, pks, i, context); + DataSetRow row = new DefaultDataSetRow(dataSet2, i); + map.put(key, row); } - return DataSetUtil.createDynamicDataSet(set1); + return DataSetUtil.createDynamicDataSet(map); } } 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 1abbbf94751fc852387faa9a059b2a53b4026b7e..bd3c68c41e20b213409582b68b8b7f770b4dbdc5 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 @@ -14,6 +14,7 @@ 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.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptContextUtil; import org.tinygroup.tinyscript.interpret.ScriptResult; import org.tinygroup.tinyscript.interpret.call.FunctionCallExpressionParameter; @@ -35,19 +36,19 @@ public class DataSetUpdateFunction extends AbstractScriptFunction { public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { try { if (parameters == null || parameters.length == 0) { - throw new ScriptException("update函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); } else if (checkParameters(parameters, 3)) { AbstractDataSet dataSet = (AbstractDataSet) getValue(parameters[0]); String colName = (String) getValue(parameters[1]); return updateExpression(dataSet, colName, parameters[2], context); } else { - throw new ScriptException("update函数的参数格式不正确!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("update函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } @@ -59,7 +60,8 @@ public class DataSetUpdateFunction extends AbstractScriptFunction { expression = getExpression(parameter); int col = DataSetUtil.getFieldIndex(dataSet, colName); if (col < 0) { - throw new ScriptException(String.format("根据字段%s没有在数据集找到匹配列", colName)); + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.notfound", colName)); } Set columns = null; @@ -71,13 +73,11 @@ public class DataSetUpdateFunction extends AbstractScriptFunction { expression = ScriptContextUtil.convertExpression(expression); Object function = null; - if (expression.indexOf("->")>0){ - function = ((FunctionCallExpressionParameter) (parameter)).eval(); - } - else{ + if (expression.indexOf("->") > 0) { + function = ((FunctionCallExpressionParameter) (parameter)).eval(); + } else { function = expression; } - if (dataSet instanceof GroupDataSet) { GroupDataSet groupDataSet = (GroupDataSet) dataSet; @@ -93,7 +93,7 @@ public class DataSetUpdateFunction extends AbstractScriptFunction { } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("执行update函数发生异常", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetXorFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetXorFunction.java index 7e380f3156b3167ff554ac663b6005d9582078e9..5decc1c5558234045e0acae6d8a270dbb8876044 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetXorFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/DataSetXorFunction.java @@ -1,7 +1,7 @@ package org.tinygroup.tinyscript.dataset.function; -import java.util.HashSet; -import java.util.Set; +import java.util.LinkedHashMap; +import java.util.Map; import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; @@ -48,18 +48,19 @@ public class DataSetXorFunction extends AbstractDataSetOperateFunction { @Override protected DataSet operate(AbstractDataSet dataSet1, AbstractDataSet dataSet2, Object pks, ScriptContext context) throws Exception { - Set newRows = new HashSet(); - Set set = createDataSetRows(dataSet1, pks, context); + Map map = createMapDataSetRows(dataSet1, pks, context); + Map newMap = new LinkedHashMap(); for (int i = 1; i <= dataSet2.getRows(); i++) { - DataSetRow row = new DefaultDataSetRow(dataSet2, i, createRowComparator(dataSet2, pks, context)); - if (!set.contains(row)) { - newRows.add(row); + String key = createRowKey(dataSet2, pks, i, context); + DataSetRow row = new DefaultDataSetRow(dataSet2, i); + if (!map.containsKey(key)) { + newMap.put(key, row); } else { - set.remove(row); + map.remove(key); } } - newRows.addAll(set); - return DataSetUtil.createDynamicDataSet(newRows); + newMap.putAll(map); + return DataSetUtil.createDynamicDataSet(newMap); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/FirstRowFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/FirstRowFunction.java index 4685163704cbcedc1deebbbb5e0307f275427822..86d69dc0364c220621c65d4775d6150d0e96f502 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/FirstRowFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/FirstRowFunction.java @@ -4,30 +4,32 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * first函数 + * * @author yancheng11334 * */ -public class FirstRowFunction extends AbstractVisitRowFunction{ +public class FirstRowFunction extends AbstractVisitRowFunction { public String getNames() { return "firstRow"; } - protected DataSetRow visit(DynamicDataSet dataSet, int index) - throws ScriptException { + protected DataSetRow visit(DynamicDataSet dataSet, int index) throws ScriptException { try { return DataSetUtil.createDataSetRow(dataSet, dataSet.getShowIndex(index)); } catch (Exception e) { - throw new ScriptException(String.format("%s函数操作失败", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected void checkIndex(int index) throws Exception{ - if(index<0){ - throw new ScriptException(String.format("%s函数操作下标不能是负整数", getNames())); + + protected void checkIndex(int index) throws Exception { + if (index < 0) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", getNames(), index)); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetAggregateFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetAggregateFunction.java index 3a01656076443be440fa1dc8b7660262fe56c66c..bbe5dd7f908f7da569e4032eccc85773543cca36 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetAggregateFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetAggregateFunction.java @@ -14,110 +14,114 @@ import org.tinygroup.tinyscript.dataset.impl.AggregateResult; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; import org.tinygroup.tinyscript.expression.ExpressionUtil; import org.tinygroup.tinyscript.function.DynamicNameScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptContextUtil; /** * 数据集分组聚合函数 + * * @author yancheng11334 * */ -public class GroupDataSetAggregateFunction extends DynamicNameScriptFunction{ +public class GroupDataSetAggregateFunction extends DynamicNameScriptFunction { public String getBindingTypes() { return GroupDataSet.class.getName(); } - - public boolean enableExpressionParameter(){ + + public boolean enableExpressionParameter() { return true; } - - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { + + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { String functionName = ScriptContextUtil.getDynamicFunctionName(context); - try{ + try { if (parameters == null || parameters.length == 0) { - throw new ScriptException(String.format("%s聚合函数的参数为空!", functionName)); - }else if(checkParameters(parameters, 2)){ + throw new ScriptException( + ResourceBundleUtil.getDefaultMessage("function.parameter.empty", functionName)); + } else if (checkParameters(parameters, 2)) { GroupDataSet dataSet = (GroupDataSet) getValue(parameters[0]); String fieldName = (String) getValue(parameters[1]); - return executeGroupDataSet(dataSet,fieldName,functionName); - - }else{ - throw new ScriptException(String.format("%s聚合函数的参数格式不正确!", functionName)); + return executeGroupDataSet(dataSet, fieldName, functionName); + + } else { + throw new ScriptException( + ResourceBundleUtil.getDefaultMessage("function.parameter.error", functionName)); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException(String.format("%s聚合函数的参数格式不正确!",functionName),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", functionName), e); } } - - protected Object executeGroupDataSet(GroupDataSet groupDataSet,String fieldName,String functionName) throws Exception{ - String aggregateName = createAggregateName(functionName,fieldName); + + protected Object executeGroupDataSet(GroupDataSet groupDataSet, String fieldName, String functionName) + throws Exception { + String aggregateName = createAggregateName(functionName, fieldName); groupDataSet.createAggregateResult(aggregateName); - int col = getColumn(groupDataSet,fieldName); - for(int i=0;i parameterList = new ArrayList(); - for(int i=0;i=0){ + int index = DataSetUtil.getFieldIndex(dataSet, name); + if (index >= 0) { return index; - }else{ - if(dataSet instanceof GroupDataSet){ - GroupDataSet groupDataSet =(GroupDataSet) dataSet; - List result = groupDataSet.getAggregateResultList(); - for(int i=0;i result = groupDataSet.getAggregateResultList(); + for (int i = 0; i < result.size(); i++) { + if (name.equals(result.get(i).getName())) { + return groupDataSet.getFields().size() + i; } } } - } + } } return -1; } - - protected String createAggregateName(String functionName,Object obj){ + + protected String createAggregateName(String functionName, Object obj) { StringBuilder sb = new StringBuilder(); sb.append(functionName).append("_"); - if(obj instanceof Integer){ - Integer index = (Integer) obj; - sb.append(index.intValue()); - }else if(obj instanceof String){ - String name = (String) obj; - sb.append(name); + if (obj instanceof Integer) { + Integer index = (Integer) obj; + sb.append(index.intValue()); + } else if (obj instanceof String) { + String name = (String) obj; + sb.append(name); } return sb.toString(); } public boolean exsitFunctionName(String name) { - return ExpressionUtil.getNumberCalculator(getCalculatorName(name))!=null; + return ExpressionUtil.getNumberCalculator(getCalculatorName(name)) != null; } - - protected String getCalculatorName(String name){ - if(name.endsWith("Group")){ - return name.substring(0,name.length()-5); + + protected String getCalculatorName(String name) { + if (name.endsWith("Group")) { + return name.substring(0, name.length() - 5); } return null; } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetFilterFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetFilterFunction.java index d424accc94d0eb31b9e0b47f152fa72f85ca9bc9..6623c5c871362ff9bada33cced6683cb78609b85 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetFilterFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetFilterFunction.java @@ -18,6 +18,7 @@ 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.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.call.FunctionCallExpressionParameter; public class GroupDataSetFilterFunction extends AbstractScriptFunction { @@ -37,19 +38,19 @@ public class GroupDataSetFilterFunction extends AbstractScriptFunction { public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { try { if (parameters == null || parameters.length == 0) { - throw new ScriptException("filterGroup函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); } else if (checkParameters(parameters, 2)) { GroupDataSet groupDataSet = (GroupDataSet) getValue(parameters[0]); LambdaFunction expression = (LambdaFunction) (((FunctionCallExpressionParameter) parameters[1]).eval()); return filterGroup(context, groupDataSet, expression); } else { - throw new ScriptException("filterGroup函数的参数格式不正确"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("filterGroup函数的参数格式不正确!", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetSortFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetSortFunction.java index 78b67805ea6a82c5a4f87795d23b74730a239a6e..3e8d70e9a1122c658d202d30157a1ee3725a955c 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetSortFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/GroupDataSetSortFunction.java @@ -5,43 +5,45 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.GroupDataSet; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 分组排序 + * * @author yancheng11334 * */ -public class GroupDataSetSortFunction extends DataSetSortFunction{ +public class GroupDataSetSortFunction extends DataSetSortFunction { public String getNames() { return "sortGroup"; } - + public String getBindingTypes() { return GroupDataSet.class.getName(); } - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - if(parameters==null || parameters.length==0){ - throw new ScriptException("sortGroup函数的参数为空!"); - }else if(checkParameters(parameters, 2)){ - return groupSort(segment,context,(GroupDataSet)parameters[0],(String)parameters[1]); - }else{ - throw new ScriptException("sortGroup函数的参数格式不正确!"); + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + if (parameters == null || parameters.length == 0) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 2)) { + return groupSort(segment, context, (GroupDataSet) parameters[0], (String) parameters[1]); + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } } - protected Object groupSort(ScriptSegment segment, ScriptContext context,GroupDataSet groupDataSet,String rule) throws ScriptException{ - try{ - for(DataSet dataSet:groupDataSet.getGroups()){ - sort(segment, context, dataSet, rule); + protected Object groupSort(ScriptSegment segment, ScriptContext context, GroupDataSet groupDataSet, String rule) + throws ScriptException { + try { + for (DataSet dataSet : groupDataSet.getGroups()) { + sort(segment, context, dataSet, rule); } return groupDataSet; - }catch(ScriptException e){ + } catch (ScriptException e) { throw e; - }catch(Exception e){ - throw new ScriptException(String.format("序列按规则[%s]分组排序发生异常:", rule),e); + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("dataset", "dataset.sort.error", rule), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/LastRowFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/LastRowFunction.java index 3c7793c438ffad980cbb629c33b495193de31743..7dda06206f12f2c2fb09c7e879faad403f4d5712 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/LastRowFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/LastRowFunction.java @@ -4,30 +4,32 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * last函数 + * * @author yancheng11334 * */ -public class LastRowFunction extends AbstractVisitRowFunction{ +public class LastRowFunction extends AbstractVisitRowFunction { public String getNames() { return "lastRow"; } - protected DataSetRow visit(DynamicDataSet dataSet, int index) - throws ScriptException { + protected DataSetRow visit(DynamicDataSet dataSet, int index) throws ScriptException { try { - return DataSetUtil.createDataSetRow(dataSet, dataSet.getShowIndex(dataSet.getRows()-1+index)); + return DataSetUtil.createDataSetRow(dataSet, dataSet.getShowIndex(dataSet.getRows() - 1 + index)); } catch (Exception e) { - throw new ScriptException(String.format("%s函数操作失败", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected void checkIndex(int index) throws Exception{ - if(index>0){ - throw new ScriptException(String.format("%s函数操作下标不能是正整数", getNames())); + + protected void checkIndex(int index) throws Exception { + if (index > 0) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", getNames(), index)); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/NextRowFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/NextRowFunction.java index eb87ec0cc1e5806715cdff25017c3221b447a393..7100491c3f610fe04a4539502a79f7f76c871d27 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/NextRowFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/NextRowFunction.java @@ -4,30 +4,32 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * next函数 + * * @author yancheng11334 * */ -public class NextRowFunction extends AbstractVisitRowFunction{ +public class NextRowFunction extends AbstractVisitRowFunction { public String getNames() { return "nextRow"; } - protected DataSetRow visit(DynamicDataSet dataSet, int index) - throws ScriptException { + protected DataSetRow visit(DynamicDataSet dataSet, int index) throws ScriptException { try { - return DataSetUtil.createDataSetRow(dataSet, dataSet.getCurrentRow()+index); + return DataSetUtil.createDataSetRow(dataSet, dataSet.getCurrentRow() + index); } catch (Exception e) { - throw new ScriptException(String.format("%s函数操作失败", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected void checkIndex(int index) throws Exception{ - if(index<1){ - throw new ScriptException(String.format("%s函数操作下标是正整数", getNames())); + + protected void checkIndex(int index) throws Exception { + if (index < 1) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", getNames(), index)); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/PreviewRowFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/PreviewRowFunction.java index a12789611b38fb93db0701facf307c47a23e4c89..52485552eee6118f099234406141c431875ea9bd 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/PreviewRowFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/PreviewRowFunction.java @@ -4,30 +4,32 @@ import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * preview函数 + * * @author yancheng11334 * */ -public class PreviewRowFunction extends AbstractVisitRowFunction{ +public class PreviewRowFunction extends AbstractVisitRowFunction { public String getNames() { return "previewRow"; } - protected DataSetRow visit(DynamicDataSet dataSet, int index) - throws ScriptException { + protected DataSetRow visit(DynamicDataSet dataSet, int index) throws ScriptException { try { - return DataSetUtil.createDataSetRow(dataSet, dataSet.getCurrentRow()-index); + return DataSetUtil.createDataSetRow(dataSet, dataSet.getCurrentRow() - index); } catch (Exception e) { - throw new ScriptException(String.format("%s函数操作失败", getNames()),e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - - protected void checkIndex(int index) throws Exception{ - if(index<1){ - throw new ScriptException(String.format("%s函数操作下标是正整数", getNames())); + + protected void checkIndex(int index) throws Exception { + if (index < 1) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", getNames(), index)); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/ToDynamicDataSetFunction.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/ToDynamicDataSetFunction.java index e753430e30cf012c6a69c88915332c5bcd08a23f..8d41dad0e60b51c3cac874c06918235ea25a5faf 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/ToDynamicDataSetFunction.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/function/ToDynamicDataSetFunction.java @@ -7,6 +7,7 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.impl.VariableDataSet; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 转换为动态序表 @@ -27,7 +28,7 @@ public class ToDynamicDataSetFunction extends AbstractScriptFunction { Object... parameters) throws ScriptException { try{ if (parameters == null || parameters.length == 0) { - throw new ScriptException(String.format("%s函数的参数为空!",getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); }else if(checkParameters(parameters, 1)){ DataSet dataSet = (DataSet) parameters[0]; if(dataSet instanceof DynamicDataSet){ @@ -35,12 +36,12 @@ public class ToDynamicDataSetFunction extends AbstractScriptFunction { } return new VariableDataSet(dataSet); }else{ - throw new ScriptException(String.format("%s函数的参数格式不正确!",getNames())); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException(String.format("%s函数的参数格式不正确!",getNames()), e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultDataSetColumn.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultDataSetColumn.java index 9545c86f5ba99805dec6cb6a80fbe29d034951f8..11c2ea4bc7d623f1994afccd812738e9f400e0f9 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultDataSetColumn.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultDataSetColumn.java @@ -5,45 +5,47 @@ import java.util.List; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DataSetColumn; import org.tinygroup.tinyscript.dataset.Field; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 默认的数据集列对象实现 + * * @author yancheng11334 * */ -public class DefaultDataSetColumn implements DataSetColumn{ +public class DefaultDataSetColumn implements DataSetColumn { - private DataSet dataSet; - private int col; //显示值 - - public DefaultDataSetColumn(DataSet dataSet,int col){ + private DataSet dataSet; + private int col; // 显示值 + + public DefaultDataSetColumn(DataSet dataSet, int col) { this.dataSet = dataSet; this.col = col; } - - public DefaultDataSetColumn(DataSet dataSet,String colName) throws Exception{ + + public DefaultDataSetColumn(DataSet dataSet, String colName) throws Exception { this.dataSet = dataSet; List fields = dataSet.getFields(); boolean tag = true; - for(int i=0;i getFields() { return dataSet.getFields(); } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultGroupDataSet.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultGroupDataSet.java index e4a0dfa11d824b4f0b352e5e494ff9ca99151028..3a8b8071bbedf89da07a7785fa1c2f7b9c9dfd95 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultGroupDataSet.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DefaultGroupDataSet.java @@ -5,114 +5,120 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.dataset.GroupDataSet; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 默认的分组数据集实现 + * * @author yancheng11334 * */ public class DefaultGroupDataSet extends GroupDataSet implements Cloneable { private List subDataSetList = new ArrayList(); - private int current = 0; //当前子结果集 - private List aggregateResultList = new ArrayList(); //聚合结果 - - public DefaultGroupDataSet(List fields){ + private int current = 0; // 当前子结果集 + private List aggregateResultList = new ArrayList(); // 聚合结果 + + public DefaultGroupDataSet(List fields) { setFields(fields); } - - public DefaultGroupDataSet(List fields,boolean tag){ + + public DefaultGroupDataSet(List fields, boolean tag) { setFields(fields); setIndexFromOne(tag); } - - public DefaultGroupDataSet(List fields,List groups){ + + public DefaultGroupDataSet(List fields, List groups) { setFields(fields); subDataSetList = groups; } - - public DefaultGroupDataSet(List fields,List groups,boolean tag){ + + public DefaultGroupDataSet(List fields, List groups, boolean tag) { setFields(fields); subDataSetList = groups; setIndexFromOne(tag); } - - public DefaultGroupDataSet(List fields,List groups,List results){ + + public DefaultGroupDataSet(List fields, List groups, List results) { setFields(fields); subDataSetList = groups; aggregateResultList = results; } - - public DefaultGroupDataSet(List fields,List groups,List results,boolean tag){ + + public DefaultGroupDataSet(List fields, List groups, List results, + boolean tag) { setFields(fields); subDataSetList = groups; aggregateResultList = results; setIndexFromOne(tag); } - + public boolean isReadOnly() { return false; } public void first() throws Exception { current = 0; - for(DynamicDataSet subDs:subDataSetList){ - subDs.first(); + for (DynamicDataSet subDs : subDataSetList) { + subDs.first(); } } - + /** * 本方法参数为实际下标 + * * @param i * @return */ - private DynamicDataSet getSubDataSet(int i){ + private DynamicDataSet getSubDataSet(int i) { return subDataSetList.get(i); } public boolean previous() throws Exception { - if(current>0){ - current--; - return true; - }else{ - return false; + if (current > 0) { + current--; + return true; + } else { + return false; } - + } public void beforeFirst() throws Exception { - throw new Exception("本数据集实现不支持beforeFirst操作"); + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "beforeFirst")); } public void afterLast() throws Exception { - throw new Exception("本数据集实现不支持afterLast操作"); + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "afterLast")); } public boolean next() throws Exception { - if(current=1 && row<=subDataSetList.size()){ - current = row-1; - return true; - } - }else{ - if(row>=0 && row<=subDataSetList.size()-1){ - current = row; - return true; - } + if (isIndexFromOne()) { + if (row >= 1 && row <= subDataSetList.size()) { + current = row - 1; + return true; + } + } else { + if (row >= 0 && row <= subDataSetList.size() - 1) { + current = row; + return true; + } } return false; } @@ -122,102 +128,102 @@ public class DefaultGroupDataSet extends GroupDataSet implements Cloneable { } public int getColumns() throws Exception { - return getFields().size()+aggregateResultList.size(); + return getFields().size() + aggregateResultList.size(); } @SuppressWarnings("unchecked") public T getData(int row, int col) throws Exception { int fieldNum = getFields().size(); - - if(isIndexFromOne()){ - if(col>=fieldNum+1){ - //取聚合结果 - return (T) aggregateResultList.get(getActualIndex(col-fieldNum)).getData(getActualIndex(row)); - }else{ - return getSubDataSet(getActualIndex(row)).getData(1,col); - } - }else{ - if(col>=fieldNum){ - //取聚合结果 - return (T) aggregateResultList.get(getActualIndex(col-fieldNum)).getData(getActualIndex(row)); - }else{ - return getSubDataSet(getActualIndex(row)).getData(0,col); - } + + if (isIndexFromOne()) { + if (col >= fieldNum + 1) { + // 取聚合结果 + return (T) aggregateResultList.get(getActualIndex(col - fieldNum)).getData(getActualIndex(row)); + } else { + return getSubDataSet(getActualIndex(row)).getData(1, col); + } + } else { + if (col >= fieldNum) { + // 取聚合结果 + return (T) aggregateResultList.get(getActualIndex(col - fieldNum)).getData(getActualIndex(row)); + } else { + return getSubDataSet(getActualIndex(row)).getData(0, col); + } } - + } public void setData(int row, int col, T data) throws Exception { int fieldNum = getFields().size(); - - if(isIndexFromOne()){ - if(col>=fieldNum+1){ - //设置聚合结果 - aggregateResultList.get(getActualIndex(col-fieldNum)).setData(getActualIndex(row), data); - }else{ - getSubDataSet(getActualIndex(row)).setData(1, col, data); - } - }else{ - if(col>=fieldNum){ - //设置聚合结果 - aggregateResultList.get(getActualIndex(col-fieldNum)).setData(getActualIndex(row), data); - }else{ - getSubDataSet(getActualIndex(row)).setData(0, col, data); - } + + if (isIndexFromOne()) { + if (col >= fieldNum + 1) { + // 设置聚合结果 + aggregateResultList.get(getActualIndex(col - fieldNum)).setData(getActualIndex(row), data); + } else { + getSubDataSet(getActualIndex(row)).setData(1, col, data); + } + } else { + if (col >= fieldNum) { + // 设置聚合结果 + aggregateResultList.get(getActualIndex(col - fieldNum)).setData(getActualIndex(row), data); + } else { + getSubDataSet(getActualIndex(row)).setData(0, col, data); + } } - + } public T getData(int col) throws Exception { - return getData(getShowIndex(current),col); + return getData(getShowIndex(current), col); } public void setData(int col, T data) throws Exception { - setData(getShowIndex(current),col,data); + setData(getShowIndex(current), col, data); } public void setData(String fieldName, T data) throws Exception { try { int col = DataSetUtil.getFieldIndex(this, fieldName); - if(isIndexFromOne()){ - if(col>=0){ - getSubDataSet(current).setData(1,col, data); - }else{ - setData(getShowIndex(current),fieldName,data); - } - }else{ - if(col>=0){ - getSubDataSet(current).setData(0,col, data); - }else{ - setData(getShowIndex(current),fieldName,data); - } + if (isIndexFromOne()) { + if (col >= 0) { + getSubDataSet(current).setData(1, col, data); + } else { + setData(getShowIndex(current), fieldName, data); + } + } else { + if (col >= 0) { + getSubDataSet(current).setData(0, col, data); + } else { + setData(getShowIndex(current), fieldName, data); + } } } catch (Exception e) { - throw new Exception("setData操作失败",e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", "setData"), e); } } - + @SuppressWarnings("unchecked") public T getData(int row, String aggregateName) { - for(AggregateResult result:aggregateResultList){ - if(result.getName().equals(aggregateName)){ - return (T) result.getData(getActualIndex(row)); - } + for (AggregateResult result : aggregateResultList) { + if (result.getName().equals(aggregateName)) { + return (T) result.getData(getActualIndex(row)); + } } return null; } public void setData(int row, String aggregateName, T value) { - for(AggregateResult result:aggregateResultList){ - if(result.getName().equals(aggregateName)){ + for (AggregateResult result : aggregateResultList) { + if (result.getName().equals(aggregateName)) { result.setData(getActualIndex(row), value); return; } - } + } } public DynamicDataSet deleteColumn(int col) throws Exception { - for(DynamicDataSet subDs:subDataSetList){ + for (DynamicDataSet subDs : subDataSetList) { subDs.deleteColumn(col); } getFields().remove(getActualIndex(col)); @@ -225,21 +231,20 @@ public class DefaultGroupDataSet extends GroupDataSet implements Cloneable { } public DynamicDataSet deleteColumn(String colName) throws Exception { - try{ + try { int col = DataSetUtil.getFieldIndex(this, colName); return deleteColumn(getShowIndex(col)); - }catch(Exception e){ - throw new Exception("deleteColumn发生异常",e); + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", "deleteColumn"), e); } } public DynamicDataSet deleteRow(int row) throws Exception { - throw new Exception("本数据集实现不支持deleteRow操作"); + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "deleteRow")); } - public DynamicDataSet insertColumn(int col, Field field) - throws Exception { - for(DynamicDataSet subDs:subDataSetList){ + public DynamicDataSet insertColumn(int col, Field field) throws Exception { + for (DynamicDataSet subDs : subDataSetList) { subDs.insertColumn(col, field); } getFields().add(getActualIndex(col), field); @@ -247,24 +252,24 @@ public class DefaultGroupDataSet extends GroupDataSet implements Cloneable { } public DynamicDataSet insertRow(int row) throws Exception { - throw new Exception("本数据集实现不支持insertRow操作"); + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "insertRow")); } - - public GroupDataSet subGroup(int beginIndex, int endIndex) throws Exception{ + + public GroupDataSet subGroup(int beginIndex, int endIndex) throws Exception { List newDataSetList = new ArrayList(); - - for(DynamicDataSet subDs:subDataSetList){ + + for (DynamicDataSet subDs : subDataSetList) { newDataSetList.add(DataSetUtil.createDynamicDataSet(subDs, beginIndex, endIndex)); } - return new DefaultGroupDataSet(getFields(),newDataSetList); + return new DefaultGroupDataSet(getFields(), newDataSetList); } - + public GroupDataSet subGroup(int beginIndex) throws Exception { List newDataSetList = new ArrayList(); - for(DynamicDataSet subDs:subDataSetList){ - newDataSetList.add(DataSetUtil.createDynamicDataSet(subDs, beginIndex, subDs.getRows()-1)); + for (DynamicDataSet subDs : subDataSetList) { + newDataSetList.add(DataSetUtil.createDynamicDataSet(subDs, beginIndex, subDs.getRows() - 1)); } - return new DefaultGroupDataSet(getFields(),newDataSetList); + return new DefaultGroupDataSet(getFields(), newDataSetList); } public List getGroups() { @@ -274,28 +279,28 @@ public class DefaultGroupDataSet extends GroupDataSet implements Cloneable { public int getCurrentRow() throws Exception { return getShowIndex(current); } - + public void setIndexFromOne(boolean tag) { super.setIndexFromOne(tag); - for(DynamicDataSet subDataSet:subDataSetList){ + for (DynamicDataSet subDataSet : subDataSetList) { subDataSet.setIndexFromOne(tag); } } - + public void createAggregateResult(String aggregateName) { - AggregateResult result = new AggregateResult(aggregateName,subDataSetList.size()); + AggregateResult result = new AggregateResult(aggregateName, subDataSetList.size()); aggregateResultList.add(result); } - - public Object clone() throws CloneNotSupportedException{ + + public Object clone() throws CloneNotSupportedException { DefaultGroupDataSet groupDataSet = (DefaultGroupDataSet) super.clone(); List dataSetList = new ArrayList(); - for(DynamicDataSet subDataSet:subDataSetList){ - DynamicDataSet newDataSet = (DynamicDataSet)subDataSet.cloneDataSet(); + for (DynamicDataSet subDataSet : subDataSetList) { + DynamicDataSet newDataSet = (DynamicDataSet) subDataSet.cloneDataSet(); dataSetList.add(newDataSet); } - List newResultList = new ArrayList(); - for(AggregateResult result:aggregateResultList){ + List newResultList = new ArrayList(); + for (AggregateResult result : aggregateResultList) { newResultList.add(result); } groupDataSet.subDataSetList = dataSetList; @@ -309,55 +314,54 @@ public class DefaultGroupDataSet extends GroupDataSet implements Cloneable { public DataSet merge() throws Exception { List newFields = new ArrayList(); - for(Field field:getFields()){ + for (Field field : getFields()) { newFields.add(field); } Object[][] dataArray = new Object[getRows()][]; - for(int row=0;row sortList = new ArrayList(); //排序中间对象 - for(int i=0;i sortList = new ArrayList(); // 排序中间对象 + for (int i = 0; i < subDataSetList.size(); i++) { + Object[] rowInfo = new Object[getColumns() + 1]; // 多一位记录原始顺序 + for (int j = 0; j < getColumns(); j++) { rowInfo[j] = getData(getShowIndex(i), getShowIndex(j)); } rowInfo[getColumns()] = i; sortList.add(rowInfo); } - //执行排序 + // 执行排序 Collections.sort(sortList, c); List newSubDataSetList = new ArrayList(); - List newAggregateResultList = new ArrayList(); - for(int i=0;i newAggregateResultList = new ArrayList(); + for (int i = 0; i < aggregateResultList.size(); i++) { AggregateResult result = aggregateResultList.get(i); - AggregateResult newResult = new AggregateResult(result.getName(),subDataSetList.size()); + AggregateResult newResult = new AggregateResult(result.getName(), subDataSetList.size()); newAggregateResultList.add(newResult); } - for(int i=0;i pks; - - public ListRowComparator(List pks) { - this.pks = pks; - } - - @Override - public boolean isEqual(DataSetRow o1, DataSetRow o2) { - for (Integer i : pks) { - try { - if (!(o1.getData(i).equals(o2.getData(i)))) { - return false; - } - } catch (Exception e) { - return false; - } - } - return true; - } - - @Override - public int countHash(DataSetRow row){ - int hash = 0; - for (Integer i : pks) { - try { - hash += row.getData(i).toString().hashCode(); - } catch (Exception e) { - //发生异常跳过当前hash值 - } - } - return hash; - } - -} diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/MatchDataSet.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/MatchDataSet.java index 5cb05f2aa90ac34e1dcf270080a247e0bc97aebf..8179c834f5b730678bce67a599557ab22d769ede 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/MatchDataSet.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/MatchDataSet.java @@ -8,13 +8,15 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * match操作后的结果集 + * * @author yancheng11334 * */ -public class MatchDataSet extends DynamicDataSet implements Cloneable{ +public class MatchDataSet extends DynamicDataSet implements Cloneable { private DynamicDataSet left; private DynamicDataSet right; @@ -23,39 +25,44 @@ public class MatchDataSet extends DynamicDataSet implements Cloneable{ /** * 默认构造函数 - * @param left 左数据集 - * @param right 右数据集 - * @param matchList 左右数据集行映射关系 + * + * @param left + * 左数据集 + * @param right + * 右数据集 + * @param matchList + * 左右数据集行映射关系 */ - public MatchDataSet(DataSet left,DataSet right,List matchList){ + public MatchDataSet(DataSet left, DataSet right, List matchList) { this.left = (DynamicDataSet) left; this.right = (DynamicDataSet) right; this.matchList = matchList; resetFields(); } - + /** * 构造函数 + * * @param left * @param right * @param matchList * @param tag */ - public MatchDataSet(DataSet left,DataSet right,List matchList,boolean tag){ - this(left,right,matchList); + public MatchDataSet(DataSet left, DataSet right, List matchList, boolean tag) { + this(left, right, matchList); setIndexFromOne(tag); } - + /** - * 重置字段信息 + * 重置字段信息 */ - protected void resetFields(){ + protected void resetFields() { List newFields = new ArrayList(); newFields.addAll(left.getFields()); newFields.addAll(right.getFields()); setFields(newFields); } - + public boolean isReadOnly() { return false; } @@ -74,11 +81,12 @@ public class MatchDataSet extends DynamicDataSet implements Cloneable{ } public void beforeFirst() throws Exception { - throw new Exception("本数据集实现不支持beforeFirst操作"); + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "beforeFirst")); } public void afterLast() throws Exception { - throw new Exception("本数据集实现不支持afterLast操作"); + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "afterLast")); } public boolean next() throws Exception { @@ -91,18 +99,18 @@ public class MatchDataSet extends DynamicDataSet implements Cloneable{ } public boolean absolute(int row) throws Exception { - if(isIndexFromOne()){ - if (row >= 1 && row <= matchList.size()) { - current = row-1; - return true; - } - }else{ - if (row >= 0 && row <= matchList.size() - 1) { - current = row; - return true; - } + if (isIndexFromOne()) { + if (row >= 1 && row <= matchList.size()) { + current = row - 1; + return true; + } + } else { + if (row >= 0 && row <= matchList.size() - 1) { + current = row; + return true; + } } - + return false; } @@ -111,33 +119,33 @@ public class MatchDataSet extends DynamicDataSet implements Cloneable{ } public int getColumns() throws Exception { - return left.getColumns()+right.getColumns(); + return left.getColumns() + right.getColumns(); } public T getData(int row, int col) throws Exception { int showRow = getActualIndex(row); int[] rows = matchList.get(showRow); int leftColumn = left.getColumns(); - if(isIndexFromOne()){ - if(col>=leftColumn+1){ - if(rows[1]>=0){ - return right.getData(getShowIndex(rows[1]), col-leftColumn); - } - }else{ - if(rows[0]>=0){ - return left.getData(getShowIndex(rows[0]), col); - } - } - }else{ - if(col>=leftColumn){ - if(rows[1]>=0){ - return right.getData(getShowIndex(rows[1]), col-leftColumn); - } - }else{ - if(rows[0]>=0){ - return left.getData(getShowIndex(rows[0]), col); - } - } + if (isIndexFromOne()) { + if (col >= leftColumn + 1) { + if (rows[1] >= 0) { + return right.getData(getShowIndex(rows[1]), col - leftColumn); + } + } else { + if (rows[0] >= 0) { + return left.getData(getShowIndex(rows[0]), col); + } + } + } else { + if (col >= leftColumn) { + if (rows[1] >= 0) { + return right.getData(getShowIndex(rows[1]), col - leftColumn); + } + } else { + if (rows[0] >= 0) { + return left.getData(getShowIndex(rows[0]), col); + } + } } return null; @@ -147,63 +155,63 @@ public class MatchDataSet extends DynamicDataSet implements Cloneable{ int showRow = getActualIndex(row); int[] rows = matchList.get(showRow); int leftColumn = left.getColumns(); - if(isIndexFromOne()){ - if(col>=leftColumn+1){ - if(rows[1]>=0){ - right.setData(getShowIndex(rows[1]), col-leftColumn, data); - } - }else{ - if(rows[0]>=0){ - left.setData(getShowIndex(rows[0]), col,data); - } - } - }else{ - if(col>=leftColumn){ - if(rows[1]>=0){ - right.setData(getShowIndex(rows[1]), col-leftColumn, data); - } - }else{ - if(rows[0]>=0){ - left.setData(getShowIndex(rows[0]), col,data); - } - } - } + if (isIndexFromOne()) { + if (col >= leftColumn + 1) { + if (rows[1] >= 0) { + right.setData(getShowIndex(rows[1]), col - leftColumn, data); + } + } else { + if (rows[0] >= 0) { + left.setData(getShowIndex(rows[0]), col, data); + } + } + } else { + if (col >= leftColumn) { + if (rows[1] >= 0) { + right.setData(getShowIndex(rows[1]), col - leftColumn, data); + } + } else { + if (rows[0] >= 0) { + left.setData(getShowIndex(rows[0]), col, data); + } + } + } } public T getData(int col) throws Exception { - return getData(getShowIndex(current),col); + return getData(getShowIndex(current), col); } public void setData(int col, T data) throws Exception { - setData(getShowIndex(current),col,data); + setData(getShowIndex(current), col, data); } public DynamicDataSet deleteColumn(int col) throws Exception { int leftColumn = left.getColumns(); - if(isIndexFromOne()){ - if(col>=leftColumn+1){ - right.deleteColumn(col-leftColumn); - }else{ - left.deleteColumn(col); - } - }else{ - if(col>=leftColumn){ - right.deleteColumn(col-leftColumn); - }else{ - left.deleteColumn(col); - } + if (isIndexFromOne()) { + if (col >= leftColumn + 1) { + right.deleteColumn(col - leftColumn); + } else { + left.deleteColumn(col); + } + } else { + if (col >= leftColumn) { + right.deleteColumn(col - leftColumn); + } else { + left.deleteColumn(col); + } } - + resetFields(); return this; } public DynamicDataSet deleteColumn(String colName) throws Exception { Integer index = getColumn(colName); - if(index==null){ - throw new Exception(String.format("本数据集没有找到字段%s", colName)); - } + if (index == null) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.notfound", colName)); + } deleteColumn(getShowIndex(index)); return this; } @@ -213,18 +221,18 @@ public class MatchDataSet extends DynamicDataSet implements Cloneable{ return this; } - public DynamicDataSet insertColumn(int col, Field field) - throws Exception { - throw new Exception("本数据集实现不支持insertColumn操作"); + public DynamicDataSet insertColumn(int col, Field field) throws Exception { + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "insertColumn")); } public DynamicDataSet insertRow(int row) throws Exception { - throw new Exception("本数据集实现不支持insertRow操作"); + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "insertRow")); } @SuppressWarnings("rawtypes") public DynamicDataSet sort(Comparator c) throws Exception { - throw new Exception("本数据集实现不支持sort操作"); + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.operate.nosupport", "sort")); } public int getCurrentRow() throws Exception { @@ -236,24 +244,25 @@ public class MatchDataSet extends DynamicDataSet implements Cloneable{ right.setIndexFromOne(tag); left.setIndexFromOne(tag); } - - public DataSet clone() throws CloneNotSupportedException{ - try{ + + public DataSet clone() throws CloneNotSupportedException { + try { Object[][] dataArray = new Object[matchList.size()][]; - for(int row=0;row newFields = new ArrayList(); - for(Field field:getFields()){ + for (Field field : getFields()) { newFields.add(field); } - return DataSetUtil.createDynamicDataSet(newFields, dataArray,isIndexFromOne()); - }catch(Exception e){ - throw new CloneNotSupportedException("MatchDataSet克隆失败:"+e.getMessage()); + return DataSetUtil.createDynamicDataSet(newFields, dataArray, isIndexFromOne()); + } catch (Exception e) { + throw new CloneNotSupportedException(ResourceBundleUtil.getResourceMessage("dataset", "dataset.clone.error", + MatchDataSet.class.getSimpleName())); } - + } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/SimpleDataSet.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/SimpleDataSet.java index ddbf7abf5c2bca753fe0b55fc28d20a3485e050f..75dd8051bb051a0bba3cc1eaebc3c73935a0a3eb 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/SimpleDataSet.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/SimpleDataSet.java @@ -8,146 +8,149 @@ import java.util.List; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DynamicDataSet; import org.tinygroup.tinyscript.dataset.Field; - +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * Created by luoguo on 2014/7/4. */ public class SimpleDataSet extends DynamicDataSet implements Cloneable { - private Object[][] dataArray; - private int currentRow = -1; //对应实际下标,而非显示下标 - - public SimpleDataSet(List fields, Object[][] dataArray) { - this.dataArray = dataArray; - super.setFields(fields); - } - - public SimpleDataSet(List fields, Object[][] dataArray,boolean tag) { - this.dataArray = dataArray; - super.setFields(fields); - this.setIndexFromOne(tag); - } - - public Object[][] getDataArray(){ - return dataArray; - } - - public boolean isReadOnly() { - return false; - } - - public void first() throws Exception { - checkValidate(); - currentRow = 0; - } - - private void checkValidate() throws Exception { - if (dataArray != null && dataArray.length == 0) { - throw new Exception("没有数据存在,无法移动到第一行!"); - } - } - - public boolean previous() throws Exception { - checkValidate(); - if (currentRow == 0) { - throw new Exception("已经移动到第一行,无法继续前移!"); - } - currentRow--; - return true; - } - - public void beforeFirst() throws Exception { - checkValidate(); - currentRow = -1; - } - - - public void afterLast() throws Exception { - checkValidate(); - currentRow = dataArray.length; - } - - public boolean next() throws Exception { - checkValidate(); - if (currentRow == dataArray.length - 1) { - return false; - } - currentRow++; - return true; - } - - public boolean absolute(int row) throws Exception { - checkValidate(); - if(isIndexFromOne()){ - if (row < 1 || row >= dataArray.length+1) { - throw new Exception(String.format("指定的行数%s不合法或越界!", row)); - } - }else{ - if (row < 0 || row >= dataArray.length) { - throw new Exception(String.format("指定的行数%s不合法或越界!", row)); - } - } - currentRow = getActualIndex(row); - return true; - } - - public int getRows() throws Exception { - return dataArray==null?0:dataArray.length; - } - - public int getColumns() { - return getFields().size(); - } - - @SuppressWarnings("unchecked") + private Object[][] dataArray; + private int currentRow = -1; // 对应实际下标,而非显示下标 + + public SimpleDataSet(List fields, Object[][] dataArray) { + this.dataArray = dataArray; + super.setFields(fields); + } + + public SimpleDataSet(List fields, Object[][] dataArray, boolean tag) { + this.dataArray = dataArray; + super.setFields(fields); + this.setIndexFromOne(tag); + } + + public Object[][] getDataArray() { + return dataArray; + } + + public boolean isReadOnly() { + return false; + } + + public void first() throws Exception { + checkValidate(); + currentRow = 0; + } + + private void checkValidate() throws Exception { + if (dataArray != null && dataArray.length == 0) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.notexist", "1")); + } + } + + public boolean previous() throws Exception { + checkValidate(); + if (currentRow == 0) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.move.error", "0")); + } + currentRow--; + return true; + } + + public void beforeFirst() throws Exception { + checkValidate(); + currentRow = -1; + } + + public void afterLast() throws Exception { + checkValidate(); + currentRow = dataArray.length; + } + + public boolean next() throws Exception { + checkValidate(); + if (currentRow == dataArray.length - 1) { + return false; + } + currentRow++; + return true; + } + + public boolean absolute(int row) throws Exception { + checkValidate(); + if (isIndexFromOne()) { + if (row < 1 || row >= dataArray.length + 1) { + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", "absolute", row)); + } + } else { + if (row < 0 || row >= dataArray.length) { + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", "absolute", row)); + } + } + currentRow = getActualIndex(row); + return true; + } + + public int getRows() throws Exception { + return dataArray == null ? 0 : dataArray.length; + } + + public int getColumns() { + return getFields().size(); + } + + @SuppressWarnings("unchecked") public T getData(int row, int col) { - row = getActualIndex(row); + row = getActualIndex(row); col = getActualIndex(col); - return (T) dataArray[row][col]; - } + return (T) dataArray[row][col]; + } - public void setData(int row, int col, T newData) throws Exception { - row = getActualIndex(row); + public void setData(int row, int col, T newData) throws Exception { + row = getActualIndex(row); col = getActualIndex(col); - dataArray[row][col] = newData; - } + dataArray[row][col] = newData; + } public T getData(int col) { - return getData(getShowIndex(currentRow), col); - } + return getData(getShowIndex(currentRow), col); + } - public void setData(int col, T data) throws Exception { - setData(getShowIndex(currentRow), col, data); - } + public void setData(int col, T data) throws Exception { + setData(getShowIndex(currentRow), col, data); + } - public void clean() { - super.clean(); - dataArray = null; - } + public void clean() { + super.clean(); + dataArray = null; + } public DynamicDataSet deleteColumn(int col) throws Exception { int colNum = getColumns(); - if(isIndexFromOne()){ - if(col<1 || col>= colNum+1){ - throw new Exception(String.format("删除第%s列数据越界.", col)); - } - }else{ - if(col<0 || col>= colNum){ - throw new Exception(String.format("删除第%s列数据越界.", col)); - } + if (isIndexFromOne()) { + if (col < 1 || col >= colNum + 1) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", + "deleteColumn", col)); + } + } else { + if (col < 0 || col >= colNum) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", + "deleteColumn", col)); + } } - + col = getActualIndex(col); - //删除列数据 - for(int i=0;i list = new ArrayList(); - for(Object obj:dataArray[i]){ + for (Object obj : dataArray[i]) { list.add(obj); } list.remove(col); dataArray[i] = list.toArray(); } - //删除字段 + // 删除字段 fields.remove(col); setFields(fields); return this; @@ -155,40 +158,42 @@ public class SimpleDataSet extends DynamicDataSet implements Cloneable { public DynamicDataSet deleteColumn(String colName) throws Exception { Integer index = getColumn(colName); - if(index==null){ - throw new Exception(String.format("本数据集没有找到字段%s", colName)); - } + if (index == null) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.fields.notfound", colName)); + } deleteColumn(getShowIndex(index)); return this; } public DynamicDataSet deleteRow(int row) throws Exception { int rowNum = getRows(); - if(isIndexFromOne()){ - if(row<1 || row>=rowNum+1){ - throw new Exception(String.format("删除第%s行数据越界.", row)); - } - }else{ - if(row<0 || row>=rowNum){ - throw new Exception(String.format("删除第%s行数据越界.", row)); - } + if (isIndexFromOne()) { + if (row < 1 || row >= rowNum + 1) { + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", "deleteRow", row)); + } + } else { + if (row < 0 || row >= rowNum) { + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", "deleteRow", row)); + } } row = getActualIndex(row); - - //删除行数据 - Object[][] newArray = new Object[rowNum-1][]; - int k=-1; - for(int i=0;i=row){ + // 调整指针 + if (currentRow >= row) { currentRow--; } return this; @@ -196,28 +201,30 @@ public class SimpleDataSet extends DynamicDataSet implements Cloneable { public DynamicDataSet insertColumn(int col, Field field) throws Exception { int colNum = getColumns(); - if(isIndexFromOne()){ - if(col<1 || col>= colNum+1){ - throw new Exception(String.format("插入第%s列数据越界.", col)); - } - }else{ - if(col<0 || col>= colNum){ - throw new Exception(String.format("插入第%s列数据越界.", col)); - } + if (isIndexFromOne()) { + if (col < 1 || col >= colNum + 1) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", + "insertColumn", col)); + } + } else { + if (col < 0 || col >= colNum) { + throw new Exception(ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", + "insertColumn", col)); + } } - + col = getActualIndex(col); - - //插入列数据 - for(int i=0;i list = new ArrayList(); - for(Object obj:dataArray[i]){ + for (Object obj : dataArray[i]) { list.add(obj); } list.add(col, null); dataArray[i] = list.toArray(); } - //插入字段 + // 插入字段 fields.add(col, field); setFields(fields); return this; @@ -225,41 +232,43 @@ public class SimpleDataSet extends DynamicDataSet implements Cloneable { public DynamicDataSet insertRow(int row) throws Exception { int rowNum = getRows(); - - if(isIndexFromOne()){ - if(row<1 || row>rowNum+1){ - throw new Exception(String.format("插入第%s行数据越界.", row)); - } - }else{ - if(row<0 || row>rowNum){ - throw new Exception(String.format("插入第%s行数据越界.", row)); - } + + if (isIndexFromOne()) { + if (row < 1 || row > rowNum + 1) { + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", "insertRow", row)); + } + } else { + if (row < 0 || row > rowNum) { + throw new Exception( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.row.outofindex", "insertRow", row)); + } } - + row = getActualIndex(row); - //插入行数据 - Object[][] newArray = new Object[rowNum+1][]; - if(row==rowNum){ - for(int i=0;i=row){ - currentRow++; + // 调整指针 + if (currentRow >= row) { + currentRow++; } return this; } @@ -273,26 +282,26 @@ public class SimpleDataSet extends DynamicDataSet implements Cloneable { public int getCurrentRow() { return getShowIndex(currentRow); } - - public DataSet cloneDataSet() throws CloneNotSupportedException{ - return clone(); - } - - public DataSet clone() throws CloneNotSupportedException{ + + public DataSet cloneDataSet() throws CloneNotSupportedException { + return clone(); + } + + public DataSet clone() throws CloneNotSupportedException { SimpleDataSet dataSet = (SimpleDataSet) super.clone(); List newFields = new ArrayList(); - for(Field field:getFields()){ + for (Field field : getFields()) { newFields.add(field); } dataSet.setFields(newFields); dataSet.dataArray = new Object[dataArray.length][]; - for(int i=0;i< dataArray.length;i++){ + for (int i = 0; i < dataArray.length; i++) { dataSet.dataArray[i] = new Object[dataArray[i].length]; - for(int j=0;j list = new ArrayList(); AbstractDataSet abstractDataSet = (AbstractDataSet) dataSet; - while(dataSet.next()){ + while (dataSet.next()) { Object[] record = new Object[col]; - for(int i=0;i getFields() { - return variableDataSet.getFields(); - } + return variableDataSet.getFields(); + } public int getRows() throws Exception { return variableDataSet.getRows(); @@ -93,10 +96,10 @@ public class VariableDataSet extends DynamicDataSet implements Cloneable { public int getColumns() throws Exception { return variableDataSet.getColumns(); } - + public T getData(String filedName) throws Exception { - return variableDataSet.getData(filedName); - } + return variableDataSet.getData(filedName); + } public T getData(int row, int col) throws Exception { return variableDataSet.getData(row, col); @@ -127,7 +130,7 @@ public class VariableDataSet extends DynamicDataSet implements Cloneable { } public DynamicDataSet deleteRow(int row) throws Exception { - return variableDataSet.deleteRow(row); + return variableDataSet.deleteRow(row); } public DynamicDataSet insertColumn(int col, Field field) throws Exception { @@ -135,7 +138,7 @@ public class VariableDataSet extends DynamicDataSet implements Cloneable { } public DynamicDataSet insertRow(int row) throws Exception { - return variableDataSet.insertRow(row); + return variableDataSet.insertRow(row); } @SuppressWarnings("rawtypes") @@ -143,21 +146,21 @@ public class VariableDataSet extends DynamicDataSet implements Cloneable { return variableDataSet.sort(c); } - public int getCurrentRow() throws Exception{ + public int getCurrentRow() throws Exception { return variableDataSet.getCurrentRow(); } - + public boolean isIndexFromOne() { return variableDataSet.isIndexFromOne(); } - + public void setIndexFromOne(boolean tag) { variableDataSet.setIndexFromOne(tag); } - public Object clone() throws CloneNotSupportedException{ + public Object clone() throws CloneNotSupportedException { VariableDataSet dataSet = (VariableDataSet) super.clone(); dataSet.variableDataSet = (DynamicDataSet) variableDataSet.cloneDataSet(); - return dataSet; + return dataSet; } } diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/objectitem/DataSetItemProcessor.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/objectitem/DataSetItemProcessor.java index fbcf56afa3d2c205fc19068e09cba69c8b9839e5..1facbdb47717ae00a702a944955d829904646a6f 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/objectitem/DataSetItemProcessor.java +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/objectitem/DataSetItemProcessor.java @@ -6,102 +6,103 @@ import org.tinygroup.tinyscript.dataset.AbstractDataSet; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.DataSetRow; import org.tinygroup.tinyscript.dataset.util.DataSetUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.objectitem.ObjectItemProcessor; /** * 下标方式访问数据集 + * * @author yancheng11334 * */ -public class DataSetItemProcessor implements ObjectItemProcessor{ +public class DataSetItemProcessor implements ObjectItemProcessor { public boolean isMatch(Object obj, Object... items) { return obj instanceof DataSet; } - public Object process(ScriptContext context, Object obj, Object... items) - throws Exception { + public Object process(ScriptContext context, Object obj, Object... items) throws Exception { AbstractDataSet abstractDataSet = (AbstractDataSet) obj; - if(DataSetUtil.isField(items[0],context)){ - String str = (String)DataSetUtil.getValue(items[0],context); - if(DataSetUtil.isExcelCell(str)){ + if (DataSetUtil.isField(items[0], context)) { + String str = (String) DataSetUtil.getValue(items[0], context); + if (DataSetUtil.isExcelCell(str)) { int[] cell = DataSetUtil.getExcelCell(str); - return abstractDataSet.getData(abstractDataSet.getShowIndex(cell[1]), abstractDataSet.getShowIndex(cell[0])); - }else{ + return abstractDataSet.getData(abstractDataSet.getShowIndex(cell[1]), + abstractDataSet.getShowIndex(cell[0])); + } else { return DataSetUtil.createDataSetColumn(abstractDataSet, str); } - - }else{ - int row = (Integer)DataSetUtil.getValue(items[0],context); + + } else { + int row = (Integer) DataSetUtil.getValue(items[0], context); return DataSetUtil.createDataSetRow(abstractDataSet, row); } } - public void assignValue(ScriptContext context, Object value, Object obj, - Object... items) throws Exception { + public void assignValue(ScriptContext context, Object value, Object obj, Object... items) throws Exception { AbstractDataSet abstractDataSet = (AbstractDataSet) obj; - if(value instanceof DataSetRow){ - //行赋值,针对一行记录进行赋值 - assignRowValue(context,(DataSetRow) value,abstractDataSet,items); - }else{ - - if(items.length==1){ - //按excel单元格下标方式赋值 - assignExcelCellValue(context,value,abstractDataSet,(String)items[0]); - }else{ - //行列赋值,针对一行记录的某个字段进行赋值 - assignCellValue(context,value,abstractDataSet,items); + if (value instanceof DataSetRow) { + // 行赋值,针对一行记录进行赋值 + assignRowValue(context, (DataSetRow) value, abstractDataSet, items); + } else { + + if (items.length == 1) { + // 按excel单元格下标方式赋值 + assignExcelCellValue(context, value, abstractDataSet, (String) items[0]); + } else { + // 行列赋值,针对一行记录的某个字段进行赋值 + assignCellValue(context, value, abstractDataSet, items); } - + } - + } - - private void assignRowValue(ScriptContext context,DataSetRow dataSetRow, AbstractDataSet dataSet, - Object... items) throws Exception { - if(DataSetUtil.isField(items[0],context)){ - throw new ScriptException("DataSet对象下标格式错误:行数据不能给列对象赋值"); - }else{ - int row = (Integer)DataSetUtil.getValue(items[0],context); - for(int i=0;i set) throws Exception { - List fields = set.iterator().next().getFields(); - Object[][] data = new Object[set.size()][fields.size()]; + public static DynamicDataSet createDynamicDataSet(Map map) throws Exception { + List fields = null; + Object[][] data = new Object[map.keySet().size()][]; int rowId = 0; - for (DataSetRow row : set) { + boolean isIndexFromOne = false; + + for (String key : map.keySet()) { + DataSetRow row = map.get(key); + isIndexFromOne = row.isIndexFromOne(); + if (fields == null) { + fields = row.getFields(); + } + + data[rowId] = new Object[fields.size()]; for (int i = 0; i < fields.size(); i++) { + data[rowId][i] = row.getData(i + 1); } rowId++; } - return new SimpleDataSet(fields, data, set.iterator().next().isIndexFromOne()); + return new SimpleDataSet(fields, data, isIndexFromOne); } /** @@ -138,8 +150,8 @@ public final class DataSetUtil { SimpleDataSet localDataSet = getSimpleDataSet(dataSet); if (localDataSet == null) { - throw new ScriptException( - String.format("%s类型的数据集无法保持副本,建议转换成SimpleDataSet类型", dataSet.getClass().getName())); + throw new ScriptException(ResourceBundleUtil.getResourceMessage("dataset", "dataset.convertdataset.error", + dataSet.getClass().getSimpleName())); } Object[][] fatherArray = localDataSet.getDataArray(); @@ -171,8 +183,8 @@ public final class DataSetUtil { SimpleDataSet localDataSet = getSimpleDataSet(dataSet); if (localDataSet == null) { - throw new ScriptException( - String.format("%s类型的数据集无法保持副本,建议转换成SimpleDataSet类型", dataSet.getClass().getName())); + throw new ScriptException(ResourceBundleUtil.getResourceMessage("dataset", "dataset.convertdataset.error", + dataSet.getClass().getSimpleName())); } Object[][] fatherArray = localDataSet.getDataArray(); @@ -400,7 +412,8 @@ public final class DataSetUtil { } cell[1] = Integer.parseInt(s.substring(cs.length, s.length())) - 1; } catch (Exception e) { - throw new RuntimeException(String.format("转换Excel单元格下标方式失败:坐标[%s]", s)); + throw new RuntimeException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.convertcell.error", s)); } return cell; diff --git a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties index b120ff285141425eded5f9f8783105c1931aea58..4e152c70f7bf79464cd1981205ccc0455f2ed6d4 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties +++ b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties @@ -1 +1,35 @@ -dataset.fields.inconsistent=Order table parameters are inconsistent! \ No newline at end of file +#DataSet Field Exception +dataset.fields.inconsistent=DataSet parameter is inconsistent! +dataset.fields.notfound=This dataset does not find fields:[%s] +dataset.fields.leftnotfound=The left dataset does not find the field:[%s] +dataset.fields.rightnotfound=The right dataset does not find the field:[%s] +dataset.onlyread.error=This dataset is a read-only dataset and can not modify data! +dataset.parameter.interror=[%s] function's parameter format is incorrect: the subscript [%s] is non-integer +dataset.parameter.executeerror=[%s] function handling the record of the [%d] row, the [%d] column, the value [%s] error occurred: + +#DataSet Conversion Exception +dataset.convertarray.error=The conversion array has an exception +dataset.convertdataset.error=[%s] can not keep a copy, it is recommended to convert to a SimpleDataSet type +dataset.convertcell.error=Convert Excel cell subscripts to failed: [%s] + +#DataSet Subscript Exception +dataset.row.notexist=No data exists, can not move to line [%s] +dataset.row.move.error=Can not move to line [%s] +dataset.row2.outofindex=[%s] function out of index: start line subscript [%d], end line subscript [%d] +dataset.row.outofindex=[%s] function out of index:[%d] +dataset.row.delete.error=Delete line failed +dataset.rlrows.error=Left dataset row [%s], right dataset row [%s], row number mismatch! +dataset.subscript.error=DataSet object subscript format error: line data can not give column object assignment +dataset.subscript.baseerror=DataSet object subscript format error + +#DataSet Other Exception +dataset.addcontext.error=Add column to context error +dataset.fill.error=The fill operation of the DataSet object has occurred abnormally: +dataset.join.condition.error=An exception occurred in the analysis join condition:[%s] +dataset.match.error=Execution match logical match condition exception:[%s] +dataset.filterdata.error=This data set is not a dynamic dataset and can not filter records! +dataset.expression.error=[%s] An exception occurred in the resolution rule:[%s] +dataset.sort.error=An exception occurred in sorted by rule:[%s] +dataset.operate.nosupport=DataSet does not support operations:[%s] +dataset.clone.error=[%s] clone failed: + diff --git a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties index 23ec379ef994f7a2b95e3b53a3fdee4c09697374..1275a373ac347807d61867d54af1d1dcac29b0a6 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties +++ b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties @@ -1 +1,35 @@ -dataset.fields.inconsistent=\u5e8f\u8868\u53c2\u6570\u4e0d\u4e00\u81f4! \ No newline at end of file +#\u5e8f\u8868\u5b57\u6bb5\u5f02\u5e38 +dataset.fields.inconsistent=\u5e8f\u8868\u53c2\u6570\u4e0d\u4e00\u81f4! +dataset.fields.notfound=\u672c\u6570\u636e\u96c6\u6ca1\u6709\u627e\u5230\u5b57\u6bb5:[%s] +dataset.fields.leftnotfound=\u5de6\u6570\u636e\u96c6\u67e5\u4e0d\u5230\u5b57\u6bb5:[%s] +dataset.fields.rightnotfound=\u53f3\u6570\u636e\u96c6\u67e5\u4e0d\u5230\u5b57\u6bb5:[%s] +dataset.onlyread.error=\u672c\u6570\u636e\u96c6\u4e3a\u53ea\u8bfb\u6570\u636e\u96c6\uff0c\u4e0d\u53ef\u4ee5\u4fee\u6539\u6570\u636e\uff01 +dataset.parameter.interror=[%s]\u51fd\u6570\u7684\u53c2\u6570\u683c\u5f0f\u4e0d\u6b63\u786e\u003a\u4e0b\u6807[%s]\u975e\u6574\u578b +dataset.parameter.executeerror=[%s]\u51fd\u6570\u5904\u7406\u7b2c[%d]\u884c\uff0c\u7b2c[%d]\u5217\u002c\u503c[%s]\u7684\u8bb0\u5f55\u53d1\u751f\u9519\u8bef\u003a + +#\u5e8f\u8868\u8f6c\u6362\u5f02\u5e38 +dataset.convertarray.error=\u8f6c\u6362\u6570\u7ec4\u53d1\u751f\u5f02\u5e38 +dataset.convertdataset.error=[%s]\u7c7b\u578b\u7684\u6570\u636e\u96c6\u65e0\u6cd5\u4fdd\u6301\u526f\u672c\u002c\u5efa\u8bae\u8f6c\u6362\u6210\u0053\u0069\u006d\u0070\u006c\u0065\u0044\u0061\u0074\u0061\u0053\u0065\u0074\u7c7b\u578b +dataset.convertcell.error=\u8f6c\u6362\u0045\u0078\u0063\u0065\u006c\u5355\u5143\u683c\u4e0b\u6807\u65b9\u5f0f\u5931\u8d25\u003a\u5750\u6807[%s] + +#\u5e8f\u8868\u4e0b\u6807\u5f02\u5e38 +dataset.row.notexist=\u6ca1\u6709\u6570\u636e\u5b58\u5728\uff0c\u65e0\u6cd5\u79fb\u52a8\u5230\u7b2c[%s]\u884c +dataset.row.move.error=\u65e0\u6cd5\u79fb\u52a8\u5230\u7b2c[%s]\u884c +dataset.row2.outofindex=[%s]\u51fd\u6570\u8d8a\u754c\u003a\u5f00\u59cb\u884c\u4e0b\u6807[%d]\u002c\u7ed3\u675f\u884c\u4e0b\u6807[%d] +dataset.row.outofindex=[%s]\u51fd\u6570\u8d8a\u754c\uff1a\u4e0b\u6807[%d] +dataset.row.delete.error=\u5220\u9664\u884c\u5931\u8d25 +dataset.rlrows.error=\u5de6\u6570\u636e\u96c6\u884c\u6570[%s]\u002c\u53f3\u6570\u636e\u96c6\u884c\u6570[%s]\u002c\u884c\u6570\u4e0d\u5339\u914d\u0021 +dataset.subscript.error=\u0044\u0061\u0074\u0061\u0053\u0065\u0074\u5bf9\u8c61\u4e0b\u6807\u683c\u5f0f\u9519\u8bef\u003a\u884c\u6570\u636e\u4e0d\u80fd\u7ed9\u5217\u5bf9\u8c61\u8d4b\u503c +dataset.subscript.baseerror=\u0044\u0061\u0074\u0061\u0053\u0065\u0074\u5bf9\u8c61\u4e0b\u6807\u683c\u5f0f\u9519\u8bef + +#\u5e8f\u8868\u5176\u4ed6\u5f02\u5e38 +dataset.addcontext.error=\u6dfb\u52a0\u5217\u5230\u4e0a\u4e0b\u6587\u51fa\u9519 +dataset.fill.error=\u0044\u0061\u0074\u0061\u0053\u0065\u0074\u5bf9\u8c61\u7684\u0066\u0069\u006c\u006c\u64cd\u4f5c\u53d1\u751f\u5f02\u5e38\u003a +dataset.join.condition.error=\u89e3\u6790\u006a\u006f\u0069\u006e\u6761\u4ef6\u003a[%s]\u53d1\u751f\u5f02\u5e38 +dataset.match.error=\u6267\u884c\u006d\u0061\u0074\u0063\u0068\u903b\u8f91\u53d1\u751f\u5f02\u5e38\u003a\u5339\u914d\u6761\u4ef6[%s] +dataset.filterdata.error=\u672c\u6570\u636e\u96c6\u4e0d\u662f\u52a8\u6001\u6570\u636e\u96c6\u002c\u65e0\u6cd5\u8fc7\u6ee4\u8bb0\u5f55\u0021 +dataset.expression.error=\u89e3\u6790[%s]\u89c4\u5219[%s]\u5931\u8d25! +dataset.sort.error=\u5e8f\u5217\u6309\u89c4\u5219[%s]\u6392\u5e8f\u53d1\u751f\u5f02\u5e38\u003a +dataset.operate.nosupport=\u6570\u636e\u96c6\u4e0d\u652f\u6301[%s]\u64cd\u4f5c +dataset.clone.error=[%s]\u514b\u9686\u5931\u8d25\u003a + diff --git a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/csv/function/WriteCsvFunction.java b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/csv/function/WriteCsvFunction.java index 6fc30452929c14d795138a8268d21c537c169376..c0bbe5e4a48cd91f07a63f3afbc9307d0f787795 100644 --- a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/csv/function/WriteCsvFunction.java +++ b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/csv/function/WriteCsvFunction.java @@ -96,40 +96,43 @@ public class WriteCsvFunction extends AbstractScriptFunction { } CSVPrinter printer = null; + try { printer = new CSVPrinter(new OutputStreamWriter(csvFile.getOutputStream(), encode), (CSVFormat) (CSVFormat.class.getDeclaredField(type).get(CSVFormat.class))); } catch (NoSuchFieldException e) { throw new ScriptException(ResourceBundleUtil.getResourceMessage("excel", "csv.type.error", type)); } + try { + List records = new ArrayList(); - List records = new ArrayList(); - - for (Field field : dataSet.getFields()) { - records.add(field.getName()); - } + for (Field field : dataSet.getFields()) { + records.add(field.getName()); + } - try { - printer.printRecord(records); - } catch (IOException e) { - printer.close(); - throw new ScriptException(ResourceBundleUtil.getResourceMessage("excel", "csv.write.title.error"), e); - } + try { + printer.printRecord(records); + } catch (IOException e) { + throw new ScriptException(ResourceBundleUtil.getResourceMessage("excel", "csv.write.title.error"), e); + } - for (int i = start; i <= end; i++) { - records = new ArrayList(); - for (int j = 1; j <= dataSet.getColumns(); j++) { - try { - records.add(dataSet.getData(i, j)); - } catch (Exception e) { - printer.close(); - throw new ScriptException(ResourceBundleUtil.getResourceMessage("excel", "write.cell.error", i, j), - e); + for (int i = start; i <= end; i++) { + records = new ArrayList(); + for (int j = 1; j <= dataSet.getColumns(); j++) { + try { + records.add(dataSet.getData(i, j)); + } catch (Exception e) { + printer.close(); + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("excel", "write.cell.error", i, j), e); + } } + printer.printRecord(records); } - printer.printRecord(records); + } finally { + printer.close(); } - printer.close(); + } } diff --git a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/function/WriteExcelFunction.java b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/function/WriteExcelFunction.java index c0d3da6787030156a8ab8358f6511f24f6659d07..a099973402b577cd61f6acb22bd122273b1b829c 100644 --- a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/function/WriteExcelFunction.java +++ b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/function/WriteExcelFunction.java @@ -17,6 +17,7 @@ import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.function.AbstractScriptFunction; import org.tinygroup.tinyscript.interpret.FileObjectUtil; import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; +import org.tinygroup.tinyscript.interpret.exception.NotMatchException; import org.tinygroup.vfs.FileObject; public class WriteExcelFunction extends AbstractScriptFunction { @@ -68,12 +69,13 @@ public class WriteExcelFunction extends AbstractScriptFunction { } return null; + } catch (ClassCastException e) { + throw new NotMatchException(e); } catch (Exception e) { throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - @SuppressWarnings("unchecked") private void writeExcel(String fileUrl, Object dataSet, List sheetNames, int start, int end) throws Exception { Workbook wb = null; @@ -93,31 +95,39 @@ public class WriteExcelFunction extends AbstractScriptFunction { throw new ScriptException(ResourceBundleUtil.getResourceMessage("excel", "file.type.nonsupport")); } - if (dataSet instanceof DataSet) { - writeSheet(wb, (DataSet) dataSet, - (sheetNames == null || sheetNames.size() == 0) ? "sheet1" : sheetNames.get(0), start, - checkEnd((DataSet) dataSet, end)); - } else if (dataSet instanceof List && ((List) dataSet).get(0) instanceof DataSet) { - List dataSets = ((List) dataSet); - - for (int i = 0; i < dataSets.size(); i++) { + try { + if (dataSet instanceof DataSet) { + writeSheet(wb, (DataSet) dataSet, + (sheetNames == null || sheetNames.size() == 0) ? "sheet1" : sheetNames.get(0), start, + checkEnd((DataSet) dataSet, end)); + } else if (dataSet instanceof List) { + + List dataSets = (List) dataSet; + + for (int i = 0; i < dataSets.size(); i++) { + + String sheetName = null; + if (sheetNames != null && sheetNames.size() > i) { + sheetName = sheetNames.get(i); + } else { + sheetName = "sheet" + (i + 1); + } + + try { + writeSheet(wb, (DataSet) dataSets.get(i), sheetName, start, + checkEnd((DataSet) dataSets.get(i), end)); + } catch (ClassCastException e) { + throw new NotMatchException(e); + } catch (ScriptException e1) { + throw e1; + } - String sheetName = null; - if (sheetNames != null && sheetNames.size() > i) { - sheetName = sheetNames.get(i); - } else { - sheetName = "sheet" + (i + 1); } - - writeSheet(wb, dataSets.get(i), sheetName, start, checkEnd(dataSets.get(i), end)); } - } else { + wb.write(excelFile.getOutputStream()); + } finally { wb.close(); - throw new ScriptException(ResourceBundleUtil.getResourceMessage("excel", "dataset.type.nonsupport")); } - - wb.write(excelFile.getOutputStream()); - } private void writeSheet(Workbook wb, DataSet dataSet, String sheetName, int start, int end) throws Exception { diff --git a/org.tinygroup.tinyscript/src/test/resources/example/fund.tinyscript b/org.tinygroup.tinyscript/src/test/resources/example/fund.tinyscript index 3998136977b41c6a42360cb805ad20c791deeed0..182c11558e2dff5feb661df24df341c7e4096532 100644 --- a/org.tinygroup.tinyscript/src/test/resources/example/fund.tinyscript +++ b/org.tinygroup.tinyscript/src/test/resources/example/fund.tinyscript @@ -23,7 +23,7 @@ groupds = groupds.sortGroup("rate desc"); //过滤回撤率大于50%的数据 groupds = groupds.sort("UP desc"); groupds = groupds.limit(1,5); -groupds = groupds.filterGroup(rate[0]<=0.5); +groupds = groupds.filterGroup(()->{return rate[0]<=0.5;}); dataset2 = readTxt("/example/history.txt"); dataset3 = readTxt("/example/score.txt");