1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
| using System;
| using System.Collections.Generic;
| using System.Linq;
| using System.Reflection;
| using System.Text;
| using Jace.Operations;
| using Jace.Util;
|
| namespace Jace.Execution
| {
| public class Interpreter : IExecutor
| {
| private readonly bool caseSensitive;
|
| public Interpreter(): this(false) { }
|
| public Interpreter(bool caseSensitive)
| {
| this.caseSensitive = caseSensitive;
| }
| public Func<IDictionary<string, double>, double> BuildFormula(Operation operation,
| IFunctionRegistry functionRegistry,
| IConstantRegistry constantRegistry)
| {
| return caseSensitive
| ? (Func<IDictionary<string, double>, double>)(variables =>
| {
| return Execute(operation, functionRegistry, constantRegistry, variables);
| })
| : (Func<IDictionary<string, double>, double>)(variables =>
| {
| variables = EngineUtil.ConvertVariableNamesToLowerCase(variables);
| return Execute(operation, functionRegistry, constantRegistry, variables);
| });
| }
|
| public double Execute(Operation operation, IFunctionRegistry functionRegistry, IConstantRegistry constantRegistry)
| {
| return Execute(operation, functionRegistry, constantRegistry, new Dictionary<string, double>());
| }
|
| public double Execute(Operation operation,
| IFunctionRegistry functionRegistry,
| IConstantRegistry constantRegistry,
| IDictionary<string, double> variables)
| {
| if (operation == null)
| throw new ArgumentNullException("operation");
|
| if (operation.GetType() == typeof(IntegerConstant))
| {
| IntegerConstant constant = (IntegerConstant)operation;
| return constant.Value;
| }
| else if (operation.GetType() == typeof(FloatingPointConstant))
| {
| FloatingPointConstant constant = (FloatingPointConstant)operation;
| return constant.Value;
| }
| else if (operation.GetType() == typeof(Variable))
| {
| Variable variable = (Variable)operation;
|
| double value;
| bool variableFound = variables.TryGetValue(variable.Name, out value);
|
| if (variableFound)
| return value;
| else
| throw new VariableNotDefinedException(string.Format("The variable \"{0}\" used is not defined.", variable.Name));
| }
| else if (operation.GetType() == typeof(Multiplication))
| {
| Multiplication multiplication = (Multiplication)operation;
| return Execute(multiplication.Argument1, functionRegistry, constantRegistry, variables) * Execute(multiplication.Argument2, functionRegistry, constantRegistry, variables);
| }
| else if (operation.GetType() == typeof(Addition))
| {
| Addition addition = (Addition)operation;
| return Execute(addition.Argument1, functionRegistry, constantRegistry, variables) + Execute(addition.Argument2, functionRegistry, constantRegistry, variables);
| }
| else if (operation.GetType() == typeof(Subtraction))
| {
| Subtraction addition = (Subtraction)operation;
| return Execute(addition.Argument1, functionRegistry, constantRegistry, variables) - Execute(addition.Argument2, functionRegistry, constantRegistry, variables);
| }
| else if (operation.GetType() == typeof(Division))
| {
| Division division = (Division)operation;
| return Execute(division.Dividend, functionRegistry, constantRegistry, variables) / Execute(division.Divisor, functionRegistry, constantRegistry, variables);
| }
| else if (operation.GetType() == typeof(Modulo))
| {
| Modulo division = (Modulo)operation;
| return Execute(division.Dividend, functionRegistry, constantRegistry, variables) % Execute(division.Divisor, functionRegistry, constantRegistry, variables);
| }
| else if (operation.GetType() == typeof(Exponentiation))
| {
| Exponentiation exponentiation = (Exponentiation)operation;
| return Math.Pow(Execute(exponentiation.Base, functionRegistry, constantRegistry, variables), Execute(exponentiation.Exponent, functionRegistry, constantRegistry, variables));
| }
| else if (operation.GetType() == typeof(UnaryMinus))
| {
| UnaryMinus unaryMinus = (UnaryMinus)operation;
| return -Execute(unaryMinus.Argument, functionRegistry, constantRegistry, variables);
| }
| else if (operation.GetType() == typeof(And))
| {
| And and = (And)operation;
| var operation1 = Execute(and.Argument1, functionRegistry, constantRegistry, variables) != 0;
| var operation2 = Execute(and.Argument2, functionRegistry, constantRegistry, variables) != 0;
|
| return (operation1 && operation2) ? 1.0 : 0.0;
| }
| else if (operation.GetType() == typeof(Or))
| {
| Or or = (Or)operation;
| var operation1 = Execute(or.Argument1, functionRegistry, constantRegistry, variables) != 0;
| var operation2 = Execute(or.Argument2, functionRegistry, constantRegistry, variables) != 0;
|
| return (operation1 || operation2) ? 1.0 : 0.0;
| }
| else if(operation.GetType() == typeof(LessThan))
| {
| LessThan lessThan = (LessThan)operation;
| return (Execute(lessThan.Argument1, functionRegistry, constantRegistry, variables) < Execute(lessThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0;
| }
| else if (operation.GetType() == typeof(LessOrEqualThan))
| {
| LessOrEqualThan lessOrEqualThan = (LessOrEqualThan)operation;
| return (Execute(lessOrEqualThan.Argument1, functionRegistry, constantRegistry, variables) <= Execute(lessOrEqualThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0;
| }
| else if (operation.GetType() == typeof(GreaterThan))
| {
| GreaterThan greaterThan = (GreaterThan)operation;
| return (Execute(greaterThan.Argument1, functionRegistry, constantRegistry, variables) > Execute(greaterThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0;
| }
| else if (operation.GetType() == typeof(GreaterOrEqualThan))
| {
| GreaterOrEqualThan greaterOrEqualThan = (GreaterOrEqualThan)operation;
| return (Execute(greaterOrEqualThan.Argument1, functionRegistry, constantRegistry, variables) >= Execute(greaterOrEqualThan.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0;
| }
| else if (operation.GetType() == typeof(Equal))
| {
| Equal equal = (Equal)operation;
| return (Execute(equal.Argument1, functionRegistry, constantRegistry, variables) == Execute(equal.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0;
| }
| else if (operation.GetType() == typeof(NotEqual))
| {
| NotEqual notEqual = (NotEqual)operation;
| return (Execute(notEqual.Argument1, functionRegistry, constantRegistry, variables) != Execute(notEqual.Argument2, functionRegistry, constantRegistry, variables)) ? 1.0 : 0.0;
| }
| else if (operation.GetType() == typeof(Function))
| {
| Function function = (Function)operation;
|
| FunctionInfo functionInfo = functionRegistry.GetFunctionInfo(function.FunctionName);
|
| double[] arguments = new double[functionInfo.IsDynamicFunc ? function.Arguments.Count : functionInfo.NumberOfParameters];
| for (int i = 0; i < arguments.Length; i++)
| arguments[i] = Execute(function.Arguments[i], functionRegistry, constantRegistry, variables);
|
| return Invoke(functionInfo.Function, arguments);
| }
| else
| {
| throw new ArgumentException(string.Format("Unsupported operation \"{0}\".", operation.GetType().FullName), "operation");
| }
| }
|
| private double Invoke(Delegate function, double[] arguments)
| {
| // DynamicInvoke is slow, so we first try to convert it to a Func
| if (function is Func<double>)
| {
| return ((Func<double>)function).Invoke();
| }
| else if (function is Func<double, double>)
| {
| return ((Func<double, double>)function).Invoke(arguments[0]);
| }
| else if (function is Func<double, double, double>)
| {
| return ((Func<double, double, double>)function).Invoke(arguments[0], arguments[1]);
| }
| else if (function is Func<double, double, double, double>)
| {
| return ((Func<double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2]);
| }
| else if (function is Func<double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3]);
| }
| else if (function is Func<double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
| }
| else if (function is Func<double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10], arguments[11]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10], arguments[11], arguments[12]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10], arguments[11], arguments[12], arguments[13]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10], arguments[11], arguments[12], arguments[13], arguments[14]);
| }
| else if (function is Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double>)
| {
| return ((Func<double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double>)function).Invoke(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6], arguments[7], arguments[8], arguments[9], arguments[10], arguments[11], arguments[12], arguments[13], arguments[14], arguments[15]);
| }
| else if (function is DynamicFunc<double, double>)
| {
| return ((DynamicFunc<double, double>)function).Invoke(arguments);
| }
| else
| {
| return (double)function.DynamicInvoke((from s in arguments select (object)s).ToArray());
| }
| }
| }
| }
|
|