001package jmri.jmrit.logixng.implementation;
002
003import java.util.*;
004
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.jmrit.logixng.*;
009
010/**
011 * Every DigitalActionBean has an DefaultMaleDigitalActionSocket as its parent.
012 *
013 * @author Daniel Bergqvist Copyright 2018
014 */
015public class DefaultMaleDigitalActionSocket
016        extends AbstractMaleSocket implements MaleDigitalActionSocket {
017
018//    private final DigitalActionBean ((DigitalActionBean)getObject());
019    private DebugConfig _debugConfig = null;
020    private boolean _enabled = true;
021
022
023    public DefaultMaleDigitalActionSocket(@Nonnull BaseManager<? extends NamedBean> manager, @Nonnull DigitalActionBean action) {
024        super(manager, action);
025    }
026
027    /** {@inheritDoc} */
028    @Override
029    public void execute() throws JmriException {
030        if (! _enabled) {
031            return;
032        }
033
034        if ((_debugConfig != null)
035                && ((DigitalActionDebugConfig)_debugConfig)._dontExecute) {
036            return;
037        }
038
039        ConditionalNG conditionalNG = getConditionalNG();
040
041        int currentStackPos = conditionalNG.getStack().getCount();
042
043        try {
044            conditionalNG.getSymbolTable().createSymbols(_localVariables);
045            ((DigitalActionBean)getObject()).execute();
046        } catch (PassThruException e) {
047            // Pass thru this exception
048            throw e;
049        } catch (JmriException e) {
050            if (e.getErrors() != null) {
051                handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
052            } else {
053                handleError(this, Bundle.getMessage("ExceptionExecuteAction", e.getLocalizedMessage()), e, log);
054            }
055        } catch (RuntimeException e) {
056            handleError(this, Bundle.getMessage("ExceptionExecuteAction", e.getLocalizedMessage()), e, log);
057        }
058
059        conditionalNG.getStack().setCount(currentStackPos);
060        conditionalNG.getSymbolTable().removeSymbols(_localVariables);
061    }
062
063    @Override
064    public void disposeMe() {
065        ((DigitalActionBean)getObject()).dispose();
066    }
067
068    /**
069     * Register listeners if this object needs that.
070     */
071    @Override
072    public void registerListenersForThisClass() {
073        ((DigitalActionBean)getObject()).registerListeners();
074    }
075
076    /**
077     * Register listeners if this object needs that.
078     */
079    @Override
080    public void unregisterListenersForThisClass() {
081        ((DigitalActionBean)getObject()).unregisterListeners();
082    }
083
084    @Override
085    public void setState(int s) throws JmriException {
086        ((DigitalActionBean)getObject()).setState(s);
087    }
088
089    @Override
090    public int getState() {
091        return ((DigitalActionBean)getObject()).getState();
092    }
093
094    @Override
095    public String describeState(int state) {
096        return ((DigitalActionBean)getObject()).describeState(state);
097    }
098
099    @Override
100    public String getComment() {
101        return ((DigitalActionBean)getObject()).getComment();
102    }
103
104    @Override
105    public void setComment(String comment) {
106        ((DigitalActionBean)getObject()).setComment(comment);
107    }
108
109    @Override
110    public void setProperty(String key, Object value) {
111        ((DigitalActionBean)getObject()).setProperty(key, value);
112    }
113
114    @Override
115    public Object getProperty(String key) {
116        return ((DigitalActionBean)getObject()).getProperty(key);
117    }
118
119    @Override
120    public void removeProperty(String key) {
121        ((DigitalActionBean)getObject()).removeProperty(key);
122    }
123
124    @Override
125    public Set<String> getPropertyKeys() {
126        return ((DigitalActionBean)getObject()).getPropertyKeys();
127    }
128
129    @Override
130    public String getBeanType() {
131        return ((DigitalActionBean)getObject()).getBeanType();
132    }
133
134    @Override
135    public int compareSystemNameSuffix(String suffix1, String suffix2, NamedBean n2) {
136        return ((DigitalActionBean)getObject()).compareSystemNameSuffix(suffix1, suffix2, n2);
137    }
138
139    /** {@inheritDoc} */
140    @Override
141    public void setDebugConfig(DebugConfig config) {
142        _debugConfig = config;
143    }
144
145    /** {@inheritDoc} */
146    @Override
147    public DebugConfig getDebugConfig() {
148        return _debugConfig;
149    }
150
151    /** {@inheritDoc} */
152    @Override
153    public DebugConfig createDebugConfig() {
154        return new DigitalActionDebugConfig();
155    }
156
157    /** {@inheritDoc} */
158    @Override
159    public void setEnabled(boolean enable) {
160        _enabled = enable;
161        if (isActive()) {
162            registerListeners();
163        } else {
164            unregisterListeners();
165        }
166    }
167
168    /** {@inheritDoc} */
169    @Override
170    public void setEnabledFlag(boolean enable) {
171        _enabled = enable;
172    }
173
174    /** {@inheritDoc} */
175    @Override
176    public boolean isEnabled() {
177        return _enabled;
178    }
179
180
181    public static class DigitalActionDebugConfig implements MaleSocket.DebugConfig {
182
183        // If true, the socket is not executing the action.
184        // It's useful if you want to test the LogixNG without affecting the
185        // layout (turnouts, sensors, and so on).
186        public boolean _dontExecute = false;
187
188        @Override
189        public DebugConfig getCopy() {
190            DigitalActionDebugConfig config = new DigitalActionDebugConfig();
191            config._dontExecute = _dontExecute;
192            return config;
193        }
194
195    }
196
197
198    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultMaleDigitalActionSocket.class);
199
200}