 |
You should take the following two steps when applying USPExpress Pro to an expression:
-
Parse the expression
- Parsing results in producing an expression tree. Once the tree is built, the expression can be evaluated many times against various variable values.
-
Evaluate the expression
- Since the expression tree is already built, evaluation of the expression is performed with maximum possible speed - no additional overhead is involved.
Below is a step-by-step guide to USPExpress usage (in C# syntax):
-
Declare and create USP.Express.Pro.Parser object:
using USP.Express.Pro;
...
Parser m_cParser = new Parser();
-
Specify variables which you intend to use in your expression. Four variables named X1, X2, X3, X4 are specified in the example below:
m_cParser.Variables.Add(new Variable("x1", typeof(Double)));
m_cParser.Variables.Add(new Variable("x2", typeof(DateTime)));
m_cParser.Variables.Add(new Variable("x3", typeof(String)));
m_cParser.Variables.Add(new Variable("x4", typeof(Boolean)));
Optionally, you may add aliases to your variables. Each variable can have one or more aliases:
m_cParser.Variables.Item(2).Aliases.Add("OrderDate");
-
Parse the expression and obtain an expression tree:
ExpressionTree m_oExpressionTree = null;
try
{
m_oExpressionTree = m_cParser.Parse("Your Expression");
}
catch(Exceptions.ParseException ex)
{
...
}
catch(Exception ex)
{
...
}
ParseException is raised, if the expression is not parsed successfully.
-
Assign actual values to the variables that you specified on step 2:
object[] m_aValues = new object[4];
m_aValues[0] = 1.0;
m_aValues[1] = DateTime.Now;
m_aValues[2] = "test string";
m_aValues[3] = true;
-
Evaluate the expression:
string sResult;
try
{
sResult = m_oExpressionTree.Evaluate(m_aValues).ToString();
}
catch(Exceptions.EvaluateException ex)
{
MessageBox.Show(ex.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
EvaluateException is raised, if the expression is not evaluated successfully.
You may also refer to the Sample projects and the Class Reference document that comes with the library.
|
 |