|
@@ -1,10 +1,12 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.Windows.Forms;
|
|
|
-using FormulaEnginePoC.FormulaEngine;
|
|
|
+using FormulaEnginePoC.FormulaEngine;
|
|
|
using FormulaEnginePoC.FormulaEngine.CspImpl;
|
|
|
-using FormulaEnginePoC.Mapping;
|
|
|
+using FormulaEnginePoC.FormulaEngine.DepImpl;
|
|
|
using FormulaEnginePoC.SampleClasses;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Windows.Forms;
|
|
|
+using FormulaEnginePoC.FormulaEngine.Mapping;
|
|
|
|
|
|
namespace FormulaEnginePoC.UI
|
|
|
{
|
|
@@ -15,6 +17,8 @@ namespace FormulaEnginePoC.UI
|
|
|
|
|
|
private CspCalcFormula<Book, object> _cspCalcFormula;
|
|
|
private CspFilterFormula<Book> _cspFilterFormula;
|
|
|
+ private DepCalcFormula<Book, object> _depCalcFormula;
|
|
|
+ private DepFilterFormula<Book> _depFilterFormula;
|
|
|
|
|
|
public SampleForm()
|
|
|
{
|
|
@@ -45,6 +49,50 @@ namespace FormulaEnginePoC.UI
|
|
|
DataSourceGrid.DataSource = _dataSource;
|
|
|
}
|
|
|
|
|
|
+ private void CompileX<T>(out T field, CodeExecPanel eventSource, Func<string, T> newFormula) where T : Formula
|
|
|
+ {
|
|
|
+ field = null;
|
|
|
+ eventSource.Code = "";
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ field = newFormula(FormulaTextBox.Text);
|
|
|
+ eventSource.Code = field.CodeExpression;
|
|
|
+ field.Compile();
|
|
|
+ }
|
|
|
+ catch (Exception exception)
|
|
|
+ {
|
|
|
+ eventSource.Code += Environment.NewLine + Environment.NewLine + exception;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ExecuteFilterX(CodeExecPanel eventSource, FilterFormula<Book> formula)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = _dataSource.Where(formula.CompiledFunc).ToArray();
|
|
|
+ eventSource.Result = $"共 {result.Length} 项{Environment.NewLine}"
|
|
|
+ + string.Join(Environment.NewLine, result.Select(p => $" - {p.Name}"));
|
|
|
+ }
|
|
|
+ catch (Exception exception)
|
|
|
+ {
|
|
|
+ eventSource.Result = exception.ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ExecuteCalcX(CodeExecPanel eventSource, CalcFormula<Book, object> formula)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = formula.CompiledFunc((Book)DataSourceGrid.SelectedRows[0].DataBoundItem);
|
|
|
+ eventSource.Result = result?.ToString() ?? "<null>";
|
|
|
+ }
|
|
|
+ catch (Exception exception)
|
|
|
+ {
|
|
|
+ eventSource.Result = exception.ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void NewToolStripButton_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
RefreshDataSource();
|
|
@@ -58,8 +106,11 @@ namespace FormulaEnginePoC.UI
|
|
|
private void FieldGrid_DoubleClick(object sender, EventArgs e)
|
|
|
{
|
|
|
if (1 != FieldGrid.SelectedRows.Count)
|
|
|
+ {
|
|
|
return;
|
|
|
+ }
|
|
|
FormulaTextBox.SelectedText = ((PropertyMapping)FieldGrid.SelectedRows[0].DataBoundItem).Display + " ";
|
|
|
+ FormulaTextBox.Focus();
|
|
|
}
|
|
|
|
|
|
private void Insert_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
@@ -69,64 +120,52 @@ namespace FormulaEnginePoC.UI
|
|
|
{
|
|
|
FormulaTextBox.SelectedText = e.ClickedItem.Text + $" ( {selected} ) ";
|
|
|
FormulaTextBox.SelectionStart -= 3;
|
|
|
- FormulaTextBox.Focus();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
FormulaTextBox.SelectedText = e.ClickedItem.Text + " ";
|
|
|
}
|
|
|
+ FormulaTextBox.Focus();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private void DepCalcExecPanel_CompileButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
+ CompileX(out _depCalcFormula, (CodeExecPanel)sender, formula => new DepCalcFormula<Book, object>(formula));
|
|
|
}
|
|
|
|
|
|
private void DepCalcExecPanel_ExecuteButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
+ ExecuteCalcX((CodeExecPanel)sender, _depCalcFormula);
|
|
|
}
|
|
|
|
|
|
private void DepFilterExecPanel_CompileButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
+ CompileX(out _depFilterFormula, (CodeExecPanel)sender, formula => new DepFilterFormula<Book>(formula));
|
|
|
}
|
|
|
|
|
|
private void DepFilterExecPanel_ExecuteButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
+ ExecuteFilterX((CodeExecPanel)sender, _depFilterFormula);
|
|
|
}
|
|
|
|
|
|
private void CspCalcExecPanel_CompileButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- _cspCalcFormula = null;
|
|
|
- _cspCalcFormula = new CspCalcFormula<Book, object>(FormulaTextBox.Text);
|
|
|
- CspCalcExecPanel.Code = _cspCalcFormula.CodeExpression;
|
|
|
- }
|
|
|
- catch (Exception exception)
|
|
|
- {
|
|
|
- CspCalcExecPanel.Code = exception.ToString();
|
|
|
- }
|
|
|
+ CompileX(out _cspCalcFormula, (CodeExecPanel)sender, formula => new CspCalcFormula<Book, object>(formula));
|
|
|
}
|
|
|
|
|
|
private void CspCalcExecPanel_ExecuteButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- var result = _cspCalcFormula.CalcFunc((Book)DataSourceGrid.SelectedRows[0].DataBoundItem);
|
|
|
- CspCalcExecPanel.Result = result?.ToString() ?? "<null>";
|
|
|
- }
|
|
|
- catch (Exception exception)
|
|
|
- {
|
|
|
- CspCalcExecPanel.Result = exception.ToString();
|
|
|
- }
|
|
|
+ ExecuteCalcX((CodeExecPanel)sender, _cspCalcFormula);
|
|
|
}
|
|
|
|
|
|
private void CspFilterExecPanel_CompileButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
+ CompileX(out _cspFilterFormula, (CodeExecPanel)sender, formula => new CspFilterFormula<Book>(formula));
|
|
|
}
|
|
|
|
|
|
private void CspFilterExecPanel_ExecuteButtonClicked(object sender, EventArgs e)
|
|
|
{
|
|
|
+ ExecuteFilterX((CodeExecPanel)sender, _cspFilterFormula);
|
|
|
}
|
|
|
}
|
|
|
}
|