Search in sources :

Example 1 with WarrantManager

use of jmri.jmrit.logix.WarrantManager in project JMRI by JMRI.

the class WarrantManagerXml method load.

@Override
public boolean load(Element shared, Element perNode) {
    WarrantManager manager = InstanceManager.getDefault(WarrantManager.class);
    if (shared.getChildren().isEmpty()) {
        return true;
    }
    List<Element> warrantList = shared.getChildren("warrant");
    if (log.isDebugEnabled())
        log.debug("Found {} Warrant objects", warrantList.size());
    for (int i = 0; i < warrantList.size(); i++) {
        Element elem = warrantList.get(i);
        if (elem.getAttribute("systemName") == null) {
            log.warn("unexpected null for systemName in elem {}", elem);
            break;
        }
        String sysName = null;
        if (elem.getAttribute("systemName") != null)
            sysName = elem.getAttribute("systemName").getValue();
        String userName = null;
        if (elem.getAttribute("userName") != null)
            userName = elem.getAttribute("userName").getValue();
        boolean SCWa = true;
        log.debug("loading warrant {}", sysName);
        Attribute wType = elem.getAttribute("wtype");
        if (wType == null) {
            log.debug("wtype is null for {}", sysName);
            SCWa = false;
        } else if (!wType.getValue().equals("SC")) {
            log.debug("wtype is {} for {}", wType.getValue(), sysName);
            SCWa = false;
        }
        long timeToPlatform = 500;
        Attribute TTP = elem.getAttribute("timeToPlatform");
        if (TTP != null) {
            try {
                timeToPlatform = TTP.getLongValue();
            } catch (DataConversionException e) {
                log.debug("ignoring DataConversionException (and reverting to default value): " + e.toString());
            }
        }
        Warrant warrant = manager.createNewWarrant(sysName, userName, SCWa, timeToPlatform);
        if (warrant == null) {
            log.info("Warrant \"{}\" (userName={}) previously loaded. This version not loaded.", sysName, userName);
            continue;
        }
        if (SCWa) {
            if (elem.getAttribute("forward") != null) {
                ((SCWarrant) warrant).setForward(elem.getAttribute("forward").getValue().equals("true"));
            }
            warrant.setNoRamp(SCWa);
            warrant.setShareRoute(SCWa);
        }
        List<Element> orders = elem.getChildren("blockOrder");
        for (int k = 0; k < orders.size(); k++) {
            BlockOrder bo = loadBlockOrder(orders.get(k));
            if (bo == null) {
                break;
            }
            warrant.addBlockOrder(bo);
        }
        String c = elem.getChildText("comment");
        if (c != null) {
            warrant.setComment(c);
        }
        Element order = elem.getChild("viaOrder");
        if (order != null) {
            warrant.setViaOrder(loadBlockOrder(order));
        }
        order = elem.getChild("avoidOrder");
        if (order != null) {
            warrant.setAvoidOrder(loadBlockOrder(order));
        }
        boolean forward = true;
        List<Element> throttleCmds = elem.getChildren("throttleCommand");
        if (throttleCmds != null) {
            for (int k = 0; k < throttleCmds.size(); k++) {
                ThrottleSetting ts = loadThrottleCommand(throttleCmds.get(k));
                warrant.addThrottleCommand(ts);
                if (ts.getCommand().toUpperCase().equals("FORWARD")) {
                    forward = ts.getValue().toUpperCase().equals("TRUE");
                }
            }
        }
        if (SCWa) {
            if (elem.getAttribute("forward") != null) {
                forward = elem.getAttribute("forward").getValue().equals("true");
            }
            ((SCWarrant) warrant).setForward(forward);
            warrant.setNoRamp(SCWa);
            warrant.setShareRoute(SCWa);
        }
        Element train = elem.getChild("train");
        if (train != null) {
            loadTrain(train, warrant);
        }
    }
    return true;
}
Also used : SCWarrant(jmri.jmrit.logix.SCWarrant) Warrant(jmri.jmrit.logix.Warrant) Attribute(org.jdom2.Attribute) Element(org.jdom2.Element) ThrottleSetting(jmri.jmrit.logix.ThrottleSetting) WarrantManager(jmri.jmrit.logix.WarrantManager) SCWarrant(jmri.jmrit.logix.SCWarrant) DataConversionException(org.jdom2.DataConversionException) BlockOrder(jmri.jmrit.logix.BlockOrder)

