Search in sources :

Example 11 with Section

use of jmri.Section in project JMRI by JMRI.

the class DefaultSignalMastLogicManager method generateSection.

/**
     * Populate Sections of type SIGNALMASTLOGIC used with Layout Editor with Signal Mast attributes
     * as stored in Signal Mast Logic.
     */
public void generateSection() {
    SectionManager sm = InstanceManager.getDefault(jmri.SectionManager.class);
    for (NamedBean nb : sm.getNamedBeanList()) {
        if (((Section) nb).getSectionType() == Section.SIGNALMASTLOGIC) {
            nb.removeProperty("intermediateSection");
        }
        nb.removeProperty("forwardMast");
    }
    for (SignalMastLogic sml : getSignalMastLogicList()) {
        jmri.jmrit.display.layoutEditor.LayoutBlock faceLBlock = sml.getFacingBlock();
        if (faceLBlock != null) {
            boolean sourceIntermediate = false;
            if (sml.getSourceMast().getProperty("intermediateSignal") != null) {
                sourceIntermediate = ((Boolean) sml.getSourceMast().getProperty("intermediateSignal")).booleanValue();
            }
            for (SignalMast destMast : sml.getDestinationList()) {
                if (sml.getAutoBlocksBetweenMasts(destMast).size() != 0) {
                    Section sec = sm.createNewSection(sml.getSourceMast().getDisplayName() + ":" + destMast.getDisplayName());
                    if (sec == null) {
                        //A Section already exists, lets grab it and check that it is one used with the SML, if so carry on using that.
                        sec = sm.getSection(sml.getSourceMast().getDisplayName() + ":" + destMast.getDisplayName());
                        if (sec.getSectionType() != Section.SIGNALMASTLOGIC) {
                            break;
                        }
                    } else {
                        sec.setSectionType(Section.SIGNALMASTLOGIC);
                        try {
                            //Auto running requires forward/reverse sensors, but at this stage SML does not support that, so just create dummy internal ones for now.
                            Sensor sen = InstanceManager.sensorManagerInstance().provideSensor("IS:" + sec.getSystemName() + ":forward");
                            sen.setUserName(sec.getSystemName() + ":forward");
                            sen = InstanceManager.sensorManagerInstance().provideSensor("IS:" + sec.getSystemName() + ":reverse");
                            sen.setUserName(sec.getSystemName() + ":reverse");
                            sec.setForwardBlockingSensorName(sec.getSystemName() + ":forward");
                            sec.setReverseBlockingSensorName(sec.getSystemName() + ":reverse");
                        } catch (IllegalArgumentException ex) {
                            log.warn("Failed to provide Sensor in generateSection");
                        }
                    }
                    sml.setAssociatedSection(sec, destMast);
                    sec.setProperty("forwardMast", destMast.getDisplayName());
                    boolean destIntermediate = false;
                    if (destMast.getProperty("intermediateSignal") != null) {
                        destIntermediate = ((Boolean) destMast.getProperty("intermediateSignal")).booleanValue();
                    }
                    if (sourceIntermediate || destIntermediate) {
                        sec.setProperty("intermediateSection", true);
                    } else {
                        sec.setProperty("intermediateSection", false);
                    }
                //Not 100% sure about this for now so will comment out
                //sml.addSensor(sec.getSystemName()+":forward", Sensor.INACTIVE, destMast);
                }
            }
        } else {
            log.info("No facing block found " + sml.getSourceMast().getDisplayName());
        }
    }
}
Also used : SectionManager(jmri.SectionManager) NamedBean(jmri.NamedBean) SignalMastLogic(jmri.SignalMastLogic) DefaultSignalMastLogic(jmri.implementation.DefaultSignalMastLogic) SignalMast(jmri.SignalMast) Section(jmri.Section) Sensor(jmri.Sensor)

Example 12 with Section

use of jmri.Section in project JMRI by JMRI.

the class DefaultSignalMastLogicManagerXml method loadSignalMastLogic.

