axioval-engine 0.3.0

Trusted capability compiler and deterministic source-neutral validation runtime
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
//! Effective coverage: how much of a footprint the union of several sources'
//! effect areas covers.
//!
//! ADR 0004: this seam measures the covered area. Which sources count, how
//! far they reach and what share must be covered are a rule's judgement.
//!
//! Every source has an **effect area** in plan, of one [`EffectReach`] for
//! the whole request: its footprint grown by the range, the points within
//! the range of travel from its centre, or the points it sees from its
//! centre within the range. The covered area is the union of the effect
//! areas clipped to the subject's footprint, reported as an interval: its
//! lower bound comes from inner bounds of the effects of *certain* sources
//! only, its upper bound from outer bounds of every effect. An effect that
//! could not be measured leaves the upper bound at the whole footprint.
//!
//! Travel and sight stay within the subject's **free region**: its footprint
//! less the footprints of the blockers. Blockers the request marks uncertain
//! narrow only the inner bounds, since they may be absent.
//!
//! A request may **continue effects into connected spaces**: the free region
//! then also holds the footprints of the connected spaces and of the doors
//! and openings (passages) joining them, so a sprinkler in the next room
//! travels or sees through an open doorway into the subject. The covered
//! area is still clipped to the subject's footprint. Connections the
//! request marks uncertain widen only the outer bounds, since they may be
//! absent; a connection that cannot be measured leaves the upper bound at
//! the whole footprint. A grown effect ignores walls already, so it takes
//! no connections.

use axioval_ir::{Evidence, ObjectId};

use crate::{PlanArea, PlanAreaError};

/// How far one source's effect reaches.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EffectReach {
    /// The source's footprint grown by the range in every plan direction.
    Grown,
    /// The points of the free region whose shortest path within it from
    /// the source's footprint centre is no longer than the range.
    Travel,
    /// The points of the free region the source's footprint centre sees
    /// within it, no farther than the range.
    Visible,
}

impl EffectReach {
    /// The reach's name, as a rule states it.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Grown => "grown",
            Self::Travel => "travel",
            Self::Visible => "visible",
        }
    }
}

/// An object taking part in a coverage request, and whether it surely does.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Participant {
    object: ObjectId,
    certain: bool,
}

impl Participant {
    /// `object`, which surely takes part when `certain` and may otherwise.
    #[must_use]
    pub fn new(object: ObjectId, certain: bool) -> Self {
        Self { object, certain }
    }

    /// The participating object.
    #[must_use]
    pub fn object(&self) -> &ObjectId {
        &self.object
    }

    /// Whether it surely takes part.
    #[must_use]
    pub fn is_certain(&self) -> bool {
        self.certain
    }
}

/// A question about one subject: how much of its footprint the sources'
/// effect areas cover.
#[derive(Clone, Debug, PartialEq)]
pub struct CoverageRequest {
    subject: ObjectId,
    reach: EffectReach,
    range: f64,
    sources: Vec<Participant>,
    blockers: Vec<Participant>,
    connected: Vec<Participant>,
    passages: Vec<Participant>,
}

impl CoverageRequest {
    /// Coverage of `subject` by `sources`, each reaching `range_metres` as
    /// `reach` says, travel and sight avoiding `blockers`.
    ///
    /// Sources and blockers are sorted by object; an object listed twice
    /// keeps its certain listing.
    ///
    /// # Errors
    ///
    /// [`PlanAreaError::Unavailable`] for a range that is negative or not
    /// finite, or an object that is both subject, source or blocker.
    pub fn try_new(
        subject: ObjectId,
        reach: EffectReach,
        range_metres: f64,
        sources: Vec<Participant>,
        blockers: Vec<Participant>,
    ) -> Result<Self, PlanAreaError> {
        if !range_metres.is_finite() || range_metres < 0.0 {
            return Err(PlanAreaError::Unavailable(format!(
                "a range of {range_metres} m is not a non-negative length"
            )));
        }
        let sources = merged(sources);
        let blockers = merged(blockers);
        let named = |list: &[Participant], object: &ObjectId| {
            list.iter()
                .any(|participant| participant.object() == object)
        };
        if named(&sources, &subject) || named(&blockers, &subject) {
            return Err(PlanAreaError::Unavailable(format!(
                "{subject} cannot cover or block its own footprint"
            )));
        }
        if let Some(both) = sources
            .iter()
            .find(|source| named(&blockers, source.object()))
        {
            return Err(PlanAreaError::Unavailable(format!(
                "{} is both a source and a blocker",
                both.object()
            )));
        }
        Ok(Self {
            subject,
            reach,
            range: range_metres,
            sources,
            blockers,
            connected: Vec::new(),
            passages: Vec::new(),
        })
    }

