Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1 Need an evaluator which would evaluate a string expression like a=b=c=...=n and return a boolean result

pending
Fathzer
None
FeatureRequest
None
2014-05-27
2013-05-24
Anonymous
No

Discussion

  • praveen
    praveen
    2013-05-25

    I want to define the operator EQUAL like this:
    public final static Operator EQUAL = new Operator("=", n, Operator.Associativity.);

    The operand count could be anything... i dont want to set a limit. Also the operator would have to equate n number of operands. So what would its associativity be?

     
  • Fathzer
    Fathzer
    2013-05-25

    Javaluator only supports unary and binary operators (n = 1 or 2).
    Nevertheless I think there is a solution using Javaluator binary operator.

    Please check the following code:

    import java.util.HashMap;
    import java.util.Iterator;
    
    import net.astesana.javaluator.AbstractEvaluator;
    import net.astesana.javaluator.Operator;
    import net.astesana.javaluator.Operator.Associativity;
    import net.astesana.javaluator.Parameters;
    
    /** This evaluator tests if all the elements of a variable set are equals.
     * <br>The evaluation returns null if one variable is not equals to others.
     */
    public class Test extends AbstractEvaluator<String> {
    private static final Operator EQUALS = new Operator("=", 2, Associativity.RIGHT, 1);
    
    public Test() {
        super(getParameters());
    }
    
    private static Parameters getParameters() {
        Parameters parameters = new Parameters();
        parameters.add(EQUALS);
        return parameters;
    }
    
    @Override
    protected String toValue(String literal, Object evaluationContext) {
        return literal;
    }
    
    @Override
    protected String evaluate(Operator operator, Iterator<String> operands, Object evaluationContext) {
        String first = operands.next();
        String second = operands.next();
        if ((first==null) || (second==null)) return null;
        String firstValue = ((HashMap<String, String>)evaluationContext).get(first);
        String secondValue = ((HashMap<String, String>)evaluationContext).get(second);
        return firstValue.equals(secondValue)?first:null;
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        AbstractEvaluator<String> eval = new Test();
        HashMap<String, String> variables = new HashMap<String, String>();
        variables.put("a", "value");
        variables.put("b", "value");
        variables.put("c", "value");
        variables.put("d", "value");
        variables.put("e", "different value");
        System.out.println (eval.evaluate("a=b=c=d=a", variables));
        System.out.println (eval.evaluate("a=b=e=c", variables));
    }
    }
    

    Best regards,

    Jean-Marc Astesana

     
  • praveen
    praveen
    2013-05-27

    Hey Jean,
    Thanks a lot for your code. However i need something that would evaluate variables of "Double" type and return a boolean result (true if all operands equal; false if all operands are not equal). Or it could return a string result itself (pass if all operands equal; fail if all operands are not equal). Your inputs would be greatly appreciated. I tried to override toValue() method with boolean return value. Its throwing an error. Im not sure how to do this.

     
  • Fathzer
    Fathzer
    2013-05-27

    Hi,

    Here is a new version that works with variables containing any type you want and returning "pass" or "fail".
    If you want to return a boolean, you can implement a method that calls super.evaluate, then returns true or false depending on the result (a string or null).

    package test;
    import java.util.HashMap;
    import java.util.Iterator;
    
    import net.astesana.javaluator.AbstractEvaluator;
    import net.astesana.javaluator.Operator;
    import net.astesana.javaluator.Operator.Associativity;
    import net.astesana.javaluator.Parameters;
    
    /** This evaluator tests if all the elements of a variable set are equals.
     * <br>The evaluation returns null if one variable is not equals to others.
     */
    public class VariablesEquality extends AbstractEvaluator<String> {
    private static final Operator EQUALS = new Operator("=", 2, Associativity.RIGHT, 1);
    
    public VariablesEquality() {
        super(getParameters());
    }
    
    private static Parameters getParameters() {
        Parameters parameters = new Parameters();
        parameters.add(EQUALS);
        return parameters;
    }
    
    @Override
    protected String toValue(String literal, Object evaluationContext) {
        return literal;
    }
    
    @Override
    protected String evaluate(Operator operator, Iterator<String> operands, Object evaluationContext) {
        String first = operands.next();
        String second = operands.next();
        if ((first==null) || (second==null)) return null;
        Object firstValue = ((HashMap<String, Object>)evaluationContext).get(first);
        Object secondValue = ((HashMap<String, Object>)evaluationContext).get(second);
        return firstValue.equals(secondValue)?first:null;
    }
    
    @Override
    public String evaluate(String expression, Object evaluationContext) {
        return super.evaluate(expression, evaluationContext)==null ? "fail" : "pass";
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        AbstractEvaluator<String> eval = new VariablesEquality();
        HashMap<String, Object> variables = new HashMap<String, Object>();
        variables.put("a", Double.valueOf(1));
        variables.put("b", Double.valueOf(1));
        variables.put("c", Double.valueOf(1));
        variables.put("d", Double.valueOf(1));
        variables.put("e", "a string");
        System.out.println (eval.evaluate("a=b=c=d=a", variables));
        System.out.println (eval.evaluate("a=b=e=c", variables));
    }
    }
    

    Another way would be to implement a function areEquals(a,b,c ...). But it's not your question ;-)

    Best regards,

    Jean-Marc Astesana

     


Anonymous


Cancel   Add attachments