Files
pipelines/app/lib/features/graph/routing/graph_edge_routing.dart
T

137 lines
3.9 KiB
Dart

import 'dart:math' as math;
import '../domain/graph_definition.dart';
import '../layout/layout_models.dart';
import 'edge_label_placement.dart';
import 'edge_route_planner.dart';
import 'route_models.dart';
/// Routes every definition edge that has a placed source and target.
///
/// The node boxes and ranks come from the landed [GraphLayout]; this function
/// never moves them, invents a node or assumes a node size. Callers supply one
/// measured [LabelExtent] for every routed edge, plus the clearances and lane
/// spacing in [RoutingConfig].
///
/// Edges with an unknown, empty or missing endpoint are returned in
/// [GraphRouting.skipped] with their definition identity intact. A routed edge
/// keeps its id, source, outcome, target, label and fallback flag. Results are
/// ordered by definition edge id and are stable for repeated input.
GraphRouting routeGraphEdges({
required GraphLayout layout,
required List<GraphEdge> edges,
required Map<int, LabelExtent> labelExtents,
required RoutingConfig config,
}) {
final ordered = <GraphEdge>[...edges]..sort((a, b) => a.id.compareTo(b.id));
final seenIds = <int>{};
for (final edge in ordered) {
if (!seenIds.add(edge.id)) {
throw ArgumentError.value(edge.id, 'edges', 'duplicate edge id');
}
}
final nodes = <String, GraphNodeBox>{
for (final node in layout.nodes) node.key: node,
};
final routed = <GraphEdge>[];
final skipped = <RouteDiagnostic>[];
for (final edge in ordered) {
if (!nodes.containsKey(edge.source)) {
skipped.add(
RouteDiagnostic(
id: edge.id,
source: edge.source,
outcome: edge.outcome,
target: edge.target,
reason: RouteSkipReason.unknownSource,
message: 'source "${edge.source}" has no placed node',
),
);
continue;
}
if (!edge.knownTarget || !nodes.containsKey(edge.target)) {
skipped.add(
RouteDiagnostic(
id: edge.id,
source: edge.source,
outcome: edge.outcome,
target: edge.target,
reason: RouteSkipReason.unknownTarget,
message: edge.target.isEmpty
? 'edge target is empty'
: 'target "${edge.target}" has no placed node',
),
);
continue;
}
routed.add(edge);
}
final missing = <int>[
for (final edge in routed)
if (!labelExtents.containsKey(edge.id)) edge.id,
];
if (missing.isNotEmpty) {
throw ArgumentError.value(
missing,
'labelExtents',
'missing measured label extents for routed edges',
);
}
var maxLabelHeight = 0.0;
for (final edge in routed) {
maxLabelHeight = math.max(maxLabelHeight, labelExtents[edge.id]!.height);
}
final planned = planEdgeRoutes(
layout: layout,
edges: routed,
maxLabelHeight: maxLabelHeight,
config: config,
);
final routes = <EdgeRoute>[for (final plan in planned) plan.toRoute()];
var floorY = layout.height;
for (final route in routes) {
for (final point in route.points) {
floorY = math.max(floorY, point.y);
}
}
final labels = <EdgeLabelBox>[];
for (var index = 0; index < routes.length; index += 1) {
labels.add(
placeEdgeLabel(
route: routes[index],
extent: labelExtents[routes[index].id]!,
nodes: layout.nodes,
placed: labels,
routes: routes,
config: config,
floorY: floorY,
),
);
}
var width = layout.width;
var height = layout.height;
for (final route in routes) {
for (final point in route.points) {
width = math.max(width, point.x);
height = math.max(height, point.y);
}
}
for (final label in labels) {
width = math.max(width, label.right);
height = math.max(height, label.bottom);
}
return GraphRouting(
routes: routes,
labels: labels,
skipped: skipped,
width: width + config.boundsPadding,
height: height + config.boundsPadding,
);
}