    /// Continues travel and sight into the `connected` spaces through the
    /// `passages` (doors and openings) joining them to the subject: the free
    /// region becomes the union of the subject's, the connected spaces' and
    /// the passages' footprints, less the blockers. A service that cannot
    /// continue effects refuses such a request; it never ignores the
    /// connections.
    ///
    /// Both lists are sorted by object; an object listed twice keeps its
    /// certain listing.
    ///
    /// # Errors
    ///
    /// [`PlanAreaError::Unavailable`] for a grown reach, which ignores walls
    /// and has nothing to continue through, or a connected space or passage
    /// that is also the subject, a source, a blocker, or both.
    pub fn with_connections(
        mut self,
        connected: Vec<Participant>,
        passages: Vec<Participant>,
    ) -> Result<Self, PlanAreaError> {
        if self.reach == EffectReach::Grown && !(connected.is_empty() && passages.is_empty()) {
            return Err(PlanAreaError::Unavailable(
                "a grown effect ignores walls, so it continues through no connection".into(),
            ));
        }
        let connected = merged(connected);
        let passages = merged(passages);
        let taken = |object: &ObjectId| {
            *object == self.subject
                || self
                    .sources
                    .iter()
                    .chain(&self.blockers)
                    .any(|participant| participant.object() == object)
        };
        if let Some(clash) = connected
            .iter()
            .chain(&passages)
            .find(|participant| taken(participant.object()))
        {
            return Err(PlanAreaError::Unavailable(format!(
                "{} cannot be a connection and the subject, a source or a blocker",
                clash.object()
            )));
        }
        if let Some(both) = connected.iter().find(|space| {
            passages
                .iter()
                .any(|passage| passage.object() == space.object())
        }) {
            return Err(PlanAreaError::Unavailable(format!(
                "{} is both a connected space and a passage",
                both.object()
            )));
        }
        self.connected = connected;
        self.passages = passages;
        Ok(self)
    }

    /// The object whose footprint is covered.
    #[must_use]
    pub fn subject(&self) -> &ObjectId {
        &self.subject
    }

    /// How every source's effect reaches.
    #[must_use]
    pub fn reach(&self) -> EffectReach {
        self.reach
    }

    /// How far, in metres.
    #[must_use]
    pub fn range_metres(&self) -> f64 {
        self.range
    }

    /// The sources, sorted by object.
    #[must_use]
    pub fn sources(&self) -> &[Participant] {
        &self.sources
    }

    /// The blockers travel and sight avoid, sorted by object.
    #[must_use]
    pub fn blockers(&self) -> &[Participant] {
        &self.blockers
    }

    /// The spaces effects continue into, sorted by object; empty unless
    /// [`Self::with_connections`] added them.
    #[must_use]
    pub fn connected(&self) -> &[Participant] {
        &self.connected
    }

    /// The doors and openings joining the connected spaces to the subject,
    /// sorted by object.
    #[must_use]
    pub fn passages(&self) -> &[Participant] {
        &self.passages
    }
}

/// Sorted by object, one entry per object, certain where any listing is.
fn merged(mut list: Vec<Participant>) -> Vec<Participant> {
    list.sort_by(|a, b| a.object.cmp(&b.object).then(b.certain.cmp(&a.certain)));
    list.dedup_by(|later, kept| later.object == kept.object);
    list
}

/// Whether one source's effect area meets the subject's footprint.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EffectMeets {
    /// Its inner bound covers part of the footprint.
    Surely,
    /// Its outer bound reaches the footprint, its inner bound does not.
    Possibly,
    /// Its outer bound misses the footprint.
    No,
    /// It could not be measured, for the reason given.
    Unmeasured(String),
}

/// The answer to a [`CoverageRequest`].
#[derive(Clone, Debug, PartialEq)]
pub struct CoverageEvidence {
    subject: ObjectId,
    footprint: PlanArea,
    covered: PlanArea,
    effects: Vec<(ObjectId, EffectMeets)>,
}

impl CoverageEvidence {
    /// The subject's `footprint`, the `covered` part of it and, per source
    /// in request order, whether its effect meets the footprint.
    ///
    /// # Errors
    ///
    /// [`PlanAreaError::InvalidMeasurement`] for a covered area that may
    /// exceed the footprint.
    pub fn try_new(
        subject: ObjectId,
        footprint: PlanArea,
        covered: PlanArea,
        effects: Vec<(ObjectId, EffectMeets)>,
    ) -> Result<Self, PlanAreaError> {
        if covered.lower_square_metres() > footprint.upper_square_metres() {
            return Err(PlanAreaError::InvalidMeasurement);
        }
        Ok(Self {
            subject,
            footprint,
            covered,
            effects,
        })
    }

    /// The object whose footprint was covered.
    #[must_use]
    pub fn subject(&self) -> &ObjectId {
        &self.subject
    }

    /// The subject's footprint area.
    #[must_use]
    pub fn footprint(&self) -> &PlanArea {
        &self.footprint
    }

