-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormulaScore.cs
More file actions
170 lines (147 loc) · 4.98 KB
/
FormulaScore.cs
File metadata and controls
170 lines (147 loc) · 4.98 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Ciloci.Flee;
namespace FormulaScore
{
public class FormulaScore
{
private const string valueIDRegexPattern = @"\[(.+?)\]";
private Dictionary<string, long> formulaIDToIntegerValue = new Dictionary<string, long>();
private Dictionary<string, double> formulaIDToFloatingPointValue = new Dictionary<string, double>();
private IGenericExpression<double> compiledExpression;
public string ScoringFormula { get; set; }
/// <summary>
///
/// </summary>
public FormulaScore()
{
ScoringFormula = "";
}
/// <summary>
///
/// </summary>
/// <param name="scoringFormula"></param>
public FormulaScore(string scoringFormula)
{
if (!string.IsNullOrWhiteSpace(scoringFormula))
{
ScoringFormula = scoringFormula;
}
else
{
ScoringFormula = "";
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool CheckFormula()
{
// TODO: refactor this.
try
{
compileExpression();
}
catch (ExpressionCompileException)
{
// Expression would not compile, so scoring formula is not valid.
// Ensure no unwanted side-effects from compilation.
compiledExpression = null;
return false;
}
return true;
}
/// <summary>
/// Checks if the formula is a vaild arithmetic formula without checking
/// </summary>
/// <returns></returns>
public bool CheckFormulaSyntax()
{
return false;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public double CalculateScore()
{
// Generate expression and calculate score!
// Note: This library is amazing and does exactly what we needed.
if (compiledExpression == null)
{
compileExpression();
}
double score = compiledExpression.Evaluate();
return score;
}
/// <summary>
///
/// </summary>
/// <param name="valueName"></param>
/// <param name="valueID"></param>
/// <param name="valueValue"></param>
public void AddScoringValue(string scoringID, long scoringValue)
{
formulaIDToIntegerValue.Add(scoringID, scoringValue);
}
/// <summary>
///
/// </summary>
/// <param name="valueName"></param>
/// <param name="valueID"></param>
/// <param name="valueValue"></param>
public void AddScoringValue(string scoringID, double scoringValue)
{
formulaIDToFloatingPointValue.Add(scoringID, scoringValue);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static List<string> FetchScoringIDs(string formula)
{
return parseScoringIds(formula);
}
#region private helper functions
private void compileExpression()
{
ExpressionContext context = new ExpressionContext();
// Allows use of all public static Math functions:
//context.Imports.AddType(typeof(Math));
// Load values into Flee context.
foreach (KeyValuePair<string, long> pair in formulaIDToIntegerValue)
{
context.Variables[pair.Key] = pair.Value;
}
foreach (KeyValuePair<string, double> pair in formulaIDToFloatingPointValue)
{
context.Variables[pair.Key] = pair.Value;
}
compiledExpression = context.CompileGeneric<double>(cleanFormula(ScoringFormula));
}
// Need to remove "[]" scoringId demarkation marks before formula can be used.
private string cleanFormula(string formula)
{
// TODO: This all seems sort of hackey. Refactor.
return formula.Replace("[", "").Replace("]", "");
}
// Get the list of scoring values that exist in the text string.
private static List<string> parseScoringIds(string formula)
{
MatchCollection matches = Regex.Matches(formula, valueIDRegexPattern, RegexOptions.IgnoreCase);
List<string> valueIDs = new List<string>();
for (int i = 0; i < matches.Count; i++)
{
// Extract values from the first grouping.
valueIDs.Add(matches[i].Groups[1].Value);
}
return valueIDs;
}
}
#endregion
}