package org.kevoree.modeling.traversal.impl.actions;

import org.kevoree.modeling.meta.impl.MetaLiteral;
import org.kevoree.modeling.traversal.KTraversalAction;
import org.kevoree.modeling.traversal.KTraversalActionContext;
import org.kevoree.modeling.util.PrimitiveHelper;
import org.kevoree.modeling.util.maths.expression.KMathExpressionEngine;
import org.kevoree.modeling.util.maths.expression.KMathVariableResolver;
import org.kevoree.modeling.util.maths.expression.impl.MathExpressionEngine;

public class MathExpressionAction implements KTraversalAction {

    private String _expression;

    private KMathExpressionEngine _engine;

    public MathExpressionAction(String p_expression) {
        this._expression = p_expression;
        this._engine = new MathExpressionEngine();
    }

    @Override
    public void chain(KTraversalAction next) {
        //terminal leaf action
    }

    @Override
    public void execute(KTraversalActionContext context) {
        Object[] selected = new Object[context.inputObjects().length];
        for (int i = 0; i < context.inputObjects().length; i++) {
            if (context.inputObjects()[i] != null) {
                final int finalI = i;
                _engine.setVarResolver(new KMathVariableResolver() {
                    @Override
                    public Double resolve(String potentialVarName) {
                        if (PrimitiveHelper.equals(potentialVarName,"PI")) {
                            return Math.PI;
                        }
                        if (PrimitiveHelper.equals(potentialVarName,"TRUE")) {
                            return 1.0;
                        }
                        if (PrimitiveHelper.equals(potentialVarName,"FALSE")) {
                            return 0.0;
                        }
                        Object resolved = context.inputObjects()[finalI].getByName(potentialVarName);
                        if (resolved != null) {
                            if (resolved instanceof MetaLiteral) {
                                return (double) ((MetaLiteral) resolved).index();
                            } else {
                                String valueString = resolved.toString();
                                if(PrimitiveHelper.equals(valueString,"true")){
                                    return 1.0;
                                } else if(PrimitiveHelper.equals(valueString,"false")){
                                    return 0.0;
                                } else {
                                    try {
                                        return PrimitiveHelper.parseDouble(resolved.toString());
                                    } catch (Exception e) {
                                        //noop
                                    }
                                }
                            }
                        }
                        return null;
                    }
                });
                selected[finalI] = _engine.eval(this._expression);
            }
        }
        if (context.finalCallback() != null) {
            context.finalCallback().on(selected);
        }
    }
}

@@ -47,16 +47,53 @@
 tions;%0A%0A
+import org.kevoree.modeling.KObject;%0A
 import o
@@ -1460,16 +1460,17 @@
 VarName,
+ 
 %22PI%22)) %7B
@@ -1608,16 +1608,17 @@
 VarName,
+ 
 %22TRUE%22))
@@ -1754,16 +1754,17 @@
 VarName,
+ 
 %22FALSE%22)
@@ -1861,23 +1861,23 @@
     
+K
 Object 
-resolved
+loopObj
  = c
@@ -1905,16 +1905,598 @@
 %5BfinalI%5D
+;%0A                        if (PrimitiveHelper.equals(potentialVarName, %22TIME_MS_NS%22)) %7B%0A                            return (double) (loopObj.now() / 1000000);%0A                        %7D%0A                        if (PrimitiveHelper.equals(potentialVarName, %22TIME_NS_MS%22)) %7B%0A                            return (double) (loopObj.now() * 1000000);%0A                        %7D%0A                        if (PrimitiveHelper.equals(potentialVarName, %22TIME%22)) %7B%0A                            return (double) loopObj.now();%0A                        %7D%0A                        Object resolved = loopObj
 .getByNa
@@ -2859,16 +2859,17 @@
       if
+ 
 (Primiti
@@ -2900,16 +2900,17 @@
 ing,
+ 
 %22true%22))
 %7B%0A  
@@ -2901,24 +2901,25 @@
 ng, %22true%22))
+ 
 %7B%0A          
@@ -2997,16 +2997,17 @@
  else if
+ 
 (Primiti
@@ -3034,16 +3034,17 @@
 eString,
+ 
 %22false%22)
@@ -3044,16 +3044,17 @@
 false%22))
+ 
 %7B%0A      

improve math expression