Search in sources :

Example 1 with Switch

use of trackmodel.model.Switch in project on-track by michaelplazek.

the class TrackModelController method importTrackData.

private void importTrackData(File f) {
    try {
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line = br.readLine();
        int i = 0;
        String filename = f.getName();
        int fileNamePeriodPosition = filename.indexOf('.');
        String lineName = filename.substring(0, fileNamePeriodPosition);
        lineName = lineName.toUpperCase();
        Track newTrack = new Track(lineName);
        listOfTracks.put(lineName, newTrack);
        ArrayList<String> sections = new ArrayList<String>();
        ArrayList<Integer> blocks = new ArrayList<Integer>();
        while (line != null) {
            if (i == 0) {
                // System.out.println("Header Line");
                // System.out.println(line);
                String[] splitLine = line.split(",");
                line = br.readLine();
                i++;
            } else {
                String[] splitLine = line.split(",");
                if (!sections.contains(splitLine[1])) {
                    sections.add(splitLine[1]);
                }
                if (!blocks.contains(Integer.parseInt(splitLine[2]))) {
                    blocks.add(Integer.parseInt(splitLine[2]));
                }
                Block b;
                if (splitLine[6].contains("SWITCH")) {
                    // Create a switch for the Track
                    final String lineId = splitLine[0];
                    final String section = splitLine[1];
                    final int number = Integer.parseInt(splitLine[2]);
                    final float len = Float.parseFloat(splitLine[3]);
                    final float grade = Float.parseFloat(splitLine[4]);
                    final int speedLimit = Integer.parseInt(splitLine[5]);
                    final String infra = splitLine[6];
                    final float elevation = Float.parseFloat(splitLine[7]);
                    final float cumEle = Float.parseFloat(splitLine[8]);
                    boolean biDirectional;
                    if (splitLine[9].equals("")) {
                        biDirectional = false;
                    } else {
                        biDirectional = true;
                    }
                    final int previous = Integer.parseInt(splitLine[10]);
                    final int next1 = Integer.parseInt(splitLine[11]);
                    final int next2 = Integer.parseInt(splitLine[12]);
                    boolean rightStation = false;
                    if (splitLine.length > 13) {
                        if (splitLine[13].equals("")) {
                            rightStation = false;
                        } else {
                            rightStation = true;
                        }
                    }
                    boolean leftStation = false;
                    if (splitLine.length > 14) {
                        if (splitLine[14].equals("")) {
                            leftStation = false;
                        } else {
                            leftStation = true;
                        }
                    }
                    b = new Switch(lineId, section, number, len, grade, speedLimit, infra, elevation, cumEle, biDirectional, previous, next1, next2, leftStation, rightStation);
                    if (splitLine[6].contains("YARD") && splitLine[6].contains("FROM")) {
                        newTrack.setStartBlock(number);
                    }
                    newTrack.addBlock(b);
                } else {
                    // Create a Block for the Track
                    // System.out.println(splitLine.length);
                    final String lineId = splitLine[0];
                    final String section = splitLine[1];
                    final int number = Integer.parseInt(splitLine[2]);
                    final float len = Float.parseFloat(splitLine[3]);
                    final float grade = Float.parseFloat(splitLine[4]);
                    final int speedLimit = Integer.parseInt(splitLine[5]);
                    final String infra = splitLine[6];
                    final float elevation = Float.parseFloat(splitLine[7]);
                    final float cumEle = Float.parseFloat(splitLine[8]);
                    boolean biDirectional;
                    if (splitLine[9].equals("")) {
                        biDirectional = false;
                    } else {
                        biDirectional = true;
                    }
                    final int previous = Integer.parseInt(splitLine[10]);
                    final int next1 = Integer.parseInt(splitLine[11]);
                    boolean rightStation = false;
                    if (splitLine.length > 13) {
                        if (splitLine[13].equals("")) {
                            rightStation = false;
                        } else {
                            rightStation = true;
                        }
                    }
                    boolean leftStation = false;
                    if (splitLine.length > 14) {
                        if (splitLine[14].equals("")) {
                            leftStation = false;
                        } else {
                            leftStation = true;
                        }
                    }
                    b = new Block(lineId, section, number, len, grade, speedLimit, infra, elevation, cumEle, biDirectional, previous, next1, leftStation, rightStation);
                    newTrack.addBlock(b);
                }
                // System.out.println();
                line = br.readLine();
            }
        }
        trackSections.put(lineName, sections);
        trackBlockNum.put(lineName, blocks);
        System.out.println(newTrack.getNumberOfBlocks());
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to find the file.");
    } catch (IOException ex) {
        System.out.println("Error reading file");
    }
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Switch(trackmodel.model.Switch) BufferedReader(java.io.BufferedReader) Block(trackmodel.model.Block) FileReader(java.io.FileReader) Track(trackmodel.model.Track)

Example 2 with Switch

use of trackmodel.model.Switch in project on-track by michaelplazek.

the class CentralTrafficControlController method updateMaintenance.

private void updateMaintenance() {
    String line = maintenanceTracks.getSelectionModel().getSelectedItem();
    if (line.equals("Select track")) {
        statusLight.setFill(Paint.valueOf("Grey"));
        occupiedLight.setFill(Paint.valueOf("Grey"));
        maintenanceBlocks.setDisable(true);
        maintenanceActions.setDisable(true);
        submitMaintenance.setDisable(true);
    } else {
        maintenanceBlocks.setDisable(false);
        maintenanceActions.setDisable(false);
        submitMaintenance.setDisable(false);
        int blockId = extractBlock();
        Block block = Track.getListOfTracks().get(line).getBlock(blockId);
        if (block.isClosedForMaintenance()) {
            statusLight.setFill(Paint.valueOf("Red"));
        } else {
            statusLight.setFill(Paint.valueOf("#24c51b"));
        }
        if (block.isOccupied()) {
            occupiedLight.setFill(Paint.valueOf("#24c51b"));
        } else {
            occupiedLight.setFill(Paint.valueOf("Red"));
        }
        if (block.isSwitch()) {
            Switch sw = (Switch) block;
            if (sw.getSwitchState()) {
                stateOne.setOpacity(100);
                stateTwo.setOpacity(0);
                stateZero.setOpacity(0);
            } else {
                stateOne.setOpacity(0);
                stateTwo.setOpacity(100);
                stateZero.setOpacity(0);
            }
        } else {
            stateOne.setOpacity(0);
            stateTwo.setOpacity(0);
            stateZero.setOpacity(100);
        }
    }
}
Also used : Switch(trackmodel.model.Switch) Block(trackmodel.model.Block) Paint(javafx.scene.paint.Paint)

Example 3 with Switch

use of trackmodel.model.Switch in project on-track by michaelplazek.

the class Runner method sampleTrackMaker.

// TODO: replace this awful function with a real one
private void sampleTrackMaker() {
    Track test = new Track("blue");
    for (int i = 0; i < 20; i++) {
        if (i == 10 || i == 15) {
            Switch block = new Switch();
            block.setNumber(i);
            block.setSection("V");
            block.setLine("blue");
            test.addBlock(block);
        } else {
            Block block = new Block();
            block.setNumber(i);
            block.setSection("V");
            block.setLine("blue");
            test.addBlock(block);
        }
    }
    test.getBlock(10).setSwitchHere(true);
    test.getBlock(15).setSwitchHere(true);
    test.getBlock(11).setLeftStation(true);
    test.getBlock(11).setStationName("SOME STATION");
    test.getBlock(16).setRightStation(true);
    test.getBlock(16).setStationName("ANOTHER STATION");
    Track test2 = new Track("yellow");
    for (int i = 0; i < 20; i++) {
        if (i == 10 || i == 15) {
            Switch block = new Switch();
            block.setNumber(i);
            block.setSection("A");
            block.setLine("yellow");
            test2.addBlock(block);
        } else {
            Block block = new Block();
            block.setNumber(i);
            block.setSection("A");
            block.setLine("yellow");
            test2.addBlock(block);
        }
    }
    test2.getBlock(10).setSwitchHere(true);
    test2.getBlock(15).setSwitchHere(true);
    test2.getBlock(11).setLeftStation(true);
    test2.getBlock(11).setStationName("yellow station");
    test2.getBlock(16).setRightStation(true);
    test2.getBlock(16).setStationName("Another yellow station");
}
Also used : Switch(trackmodel.model.Switch) Block(trackmodel.model.Block) Track(trackmodel.model.Track)

Aggregations

Block (trackmodel.model.Block)3 Switch (trackmodel.model.Switch)3 Track (trackmodel.model.Track)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Paint (javafx.scene.paint.Paint)1