c1f140bc78
Pure-Dart SCC ranking and column placement over the landed graph identities. Every extent and gap is caller supplied; missing measurements are rejected rather than defaulted. Focused tests cover linear, diamond, cycle, self-loop, disconnected, terminal, expanded-text and invalid-input cases.
143 lines
4.1 KiB
Dart
143 lines
4.1 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import '../domain/graph_definition.dart';
|
|
import 'graph_ranks.dart';
|
|
import 'layout_models.dart';
|
|
|
|
/// Places definition nodes in topological columns.
|
|
///
|
|
/// Nodes keep definition order inside a rank, each column is as wide as its
|
|
/// widest measured node, and columns are top-aligned. A node is centred in its
|
|
/// column, so heterogeneous widths and text-expanded heights stay separated by
|
|
/// [GraphLayoutConfig.rowGap] inside a rank and [GraphLayoutConfig.columnGap]
|
|
/// between ranks. Nothing is clipped to a fixed box: the supplied measured
|
|
/// width and height are used verbatim and the returned bounds include them.
|
|
///
|
|
/// [nodes] and [edges] normally come from `graphNodes`/`graphEdges`. Edges whose
|
|
/// source or target is not one of [nodes] are ignored, so an unknown target can
|
|
/// never invent a node. A measurement is required for every node; a missing one
|
|
/// is an error rather than a made-up size. Extra measurement entries are
|
|
/// ignored because they cannot become nodes.
|
|
GraphLayout layoutGraphNodes({
|
|
required List<GraphNode> nodes,
|
|
required List<GraphEdge> edges,
|
|
required Map<String, NodeExtent> extents,
|
|
required GraphLayoutConfig config,
|
|
}) {
|
|
_validateNodeKeys(nodes);
|
|
final ranks = assignGraphRanks(nodes, edges);
|
|
final measured = <String, NodeExtent>{};
|
|
final missing = <String>[];
|
|
for (final node in nodes) {
|
|
final extent = extents[node.key];
|
|
if (extent == null) {
|
|
missing.add(node.key);
|
|
} else {
|
|
measured[node.key] = extent;
|
|
}
|
|
}
|
|
if (missing.isNotEmpty) {
|
|
throw ArgumentError.value(
|
|
missing,
|
|
'extents',
|
|
'missing measured extents for graph nodes',
|
|
);
|
|
}
|
|
|
|
final columns = List<List<GraphNode>>.generate(
|
|
ranks.rankCount,
|
|
(_) => <GraphNode>[],
|
|
);
|
|
for (final node in nodes) {
|
|
columns[ranks.rankOf[node.key]!].add(node);
|
|
}
|
|
|
|
final columnWidths = <double>[
|
|
for (final column in columns)
|
|
column.fold<double>(
|
|
0,
|
|
(width, node) => math.max(width, measured[node.key]!.width),
|
|
),
|
|
];
|
|
final columnHeights = <double>[
|
|
for (final column in columns)
|
|
_columnHeight(column, measured, config.rowGap),
|
|
];
|
|
final columnX = <double>[];
|
|
var nextX = config.padding;
|
|
for (var rank = 0; rank < columns.length; rank += 1) {
|
|
columnX.add(nextX);
|
|
nextX += columnWidths[rank];
|
|
if (rank + 1 < columns.length) nextX += config.columnGap;
|
|
}
|
|
|
|
final boxes = <GraphNodeBox>[];
|
|
for (var rank = 0; rank < columns.length; rank += 1) {
|
|
var nextY = config.padding;
|
|
for (final node in columns[rank]) {
|
|
final extent = measured[node.key]!;
|
|
boxes.add(
|
|
GraphNodeBox(
|
|
key: node.key,
|
|
terminal: node.terminal,
|
|
stageIndex: node.stageIndex,
|
|
rank: rank,
|
|
x: columnX[rank] + (columnWidths[rank] - extent.width) / 2,
|
|
y: nextY,
|
|
width: extent.width,
|
|
height: extent.height,
|
|
),
|
|
);
|
|
nextY += extent.height + config.rowGap;
|
|
}
|
|
}
|
|
|
|
final width =
|
|
config.padding * 2 +
|
|
columnWidths.fold<double>(0, (sum, column) => sum + column) +
|
|
config.columnGap * math.max(0, columns.length - 1);
|
|
final height =
|
|
config.padding * 2 +
|
|
columnHeights.fold<double>(
|
|
0,
|
|
(tallest, column) => math.max(tallest, column),
|
|
);
|
|
|
|
return GraphLayout(
|
|
nodes: boxes,
|
|
width: width,
|
|
height: height,
|
|
rankCount: ranks.rankCount,
|
|
ranks: ranks.rankOf,
|
|
);
|
|
}
|
|
|
|
void _validateNodeKeys(List<GraphNode> nodes) {
|
|
final seen = <String>{};
|
|
for (final node in nodes) {
|
|
if (node.key.isEmpty) {
|
|
throw ArgumentError.value(
|
|
node.key,
|
|
'nodes',
|
|
'node key must not be empty',
|
|
);
|
|
}
|
|
if (!seen.add(node.key)) {
|
|
throw ArgumentError.value(node.key, 'nodes', 'duplicate graph node key');
|
|
}
|
|
}
|
|
}
|
|
|
|
double _columnHeight(
|
|
List<GraphNode> column,
|
|
Map<String, NodeExtent> measured,
|
|
double rowGap,
|
|
) {
|
|
if (column.isEmpty) return 0;
|
|
var height = 0.0;
|
|
for (final node in column) {
|
|
height += measured[node.key]!.height;
|
|
}
|
|
return height + rowGap * (column.length - 1);
|
|
}
|