Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/decimal support updated #55

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Jace.Benchmark/BenchMarkOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

namespace Jace.Benchmark
{
public class BenchMarkOperation
public class BenchMarkOperation<T>
{
public string Formula { get; set; }
public BenchmarkMode Mode { get; set; }
public Func<CalculationEngine, string, TimeSpan> BenchMarkDelegate { get; set; }
public Func<ICalculationEngine<T>, string, TimeSpan> BenchMarkDelegate { get; set; }
}
}
58 changes: 45 additions & 13 deletions Jace.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,47 @@ private static DataTable Benchmark(BenchmarkMode mode, CaseSensitivity caseSensi
{
TimeSpan duration;

CalculationEngine interpretedEngine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, true, true, true);
CalculationEngine interpretedEngineCaseSensitive = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Interpreted, true, true, false);
CalculationEngine compiledEngine = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, true, true);
CalculationEngine compiledEngineCaseSensitive = new CalculationEngine(CultureInfo.InvariantCulture, ExecutionMode.Compiled, true, true, false);

BenchMarkOperation[] benchmarks = {
new BenchMarkOperation() { Formula = "2+3*7", Mode = BenchmarkMode.Static, BenchMarkDelegate = BenchMarkCalculationEngine },
new BenchMarkOperation() { Formula = "logn(var1, (2+3) * 500)", Mode = BenchmarkMode.SimpleFunction , BenchMarkDelegate = BenchMarkCalculationEngineFunctionBuild },
new BenchMarkOperation() { Formula = "(var1 + var2 * 3)/(2+3) - something", Mode = BenchmarkMode.Simple , BenchMarkDelegate = BenchMarkCalculationEngineFunctionBuild },
var interpretedEngine = CalculationEngine.New<double>(new JaceOptions
{
CultureInfo = CultureInfo.InvariantCulture,
ExecutionMode = ExecutionMode.Interpreted,
CacheEnabled = true,
OptimizerEnabled = true,
CaseSensitive = true
});

var interpretedEngineCaseSensitive = CalculationEngine.New<double>(new JaceOptions
{
CultureInfo = CultureInfo.InvariantCulture,
ExecutionMode = ExecutionMode.Interpreted,
CacheEnabled = true,
OptimizerEnabled = true,
CaseSensitive = false
});

var compiledEngine = CalculationEngine.New<double>(new JaceOptions
{
CultureInfo = CultureInfo.InvariantCulture,
ExecutionMode = ExecutionMode.Compiled,
CacheEnabled = true,
OptimizerEnabled = true,
CaseSensitive = true
});

var compiledEngineCaseSensitive = CalculationEngine.New<double>(new JaceOptions
{
CultureInfo = CultureInfo.InvariantCulture,
ExecutionMode = ExecutionMode.Compiled,
CacheEnabled = true,
OptimizerEnabled = true,
CaseSensitive = false
});


BenchMarkOperation<double>[] benchmarks = {
new BenchMarkOperation<double>() { Formula = "2+3*7", Mode = BenchmarkMode.Static, BenchMarkDelegate = BenchMarkCalculationEngine },
new BenchMarkOperation<double>() { Formula = "logn(var1, (2+3) * 500)", Mode = BenchmarkMode.SimpleFunction , BenchMarkDelegate = BenchMarkCalculationEngineFunctionBuild },
new BenchMarkOperation<double>() { Formula = "(var1 + var2 * 3)/(2+3) - something", Mode = BenchmarkMode.Simple , BenchMarkDelegate = BenchMarkCalculationEngineFunctionBuild },
};

DataTable table = new DataTable();
Expand All @@ -51,7 +83,7 @@ private static DataTable Benchmark(BenchmarkMode mode, CaseSensitivity caseSensi
table.Columns.Add("Total Iteration", typeof(int));
table.Columns.Add("Total Duration");

foreach (BenchMarkOperation benchmark in benchmarks)
foreach (var benchmark in benchmarks)
{
if (mode == BenchmarkMode.All || mode == benchmark.Mode)
{
Expand Down Expand Up @@ -121,7 +153,7 @@ private static DataTable Benchmark(BenchmarkMode mode, CaseSensitivity caseSensi
return table;
}

private static TimeSpan BenchMarkCalculationEngine(CalculationEngine engine, string functionText)
private static TimeSpan BenchMarkCalculationEngine(ICalculationEngine<double> engine, string functionText)
{
DateTime start = DateTime.Now;

Expand All @@ -135,7 +167,7 @@ private static TimeSpan BenchMarkCalculationEngine(CalculationEngine engine, str
return end - start;
}

private static TimeSpan BenchMarkCalculationEngineFunctionBuild(CalculationEngine engine, string functionText)
private static TimeSpan BenchMarkCalculationEngineFunctionBuild(ICalculationEngine<double> engine, string functionText)
{
DateTime start = DateTime.Now;

Expand Down Expand Up @@ -169,7 +201,7 @@ private static List<string> GenerateRandomFunctions(int numberOfFunctions)
return result;
}

private static TimeSpan BenchMarkCalculationEngineRandomFunctionBuild(CalculationEngine engine, List<string> functions,
private static TimeSpan BenchMarkCalculationEngineRandomFunctionBuild(ICalculationEngine<double> engine, List<string> functions,
int numberOfTests)
{
Random random = new Random();
Expand Down
3 changes: 2 additions & 1 deletion Jace.Core.Tests/Jace.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<Compile Include="..\Jace.Tests\AssertExtensions.cs" Link="AssertExtensions.cs" />
<Compile Include="..\Jace.Tests\AstBuilderTests.cs" Link="AstBuilderTests.cs" />
<Compile Include="..\Jace.Tests\BasicInterpreterTests.cs" Link="BasicInterpreterTests.cs" />
<Compile Include="..\Jace.Tests\CalculationEngineTests.cs" Link="CalculationEngineTests.cs" />
<Compile Include="..\Jace.Tests\DoubleCalculationEngineTests.cs" Link="DoubleCalculationEngineTests.cs" />
<Compile Include="..\Jace.Tests\DecimalCalculationEngineTests.cs" Link="DecimalCalculationEngineTests.cs" />
<Compile Include="..\Jace.Tests\ConstantRegistryTests.cs" Link="ConstantRegistryTests.cs" />
<Compile Include="..\Jace.Tests\FuncAdapterTests.cs" Link="FuncAdapterTests.cs" />
<Compile Include="..\Jace.Tests\FunctionRegistryTests.cs" Link="FunctionRegistryTests.cs" />
Expand Down
10 changes: 5 additions & 5 deletions Jace.DemoApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ private void calculateButton_Click(object sender, RoutedEventArgs e)

string formula = formulaTextBox.Text;

TokenReader reader = new TokenReader(CultureInfo.InvariantCulture);
var reader = new TokenReader<double>(CultureInfo.InvariantCulture, DoubleNumericalOperations.Instance);
List<Token> tokens = reader.Read(formula);

ShowTokens(tokens);

IFunctionRegistry functionRegistry = new FunctionRegistry(false);
IFunctionRegistry<double> functionRegistry = new FunctionRegistry<double>(false);

AstBuilder astBuilder = new AstBuilder(functionRegistry, false);
AstBuilder<double> astBuilder = new AstBuilder<double>(functionRegistry, false);
Operation operation = astBuilder.Build(tokens);

ShowAbstractSyntaxTree(operation);
Expand All @@ -55,7 +55,7 @@ private void calculateButton_Click(object sender, RoutedEventArgs e)
variables.Add(variable.Name, value);
}

IExecutor executor = new Interpreter();
IExecutor<double> executor = new Interpreter<double>(DoubleNumericalOperations.Instance);
double result = executor.Execute(operation, null, null, variables);

resultTextBox.Text = "" + result;
Expand Down Expand Up @@ -171,7 +171,7 @@ private string GetLabelText(Operation operation)
}
else
{
FloatingPointConstant floatingPointConstant = operation as FloatingPointConstant;
var floatingPointConstant = operation as FloatingPointConstant<double>;
if (floatingPointConstant != null)
{
value = "(" + floatingPointConstant.Value + ")";
Expand Down
80 changes: 40 additions & 40 deletions Jace.Tests/AstBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class AstBuilderTests
[TestMethod]
public void TestBuildFormula1()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = '(', TokenType = TokenType.LeftBracket },
new Token() { Value = 42, TokenType = TokenType.Integer },
Expand All @@ -49,9 +49,9 @@ public void TestBuildFormula1()
[TestMethod]
public void TestBuildFormula2()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 2, TokenType = TokenType.Integer },
new Token() { Value = '+', TokenType = TokenType.Operation },
Expand All @@ -71,9 +71,9 @@ public void TestBuildFormula2()
[TestMethod]
public void TestBuildFormula3()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 2, TokenType = TokenType.Integer },
new Token() { Value = '*', TokenType = TokenType.Operation },
Expand All @@ -93,9 +93,9 @@ public void TestBuildFormula3()
[TestMethod]
public void TestDivision()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 10, TokenType = TokenType.Integer },
new Token() { Value = '/', TokenType = TokenType.Operation },
Expand All @@ -113,9 +113,9 @@ public void TestDivision()
[TestMethod]
public void TestMultiplication()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 10, TokenType = TokenType.Integer },
new Token() { Value = '*', TokenType = TokenType.Operation },
Expand All @@ -125,15 +125,15 @@ public void TestMultiplication()
Multiplication multiplication = (Multiplication)operation;

Assert.AreEqual(new IntegerConstant(10), multiplication.Argument1);
Assert.AreEqual(new FloatingPointConstant(2.0), multiplication.Argument2);
Assert.AreEqual(new FloatingPointConstant<double>(2.0), multiplication.Argument2);
}

[TestMethod]
public void TestExponentiation()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 2, TokenType = TokenType.Integer },
new Token() { Value = '^', TokenType = TokenType.Operation },
Expand All @@ -149,9 +149,9 @@ public void TestExponentiation()
[TestMethod]
public void TestModulo()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 2.7, TokenType = TokenType.FloatingPoint },
new Token() { Value = '%', TokenType = TokenType.Operation },
Expand All @@ -160,16 +160,16 @@ public void TestModulo()

