Results 1 to 10 of 10
- 03-09-2011, 11:23 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
I need to execute a formula taken from a db at runtime How-To
I have Formulas stored in a database for UOM conversion... stored in the following way..
Formula = F/T
Where F=1500 and T=2000
I want to substitue F with 1500 and T with 2000 and execute the
formula at runtime..
The formula that can be kept in this db field is dynamic and may change
based on how the user sets up the UOM conversion for many different cases.
the example above is From lbs to To Tons
F=From T=To
- 03-09-2011, 01:47 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,428
- Blog Entries
- 7
- Rep Power
- 17
You can write your own expression parser and evaluator but you can also pass your formula to Javascript; Javascript comes bundled with JAVA SE 1.6. Here's how to use it:
kind regards,Java Code:import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class ScriptDemo { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); try { engine.put("T", 1500); engine.put("F", 2000); System.out.println("result: "+engine.eval("F/T")); } catch(ScriptException se) { se.printStackTrace(); } } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-09-2011, 05:50 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
This is exactly what I was looking for.
I tip my hat to you. This is exactly what I needed.
Your example helps out a lot.. I have the tokenizer part figured out, but, I'm relatively new to Java, so, I'm still trying to figure some of the finer things...
Jeff
- 03-09-2011, 06:47 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,428
- Blog Entries
- 7
- Rep Power
- 17
You're welcome of course; you can play quite nasty tricks with those script engines. Here's a more elaborate example; maybe you can use some of the ideas:
kind regards,Java Code:import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class ScriptDemo { public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); try { String expression = "function add(x, y) { return x+y; }"; engine.eval(expression); engine.put("a", 41); engine.put("b", 1); Invocable invocable= (Invocable)engine; System.out.println("add(1, 41)= "+invocable.invokeFunction("add", 1, 41)); System.out.println("add(a, b)= "+invocable.invokeFunction("add", "a", "b")); System.out.println("add(a, b)= "+engine.eval("add(a, b)")); } catch(ScriptException se) { se.printStackTrace(); } catch (NoSuchMethodException nsme) { nsme.printStackTrace(); } } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-10-2011, 02:21 PM #5
@JosAH
I'm very grateful too.
Too bad I made my own parser.;)
- 03-10-2011, 03:32 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,428
- Blog Entries
- 7
- Rep Power
- 17
Is your parser fully functional? If yes, it's easy to construct an AST (Abstract Syntax Tree) from the input or even a postfix representation of the expression and create an evaluator for that. You end up with a lot less code because the entire Javascript interpreter brings more code with it. Your choice ;-) An evaluator for expression evaluation isn't very complex (given a suitable AST or postfix expression form).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-10-2011, 03:47 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
I'm having what I'm sure, is a simple problem with the ScriptEngine.eval method.. It passes back a type Object, which I can't seem to be able to cast to type double.. It prints everything nice to the screen w/System.out.println(); I'm going to try your new example and see if the Invoker class will send my value back as a double..
Thanks Again.. your help is very appreciated.
- 03-10-2011, 04:21 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,428
- Blog Entries
- 7
- Rep Power
- 17
Last edited by JosAH; 03-10-2011 at 04:25 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-10-2011, 05:25 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
Here is how I ended up do my conversion from String type to type double..
objTest = invocable.invokeFunction("add", 1, 41);
strTest = objTest.toString();
dblTest = Double.valueOf(strTest.trim()).doubleValue();
System.out.println("This is dblTest: < " + dblTest + " >");
Seems to work great.. kind of new to Java (about 3 weeks in).. Is there a better way to do this.. and does Java only allow me to 'cast' the primitive types?
- 03-10-2011, 05:30 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,428
- Blog Entries
- 7
- Rep Power
- 17
Yep, also see my previous reply: the result type of the evaluation is Double; autoboxing in Java 'automagically' can cast it to the type double. Your solution takes a detour: Double --> String --> double. The middle step is not necessary:
kind regards,Java Code:double d= (Double)invocable.invokeFunction("add", 1, 41);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Formula class
By imorio in forum New To JavaReplies: 3Last Post: 02-23-2011, 09:38 PM -
changing TF formula
By o.imen in forum LuceneReplies: 0Last Post: 09-16-2010, 01:20 PM -
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07-03-2009, 06:57 PM -
Using Runtime to execute external commands
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:14 AM -
What is the formula?
By yuchuang in forum New To JavaReplies: 3Last Post: 04-30-2007, 10:00 PM


LinkBack URL
About LinkBacks


Bookmarks