package com.ruoyi.project.business.dto.JEPCustomFunction;

import org.nfunk.jep.EvaluatorI;
import org.nfunk.jep.Node;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.CallbackEvaluationI;
import org.nfunk.jep.function.PostfixMathCommand;

/**
 * @author jubs
 */
public class IncludeFunction extends PostfixMathCommand implements CallbackEvaluationI {

    public IncludeFunction() {
        numberOfParameters = 2;
    }

    @Override
    public Object evaluate(Node node, EvaluatorI pv) throws ParseException {
        int num = node.jjtGetNumChildren();
        if (!this.checkNumberOfParameters(num)) {
            throw new ParseException("If operator must have 2 arguments.");
        } else {
            Object searchBy = pv.eval(node.jjtGetChild(0));
            Object searchIn = pv.eval(node.jjtGetChild(1));
            //该方法服务于，在以”；“拼接的字符串中，完全匹配另一个字符串
            String[] searchInArr = searchIn.toString().replaceAll("；",";").split(";");
            for (String str : searchInArr) {
                if (str.equals(searchBy.toString())) {
                    return 1.0;
                }
            }
            return 0.0;
        }
    }
}
