Search in sources :

Example 16 with Node

use of net.parostroj.timetable.model.Node in project grafikon by jub77.

the class FreightConnectionAnalyser method analyse.

/**
 * Returns freight connections between stations - if some exists.
 *
 * @param from start node
 * @param to end node
 * @return shortest freight connection
 */
public Set<NodeFreightConnection> analyse(Node from, Node to) {
    FreightConnectionAnalysis analysis = new FreightConnectionAnalysis(new FreightConnectionFinder(analyser), from, to);
    Context context = analysis.createContext();
    while (context != null) {
        switch(context.stage) {
            case START:
                analysis.init(context);
                break;
            case TO_NODE:
                analysis.toNode(context);
                break;
            case TO_CENTER:
                analysis.toCenter(context);
                break;
            case BETWEEN_CENTERS:
                analysis.betweenCenters(context);
                break;
            case CONNECTION:
            case NO_CONNECTION:
                // get next context
                context = analysis.getNextContext();
                break;
            default:
                throw new IllegalStateException("Unknown state");
        }
    }
    return analysis.getContexts().stream().<NodeFreightConnection>map(ctx -> new NodeFreightConnection() {

        private Integer weight;

        @Override
        public Node getFrom() {
            return from;
        }

        @Override
        public Node getTo() {
            return to;
        }

        @Override
        public List<DirectNodeConnection> getSteps() {
            return ctx.steps.stream().collect(toList());
        }

        @Override
        public boolean isComplete() {
            return ctx.stage == Stage.CONNECTION;
        }

        @Override
        public int getLength() {
            if (weight == null) {
                weight = isComplete() ? getLengthOfPath(ctx.steps) : Integer.MAX_VALUE;
            }
            return weight.intValue();
        }

        private int getLengthOfPath(List<StepImpl> steps) {
            return steps.stream().mapToInt(StepImpl::getWeight).sum();
        }
    }).collect(toSet());
}
Also used : Context(net.parostroj.timetable.model.freight.FreightConnectionAnalysis.Context) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Set(java.util.Set) Stage(net.parostroj.timetable.model.freight.FreightConnectionAnalysis.Stage) StepImpl(net.parostroj.timetable.model.freight.FreightConnectionAnalysis.StepImpl) Node(net.parostroj.timetable.model.Node) Context(net.parostroj.timetable.model.freight.FreightConnectionAnalysis.Context) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Comparator(java.util.Comparator) Collectors.toSet(java.util.stream.Collectors.toSet) TimeUtil(net.parostroj.timetable.utils.TimeUtil) Train(net.parostroj.timetable.model.Train) Collections.emptyList(java.util.Collections.emptyList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List)

Example 17 with Node

use of net.parostroj.timetable.model.Node in project grafikon by jub77.

the class RegionGraphDelegate method getRegionGraph.

Graph<Node, DirectNodeConnection> getRegionGraph() {
    SimpleDirectedWeightedGraph<Node, DirectNodeConnection> graph = new SimpleDirectedWeightedGraph<>(DirectNodeConnection.class);
    net.getNodes().stream().filter(Node::isCenterOfRegions).forEach(graph::addVertex);
    for (Node node : graph.vertexSet()) {
        getRegionConnections(node).forEach(connection -> {
            Node n1 = connection.getFrom();
            Node n2 = connection.getTo().getNode();
            DirectNodeConnectionImpl edge = (DirectNodeConnectionImpl) graph.getEdge(n1, n2);
            if (edge == null) {
                edge = new DirectNodeConnectionImpl(connection.getPath());
                graph.addEdge(n1, n2, edge);
            } else {
                edge.connections.add(connection.getPath());
            }
        });
    }
    return graph;
}
Also used : SimpleDirectedWeightedGraph(org.jgrapht.graph.SimpleDirectedWeightedGraph) Node(net.parostroj.timetable.model.Node)

Example 18 with Node

use of net.parostroj.timetable.model.Node in project grafikon by jub77.

the class LibraryBuilder method importNode.

