oxgraph-topology 0.2.0

Storage-agnostic core traits for discrete topology views.
Documentation
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
//! Tests that `oxgraph-topology` traits support static-dispatch generic consumers.

use oxgraph_topology::{
    CanonicalElementIdentity, CanonicalIncidenceIdentity, CanonicalRelationIdentity,
    ContainsElement, ContainsIncidence, ContainsRelation, ElementIncidenceCount, ElementIndex,
    ElementWeight, IncidenceBase, IncidenceCounts, IncidenceElement, IncidenceIndex,
    IncidenceRelation, IncidenceRole, IncidenceView, IncidenceWeight, LocalElementIdentity,
    LocalIncidenceIdentity, LocalRelationIdentity, RelationIncidenceCount, RelationIncidences,
    RelationIndex, RelationWeight, TopologyBase, TopologyCounts,
};
use proptest::prelude::*;

/// Element identifier used by the test fixture.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Element(usize);

/// Relation identifier used by the test fixture.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Relation(usize);

/// Incidence identifier used by the test fixture.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Incidence(usize);

/// Role used by the test fixture.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum FixtureRole {
    /// First endpoint or participant.
    First,
    /// Second endpoint or participant.
    Second,
    /// Additional endpoint or participant.
    Extra,
}

/// One incidence record in the fixture.
#[derive(Clone, Copy, Debug)]
struct IncidenceRecord {
    /// Element participating in the relation.
    element: Element,
    /// Relation containing the incidence.
    relation: Relation,
    /// Role of the participation.
    role: FixtureRole,
}

/// Tiny topology fixture with fixed relation offsets.
#[derive(Debug)]
struct FixtureTopology {
    /// Number of elements in the fixture.
    element_count: usize,
    /// Relation start offsets into `incidences`.
    relation_offsets: &'static [usize],
    /// Flat incidence records.
    incidences: &'static [IncidenceRecord],
}

impl TopologyBase for FixtureTopology {
    type ElementId = Element;
    type RelationId = Relation;
}

impl IncidenceBase for FixtureTopology {
    type IncidenceId = Incidence;
    type Role = FixtureRole;
}

impl TopologyCounts for FixtureTopology {
    fn element_count(&self) -> usize {
        self.element_count
    }

    fn relation_count(&self) -> usize {
        self.relation_offsets.len() - 1
    }
}

impl IncidenceCounts for FixtureTopology {
    fn incidence_count(&self) -> usize {
        self.incidences.len()
    }
}

impl ElementIndex for FixtureTopology {
    fn element_bound(&self) -> usize {
        self.element_count
    }

    fn element_index(&self, element: Element) -> usize {
        element.0
    }
}

impl ContainsElement for FixtureTopology {
    fn contains_element(&self, element: Element) -> bool {
        element.0 < self.element_count
    }
}

impl RelationIndex for FixtureTopology {
    fn relation_bound(&self) -> usize {
        self.relation_offsets.len() - 1
    }

    fn relation_index(&self, relation: Relation) -> usize {
        relation.0
    }
}

impl ContainsRelation for FixtureTopology {
    fn contains_relation(&self, relation: Relation) -> bool {
        relation.0 < self.relation_offsets.len() - 1
    }
}

impl IncidenceIndex for FixtureTopology {
    fn incidence_bound(&self) -> usize {
        self.incidences.len()
    }

    fn incidence_index(&self, incidence: Incidence) -> usize {
        incidence.0
    }
}

impl ContainsIncidence for FixtureTopology {
    fn contains_incidence(&self, incidence: Incidence) -> bool {
        incidence.0 < self.incidences.len()
    }
}

impl RelationIncidences for FixtureTopology {
    type Incidences<'view>
        = RelationIncidenceIter
    where
        Self: 'view;

    fn relation_incidences(&self, relation: Relation) -> Self::Incidences<'_> {
        let start = self.relation_offsets[relation.0];
        let end = self.relation_offsets[relation.0 + 1];
        RelationIncidenceIter { next: start, end }
    }
}

impl IncidenceElement for FixtureTopology {
    fn incidence_element(&self, incidence: Incidence) -> Element {
        self.incidences[incidence.0].element
    }
}

impl IncidenceRelation for FixtureTopology {
    fn incidence_relation(&self, incidence: Incidence) -> Relation {
        self.incidences[incidence.0].relation
    }
}

impl IncidenceRole for FixtureTopology {
    fn incidence_role(&self, incidence: Incidence) -> FixtureRole {
        self.incidences[incidence.0].role
    }
}

impl RelationIncidenceCount for FixtureTopology {
    fn relation_incidence_count(&self, relation: Relation) -> usize {
        self.relation_offsets[relation.0 + 1] - self.relation_offsets[relation.0]
    }
}

