Announcement Announcement Module
Collapse
No announcement yet.
how to execute a string expression? e.g. "1.0 / sqrt(sqrt(x))" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 12:18 AM.

  • #2
    I found one... JEP

    Thanks a lot.

    Comment


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

      Comment

      Working...
      X