001package jmri.jmrit.logixng.expressions;
002
003import java.beans.PropertyChangeEvent;
004import java.beans.PropertyChangeListener;
005import java.util.*;
006
007import jmri.*;
008import jmri.jmrit.logixng.*;
009import jmri.jmrit.logixng.util.LogixNG_SelectString;
010
011/**
012 * Reads a local variable.
013 *
014 * @author Daniel Bergqvist Copyright 2022
015 */
016public class AnalogExpressionLocalVariable extends AbstractAnalogExpression
017        implements PropertyChangeListener {
018
019    private final LogixNG_SelectString _selectNamedBean =
020            new LogixNG_SelectString(this, this);
021
022    public AnalogExpressionLocalVariable(String sys, String user)
023            throws BadUserNameException, BadSystemNameException {
024
025        super(sys, user);
026    }
027
028    @Override
029    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws JmriException {
030        AnalogExpressionManager manager = InstanceManager.getDefault(AnalogExpressionManager.class);
031        String sysName = systemNames.get(getSystemName());
032        String userName = userNames.get(getSystemName());
033        if (sysName == null) sysName = manager.getAutoSystemName();
034        AnalogExpressionLocalVariable copy = new AnalogExpressionLocalVariable(sysName, userName);
035        copy.setComment(getComment());
036        _selectNamedBean.copy(copy._selectNamedBean);
037        return manager.registerExpression(copy).deepCopyChildren(this, systemNames, userNames);
038    }
039
040    public LogixNG_SelectString getSelectNamedBean() {
041        return _selectNamedBean;
042    }
043
044    /** {@inheritDoc} */
045    @Override
046    public Category getCategory() {
047        return Category.ITEM;
048    }
049
050    /** {@inheritDoc} */
051    @Override
052    public double evaluate() throws JmriException {
053        String localVariable = _selectNamedBean.evaluateValue(getConditionalNG());
054        if (localVariable == null) return 0.0;
055
056        Object value = getConditionalNG().getSymbolTable().getValue(localVariable);
057        if (value != null) {
058            return jmri.util.TypeConversionUtil.convertToDouble(value, false);
059        } else {
060            return 0.0;
061        }
062    }
063
064    /** {@inheritDoc} */
065    @Override
066    public FemaleSocket getChild(int index)
067            throws IllegalArgumentException, UnsupportedOperationException {
068        throw new UnsupportedOperationException("Not supported.");
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public int getChildCount() {
074        return 0;
075    }
076
077    /** {@inheritDoc} */
078    @Override
079    public String getShortDescription(Locale locale) {
080        return Bundle.getMessage(locale, "AnalogExpressionLocalVariable_Short");
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public String getLongDescription(Locale locale) {
086        return Bundle.getMessage(locale, "AnalogExpressionLocalVariable_Long", _selectNamedBean.getDescription(locale));
087    }
088
089    /** {@inheritDoc} */
090    @Override
091    public void setup() {
092        // Do nothing
093    }
094
095    /** {@inheritDoc} */
096    @Override
097    public void registerListenersForThisClass() {
098        // Do nothing
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public void unregisterListenersForThisClass() {
104        // Do nothing
105    }
106
107    /** {@inheritDoc} */
108    @Override
109    public void propertyChange(PropertyChangeEvent evt) {
110        if (getTriggerOnChange()) {
111            getConditionalNG().execute();
112        }
113    }
114
115    /** {@inheritDoc} */
116    @Override
117    public void disposeMe() {
118    }
119
120    /** {@inheritDoc} */
121    @Override
122    public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) {
123        log.debug("getUsageReport :: AnalogExpressionLocalVariable: bean = {}, report = {}", cdl, report);
124    }
125
126    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AnalogExpressionLocalVariable.class);
127
128}