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