public LibraryItem importNode(Node node) {
    Node nodeCopy = copyFactory.copy(node, node.getId());
    addHandler.stripObjectIdAttributes(nodeCopy);
    for (NodeTrack track : nodeCopy.getTracks()) {
        addHandler.stripObjectIdAttributes(track);
    }
    // create item and add it to library
    return addImpl(nodeCopy, LibraryItemType.NODE);
}
Also used : NodeTrack(net.parostroj.timetable.model.NodeTrack) Node(net.parostroj.timetable.model.Node)

Example 19 with Node

use of net.parostroj.timetable.model.Node in project grafikon by jub77.

the class SelectNodesDialog method initComponents.

private void initComponents() {
    nodesPanel = new javax.swing.JPanel();
    nodesPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    nodesComboBox = new javax.swing.JComboBox<Node>();
    javax.swing.JPanel buttonsPanel = new javax.swing.JPanel();
    javax.swing.JButton okButton = new javax.swing.JButton();
    javax.swing.JButton cancelButton = new javax.swing.JButton();
    nodesPanel.setLayout(new BorderLayout(0, 0));
    nodesPanel.add(nodesComboBox, BorderLayout.NORTH);
    getContentPane().add(nodesPanel, java.awt.BorderLayout.CENTER);
    buttonsPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
    // NOI18N
    okButton.setText(ResourceLoader.getString("button.ok"));
    okButton.addActionListener(evt -> {
        selectedNode = (Node) nodesComboBox.getSelectedItem();
        setVisible(false);
    });
    buttonsPanel.add(okButton);
    // NOI18N
    cancelButton.setText(ResourceLoader.getString("button.cancel"));
    cancelButton.addActionListener(evt -> {
        selectedNode = null;
        setVisible(false);
    });
    buttonsPanel.add(cancelButton);
    getContentPane().add(buttonsPanel, java.awt.BorderLayout.SOUTH);
    setResizable(false);
    pack();
}
Also used : BorderLayout(java.awt.BorderLayout) Node(net.parostroj.timetable.model.Node) EmptyBorder(javax.swing.border.EmptyBorder)

Example 20 with Node

use of net.parostroj.timetable.model.Node in project grafikon by jub77.

the class ThroughNodesDialog method setNodes.

/**
 * sets list of nodes.
 *
 * @param nodes list of nodes
 */
public void setNodes(List<Node> nodes, Collection<Node> allNodes) {
    this.nodes = nodes;
    // update values for nodes
    ElementSort<Node> sort = new ElementSort<Node>(new NodeComparator());
    List<Node> sorted = sort.sort(allNodes);
    nodeComboBox.removeAllItems();
    for (Node node : sorted) {
        nodeComboBox.addItem(node);
    }
    // update list
    nodeModel = new WrapperListModel<Node>(false);
    for (Node n : nodes) {
        nodeModel.addWrapper(Wrapper.getWrapper(n));
    }
    nodeList.setModel(nodeModel);
    this.pack();
}
Also used : NodeComparator(net.parostroj.timetable.actions.NodeComparator) Node(net.parostroj.timetable.model.Node) ElementSort(net.parostroj.timetable.actions.ElementSort)

Aggregations

Node (net.parostroj.timetable.model.Node)39 List (java.util.List)10 ArrayList (java.util.ArrayList)9 TimeInterval (net.parostroj.timetable.model.TimeInterval)9 BorderLayout (java.awt.BorderLayout)8 Collection (java.util.Collection)8 Region (net.parostroj.timetable.model.Region)8 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)8 FlowLayout (java.awt.FlowLayout)7 Comparator (java.util.Comparator)7 Set (java.util.Set)7 BorderFactory (javax.swing.BorderFactory)7 JButton (javax.swing.JButton)7 JPanel (javax.swing.JPanel)7 JScrollPane (javax.swing.JScrollPane)7 RxActionHandler (net.parostroj.timetable.gui.actions.execution.RxActionHandler)7 GuiComponentUtils (net.parostroj.timetable.gui.utils.GuiComponentUtils)7 GuiIcon (net.parostroj.timetable.gui.utils.GuiIcon)7 ResourceLoader (net.parostroj.timetable.gui.utils.ResourceLoader)7 ItemEvent (java.awt.event.ItemEvent)6