impl ElementIncidenceCount for FixtureTopology {
    fn element_incidence_count(&self, element: Element) -> usize {
        self.incidences
            .iter()
            .filter(|record| record.element == element)
            .count()
    }
}

impl ElementWeight for FixtureTopology {
    type Weight = usize;

    fn element_weight(&self, element: Element) -> Self::Weight {
        element.0 + 10
    }
}

impl RelationWeight for FixtureTopology {
    type Weight = usize;

    fn relation_weight(&self, relation: Relation) -> Self::Weight {
        relation.0 + 20
    }
}

impl IncidenceWeight for FixtureTopology {
    type Weight = usize;

    fn incidence_weight(&self, incidence: Incidence) -> Self::Weight {
        incidence.0 + 30
    }
}

impl CanonicalElementIdentity for FixtureTopology {
    type CanonicalElementId = Element;

    fn canonical_element_id(&self, element: Element) -> Self::CanonicalElementId {
        element
    }
}

impl LocalElementIdentity for FixtureTopology {
    fn local_element_id(&self, canonical: Self::CanonicalElementId) -> Option<Self::ElementId> {
        self.contains_element(canonical).then_some(canonical)
    }
}

impl CanonicalRelationIdentity for FixtureTopology {
    type CanonicalRelationId = Relation;

    fn canonical_relation_id(&self, relation: Relation) -> Self::CanonicalRelationId {
        relation
    }
}

impl LocalRelationIdentity for FixtureTopology {
    fn local_relation_id(&self, canonical: Self::CanonicalRelationId) -> Option<Self::RelationId> {
        self.contains_relation(canonical).then_some(canonical)
    }
}

impl CanonicalIncidenceIdentity for FixtureTopology {
    type CanonicalIncidenceId = Incidence;

    fn canonical_incidence_id(&self, incidence: Incidence) -> Self::CanonicalIncidenceId {
        incidence
    }
}

impl LocalIncidenceIdentity for FixtureTopology {
    fn local_incidence_id(
        &self,
        canonical: Self::CanonicalIncidenceId,
    ) -> Option<Self::IncidenceId> {
        self.contains_incidence(canonical).then_some(canonical)
    }
}

/// Iterator over contiguous incidence IDs for one fixture relation.
#[derive(Debug)]
struct RelationIncidenceIter {
    /// Next flat incidence index to yield.
    next: usize,
    /// Exclusive end of the relation's incidence range.
    end: usize,
}

impl Iterator for RelationIncidenceIter {
    type Item = Incidence;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next == self.end {
            return None;
        }

        let incidence = Incidence(self.next);
        self.next += 1;
        Some(incidence)
    }
}

/// Returns the elements participating in one relation through static dispatch.
fn relation_elements<T>(topology: &T, relation: T::RelationId) -> Vec<T::ElementId>
where
    T: RelationIncidences + IncidenceElement,
{
    topology
        .relation_incidences(relation)
        .map(|incidence| topology.incidence_element(incidence))
        .collect()
}

/// Returns relation roles through the complete incidence-view bundle.
fn relation_roles<T>(topology: &T, relation: T::RelationId) -> Vec<T::Role>
where
    T: IncidenceView,
{
    topology
        .relation_incidences(relation)
        .map(|incidence| topology.incidence_role(incidence))
        .collect()
}

/// Reads all optional weight capabilities through static dispatch.
fn total_fixture_weight<T>(topology: &T) -> usize
where
    T: TopologyBase<ElementId = Element, RelationId = Relation>
        + IncidenceBase<IncidenceId = Incidence>
        + ElementWeight<Weight = usize>
        + RelationWeight<Weight = usize>
        + IncidenceWeight<Weight = usize>,
{
    topology.element_weight(Element(0))
        + topology.relation_weight(Relation(0))
        + topology.incidence_weight(Incidence(0))
}

/// Roundtrips all optional identity capabilities through static dispatch.
fn identity_roundtrip<T>(topology: &T) -> bool
where
    T: LocalElementIdentity<CanonicalElementId = Element, ElementId = Element>
        + LocalRelationIdentity<CanonicalRelationId = Relation, RelationId = Relation>
        + LocalIncidenceIdentity<CanonicalIncidenceId = Incidence, IncidenceId = Incidence>,
{
    topology.local_element_id(topology.canonical_element_id(Element(0))) == Some(Element(0))
        && topology.local_relation_id(topology.canonical_relation_id(Relation(0)))
            == Some(Relation(0))
        && topology.local_incidence_id(topology.canonical_incidence_id(Incidence(0)))
            == Some(Incidence(0))
}

