Is Variables needed for equations having DateTime?

Jul 1, 2009 at 5:24 PM
Edited Jul 1, 2009 at 5:29 PM

If I use directly DateTime objects in the equation, its throwing an error but if I create varaibles and use the variables in the equation then its working fine. Does it mean that variables are needed for the DateTime equations? if yes then whenever equations get updated then need to update the variables ??

 Option 1:

engine.Add("DateDiff1", "DateTime.Parse(\"1/1/2010\") - DateTime.Parse(\"1/1/2009\")", context);

object obj = engine.GetResult("DateDiff1");

Option 2:

variables.Add("Date1", DateTime.Parse("1/1/2009"));

variables.Add("Date2", DateTime.Parse("1/1/2010"));

engine.Add("DateDiff2", "Date2-Date1", context);

obj = engine.GetResult("DateDiff2");

 

Thanks,

Coordinator
Jul 2, 2009 at 12:16 AM

Are you importing the DateTime type into your context?

The following code works:

CalculationEngine engine = new CalculationEngine();
ExpressionContext context = new ExpressionContext();
context.Imports.AddType(typeof(DateTime), "DateTime");      // Import the DateTime functions under the "DateTime" prefix
engine.Add("DateDiff1", "DateTime.Parse(\"1/1/2010\") - DateTime.Parse(\"1/1/2009\")", context);

object obj = engine.GetResult("DateDiff1");

The error message that you get if you don't import the DateTime type is not very helpful so I'll try to make it better.

However, FLEE supports DateTime literals, so you can write the following instead:

CalculationEngine engine = new CalculationEngine();
ExpressionContext context = new ExpressionContext();                       
engine.Add("DateDiff1", "#01/01/2010# - #01/01/2009#", context);

object obj = engine.GetResult("DateDiff1");

You can use the context.ParserOptions.DateTimeFormat to control the format string for DateTime literals.  By default, it is set to the short date string for the current culture.

Jul 6, 2009 at 4:37 PM

 

Thanks for your reply.

I'm still getting errors if I use context object instead of engine:

               ExpressionContext context = new ExpressionContext();
                context.Imports.AddType(typeof(DateTime), "DateTime");     

                //context.Variables.Add("Date1", "DateTime.Parse(\"1/1/2010\")");
                //context.Variables.Add("Date2", "DateTime.Parse(\"1/1/2009\")");

                context.Variables.Add("Date1", "#1/1/2009#");
                context.Variables.Add("Date2", "#1/1/2010#");


                IDynamicExpression exp =  context.CompileDynamic("if(Date1 > Date2,true,false)");
                string value =  exp.Evaluate().ToString();

Coordinator
Jul 7, 2009 at 9:50 PM

Can you post the error details?

Jul 8, 2009 at 2:56 PM

The error is raised at context.CompileDynamic and here is the error message:

ex.Message

"CompareElement: Operation 'GreaterThan' is not defined for types 'String' and 'String'"

ex.StackTrace

" at Ciloci.Flee.ExpressionElement.ThrowCompileException(String messageKey, CompileExceptionReason reason, Object[] arguments)\r\n at Ciloci.Flee.BinaryExpressionElement.ThrowOperandTypeMismatch(Object operation, Type leftType, Type rightType)\r\n at Ciloci.Flee.BinaryExpressionElement.ValidateInternal(Object op)\r\n at Ciloci.Flee.BinaryExpressionElement.Configure(ExpressionElement leftChild, ExpressionElement rightChild, Object op)\r\n at Ciloci.Flee.BinaryExpressionElement.CreateElement(IList childValues, Type elementType)\r\n at Ciloci.Flee.FleeExpressionAnalyzer.AddBinaryOp(Production node, Type elementType)\r\n at Ciloci.Flee.FleeExpressionAnalyzer.ExitCompareExpression(Production node)\r\n at Ciloci.Flee.ExpressionAnalyzer.Exit(Node node)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.Parser.ExitNode(Node node)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.R

untime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.Recur

siveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.Pars

eElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.Parse

Pattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(

ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, Productio

nPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\

r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r

\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseElement(Production node, ProductionPatternElement elem)\r\n at Cilo

ci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseAlternative(ProductionPatternAlternative alt)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParsePattern(ProductionPattern pattern)\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.RecursiveDescentParser.ParseStart()\r\n at Ciloci.Flee.PerCederberg.Grammatica.Runtime.Parser.Parse()\r\n at Ciloci.Flee.ExpressionContext.DoParse()\r\n at Ciloci.Flee.ExpressionContext.Parse(String expression, IServiceProvider services)\r\n at Ciloci.Flee.Expression`1.Compile(String expression, ExpressionOptions options)\r\n at Ciloci.Flee.Expression`1..ctor(String expression, ExpressionContext context, Boolean isGeneric)\r\n at Ciloci.Flee.ExpressionContext.CompileDynamic(String expression)\r\n at EquationEditorDemo.EquationEditor.buttonCalculate_Click(Object sender, EventArgs e) in C:\\EquationEditorDemo\\EquationEditorDemo\\EquationEditor.cs:line 151"

Coordinator
Jul 8, 2009 at 7:56 PM

Your datetime variables are being initialized with strings instead of actual datetimes.

It should be like this:

ExpressionContext context = new ExpressionContext();
                context.Imports.AddType(typeof(DateTime), "DateTime");     

                context.Variables.Add("Date1", new DateTime(2010, 1, 1));

                context.Variables.Add("Date2", new DateTime(2009, 1, 1));


                IDynamicExpression exp =  context.CompileDynamic("if(Date1 > Date2,true,false)");
                string value =  exp.Evaluate().ToString();

The literal notation only works inside an expression since a literal is a constant and would never need to be a variable.

So you could do this:

context.Variables.Add("Date1", new DateTime(2010, 1, 1));

IDynamicExpression exp =  context.CompileDynamic("if(Date1 > #01/01/2009#, true, false)");