001package jmri.jmrit.logixng.util.parser;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.JmriException;
007import jmri.jmrit.logixng.SymbolTable;
008
009/**
010 * A parsed expression
011 * 
012 * @author Daniel Bergqvist 2019
013 */
014public class ExpressionNodeFunction implements ExpressionNode {
015
016    private final String _identifier;
017    private final Function _function;
018    private final List<ExpressionNode> _parameterList;
019    
020    
021    public ExpressionNodeFunction(String identifier, List<ExpressionNode> parameterList) throws FunctionNotExistsException {
022        _identifier = identifier;
023        _function = InstanceManager.getDefault(FunctionManager.class).get(identifier);
024        _parameterList = parameterList;
025        
026        if (_function == null) {
027            throw new FunctionNotExistsException(Bundle.getMessage("FunctionNotExists", identifier), identifier);
028        }
029        
030//        System.err.format("Function %s, %s%n", _function.getName(), _function.getClass().getName());
031    }
032    
033    @Override
034    public Object calculate(SymbolTable symbolTable) throws JmriException {
035        return _function.calculate(symbolTable, _parameterList);
036    }
037    
038    /** {@inheritDoc} */
039    @Override
040    public String getDefinitionString() {
041        StringBuilder str = new StringBuilder();
042        str.append("Function:");
043        str.append(_identifier);
044        str.append("(");
045        for (int i=0; i < _parameterList.size(); i++) {
046            if (i > 0) {
047                str.append(",");
048            }
049            str.append(_parameterList.get(i).getDefinitionString());
050        }
051        str.append(")");
052        return str.toString();
053    }
054    
055}