use of net.parostroj.timetable.model.freight.FreightConnectionAnalysis.Context 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());
}
Aggregations