    /// The covered part of the footprint.
    #[must_use]
    pub fn covered(&self) -> &PlanArea {
        &self.covered
    }

    /// Per source, whether its effect meets the footprint.
    #[must_use]
    pub fn effects(&self) -> &[(ObjectId, EffectMeets)] {
        &self.effects
    }

    /// Reviewable provenance of the footprint and the covered area.
    #[must_use]
    pub fn evidence(&self) -> [&Evidence; 2] {
        [self.footprint.evidence(), self.covered.evidence()]
    }
}

/// Checks an answer against its request: the same subject, one entry per
/// requested source in order, and a covered area no larger than the
/// footprint could be.
pub(crate) fn check_answer(
    request: &CoverageRequest,
    answer: &CoverageEvidence,
) -> Result<(), PlanAreaError> {
    if answer.subject() != request.subject() {
        return Err(PlanAreaError::Unavailable(format!(
            "coverage of {} was returned for {}",
            answer.subject(),
            request.subject()
        )));
    }
    let answered = answer.effects().iter().map(|(object, _)| object);
    let asked = request.sources().iter().map(Participant::object);
    if !answered.eq(asked) {
        return Err(PlanAreaError::Unavailable(
            "the coverage answer does not list the requested sources".into(),
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use axioval_ir::SourceId;

    fn id(local: &str) -> ObjectId {
        ObjectId::new(SourceId::new("cad", "m").unwrap(), local).unwrap()
    }

    fn area(value: f64) -> PlanArea {
        PlanArea::try_new(
            value,
            value,
            Evidence::exact(SourceId::new("cad", "m").unwrap(), "area"),
        )
        .unwrap()
    }

    #[test]
    fn a_request_merges_listings_and_refuses_overlaps_and_bad_ranges() {
        let source = |local, certain| Participant::new(id(local), certain);
        let request = CoverageRequest::try_new(
            id("s"),
            EffectReach::Grown,
            1.0,
            vec![source("b", false), source("a", false), source("b", true)],
            vec![],
        )
        .unwrap();
        assert_eq!(request.sources(), [source("a", false), source("b", true)]);
        for range in [-1.0, f64::NAN] {
            assert!(
                CoverageRequest::try_new(id("s"), EffectReach::Grown, range, vec![], vec![])
                    .is_err()
            );
        }
        assert!(
            CoverageRequest::try_new(
                id("s"),
                EffectReach::Travel,
                1.0,
                vec![source("s", true)],
                vec![]
            )
            .is_err()
        );
        assert!(
            CoverageRequest::try_new(
                id("s"),
                EffectReach::Travel,
                1.0,
                vec![source("a", true)],
                vec![source("a", false)]
            )
            .is_err()
        );
    }

    #[test]
    fn connections_are_merged_and_refused_where_they_overlap() {
        let one = |local, certain| Participant::new(id(local), certain);
        let request = || {
            CoverageRequest::try_new(
                id("s"),
                EffectReach::Travel,
                5.0,
                vec![one("a", true)],
                vec![one("w", true)],
            )
            .unwrap()
        };
        let joined = request()
            .with_connections(vec![one("n", false), one("n", true)], vec![one("d", false)])
            .unwrap();
        assert_eq!(joined.connected(), [one("n", true)]);
        assert_eq!(joined.passages(), [one("d", false)]);
        assert!(request().connected().is_empty() && request().passages().is_empty());
        for (connected, passages) in [
            (vec![one("s", true)], vec![]),
            (vec![one("a", true)], vec![]),
            (vec![], vec![one("w", false)]),
            (vec![one("n", true)], vec![one("n", true)]),
        ] {
            assert!(request().with_connections(connected, passages).is_err());
        }
        let grown =
            CoverageRequest::try_new(id("s"), EffectReach::Grown, 1.0, vec![], vec![]).unwrap();
        assert!(
            grown
                .clone()
                .with_connections(vec![one("n", true)], vec![])
                .is_err()
        );
        assert!(grown.with_connections(vec![], vec![]).is_ok());
    }

    #[test]
    fn an_answer_must_fit_its_request() {
        let request = CoverageRequest::try_new(
            id("s"),
            EffectReach::Grown,
            1.0,
            vec![Participant::new(id("a"), true)],
            vec![],
        )
        .unwrap();
        assert!(CoverageEvidence::try_new(id("s"), area(1.0), area(2.0), vec![]).is_err());
        let wrong = CoverageEvidence::try_new(id("s"), area(1.0), area(0.5), vec![]).unwrap();
        assert!(check_answer(&request, &wrong).is_err());
        let right = CoverageEvidence::try_new(
            id("s"),
            area(1.0),
            area(0.5),
            vec![(id("a"), EffectMeets::Surely)],
        )
        .unwrap();
        assert!(check_answer(&request, &right).is_ok());
    }
}