/// Returns a fixture shaped like one binary edge and one ternary hyperedge.
fn fixture() -> FixtureTopology {
    static OFFSETS: &[usize] = &[0, 2, 5];
    static INCIDENCES: &[IncidenceRecord] = &[
        IncidenceRecord {
            element: Element(0),
            relation: Relation(0),
            role: FixtureRole::First,
        },
        IncidenceRecord {
            element: Element(1),
            relation: Relation(0),
            role: FixtureRole::Second,
        },
        IncidenceRecord {
            element: Element(0),
            relation: Relation(1),
            role: FixtureRole::First,
        },
        IncidenceRecord {
            element: Element(1),
            relation: Relation(1),
            role: FixtureRole::Second,
        },
        IncidenceRecord {
            element: Element(2),
            relation: Relation(1),
            role: FixtureRole::Extra,
        },
    ];

    FixtureTopology {
        element_count: 3,
        relation_offsets: OFFSETS,
        incidences: INCIDENCES,
    }
}

#[test]
fn generic_consumer_reads_relation_elements() {
    let topology = fixture();

    assert_eq!(
        relation_elements(&topology, Relation(0)),
        [Element(0), Element(1)]
    );
    assert_eq!(
        relation_elements(&topology, Relation(1)),
        [Element(0), Element(1), Element(2)]
    );
}

#[test]
fn incidence_records_resolve_both_sides_and_roles() {
    let topology = fixture();

    assert_eq!(topology.incidence_relation(Incidence(4)), Relation(1));
    assert_eq!(topology.incidence_element(Incidence(4)), Element(2));
    assert_eq!(topology.incidence_role(Incidence(4)), FixtureRole::Extra);
}

#[test]
fn dense_indexes_are_within_bounds() {
    let topology = fixture();

    assert_eq!(topology.element_bound(), 3);
    assert_eq!(topology.relation_bound(), 2);
    assert_eq!(topology.incidence_bound(), 5);

    for element in [Element(0), Element(1), Element(2)] {
        assert!(topology.element_index(element) < topology.element_bound());
    }

    for relation in [Relation(0), Relation(1)] {
        assert!(topology.relation_index(relation) < topology.relation_bound());
    }

    for incidence in [
        Incidence(0),
        Incidence(1),
        Incidence(2),
        Incidence(3),
        Incidence(4),
    ] {
        assert!(topology.incidence_index(incidence) < topology.incidence_bound());
    }
}

#[test]
fn dense_indexes_are_distinct_for_visible_ids() {
    let topology = fixture();

    assert_ne!(
        topology.element_index(Element(0)),
        topology.element_index(Element(1))
    );
    assert_ne!(
        topology.relation_index(Relation(0)),
        topology.relation_index(Relation(1))
    );
    assert_ne!(
        topology.incidence_index(Incidence(0)),
        topology.incidence_index(Incidence(1))
    );
}

#[test]
fn containment_reports_visible_ids() {
    let topology = fixture();

    assert!(topology.contains_element(Element(2)));
    assert!(!topology.contains_element(Element(3)));
    assert!(topology.contains_relation(Relation(1)));
    assert!(!topology.contains_relation(Relation(2)));
    assert!(topology.contains_incidence(Incidence(4)));
    assert!(!topology.contains_incidence(Incidence(5)));
}

#[test]
fn incidence_view_blanket_impl_resolves_roles() {
    let topology = fixture();

    assert_eq!(
        relation_roles(&topology, Relation(1)),
        [FixtureRole::First, FixtureRole::Second, FixtureRole::Extra]
    );
}

#[test]
fn weight_and_identity_capabilities_are_static_dispatch() {
    let topology = fixture();

    assert_eq!(total_fixture_weight(&topology), 60);
    assert!(identity_roundtrip(&topology));
}

proptest! {
    #![proptest_config(ProptestConfig {
        failure_persistence: None,
        ..ProptestConfig::default()
    })]

    /// Relation incidence count matches traversal count for all fixture relations.
    #[test]
    fn relation_count_matches_iterator_count(index in 0usize..2) {
        let topology = fixture();
        let relation = Relation(index);

        prop_assert_eq!(
            topology.relation_incidence_count(relation),
            topology.relation_incidences(relation).count()
        );
    }

    /// Element incidence count matches scanning the fixture incidence records.
    #[test]
    fn element_count_matches_fixture_records(index in 0usize..3) {
        let topology = fixture();
        let element = Element(index);
        let expected = topology.incidences
            .iter()
            .filter(|record| record.element == element)
            .count();

        prop_assert_eq!(topology.element_incidence_count(element), expected);
    }
}