public boolean loadSignalMastLogic(Element signalMastLogic) {
    List<Element> logicList = signalMastLogic.getChildren("signalmastlogic");
    if (log.isDebugEnabled()) {
        log.debug("Found " + logicList.size() + " signal mast logics");
    }
    SignalMastManager sm = InstanceManager.getDefault(jmri.SignalMastManager.class);
    SignalMastLogicManager sml = InstanceManager.getDefault(jmri.SignalMastLogicManager.class);
    try {
        String logicDelay = signalMastLogic.getChild("logicDelay").getText();
        sml.setSignalLogicDelay(Long.parseLong(logicDelay));
    } catch (java.lang.NullPointerException e) {
    //Considered normal if it doesn't exists
    }
    boolean loadOk = true;
    for (Element so : logicList) {
        String source = so.getChild("sourceSignalMast").getText();
        SignalMast sourceMast = sm.getSignalMast(source);
        if (sourceMast != null) {
            SignalMastLogic logic = sml.newSignalMastLogic(sourceMast);
            List<Element> destList = so.getChildren("destinationMast");
            for (Element s : destList) {
                String destination = s.getChild("destinationSignalMast").getText();
                SignalMast dest = sm.getSignalMast(destination);
                if (dest != null) {
                    logic.setDestinationMast(dest);
                    if (s.getChild("comment") != null) {
                        logic.setComment(s.getChild("comment").getText(), dest);
                    }
                    if (s.getChild("enabled") != null) {
                        if (s.getChild("enabled").getText().equals("yes")) {
                            logic.setEnabled(dest);
                        } else {
                            logic.setDisabled(dest);
                        }
                    }
                    if (s.getChild("allowAutoMaticSignalMastGeneration") != null) {
                        if (s.getChild("allowAutoMaticSignalMastGeneration").getText().equals("no")) {
                            logic.allowAutoMaticSignalMastGeneration(false, dest);
                        } else {
                            logic.allowAutoMaticSignalMastGeneration(true, dest);
                        }
                    }
                    boolean useLayoutEditorTurnout = true;
                    boolean useLayoutEditorBlock = true;
                    if (s.getChild("useLayoutEditorTurnouts") != null) {
                        if (s.getChild("useLayoutEditorTurnouts").getText().equals("no")) {
                            useLayoutEditorTurnout = false;
                        }
                    }
                    if (s.getChild("useLayoutEditorBlocks") != null) {
                        if (s.getChild("useLayoutEditorBlocks").getText().equals("no")) {
                            useLayoutEditorBlock = false;
                        }
                    }
                    try {
                        logic.useLayoutEditorDetails(useLayoutEditorTurnout, useLayoutEditorBlock, dest);
                    } catch (jmri.JmriException ex) {
                    }
                    if (s.getChild("useLayoutEditor") != null) {
                        try {
                            if (s.getChild("useLayoutEditor").getText().equals("yes")) {
                                logic.useLayoutEditor(true, dest);
                            } else {
                                logic.useLayoutEditor(false, dest);
                            }
                        } catch (jmri.JmriException e) {
                        //Considered normal if layout editor hasn't yet been set up.
                        }
                    }
                    if (s.getChild("associatedSection") != null) {
                        Section sect = InstanceManager.getDefault(jmri.SectionManager.class).getSection(s.getChild("associatedSection").getText());
                        logic.setAssociatedSection(sect, dest);
                    }
                    Element turnoutElem = s.getChild("turnouts");
                    if (turnoutElem != null) {
                        List<Element> turnoutList = turnoutElem.getChildren("turnout");
                        if (turnoutList.size() > 0) {
                            Hashtable<NamedBeanHandle<Turnout>, Integer> list = new Hashtable<NamedBeanHandle<Turnout>, Integer>();
                            for (Element t : turnoutList) {
                                String turnout = t.getChild("turnoutName").getText();
                                String state = t.getChild("turnoutState").getText();
                                int value = Turnout.CLOSED;
                                if (state.equals("thrown")) {
                                    value = Turnout.THROWN;
                                }
                                Turnout turn = InstanceManager.turnoutManagerInstance().getTurnout(turnout);
                                if (turn != null) {
                                    NamedBeanHandle<Turnout> namedTurnout = nbhm.getNamedBeanHandle(turnout, turn);
                                    list.put(namedTurnout, value);
                                }
                                log.debug("Unable to add Turnout {} as it does not exist in the panel file", turnout);
                            }
                            logic.setTurnouts(list, dest);
                        }
                    }
                    Element sensorElem = s.getChild("sensors");
                    if (sensorElem != null) {
                        List<Element> sensorList = sensorElem.getChildren("sensor");
                        if (sensorList.size() > 0) {
                            Hashtable<NamedBeanHandle<Sensor>, Integer> list = new Hashtable<NamedBeanHandle<Sensor>, Integer>();
                            for (Element sl : sensorList) {
                                String sensorName = sl.getChild("sensorName").getText();
                                String state = sl.getChild("sensorState").getText();
                                int value = Sensor.INACTIVE;
                                if (state.equals("active")) {
                                    value = Sensor.ACTIVE;
                                }
                                Sensor sen = InstanceManager.sensorManagerInstance().getSensor(sensorName);
                                if (sen != null) {
                                    NamedBeanHandle<Sensor> namedSensor = nbhm.getNamedBeanHandle(sensorName, sen);
                                    list.put(namedSensor, value);
                                }
                                log.debug("Unable to add sensor {} as it does not exist in the panel file", sensorName);
                            }
                            logic.setSensors(list, dest);
                        }
                    }
                    Element blockElem = s.getChild("blocks");
                    if (blockElem != null) {
                        List<Element> blockList = blockElem.getChildren("block");
                        if (blockList.size() > 0) {
                            Hashtable<Block, Integer> list = new Hashtable<Block, Integer>();
                            for (Element b : blockList) {
                                String block = b.getChild("blockName").getText();
                                String state = b.getChild("blockState").getText();
                                int value = 0x03;
                                if (state.equals("occupied")) {
                                    value = Block.OCCUPIED;
                                } else if (state.equals("unoccupied")) {
                                    value = Block.UNOCCUPIED;
                                }
                                Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getBlock(block);
                                if (blk != null) {
                                    list.put(blk, value);
                                }
                                log.debug("Unable to add Block {} as it does not exist in the panel file", block);
                            }
                            logic.setBlocks(list, dest);
                        }
                    }
                    Element mastElem = s.getChild("masts");
                    if (mastElem != null) {
                        List<Element> mastList = mastElem.getChildren("mast");
                        if (mastList.size() > 0) {
                            Hashtable<SignalMast, String> list = new Hashtable<SignalMast, String>();
                            for (Element m : mastList) {
                                String mast = m.getChild("mastName").getText();
                                String state = m.getChild("mastState").getText();
                                SignalMast mst = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(mast);
                                if (mst != null) {
                                    list.put(mst, state);
                                }
                                log.debug("Unable to add Signal Mast {} as it does not exist in the panel file", mast);
                            }
                            logic.setMasts(list, dest);
                        }
                    }
                } else {
                    log.error("Destination Mast " + destination + " Not found, logic not loaded");
                    loadOk = false;
                }
            }
        } else {
            log.error("Source Mast " + source + " Not found, logic not loaded");
            loadOk = false;
        }
    }
    sml.initialise();
    return loadOk;
}
Also used : Element(org.jdom2.Element) SignalMastLogicManager(jmri.SignalMastLogicManager) SignalMast(jmri.SignalMast) NamedBeanHandle(jmri.NamedBeanHandle) Hashtable(java.util.Hashtable) SignalMastManager(jmri.SignalMastManager) Section(jmri.Section) SignalMastLogic(jmri.SignalMastLogic) Block(jmri.Block) Turnout(jmri.Turnout) Sensor(jmri.Sensor)

