Results 1 to 10 of 10
  1. #1
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    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:

    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();
            }
        }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default 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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by fortwnty420 View Post
    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...
    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:

    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();
            }
    
        }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @JosAH
    I'm very grateful too.
    Too bad I made my own parser.;)

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Jodokus View Post
    @JosAH
    I'm very grateful too.
    Too bad I made my own parser.;)
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    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.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by fortwnty420 View Post
    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.
    The result type of the evaluation is Double (with a capital-D). So this should work:

    Java Code:
    double d= (Double)engine.eval("add(a, b)");
    kind regards,

    Jos
    Last edited by JosAH; 03-10-2011 at 04:25 PM.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    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?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by fortwnty420 View Post
    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?
    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:

    Java Code:
    double d= (Double)invocable.invokeFunction("add", 1, 41);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Formula class
    By imorio in forum New To Java
    Replies: 3
    Last Post: 02-23-2011, 09:38 PM
  2. changing TF formula
    By o.imen in forum Lucene
    Replies: 0
    Last Post: 09-16-2010, 01:20 PM
  3. Formula Builder
    By rbs100 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2009, 06:57 PM
  4. Using Runtime to execute external commands
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:14 AM
  5. What is the formula?
    By yuchuang in forum New To Java
    Replies: 3
    Last Post: 04-30-2007, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •