(Math) Expression Evaluator

C

C P

I'm converting part of a Win32 app that evaluates some mathematical
expresions at runtime. For example "SUM( @10001@, @10002@, @10003@)" is an
expression stored in a table's record, where 10001, 10002, 10003 are id's of
records in a specific table. I want to grab the associated values for those
ID's, then evaluate the Sum of them. I can substitute in the values of for
the ID's easily enough. However I haven't found anything in the .NET
framework yet that will allow (or help) me evaluate the Sum. To try and be
more clear, I can convert "SUM( @10001@, @10002@, @10003@)", to "SUM(
5,3,NULL)", easily enough, but I'm looking for something that will help me
get the sum of 5,3,NULL. Any suggestions for classes to look at?

Thanks,
Chris
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

It might not be that performant, but it seems that you are storing the
expression in a format that is native to the database that you are getting
the data from. If this is the case, then you could always take the
expression (that has had the values substituted) and issue it against the
server, in this case:

select SUM(5, 3, NULL)

Then you can just call the ExecuteScalar method on the command and get
the result. Having something on the client side that will be able to
interpret this would mean that you are basically placing your db command
parser on each side of the app, and that might not be the most efficient
thing.

Instead, you might want to consider converting your expressions into
something that can be interpreted on the client side a little easier,
perhaps an expression for a DataColumn, or an XSLT transformation which will
produce the desired output.

Hope this helps.
 
C

C P

The DataColumn.Expression property looks fairly promising. For a number of
reasons, I don't have a server that I can pass these calculations to, so
this all has to be done on the client. Luckily the calculations aren't too
complex, so hopefully it won't be too hard to convert them into
DataColumn.Expression(s).

Thanks,
Chris


Nicholas Paldino said:
Chris,

It might not be that performant, but it seems that you are storing the
expression in a format that is native to the database that you are getting
the data from. If this is the case, then you could always take the
expression (that has had the values substituted) and issue it against the
server, in this case:

select SUM(5, 3, NULL)

Then you can just call the ExecuteScalar method on the command and get
the result. Having something on the client side that will be able to
interpret this would mean that you are basically placing your db command
parser on each side of the app, and that might not be the most efficient
thing.

Instead, you might want to consider converting your expressions into
something that can be interpreted on the client side a little easier,
perhaps an expression for a DataColumn, or an XSLT transformation which will
produce the desired output.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

C P said:
I'm converting part of a Win32 app that evaluates some mathematical
expresions at runtime. For example "SUM( @10001@, @10002@, @10003@)" is an
expression stored in a table's record, where 10001, 10002, 10003 are
id's
of
records in a specific table. I want to grab the associated values for those
ID's, then evaluate the Sum of them. I can substitute in the values of for
the ID's easily enough. However I haven't found anything in the .NET
framework yet that will allow (or help) me evaluate the Sum. To try and be
more clear, I can convert "SUM( @10001@, @10002@, @10003@)", to "SUM(
5,3,NULL)", easily enough, but I'm looking for something that will help me
get the sum of 5,3,NULL. Any suggestions for classes to look at?

Thanks,
Chris
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

If the expressions are complex, then you might want to consider an XSLT
transformation as well.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

C P said:
The DataColumn.Expression property looks fairly promising. For a number of
reasons, I don't have a server that I can pass these calculations to, so
this all has to be done on the client. Luckily the calculations aren't too
complex, so hopefully it won't be too hard to convert them into
DataColumn.Expression(s).

Thanks,
Chris


message news:[email protected]...
Chris,

It might not be that performant, but it seems that you are storing the
expression in a format that is native to the database that you are getting
the data from. If this is the case, then you could always take the
expression (that has had the values substituted) and issue it against the
server, in this case:

select SUM(5, 3, NULL)

Then you can just call the ExecuteScalar method on the command and get
the result. Having something on the client side that will be able to
interpret this would mean that you are basically placing your db command
parser on each side of the app, and that might not be the most efficient
thing.

Instead, you might want to consider converting your expressions into
something that can be interpreted on the client side a little easier,
perhaps an expression for a DataColumn, or an XSLT transformation which will
produce the desired output.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

C P said:
I'm converting part of a Win32 app that evaluates some mathematical
expresions at runtime. For example "SUM( @10001@, @10002@, @10003@)"
is
an
expression stored in a table's record, where 10001, 10002, 10003 are
id's
of
records in a specific table. I want to grab the associated values for those
ID's, then evaluate the Sum of them. I can substitute in the values
of
for
the ID's easily enough. However I haven't found anything in the .NET
framework yet that will allow (or help) me evaluate the Sum. To try
and
be
more clear, I can convert "SUM( @10001@, @10002@, @10003@)", to "SUM(
5,3,NULL)", easily enough, but I'm looking for something that will
help
 
C

C P

The expressions are not complex, but as I thought more about this, the
DataColumn.Expression won't do the trick, and I don't think XSLT will
either. :(

Essentially I have 2 tables. The first table (FormStructure) defines a
series of calculations/questions for a form. There's one calc or question
per row, and the calculations are based on values for questions/calculations
(from other rows). The second table (FormAnswers) contains all of the
values for a particular instance of a form. Each row in FormAnswers
corresponds to a user's response to a question, or the result of a
calculation. Each of these rows relates to a calc or question from
FormStructure.

I am doing this in rows to allow me to update the questions and/or
calculations without having to change my application code, and because a
form will have many questions - too many to set up each question/calc as a
column in a table.

So, what I need to do is take something like, "@10001@ + @10002@ + @10003@"
from FormStructure, and substitute in the corresponding values from
FormAnswers, leaving me with "1+3+5". Then, I need to somehow evaluate the
string "1+3+5". I can substitute in the values easily enough using Regular
Expressions (I think), but I'm not sure how to evaluate "1+3+5". I do
recall reading how to construct and compile .NET classes at runtime, but I
have over 200 of these calculations, so performance to compile these classes
would be a real concern. What I really need is to call something like,
Evaluator.Evaluate("1+3+5").

Maybe I can just create a DataColumn on it's own, and set it's Expression to
"1+3+5", then look at the value of the column.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top