Example 13 with Section

use of jmri.Section in project JMRI by JMRI.

the class SectionTableAction method createModel.

/**
     * Create the JTable DataModel, along with the changes for the specific case
     * of Section objects
     */
@Override
protected void createModel() {
    m = new BeanTableDataModel() {

        public static final int BEGINBLOCKCOL = NUMCOLUMN;

        public static final int ENDBLOCKCOL = BEGINBLOCKCOL + 1;

        public static final int EDITCOL = ENDBLOCKCOL + 1;

        @Override
        public String getValue(String name) {
            return "";
        }

        @Override
        public Manager getManager() {
            return jmri.InstanceManager.getDefault(jmri.SectionManager.class);
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return jmri.InstanceManager.getDefault(jmri.SectionManager.class).getBySystemName(name);
        }

        @Override
        public NamedBean getByUserName(String name) {
            return jmri.InstanceManager.getDefault(jmri.SectionManager.class).getByUserName(name);
        }

        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        @Override
        public void clickOn(NamedBean t) {
        }

        @Override
        public int getColumnCount() {
            return EDITCOL + 1;
        }

        @Override
        public Object getValueAt(int row, int col) {
            // some error checking
            if (row >= sysNameList.size()) {
                log.debug("row is greater than name list");
                return "";
            }
            if (col == BEGINBLOCKCOL) {
                Section z = (Section) getBySystemName(sysNameList.get(row));
                if (z != null) {
                    return z.getBeginBlockName();
                }
                return "  ";
            } else if (col == ENDBLOCKCOL) {
                Section z = (Section) getBySystemName(sysNameList.get(row));
                if (z != null) {
                    return z.getEndBlockName();
                }
                return "  ";
            } else if (col == VALUECOL) {
                Section z = (Section) getBySystemName(sysNameList.get(row));
                if (z == null) {
                    return "";
                } else {
                    int state = z.getState();
                    if (state == Section.FREE) {
                        return (rbx.getString("SectionFree"));
                    } else if (state == Section.FORWARD) {
                        return (rbx.getString("SectionForward"));
                    } else if (state == Section.REVERSE) {
                        return (rbx.getString("SectionReverse"));
                    }
                }
            } else if (col == EDITCOL) {
                return Bundle.getMessage("ButtonEdit");
            } else {
                return super.getValueAt(row, col);
            }
            return null;
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            if ((col == BEGINBLOCKCOL) || (col == ENDBLOCKCOL)) {
                return;
            } else if (col == EDITCOL) {
                class WindowMaker implements Runnable {

                    int row;

                    WindowMaker(int r) {
                        row = r;
                    }

                    @Override
                    public void run() {
                        String sName = (String) getValueAt(row, SYSNAMECOL);
                        editPressed(sName);
                    }
                }
                WindowMaker t = new WindowMaker(row);
                javax.swing.SwingUtilities.invokeLater(t);
            } else if (col == DELETECOL) {
                deleteSectionPressed(sysNameList.get(row));
            } else {
                super.setValueAt(value, row, col);
            }
        }

        @Override
        public String getColumnName(int col) {
            if (col == BEGINBLOCKCOL) {
                return (rbx.getString("SectionFirstBlock"));
            }
            if (col == ENDBLOCKCOL) {
                return (rbx.getString("SectionLastBlock"));
            }
            if (col == EDITCOL) {
                // no namne on Edit column
                return "";
            }
            return super.getColumnName(col);
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == VALUECOL) {
                // not a button
                return String.class;
            }
            if (col == BEGINBLOCKCOL) {
                // not a button
                return String.class;
            }
            if (col == ENDBLOCKCOL) {
                // not a button
                return String.class;
            }
            if (col == EDITCOL) {
                return JButton.class;
            } else {
                return super.getColumnClass(col);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == BEGINBLOCKCOL) {
                return false;
            }
            if (col == ENDBLOCKCOL) {
                return false;
            }
            if (col == VALUECOL) {
                return false;
            }
            if (col == EDITCOL) {
                return true;
            } else {
                return super.isCellEditable(row, col);
            }
        }

        @Override
        public int getPreferredWidth(int col) {
            // override default value for SystemName and UserName columns
            if (col == SYSNAMECOL) {
                return new JTextField(9).getPreferredSize().width;
            }
            if (col == USERNAMECOL) {
                return new JTextField(17).getPreferredSize().width;
            }
            if (col == VALUECOL) {
                return new JTextField(6).getPreferredSize().width;
            }
            // new columns
            if (col == BEGINBLOCKCOL) {
                return new JTextField(15).getPreferredSize().width;
            }
            if (col == ENDBLOCKCOL) {
                return new JTextField(15).getPreferredSize().width;
            }
            if (col == EDITCOL) {
                return new JTextField(6).getPreferredSize().width;
            } else {
                return super.getPreferredWidth(col);
            }
        }

        @Override
        public void configValueColumn(JTable table) {
        // value column isn't button, so config is null
        }

        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            return true;
        // return (e.getPropertyName().indexOf("alue")>=0);
        }

        @Override
        protected String getBeanType() {
            return "Section";
        }
    };
}
Also used : NamedBean(jmri.NamedBean) SectionManager(jmri.SectionManager) BlockManager(jmri.BlockManager) Manager(jmri.Manager) JTextField(javax.swing.JTextField) Section(jmri.Section) EntryPoint(jmri.EntryPoint) SectionManager(jmri.SectionManager) JTable(javax.swing.JTable)

