Search in sources :

Example 1 with StepImpl

use of net.parostroj.timetable.model.freight.FreightConnectionAnalysis.StepImpl 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)

Aggregations

Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Stream (java.util.stream.Stream)1 Node (net.parostroj.timetable.model.Node)1 Train (net.parostroj.timetable.model.Train)1 Context (net.parostroj.timetable.model.freight.FreightConnectionAnalysis.Context)1 Stage (net.parostroj.timetable.model.freight.FreightConnectionAnalysis.Stage)1 StepImpl (net.parostroj.timetable.model.freight.FreightConnectionAnalysis.StepImpl)1 TimeUtil (net.parostroj.timetable.utils.TimeUtil)1