|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Execute Arbitrary Math FormulasI have the following scenario. A user requests some math calculations
from a server. The data and a library of basic formulas reside on the server. Now the user should be able to create more complex formulas based on the basic built in formulas as well as other complex formulas that the user created himself. These formulas will be either stored on the server or client and will be applied to the data on the server. Some of the formulas will be used only once (if they don't work as expected) wheras others might get used on a frequent basis. What is the best way to approach this problem? Thanks Don't know if this of any use.
http://www.codeguru.com/csharp/csharp/cs_misc/mathematics/article.php/c4245/ rob wrote: Show quote > I have the following scenario. A user requests some math calculations > from a server. The data and a library of basic formulas reside on the > server. Now the user should be able to create more complex formulas > based on the basic built in formulas as well as other complex formulas > that the user created himself. These formulas will be either stored on > the server or client and will be applied to the data on the server. > Some of the formulas will be used only once (if they don't work as > expected) wheras others might get used on a frequent basis. What is the > best way to approach this problem? > > Thanks Hi,
Depends, where the logic for evaluate the formulas reside? if you are going to allow arbitrary formulas you need a formula evaluator. Depending of the framework you are using you can evaluate them in the server or in the client. IMO you should provide more details about your environment. -- Show quote-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation "rob" <rmdiv2***@yahoo.com> wrote in message news:1150731525.879284.305110@i40g2000cwc.googlegroups.com... >I have the following scenario. A user requests some math calculations > from a server. The data and a library of basic formulas reside on the > server. Now the user should be able to create more complex formulas > based on the basic built in formulas as well as other complex formulas > that the user created himself. These formulas will be either stored on > the server or client and will be applied to the data on the server. > Some of the formulas will be used only once (if they don't work as > expected) wheras others might get used on a frequent basis. What is the > best way to approach this problem? > > Thanks > rob schrieb:
> I have the following scenario. A user requests some math calculations Write a parser. Dunno if yacc/lex are available for visual studio. If not,> from a server. The data and a library of basic formulas reside on the > server. Now the user should be able to create more complex formulas > based on the basic built in formulas as well as other complex formulas > that the user created himself. These formulas will be either stored on > the server or client and will be applied to the data on the server. > Some of the formulas will be used only once (if they don't work as > expected) wheras others might get used on a frequent basis. What is the > best way to approach this problem? get a book about compiler construction and do a recursive parser. Lots of Greetings! Volker -- For email replies, please substitute the obvious. ANTLR <http://www.antlr.org/> is compiler compiler that generates C#
code. Volker Hetzer wrote: Show quote > Write a parser. Dunno if yacc/lex are available for visual studio. If not, > get a book about compiler construction and do a recursive parser. > > Lots of Greetings! > Volker > -- > For email replies, please substitute the obvious. What is your opinion about on-the-fly/runtime compiling as another
solution and how would this be approched. Thanks Rob,
If your talking about using reflection then that would definitely be the easiest. However, the security vulnerability you open up could be huge. The compiler compiler option is probably the best overall, but there is a huge learning curve. And of course you could always whip up your own evaluator. Writing your own isn't terribly difficult. It consists of 3 basic steps: First, tokenize the input string. Then perform an infix to postfix conversion. That gets rid of paranthesis and cumbersome operator precedence rules. Finally, evaluate by popping operands of a stack and pushing the result until all operators are consumed. Brian rob wrote: Show quote > What is your opinion about on-the-fly/runtime compiling as another > solution and how would this be approched. > > Thanks rob schrieb:
> What is your opinion about on-the-fly/runtime compiling as another There is no "eval" statement there, probably for security reasons.> solution and how would this be approched. Also, the JIT compiler expects some IL code as far as I know, not source code. You *could* create a little C# program and compile it. Requires Visual studio on the server (eurgh!!!) Won't work for more than very infrequent use and creates a huge workload on the server. You could install perl or tcl on the server. They *do* have an eval statement. Write a small server that listens on a port, gets the expression string, evaluates it and returns the result. But really, an expression parser isn't that hard to do. A simple one was a (very small) part of my diploma thesis and every CS student does several during the course of his studies. Lots of Greetings! Volker -- For email replies, please substitute the obvious. Volker Hetzer wrote:
Show quote > rob schrieb: Unfortunatelly (for this particular problem) I have no CS degree> > What is your opinion about on-the-fly/runtime compiling as another > > solution and how would this be approched. > There is no "eval" statement there, probably for security reasons. > Also, the JIT compiler expects some IL code as far as I know, not source > code. > You *could* create a little C# program and compile it. Requires Visual > studio on the server (eurgh!!!) Won't work for more than very infrequent > use and creates a huge workload on the server. > > You could install perl or tcl on the server. They *do* have > an eval statement. Write a small server that listens on a port, > gets the expression string, evaluates it and returns the result. > > But really, an expression parser isn't that hard to do. A simple one > was a (very small) part of my diploma thesis and every CS student > does several during the course of his studies. > > Lots of Greetings! > Volker > -- > For email replies, please substitute the obvious. (though one from a different field). Well, I guess I'll have to learn how to write a parser then. I just wonder if a parser isn't quite inefficient. I need to repeate the calculation on hundreds of data points. The first two steps of the parser probably would have to be done only once but the last step seems still quite costly. But it seems short of doing compilation at run time there isn't much else that can be done. You can get a good start on your parser, in C# V2, by consulting
http://www.frontiernet.net/~fredm/dps/Contents.htm , Chapter 3. The sample parser will parse simple arithmetic expressions and can be expanded fairly easily. You will have to write a lexical analyzer, but this is easily done by using Regex. Show quote "rob" <rmdiv2***@yahoo.com> wrote in message news:1150847655.976936.89010@i40g2000cwc.googlegroups.com... > > Volker Hetzer wrote: >> rob schrieb: >> > What is your opinion about on-the-fly/runtime compiling as another >> > solution and how would this be approched. >> There is no "eval" statement there, probably for security reasons. >> Also, the JIT compiler expects some IL code as far as I know, not source >> code. >> You *could* create a little C# program and compile it. Requires Visual >> studio on the server (eurgh!!!) Won't work for more than very infrequent >> use and creates a huge workload on the server. >> >> You could install perl or tcl on the server. They *do* have >> an eval statement. Write a small server that listens on a port, >> gets the expression string, evaluates it and returns the result. >> >> But really, an expression parser isn't that hard to do. A simple one >> was a (very small) part of my diploma thesis and every CS student >> does several during the course of his studies. >> >> Lots of Greetings! >> Volker >> -- >> For email replies, please substitute the obvious. > > Unfortunatelly (for this particular problem) I have no CS degree > (though one from a different field). Well, I guess I'll have to learn > how to write a parser then. I just wonder if a parser isn't quite > inefficient. I need to repeate the calculation on hundreds of data > points. The first two steps of the parser probably would have to be > done only once but the last step seems still quite costly. But it seems > short of doing compilation at run time there isn't much else that can > be done. > "rob" <rmdiv2***@yahoo.com> wrote in message Sounds like SQL server for the data, predefined formulas as stored news:1150731525.879284.305110@i40g2000cwc.googlegroups.com... >I have the following scenario. A user requests some math calculations > from a server. The data and a library of basic formulas reside on the > server. Now the user should be able to create more complex formulas > based on the basic built in formulas as well as other complex formulas > that the user created himself. These formulas will be either stored on > the server or client and will be applied to the data on the server. > Some of the formulas will be used only once (if they don't work as > expected) wheras others might get used on a frequent basis. What is the > best way to approach this problem? procedures written in .NET, and user formulas as queries, possibly with Javascript.NET postprocessing. This would permit compiling the queries for repeated use. Show quote > > Thanks > ["Followup-To:" header set to microsoft.public.dotnet.framework.]
On 2006-06-19, rob <rmdiv2***@yahoo.com> wrote: > I have the following scenario. A user requests some math calculations As already mentionned, store the 'command string' and evaluate it..> from a server. The data and a library of basic formulas reside on the > server. Now the user should be able to create more complex formulas > based on the basic built in formulas as well as other complex formulas > that the user created himself. These formulas will be either stored on > the server or client and will be applied to the data on the server. > Some of the formulas will be used only once (if they don't work as > expected) wheras others might get used on a frequent basis. What is the > best way to approach this problem? http://en.wikipedia.org/wiki/Reverse_polish_notation can be a good start... |
|||||||||||||||||||||||