Example 14 with Section

use of jmri.Section in project JMRI by JMRI.

the class AutoAllocate method checkForXingPlan.

private boolean checkForXingPlan(AllocationRequest ar, ActiveTrain nt, ArrayList<ActiveTrain> neededByTrainList) {
    // returns 'true' if an AllocationPlan has been set up, returns 'false' otherwise
    Section nSec = null;
    Section aSec = null;
    int nSecSeq = 0;
    int aSecSeq = 0;
    ActiveTrain at = ar.getActiveTrain();
    AllocationPlan apx = getPlanThisTrain(nt);
    if (apx != null) {
        if (apx.getPlanType() != AllocationPlan.XING_MEET) {
            return false;
        }
        // already in a XING_MEET Allocation Plan - find target Section and sequence
        if (apx.getActiveTrain(1) == nt) {
            nSecSeq = apx.getTargetSectionSequenceNum(1);
            nSec = apx.getTargetSection(1);
        } else {
            nSecSeq = apx.getTargetSectionSequenceNum(2);
            nSec = apx.getTargetSection(2);
        }
        ArrayList<Section> nSections = nt.getTransit().getSectionListBySeq(nSecSeq);
        if (nSections.size() <= 1) {
            return false;
        }
        // is a passing siding, find a suitable track
        aSec = getBestOtherSection(nSections, nSec);
        if (aSec == null) {
            return false;
        }
        aSecSeq = willTraverse(aSec, at, getCurrentSequenceNumber(at));
        if (aSecSeq == 0) {
            return false;
        }
    } else {
        // neither train is in an AllocationPlan currently, check for suitable passing siding
        int aSeq = ar.getSectionSeqNumber();
        // is an alternate Section available here or ahead
        aSecSeq = findPassingSection(at, aSeq);
        if (aSecSeq == 0) {
            // none in at's Transit, is there one in nt's Transit
            int nCurrentSeq = getCurrentSequenceNumber(nt);
            nSecSeq = findPassingSection(nt, nCurrentSeq);
            if (nSecSeq > 0) {
                // has passing section ahead, will this train traverse a Section in it
                ArrayList<Section> nSections = nt.getTransit().getSectionListBySeq(nSecSeq);
                for (int i = 0; (i < nSections.size()) && (aSec == null); i++) {
                    aSecSeq = willTraverse(nSections.get(i), at, aSeq);
                    if (aSecSeq > 0) {
                        aSec = at.getTransit().getSectionListBySeq(aSecSeq).get(0);
                    }
                }
                if (aSec != null) {
                    // found passing Section that should work out
                    nSec = getBestOtherSection(nSections, aSec);
                }
            }
        } else {
            // will other train go through any of these alternate sections
            ArrayList<Section> aSections = at.getTransit().getSectionListBySeq(aSecSeq);
            int nCurrentSeq = getCurrentSequenceNumber(nt);
            for (int i = 0; (i < aSections.size()) && (aSec == null); i++) {
                nSecSeq = willTraverse(aSections.get(i), nt, nCurrentSeq);
                if (nSecSeq > 0) {
                    nSec = aSections.get(i);
                    aSec = getBestOtherSection(aSections, nSec);
                }
            }
        }
        // if could not find a suitable siding for a crossing meet, return 
        if ((aSec == null) || (nSec == null)) {
            return false;
        }
    }
    // check for conflicting train or conflicting plan that could cause gridlock
    if (neededByTrainList.size() > 2) {
        // is there another train between these two
        if (!areTrainsAdjacent(at, nt)) {
            return false;
        }
        if (isThereConflictingPlan(at, aSec, aSecSeq, nt, nSec, nSecSeq, AllocationPlan.XING_MEET)) {
            return false;
        }
    }
    // set up allocation plan
    AllocationPlan ap = new AllocationPlan(this, nextPlanNum);
    nextPlanNum++;
    ap.setPlanType(AllocationPlan.XING_MEET);
    ap.setActiveTrain(at, 1);
    ap.setTargetSection(aSec, aSecSeq, 1);
    ap.setActiveTrain(nt, 2);
    ap.setTargetSection(nSec, nSecSeq, 2);
    _planList.add(ap);
    return true;
}
Also used : Section(jmri.Section) TransitSection(jmri.TransitSection)