Modulo modulo = (Modulo)operation;

Assert.AreEqual(new FloatingPointConstant(2.7), modulo.Dividend);
Assert.AreEqual(new FloatingPointConstant<double>(2.7), modulo.Dividend);
Assert.AreEqual(new IntegerConstant(3), modulo.Divisor);
}

[TestMethod]
public void TestVariable()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 10, TokenType = TokenType.Integer },
new Token() { Value = '*', TokenType = TokenType.Operation },
Expand All @@ -185,9 +185,9 @@ public void TestVariable()
[TestMethod]
public void TestMultipleVariable()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = "var1", TokenType = TokenType.Text },
new Token() { Value = '+', TokenType = TokenType.Operation },
Expand All @@ -213,9 +213,9 @@ public void TestMultipleVariable()
[TestMethod]
public void TestSinFunction1()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = "sin", TokenType = TokenType.Text },
new Token() { Value = '(', TokenType = TokenType.LeftBracket },
Expand All @@ -230,9 +230,9 @@ public void TestSinFunction1()
[TestMethod]
public void TestSinFunction2()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = "sin", TokenType = TokenType.Text },
new Token() { Value = '(', TokenType = TokenType.LeftBracket },
Expand All @@ -252,9 +252,9 @@ public void TestSinFunction2()
[TestMethod]
public void TestSinFunction3()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = "sin", TokenType = TokenType.Text },
new Token() { Value = '(', TokenType = TokenType.LeftBracket },
Expand All @@ -274,15 +274,15 @@ public void TestSinFunction3()
Assert.AreEqual(new IntegerConstant(2), addition.Argument1);
Assert.AreEqual(new IntegerConstant(3), addition.Argument2);

