1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Copyright 2026 Mark AB (markik)
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Graph query + traversal — node / edge lookups, neighbor
//! iteration, BFS / shortest-path / connected-components / SCC, and
//! the derived-edge views (semantic / arrangement / containment).
//!
//! Extracted from `graph/mod.rs` per the 2026-05-11 kernel
//! decomposition pass. These are the read-only and read-mostly
//! methods on `Graph`; the mutating counterparts live in `mod.rs`
//! (node-property setters), `edge_ops.rs` (edge mutators), and
//! `snapshot.rs` (serialization).
use std::collections::{HashMap, HashSet, VecDeque};
use petgraph::Direction;
use petgraph::algo::{astar, dijkstra, has_path_connecting, kosaraju_scc};
use petgraph::visit::{EdgeRef, IntoEdgeReferences, UndirectedAdaptor};
use uuid::Uuid;
use super::edge_taxonomy::{ContainmentSubKind, EdgeAssertion, RelationSelector};
use super::identity::{EdgeKey, NodeKey};
use super::node::Node;
use super::snapshot::containment_parent_url;
use super::{ArrangementEdgeView, ContainmentEdgeView, Graph, RelationView, SemanticEdgeView};
impl Graph {
/// Get a node by key
pub fn get_node(&self, key: NodeKey) -> Option<&Node> {
self.inner.node(key)
}
/// Get a mutable node by key.
pub(crate) fn get_node_mut(&mut self, key: NodeKey) -> Option<&mut Node> {
self.inner.node_mut(key)
}
/// Find a node whose AddressClaims include the given address (any
/// `AddressRole`). Returns an arbitrary matching node when multiple
/// nodes share the address — the caller is expected to handle
/// duplicates explicitly when relevant.
///
/// Canonical address lookup per the [node identity + duplicates
/// brief](https://github.com/merely-made/mere/blob/main/design_docs/mere_docs/research/2026-05-18_node_identity_and_duplicates_brief.md);
/// supersedes the URL-string-keyed `get_node_by_url` (which is
/// retained as a transitional shim — see below).
pub fn find_node_by_address(
&self,
address: &crate::address::Address,
) -> Option<(NodeKey, &Node)> {
let url = address.as_url_str();
let key = self.url_to_nodes.get(url)?.last().copied()?;
Some((key, self.inner.node(key)?))
}
/// Convenience wrapper over [`Graph::find_node_by_address`] for
/// callers that have a URL string rather than a typed [`Address`].
/// Builds the [`Address`] internally and delegates.
///
/// Use this for tests and quick lookups; prefer
/// [`Graph::find_node_by_address`] in code paths that already have a
/// typed [`Address`] in hand.
pub fn get_node_by_url(&self, url: &str) -> Option<(NodeKey, &Node)> {
let address = crate::address::address_from_url(url);
self.find_node_by_address(&address)
}
/// Get all node keys currently mapped to a URL.
pub fn get_nodes_by_url(&self, url: &str) -> Vec<NodeKey> {
self.url_to_nodes.get(url).cloned().unwrap_or_default()
}
/// Get a node by UUID.
pub fn get_node_by_id(&self, id: Uuid) -> Option<(NodeKey, &Node)> {
let key = self.inner.key_of(&id)?;
Some((key, self.inner.node(key)?))
}
/// Get node key by UUID.
pub fn get_node_key_by_id(&self, id: Uuid) -> Option<NodeKey> {
self.inner.key_of(&id)
}
/// Iterate over all nodes as (key, node) pairs
pub fn nodes(&self) -> impl Iterator<Item = (NodeKey, &Node)> {
self.inner.nodes()
}
/// Iterate over all stored relations, one row per recognized
/// statement/family carried by the pair-local edge bucket.
/// One `EdgePayload` between two nodes may therefore yield
/// multiple semantic rows.
///
/// Canonical read surface per the 2026-05-11 relation-taxonomy
/// plan §2. Stage 4 removed the legacy `EdgeType`-flavoured
/// `edges()` iterator that this replaces.
pub fn relations(&self) -> impl Iterator<Item = RelationView> + '_ {
use super::edge_taxonomy::RelationKind;
self.inner.inner().edge_references().flat_map(|edge| {
let from = edge.source();
let to = edge.target();
let payload = edge.weight();
let mut out: Vec<RelationView> = Vec::new();
for statement in payload.semantic_statements() {
if let Some(sub_kind) = statement.recognized_sub_kind {
out.push(RelationView {
from,
to,
kind: RelationKind::Semantic(sub_kind),
});
}
}
// Traversal is event-shaped, no sub-kind. Presence of
// any traversal data (events or metrics) yields one
// row. Family-aware view callers reach for the
// typed payload for actual events.
if let Some(traversal) = payload.traversal_data()
&& (!traversal.traversals.is_empty() || traversal.metrics.total_navigations > 0)
{
out.push(RelationView {
from,
to,
kind: RelationKind::Traversal,
});
}
if let Some(containment) = payload.containment_data() {
for &sub_kind in &containment.sub_kinds {
out.push(RelationView {
from,
to,
kind: RelationKind::Containment(sub_kind),
});
}
}
if let Some(arrangement) = payload.arrangement_data() {
for &sub_kind in &arrangement.sub_kinds {
out.push(RelationView {
from,
to,
kind: RelationKind::Arrangement(sub_kind),
});
}
}
if let Some(imported) = payload.imported_data() {
for &sub_kind in &imported.sub_kinds {
out.push(RelationView {
from,
to,
kind: RelationKind::Imported(sub_kind),
});
}
}
if let Some(prov) = payload.provenance_data() {
for &sub_kind in &prov.sub_kinds {
out.push(RelationView {
from,
to,
kind: RelationKind::Provenance(sub_kind),
});
}
}
out.into_iter()
})
}
pub fn semantic_edges(&self) -> impl Iterator<Item = SemanticEdgeView> + '_ {
self.inner.inner().edge_references().flat_map(|edge| {
let from = edge.source();
let to = edge.target();
let payload = edge.weight();
payload
.semantic_statements()
.iter()
.filter_map(|statement| {
statement
.recognized_sub_kind
.map(|sub_kind| SemanticEdgeView {
from,
to,
sub_kind,
label: statement.label.clone(),
})
})
.collect::<Vec<_>>()
.into_iter()
})
}
pub fn arrangement_edges(&self) -> impl Iterator<Item = ArrangementEdgeView> + '_ {
self.inner.inner().edge_references().flat_map(|e| {
let from = e.source();
let to = e.target();
e.weight()
.arrangement_data()
.map(|data| {
data.sub_kinds
.iter()
.copied()
.map(move |sub_kind| ArrangementEdgeView { from, to, sub_kind })
})
.into_iter()
.flatten()
})
}
pub fn containment_edges(&self) -> impl Iterator<Item = ContainmentEdgeView> + '_ {
self.inner.inner().edge_references().flat_map(|e| {
let from = e.source();
let to = e.target();
e.weight()
.containment_data()
.map(|data| {
data.sub_kinds
.iter()
.copied()
.map(move |sub_kind| ContainmentEdgeView { from, to, sub_kind })
})
.into_iter()
.flatten()
})
}
/// Rebuild derived containment edges from current node URLs.
///
/// Derived relations are additive/read-only and are never persisted.
/// Derive the containment this one node's address implies, at the moment
/// the node is created.
///
/// The **UrlPath half** of [`rebuild_derived_containment_relations`],
/// applied to a single node. It exists because that rebuild runs only on
/// snapshot load, which used to mean a node visited under a folder had no
/// containment edge until the session was saved and reloaded. Anything
/// reading containment live (a graph behavior's watch, which names a
/// region by its container) saw nothing there. Deriving at creation makes
/// containment mean the same thing live and loaded.
///
/// The **Domain half is deliberately not here.** A domain anchor is the
/// shallowest node on a host, so minting one node can re-anchor every
/// other node sharing that host: a whole-graph fact, which stays with the
/// whole-graph rebuild. This one is purely local, which is what makes it
/// cheap enough to run per node.
pub fn derive_containment_for(&mut self, key: NodeKey) {
let Some(node) = self.get_node(key) else {
return;
};
let Ok(parsed) = url::Url::parse(node.url()) else {
return;
};
let Some(parent_url) = containment_parent_url(&parsed) else {
return;
};
let Some((parent_key, _)) = self.get_node_by_url(&parent_url) else {
return;
};
if parent_key == key {
return;
}
// Child to container, the direction the rebuild uses.
let _ = self.assert_relation(
key,
parent_key,
EdgeAssertion::Containment {
sub_kind: ContainmentSubKind::UrlPath,
},
);
}
pub(crate) fn rebuild_derived_containment_relations(&mut self) {
let edge_ids: Vec<EdgeKey> = self.inner.inner().edge_indices().collect();
let mut empty_edges = Vec::new();
// This rebuild retracts containment relations and removes empty edges directly on `inner`
// (bypassing the bumping API), so track the structural change and bump once. The
// `assert_relation` calls below bump on their own when they re-derive the edges.
let mut changed = false;
for edge_id in edge_ids {
if let Some(payload) = self.inner.edge_mut(edge_id) {
let mut removed_any = false;
removed_any |= payload
.retract_relation(RelationSelector::Containment(ContainmentSubKind::UrlPath));
removed_any |= payload
.retract_relation(RelationSelector::Containment(ContainmentSubKind::Domain));
if removed_any {
changed = true;
}
if removed_any && payload.is_empty() {
empty_edges.push(edge_id);
}
}
}
for edge_id in empty_edges {
let _ = self.inner.disconnect(edge_id);
}
if changed {
self.bump_revision();
}
let mut domain_anchor: HashMap<String, (NodeKey, usize, Uuid)> = HashMap::new();
for (key, node) in self.nodes() {
let Ok(parsed) = url::Url::parse(node.url()) else {
continue;
};
let depth = parsed.path_segments().map_or(0, |segments| {
segments.filter(|segment| !segment.is_empty()).count()
});
let Some(host) = parsed.host_str() else {
continue;
};
let host = host.to_ascii_lowercase();
let candidate = (key, depth, node.id);
domain_anchor
.entry(host)
.and_modify(|current| {
if candidate.1 < current.1
|| (candidate.1 == current.1 && candidate.2 < current.2)
{
*current = candidate;
}
})
.or_insert(candidate);
}
let mut url_parent_edges = Vec::new();
let mut domain_edges = Vec::new();
for (key, node) in self.nodes() {
let Ok(parsed) = url::Url::parse(node.url()) else {
continue;
};
if let Some(parent_url) = containment_parent_url(&parsed)
&& let Some((parent_key, _)) = self.get_node_by_url(&parent_url)
&& parent_key != key
{
url_parent_edges.push((key, parent_key));
}
if let Some(host) = parsed.host_str() {
let host = host.to_ascii_lowercase();
if let Some((anchor_key, _, _)) = domain_anchor.get(&host)
&& *anchor_key != key
{
domain_edges.push((key, *anchor_key));
}
}
}
for (from, to) in url_parent_edges {
let _ = self.assert_relation(
from,
to,
EdgeAssertion::Containment {
sub_kind: ContainmentSubKind::UrlPath,
},
);
}
for (from, to) in domain_edges {
let _ = self.assert_relation(
from,
to,
EdgeAssertion::Containment {
sub_kind: ContainmentSubKind::Domain,
},
);
}
}
/// Iterate outgoing neighbor keys for a node
pub fn out_neighbors(&self, key: NodeKey) -> impl Iterator<Item = NodeKey> + '_ {
self.inner
.inner()
.neighbors_directed(key, Direction::Outgoing)
}
/// Iterate incoming neighbor keys for a node
pub fn in_neighbors(&self, key: NodeKey) -> impl Iterator<Item = NodeKey> + '_ {
self.inner
.inner()
.neighbors_directed(key, Direction::Incoming)
}
/// Iterate undirected neighbor keys for a node.
pub fn neighbors_undirected(&self, key: NodeKey) -> impl Iterator<Item = NodeKey> + '_ {
self.inner.inner().neighbors_undirected(key)
}
/// Undirected neighbors sorted by stable node-key order.
pub fn neighbors_undirected_sorted(&self, key: NodeKey) -> Vec<NodeKey> {
let mut neighbors: Vec<NodeKey> = self
.neighbors_undirected(key)
.filter(|neighbor| *neighbor != key && self.get_node(*neighbor).is_some())
.collect::<HashSet<_>>()
.into_iter()
.collect();
neighbors.sort_by_key(|neighbor| neighbor.index());
neighbors
}
/// Seed nodes plus one-hop undirected neighbors for frame import workflows.
pub fn connected_frame_import_nodes(&self, seeds: &[NodeKey]) -> Vec<NodeKey> {
let mut out = HashSet::new();
for seed in seeds {
if self.get_node(*seed).is_none() {
continue;
}
out.insert(*seed);
out.extend(self.neighbors_undirected(*seed));
}
let mut nodes: Vec<NodeKey> = out
.into_iter()
.filter(|key| self.get_node(*key).is_some())
.collect();
nodes.sort_by_key(|key| key.index());
nodes
}
/// Connected candidate expansion around a source node with depth annotations.
///
/// `max_depth` currently supports 1 or 2, matching connected-open scope behavior.
pub fn connected_candidates_with_depth(
&self,
source: NodeKey,
max_depth: u8,
) -> Vec<(NodeKey, u8)> {
if self.get_node(source).is_none() || max_depth == 0 {
return Vec::new();
}
let mut out = Vec::new();
let mut visited = HashSet::from([source]);
let depth1 = self.neighbors_undirected_sorted(source);
for neighbor in depth1 {
if visited.insert(neighbor) {
out.push((neighbor, 1));
}
}
if max_depth < 2 {
return out;
}
let depth1_nodes: Vec<NodeKey> = out
.iter()
.filter_map(|(node, depth)| (*depth == 1).then_some(*node))
.collect();
for depth1_node in depth1_nodes {
for neighbor in self.neighbors_undirected_sorted(depth1_node) {
if visited.insert(neighbor) {
out.push((neighbor, 2));
}
}
}
out
}
/// Undirected hop distances from `source` using unit edge weights.
pub fn hop_distances_from(&self, source: NodeKey) -> HashMap<NodeKey, usize> {
if self.get_node(source).is_none() {
return HashMap::new();
}
dijkstra(&UndirectedAdaptor(self.inner.inner()), source, None, |_| {
1_usize
})
.into_iter()
.collect()
}
/// Nodes with no incoming or outgoing edges.
pub fn orphan_node_keys(&self) -> Vec<NodeKey> {
let inner = self.inner.inner();
inner
.node_indices()
.filter(|&key| {
inner
.edges_directed(key, Direction::Outgoing)
.next()
.is_none()
&& inner
.edges_directed(key, Direction::Incoming)
.next()
.is_none()
})
.collect()
}
/// Shortest undirected path between two nodes using unit edge weights.
pub fn shortest_path(&self, from: NodeKey, to: NodeKey) -> Option<Vec<NodeKey>> {
if self.get_node(from).is_none() || self.get_node(to).is_none() {
return None;
}
astar(
&UndirectedAdaptor(self.inner.inner()),
from,
|node| node == to,
|_| 1_usize,
|_| 0_usize,
)
.map(|(_, path)| path)
}
/// Reachability in the undirected graph.
pub fn is_reachable(&self, from: NodeKey, to: NodeKey) -> bool {
if self.get_node(from).is_none() || self.get_node(to).is_none() {
return false;
}
has_path_connecting(&UndirectedAdaptor(self.inner.inner()), from, to, None)
}
/// Weakly connected components (undirected projection).
pub fn weakly_connected_components(&self) -> Vec<Vec<NodeKey>> {
let mut visited = HashSet::new();
let mut components = Vec::new();
for start in self.inner.inner().node_indices() {
if !visited.insert(start) {
continue;
}
let mut component = Vec::new();
let mut stack = vec![start];
while let Some(current) = stack.pop() {
component.push(current);
for neighbor in self.neighbors_undirected(current) {
if visited.insert(neighbor) {
stack.push(neighbor);
}
}
}
components.push(component);
}
components
}
/// The connected component of `seed` — it plus every node reachable through relations
/// (undirected), breadth-first. Empty if `seed` is not in the graph. `selectors` is the
/// **edge projection**: only edges matching a selector are followed (an empty slice
/// follows every family). So the *same* nodes can be one Component under one projection
/// and a different one under another. The **Component** graphlet's derivation. (Graphlet
/// derivation, Phase 3 — selectors.)
pub fn component_members(
&self,
seed: uuid::Uuid,
selectors: &[super::RelationSelector],
) -> Vec<uuid::Uuid> {
self.bfs_members(seed, None, selectors)
}
/// The **Ego** neighborhood of `seed`: itself plus every node within `radius`
/// undirected hops, breadth-first (`radius` 0 = just the seed), over the `selectors`
/// edge projection (empty = all families). The Ego graphlet's derivation. (Graphlet
/// derivation, Phase 3 — selectors.)
pub fn ego_members(
&self,
seed: uuid::Uuid,
radius: u8,
selectors: &[super::RelationSelector],
) -> Vec<uuid::Uuid> {
self.bfs_members(seed, Some(radius), selectors)
}
/// Breadth-first member uuids from `seed` over undirected neighbors, bounded to
/// `max_depth` hops (`None` = unbounded = the whole component) and to the `selectors`
/// edge projection. Shared by [`component_members`](Self::component_members) and
/// [`ego_members`](Self::ego_members).
fn bfs_members(
&self,
seed: uuid::Uuid,
max_depth: Option<u8>,
selectors: &[super::RelationSelector],
) -> Vec<uuid::Uuid> {
let Some((start, _)) = self.get_node_by_id(seed) else {
return Vec::new();
};
let mut seen = HashSet::new();
let mut order = Vec::new();
let mut queue = std::collections::VecDeque::new();
seen.insert(start);
queue.push_back((start, 0u8));
while let Some((key, depth)) = queue.pop_front() {
if let Some(node) = self.get_node(key) {
order.push(node.id);
}
if max_depth.is_some_and(|m| depth >= m) {
continue;
}
for neighbor in self.neighbors_undirected_sorted(key) {
// Edge projection: only follow an edge matching a selector (empty = all
// families). This is what makes the same nodes derive a different shape
// under a different relation projection. (Graphlet derivation — selectors.)
if !selectors.is_empty() && !self.edge_matches_selectors(key, neighbor, selectors) {
continue;
}
if seen.insert(neighbor) {
queue.push_back((neighbor, depth + 1));
}
}
}
order
}
/// Whether the edge between `a` and `b` (either direction) matches any of `selectors`
/// (a relation family or sub-kind). Used by the selector-projected derivation walk.
fn edge_matches_selectors(
&self,
a: NodeKey,
b: NodeKey,
selectors: &[super::RelationSelector],
) -> bool {
self.find_edge_key(a, b)
.or_else(|| self.find_edge_key(b, a))
.and_then(|k| self.inner.edge(k))
.is_some_and(|payload| selectors.iter().any(|&s| payload.has_relation(s)))
}
/// Strongly connected components in the directed graph.
pub fn strongly_connected_components(&self) -> Vec<Vec<NodeKey>> {
kosaraju_scc(self.inner.inner())
}
/// Check if a directed edge exists from `from` to `to`
pub fn has_edge_between(&self, from: NodeKey, to: NodeKey) -> bool {
self.inner.inner().find_edge(from, to).is_some()
}
/// Count of nodes in the graph
pub fn node_count(&self) -> usize {
self.inner.node_count()
}
/// Count of edges in the graph
pub fn edge_count(&self) -> usize {
self.inner.edge_count()
}
}
#[cfg(test)]
mod derivation_tests {
use super::*;
use crate::graph::SemanticSubKind;
use euclid::default::Point2D;
/// An A–B–C chain plus an isolated D. Returns the graph and `[a, b, c, d]` uuids.
fn chain_plus_isolate() -> (Graph, [uuid::Uuid; 4]) {
let mut g = Graph::new();
let a = g.add_node("https://a".to_string(), Point2D::new(0.0, 0.0));
let b = g.add_node("https://b".to_string(), Point2D::new(1.0, 0.0));
let c = g.add_node("https://c".to_string(), Point2D::new(2.0, 0.0));
let d = g.add_node("https://d".to_string(), Point2D::new(9.0, 9.0));
let sem = || EdgeAssertion::Semantic {
sub_kind: SemanticSubKind::Hyperlink,
label: None,
decay_progress: None,
};
g.assert_relation(a, b, sem());
g.assert_relation(b, c, sem());
let ids = [
g.get_node(a).unwrap().id,
g.get_node(b).unwrap().id,
g.get_node(c).unwrap().id,
g.get_node(d).unwrap().id,
];
(g, ids)
}
#[test]
fn component_members_is_the_whole_connected_component() {
let (g, [a, _b, _c, d]) = chain_plus_isolate();
assert_eq!(g.component_members(a, &[]).len(), 3, "A reaches B and C");
assert_eq!(g.component_members(d, &[]).len(), 1, "D is isolated");
assert!(
g.component_members(uuid::Uuid::nil(), &[]).is_empty(),
"unknown seed: empty"
);
}
#[test]
fn ego_members_is_radius_bounded() {
let (g, [a, _b, _c, _d]) = chain_plus_isolate();
assert_eq!(
g.ego_members(a, 0, &[]).len(),
1,
"radius 0 is just the seed"
);
assert_eq!(
g.ego_members(a, 1, &[]).len(),
2,
"radius 1 reaches B, not C"
);
assert_eq!(g.ego_members(a, 2, &[]).len(), 3, "radius 2 reaches C");
}
#[test]
fn the_selector_projection_changes_the_derived_shape() {
use crate::graph::{ContainmentSubKind, EdgeFamily, RelationSelector};
// A —Semantic→ B —Containment→ C. The same three nodes, two relation families.
let mut g = Graph::new();
let a = g.add_node("https://a".to_string(), Point2D::new(0.0, 0.0));
let b = g.add_node("https://b".to_string(), Point2D::new(1.0, 0.0));
let c = g.add_node("https://c".to_string(), Point2D::new(2.0, 0.0));
g.assert_relation(
a,
b,
EdgeAssertion::Semantic {
sub_kind: SemanticSubKind::Hyperlink,
label: None,
decay_progress: None,
},
);
g.assert_relation(
b,
c,
EdgeAssertion::Containment {
sub_kind: ContainmentSubKind::CollectionMember,
},
);
let a_id = g.get_node(a).unwrap().id;
let semantic = [RelationSelector::Family(EdgeFamily::Semantic)];
let containment = [RelationSelector::Family(EdgeFamily::Containment)];
// Whole graph under no projection; Semantic stops at B; Containment never leaves A.
assert_eq!(
g.component_members(a_id, &[]).len(),
3,
"all families: A, B, C"
);
assert_eq!(
g.component_members(a_id, &semantic).len(),
2,
"Semantic projection: A, B (B→C is Containment, not followed)"
);
assert_eq!(
g.component_members(a_id, &containment).len(),
1,
"Containment projection: just A (no Containment edge from A)"
);
}
}