storing formula in database

  • Thread starter Thread starter JDF
  • Start date Start date
J

JDF

Does anyone have experience creating a formula generator and storing the
formula in a database for re-use? I want to give my end user the ability to
create custom formulas. The end user would create the formulas in a grid
selecting fields and controls to use in the formulas. The problem I am
having is coming up with a method of storing the formula and then using the
formula in my app. Any help will be greatly appreciated.




mbs
 
It really depends on what your options are for evaluating the formula, and
what kind of things you are comfortable with. It also depends on the context
of this, and how you have your variable placeholders, etc.

In any case, if we can assumey our formula has some placeholders that you
can replace, then I can offer a couple of suggestions - which are really
similar.

1. Pop the placeholders in as columns in a datatable, insert a row, and set
the values on the column appropriate to the placeholders. Then create an
expression column that is your formula, and then check the value in the
column - it should have the computed expression.
2. Replace the placeholders in the formula string with their values, and run
it through a sql engine as a SELECT statement that is selecting your
formula.

Both these suggestions assume that your formula is something that either the
datatable's expression column can handle, or that a SQL engine can handle.
There is some overhead with going to a sql engine to evaluate an arithmetic
expression of course.

Otherwise you have to use some old fashioned formula parsing techniques and
then evaluate it. I'm sure there is tons of information on how to do this
if you search the web for it.
 
You could use the JavaScript Eval function from DotNet storing the formulas
and token values in the database.

Here is a link that describes a few options.

http://www.codecomments.com/.NET_Scripting/message365093-1.html

My personal favorite is:

\\\Eval by Nigel Amstrong
1. Create a file called: DynamicMath.js
2. Add this code to it:

class DynamicMath
{
static function Eval(MathExpression : String) : double
{
return eval(MathExpression);

};
}

3. Compile it with the command line jsc compiler: jsc /t:library
DynamicMath.js
4. Add a reference to DynamicMath.dll to your project (and to
Microsoft.JScript.dll as well)
5. Use from your favourite .NET language:
Dim d As Double = DynamicMath.Eval("2 + 3 + 4")
MessageBox.Show(d)
6. That's it..
///
 
Back
Top