Assert.AreEqual(new FloatingPointConstant(4.9), multiplication.Argument2);
Assert.AreEqual(new FloatingPointConstant<double>(4.9), multiplication.Argument2);
}

[TestMethod]
public void TestUnaryMinus()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);
Operation operation = builder.Build(new List<Token>() {
new Token() { Value = 5.3, TokenType = TokenType.FloatingPoint },
new Token() { Value = '*', TokenType = TokenType.Operation},
Expand All @@ -295,7 +295,7 @@ public void TestUnaryMinus()
});

Multiplication multiplication = (Multiplication)operation;
Assert.AreEqual(new FloatingPointConstant(5.3), multiplication.Argument1);
Assert.AreEqual(new FloatingPointConstant<double>(5.3), multiplication.Argument1);

UnaryMinus unaryMinus = (UnaryMinus)multiplication.Argument2;

Expand All @@ -307,9 +307,9 @@ public void TestUnaryMinus()
[TestMethod]
public void TestBuildInvalidFormula1()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);

AssertExtensions.ThrowsException<ParseException>(() =>
{
Expand All @@ -327,9 +327,9 @@ public void TestBuildInvalidFormula1()
[TestMethod]
public void TestBuildInvalidFormula2()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);

AssertExtensions.ThrowsException<ParseException>(() =>
{
Expand All @@ -347,9 +347,9 @@ public void TestBuildInvalidFormula2()
[TestMethod]
public void TestBuildInvalidFormula3()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);

AssertExtensions.ThrowsException<ParseException>(() =>
{
Expand All @@ -365,9 +365,9 @@ public void TestBuildInvalidFormula3()
[TestMethod]
public void TestBuildInvalidFormula4()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);

AssertExtensions.ThrowsException<ParseException>(() =>
{
Expand All @@ -383,9 +383,9 @@ public void TestBuildInvalidFormula4()
[TestMethod]
public void TestBuildInvalidFormula5()
{
IFunctionRegistry registry = new MockFunctionRegistry();
var registry = new MockFunctionRegistry<double>();

AstBuilder builder = new AstBuilder(registry, false);
var builder = new AstBuilder<double>(registry, false);

AssertExtensions.ThrowsException<ParseException>(() =>
{
Expand Down
Loading