Example 2 with WarrantManager

use of jmri.jmrit.logix.WarrantManager in project JMRI by JMRI.

the class WarrantManagerXml method store.

/**
     * Store the contents of a WarrantManager.
     *
     * @param o Object to store, of type warrantManager
     * @return Element containing the complete info
     */
@Override
public Element store(Object o) {
    Element warrants = new Element("warrants");
    warrants.setAttribute("class", "jmri.jmrit.logix.configurexml.WarrantManagerXml");
    WarrantManager manager = (WarrantManager) o;
    Iterator<String> iter = manager.getSystemNameList().iterator();
    while (iter.hasNext()) {
        String sname = iter.next();
        Warrant warrant = manager.getBySystemName(sname);
        String uname = warrant.getUserName();
        if (log.isDebugEnabled())
            log.debug("Warrant: sysName= {}, userName= {}", sname, uname);
        Element elem = new Element("warrant");
        elem.setAttribute("systemName", sname);
        if (uname == null)
            uname = "";
        if (uname.length() > 0) {
            elem.setAttribute("userName", uname);
        }
        if (warrant instanceof SCWarrant) {
            elem.setAttribute("wtype", "SC");
            elem.setAttribute("timeToPlatform", "" + ((SCWarrant) warrant).getTimeToPlatform());
            elem.setAttribute("forward", ((SCWarrant) warrant).getForward() ? "true" : "false");
        } else {
            elem.setAttribute("wtype", "normal");
        }
        String comment = warrant.getComment();
        if (comment != null) {
            Element c = new Element("comment");
            c.addContent(comment);
            elem.addContent(c);
        }
        List<BlockOrder> orders = warrant.getSavedOrders();
        for (int j = 0; j < orders.size(); j++) {
            elem.addContent(storeOrder(orders.get(j), "blockOrder"));
        }
        BlockOrder viaOrder = warrant.getViaOrder();
        if (viaOrder != null) {
            elem.addContent(storeOrder(viaOrder, "viaOrder"));
        }
        BlockOrder avoidOrder = warrant.getAvoidOrder();
        if (avoidOrder != null) {
            elem.addContent(storeOrder(avoidOrder, "avoidOrder"));
        }
        List<ThrottleSetting> throttleCmds = warrant.getThrottleCommands();
        for (int j = 0; j < throttleCmds.size(); j++) {
            elem.addContent(storeCommand(throttleCmds.get(j), "throttleCommand"));
        }
        elem.addContent(storeTrain(warrant, "train"));
        // and put this element out
        warrants.addContent(elem);
    }
    return warrants;
}
Also used : SCWarrant(jmri.jmrit.logix.SCWarrant) Warrant(jmri.jmrit.logix.Warrant) SCWarrant(jmri.jmrit.logix.SCWarrant) Element(org.jdom2.Element) ThrottleSetting(jmri.jmrit.logix.ThrottleSetting) WarrantManager(jmri.jmrit.logix.WarrantManager) BlockOrder(jmri.jmrit.logix.BlockOrder)

Example 3 with WarrantManager

use of jmri.jmrit.logix.WarrantManager in project JMRI by JMRI.

the class InstanceManagerTest method testWarrantManager.

public void testWarrantManager() {
    WarrantManager obj = InstanceManager.getDefault(WarrantManager.class);
    Assert.assertNotNull(obj);
    Assert.assertEquals(obj, InstanceManager.getDefault(WarrantManager.class));
    Assert.assertEquals(obj, InstanceManager.getDefault(WarrantManager.class));
    Assert.assertEquals(obj, InstanceManager.getDefault(WarrantManager.class));
}
Also used : WarrantManager(jmri.jmrit.logix.WarrantManager)

Aggregations

WarrantManager (jmri.jmrit.logix.WarrantManager)3 BlockOrder (jmri.jmrit.logix.BlockOrder)2 SCWarrant (jmri.jmrit.logix.SCWarrant)2 ThrottleSetting (jmri.jmrit.logix.ThrottleSetting)2 Warrant (jmri.jmrit.logix.Warrant)2 Element (org.jdom2.Element)2 Attribute (org.jdom2.Attribute)1 DataConversionException (org.jdom2.DataConversionException)1