Example 15 with Section

use of jmri.Section in project JMRI by JMRI.

the class AutoAllocate method isSignalHeldAtStartOfSection.

private boolean isSignalHeldAtStartOfSection(AllocationRequest ar) {
    if (ar == null) {
        return false;
    }
    Section sec = ar.getSection();
    ActiveTrain mActiveTrain = ar.getActiveTrain();
    if (sec == null || mActiveTrain == null) {
        return false;
    }
    Section lastSec = mActiveTrain.getLastAllocatedSection();
    if (lastSec == null) {
        return false;
    }
    if (!sec.equals(mActiveTrain.getNextSectionToAllocate())) {
        log.error("Allocation request section does not match active train next section to allocate");
        log.error("Section to allocate " + sec.getDisplayName());
        if (mActiveTrain.getNextSectionToAllocate() != null) {
            log.error("Active Train expected " + mActiveTrain.getNextSectionToAllocate().getDisplayName());
        }
        return false;
    }
    Block facingBlock;
    Block protectingBlock;
    if (ar.getSectionDirection() == jmri.Section.FORWARD) {
        protectingBlock = sec.getBlockBySequenceNumber(0);
        facingBlock = lastSec.getBlockBySequenceNumber(lastSec.getNumBlocks() - 1);
    } else {
        //Reverse
        protectingBlock = sec.getBlockBySequenceNumber(sec.getNumBlocks() - 1);
        facingBlock = lastSec.getBlockBySequenceNumber(0);
    }
    if (protectingBlock == null || facingBlock == null) {
        return false;
    }
    jmri.SignalMast sm = jmri.InstanceManager.getDefault(jmri.jmrit.display.layoutEditor.LayoutBlockManager.class).getFacingSignalMast(facingBlock, protectingBlock);
    if (sm != null && sm.getHeld() && !_dispatcher.isMastHeldByDispatcher(sm, mActiveTrain)) {
        ar.setWaitingForSignalMast(sm);
        return true;
    }
    return false;
}
Also used : Block(jmri.Block) Section(jmri.Section) TransitSection(jmri.TransitSection)

Aggregations

Section (jmri.Section)33 TransitSection (jmri.TransitSection)22 EntryPoint (jmri.EntryPoint)19 ArrayList (java.util.ArrayList)5 Block (jmri.Block)5 SectionManager (jmri.SectionManager)5 SignalMast (jmri.SignalMast)4 Transit (jmri.Transit)4 Element (org.jdom2.Element)4 NamedBean (jmri.NamedBean)3 Sensor (jmri.Sensor)3 SignalMastLogic (jmri.SignalMastLogic)2 TransitManager (jmri.TransitManager)2 FlowLayout (java.awt.FlowLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 Hashtable (java.util.Hashtable)1 BoxLayout (javax.swing.BoxLayout)1 JButton (javax.swing.JButton)1 JDialog (javax.swing.JDialog)1