Identify Variables

Aug 20, 2007 at 12:29 PM
Thanks for the FLEE project,We are learning new concepts from this.

We have one question regarding identifying the variaibles and operators of an expression.
In "Expression.vb" class ,in "Compile" function, "topElement" object has all the variables and operators of an expression.
Will u please tell how to identify these in the interface form?
Is there any other best way to identify variables of an expression?

Please reply.
Coordinator
Aug 20, 2007 at 2:46 PM
Edited Aug 20, 2007 at 2:47 PM
Hi,

Can you could tell me what you want to accomplish so I can see if it is feasible and what the best approach is? Why do you need to know the operators and variables of an expression?

Thanks,

Eugene
Aug 20, 2007 at 3:53 PM
Edited Aug 20, 2007 at 4:23 PM

Hi,
Thanks for the prompt reply.

We want to use these variables after compilation.So once they are identified they can be used in any custom expression and they will not be compiled again.
We do not want to recompile the code after identifying variables.Is this feasible?

In "Expression.vb" class ,in "Compile" function, "topElement" object has all the variables and operators of an expression.
All the Topelement,Left Elements,Right Elements available in expression are to be identified in a form.
e.g a plus b ( "plus" top element , "a" Left Element , "b" Right Element ) This is to be identified in the form.

Please suggest the best suitable solution for this?

Please correct if there is anything wrong in this.

Thank you.

With Best Regards,
Nanded


Coordinator
Aug 20, 2007 at 5:50 PM
Hi,

Sounds like you want to be able to define variables at runtime and use them in an expression.

// Define the dynamic expression owner, strongly-typed to int
DynamicExpressionOwner<int> owner = new DynamicExpressionOwner<int>();
// Define some variables
owner.Variables["a"] = 100;
owner.Variables["b"] = 200;
 
// Create an expression that uses the variables and is owned by our owner
Expression e = new Expression("a+b", owner);
ExpressionEvaluator<int> evaluator = (ExpressionEvaluator<int>) e.Evaluator;
// Get the result
int result = evaluator();
 
// Change the value of a variable
owner.Variables["a"] = 222;
// Re-evaluate to get the updated result
result = evaluator();

I just implemented this over the weekend. It is still in rough form and needs to be tweaked but the concept is there.
Coordinator
Aug 21, 2007 at 1:50 AM
Edited Aug 21, 2007 at 2:42 PM
Hi,

I've refactored how dynamic variables work. You can now have multiple variables of different types. See the examples page for sample code. You'll have to re-download 9.0.4.0 to get the changes.

Thanks.
Aug 21, 2007 at 9:13 AM

Hi,
Thank you very much for your reply.

We are going through your suggestions and FLEE 9.0.4.0
These are really helpful.


Regards,
Nanded

Aug 23, 2007 at 3:03 PM
Hi,

That dynamic variable definition is very cool. Is there a way to hook into your parser and retrieve a listing of the variables in a formula before the Dynamic ExpressionOwner is created?

Regards,
Nanded
Aug 23, 2007 at 4:30 PM
I thought I'd provide a little background as to what I was trying to do. It might clarify my last posting.

The FLEE framework is awesome! I'm trying to use it in a computationally intensive time driven application. The usage might be something like

dim and set array a's values
dim and set array b's values
dim retval
For (int idx = 0 to 100000) {
FleeClass.a = aidx
FleeClass.b = bidx
retvalidx = FleeClass.Evaluate
}

Where the FleeClass uses the expression ab. Now if I have similiar expression appear, zd, I'd like to be able to recognize this is really the same formula as a+b and reuse the already compiled FleeClass. To do this, reusing your tokenizer logic to pick out the variables would be the first step in genericizing the formula. I'd rather not bring another tokenizer into the project.

Any thoughts?

Coordinator
Aug 23, 2007 at 8:57 PM
Ahh, you are trying to cache expressions to minimize the overhead of compilation.

In that case, knowing the variables won't help because the expressions a + b and a * b have the same variables but different results. Also, if you are using fields as the variables, the expression a + b will be customized to use fields a and b and will not work for fields z and d (I'm guessing at the variable names as the formatting scrambled that part of your post).

I'll have to think about how to achieve this.

Have you tried it and experienced performance issues or are you trying to pre-optimize?

Thanks,
Coordinator
Aug 24, 2007 at 12:33 AM
dim and set array a's values
dim and set array b's values
dim retval
For (int idx = 0 to 100000) {
FleeClass.a = aidx
FleeClass.b = bidx
retvalidx = FleeClass.Evaluate
}

What happens in the retvalidx = FleeClass.Evaluate method? Usually, by the time you get to the inner loop, the expressions are already defined and you are just looping through the values and evaluating.
Aug 28, 2007 at 4:27 PM
Forgetting the complexities of my instance, here is a simple reason why you might want to expose a method to return a listing of the variables used in an expression.

In your DynamicExpressionOwner example above. If 30 variables could be used in an expression and only 2 are used in the expression. It's inefficient to loop over and set all 30 variables for each evaluate call. It would be ideal to know that your method only used two of them and to only set those 2.


btw, I'm trying to pre-optimize. If we can get this to work, we would be calling the evaluating method ~ 600 million times/run. Cool stuff.
Coordinator
Aug 28, 2007 at 10:18 PM
Cool. I will put in the ability to find out which variables an expression uses.

>we would be calling the evaluating method ~ 600 million times/run.
Since expressions generally run in the 1 million evaluations/sec, you are looking at 10 minutes for each run. Is that good enough or are you aiming for real-time?
Aug 30, 2007 at 7:10 PM
Edited Aug 30, 2007 at 7:12 PM
Real time would be kick ass, but I think I can live with 10 minutes. ;) Any idea when it will be in?
Aug 30, 2007 at 7:10 PM
btw, Thanks a ton!
Coordinator
Aug 30, 2007 at 8:14 PM
Edited Aug 30, 2007 at 8:15 PM
>Real time would be kick ass, but I think I can live with 10 minutes. ;)
You could also have multiple threads evaluate the same expression as evaluation is thread-safe. Your loop looks perfect for paralellization.

>Any idea when it will be in?
I'll see if I can get it in tonight
Coordinator
Aug 31, 2007 at 2:06 AM
I've implemented the feature in release 0.9.5.5. It is an interim release and so there is no documentation yet.

Here's how to get the referenced variables:
Expression e = new Expression("a + b", owner);
// Get the referenced variables; not a property since it returns a new array each time
string[] variables = e.Info.GetReferencedVariables();

Aug 31, 2007 at 7:07 PM
awesome! I'll try to play with it tonight.
Sep 6, 2007 at 3:39 PM

Hi,
Now referenced variables can be identified.
It is as per our requirement.

Millions of Thanks!!!

With Best Regards,
Nanded