Results 1 to 3 of 3

Thread: how to execute a string expression? e.g. "1.0 / sqrt(sqrt(x))"

  1. #1
    Join Date
    Oct 2008
    Posts
    286

    Default how to execute a string expression? e.g. "1.0 / sqrt(sqrt(x))"

    i'm not sure if the thread's location is correct..
    please move it if not appropriate.

    I think it is possible to execute a string expression like the Webflow's <evaluate/> tag.

    I have dynamic formula to execute. It changes frequently that's why I am planning to put it in xml as <property/> of a <bean/> tag...
    then evaluate it, get the result and do other process..

    here's image:

    xml
    Code:
    	<bean id="computeService"
    		class="abs.service.ComputeServiceImpl">
    		<property name="expression" value="1.0 / sqrt( sqrt( x1 ) )"/>				
    	</bean>
    java
    Code:
    public class ComputeServiceImpl implements ComputeService {
    	private String expression;
    	//getter setter here
    	
    	@Override
    	public BigDecimal compute(BigDecimal x1) {
    		// evaluateExpression(String) is sample only
    		// the method will be able to evaluate the given expression
    		BigDecimal result = evaluateExpression(expression, x1);
    		
    		// do other computations here
    		
    		return result;
    	}
    	
    	private BigDecimal evaluateExpression(String expression, BigDecimal input) {
    		BigDecimal result;
    		
    		// code here, it will update the result variable from the given expression
    		
    		return result;
    	}
    	
    }
    sample input/output
    Code:
    // the expression will be: 
    //                = 1.0 / Math.sqrt( Math.sqrt( x1.doubleValue() ) )
    //                = 0.562341325
    BigDecimal result = computeService.compute(BigDecimal.TEN);
    
    System.out.println(result);
    
    >Prints
    0.562341325
    Thanks in advance.
    Last edited by eros; Dec 2nd, 2010 at 12:18 AM.
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

  2. #2
    Join Date
    Oct 2008
    Posts
    286

    Default

    I found one... JEP

    Thanks a lot.
    Eros

    Environment:
    JSP 2.0
    Dojo 1.4.1
    Ext JS 3.1 (testing)
    Spring MVC 2.5.6.SEC01 (planning to Spring 3 using STS)
    STS
    SWF 2.0.9.RELEASE
    Tiles 2.0.5
    iBatis: ibatis-sqlmap-2.3.4.726

  3. #3
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    Web Flow uses Spring EL. It's part of the Spring Framework and is used in many other Spring projects.

Posting Permissions

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