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