001package jmri.jmrit.logixng.expressions.configurexml;
002
003import java.util.*;
004
005import jmri.*;
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.logixng.DigitalExpressionManager;
008import jmri.jmrit.logixng.MaleSocket;
009import jmri.jmrit.logixng.expressions.DigitalFormula;
010import jmri.jmrit.logixng.util.parser.ParserException;
011
012import org.jdom2.Element;
013
014/**
015 * Handle XML configuration for ActionLightXml objects.
016 *
017 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
018 * @author Daniel Bergqvist Copyright (C) 2019
019 */
020public class DigitalFormulaXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
021
022    public DigitalFormulaXml() {
023    }
024    
025    /**
026     * Default implementation for storing the contents of a SE8cSignalHead
027     *
028     * @param o Object to store, of type TripleLightSignalHead
029     * @return Element containing the complete info
030     */
031    @Override
032    public Element store(Object o) {
033        DigitalFormula p = (DigitalFormula) o;
034
035        Element element = new Element("DigitalFormula");
036        element.setAttribute("class", this.getClass().getName());
037        element.addContent(new Element("systemName").addContent(p.getSystemName()));
038
039        storeCommon(p, element);
040
041        Element e = new Element("Expressions");
042        for (int i=0; i < p.getChildCount(); i++) {
043            Element e2 = new Element("Socket");
044            e2.addContent(new Element("socketName").addContent(p.getChild(i).getName()));
045            MaleSocket socket = p.getChild(i).getConnectedSocket();
046            
047            String socketSystemName;
048            String socketManager;
049            if (socket != null) {
050                socketSystemName = socket.getSystemName();
051                socketManager = socket.getManager().getClass().getName();
052            } else {
053                socketSystemName = p.getExpressionSystemName(i);
054                socketManager = p.getExpressionManager(i);
055            }
056            if (socketSystemName != null) {
057                e2.addContent(new Element("systemName").addContent(socketSystemName));
058            }
059            if (socketManager != null) {
060                e2.addContent(new Element("manager").addContent(socketManager));
061            }
062            e.addContent(e2);
063        }
064        
065        element.addContent(e);
066        
067        element.addContent(new Element("formula").addContent(p.getFormula()));
068
069        return element;
070    }
071    
072    @Override
073    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
074        List<DigitalFormula.SocketData> expressionSystemNames = new ArrayList<>();
075        
076        Element actionElement = shared.getChild("Expressions");
077        for (Element socketElement : actionElement.getChildren()) {
078            String socketName = socketElement.getChild("socketName").getTextTrim();
079            Element systemNameElement = socketElement.getChild("systemName");
080            String systemName = null;
081            if (systemNameElement != null) {
082                systemName = systemNameElement.getTextTrim();
083            }
084            Element managerElement = socketElement.getChild("manager");
085            String manager = null;
086            if (managerElement != null) {
087                manager = managerElement.getTextTrim();
088            }
089            expressionSystemNames.add(new DigitalFormula.SocketData(socketName, systemName, manager));
090        }
091        
092        // put it together
093        String sys = getSystemName(shared);
094        String uname = getUserName(shared);
095        
096        DigitalFormula h = new DigitalFormula(sys, uname, expressionSystemNames);
097
098        loadCommon(h, shared);
099
100        Element formula = shared.getChild("formula");
101        if (formula != null) {
102            try {
103                h.setFormula(formula.getTextTrim());
104            } catch (ParserException e) {
105                log.error("cannot set formula: {}", formula.getTextTrim(), e);
106            }
107        }
108
109        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
110        return true;
111    }
112    
113    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DigitalFormulaXml.class);
114}