diff --git a/crates/weksa/src/inventory.rs b/crates/weksa/src/inventory.rs index c9d69a2..d5f2e81 100644 --- a/crates/weksa/src/inventory.rs +++ b/crates/weksa/src/inventory.rs @@ -228,10 +228,21 @@ impl InventoryEntry { } /// Whether the entry's shape matches its family: features carry a value kind - /// and slots, extensions a value kind and no slots, nothing else either. + /// and slots, extensions a value kind and no slots, nothing else either. A + /// feature of slot `Stance` or `Scene` sits in that slot alone: each + /// speaker-state layer has one writer and one lifetime, so its features sit + /// nowhere else. pub fn well_formed(&self) -> bool { match self.family() { - Family::Feature => self.value_kind.is_some() && !self.applies_to.is_empty(), + Family::Feature => { + self.value_kind.is_some() + && !self.applies_to.is_empty() + && (self.applies_to.len() == 1 + || !self + .applies_to + .iter() + .any(|s| matches!(s, Slot::Stance | Slot::Scene))) + } Family::Extension => self.value_kind.is_some() && self.applies_to.is_empty(), _ => self.value_kind.is_none() && self.applies_to.is_empty(), } diff --git a/crates/weksa/src/validate.rs b/crates/weksa/src/validate.rs index 6a0075f..e5a917e 100644 --- a/crates/weksa/src/validate.rs +++ b/crates/weksa/src/validate.rs @@ -3,10 +3,9 @@ //! returns `Ok(())` or the refusals. [`LoweringInput`], the only thing a //! lowerer sees, comes from `PacketStore::admit` alone, which builds it through //! the crate-private `admissible`. The stance is checked like every feature -//! list, in slot `Stance`. The rules are V1..V8 and V10..V15: the shape of an id -//! or a grade is its type's parse, not a rule, so V9 is retired and not -//! renumbered. V13 guards the one other way features reach a lowering view: -//! [`LoweringInput::with_scene`]. +//! list, in slot `Stance`. The rules are V1..V8, V10..V12 and V14..V15: the +//! shape of an id or a grade is its type's parse, not a rule, so V9 is retired +//! and not renumbered; V13 is retired too. //! //! `validate` owns form and capacity and makes no leak decision. A packet's one //! free text is its trace (`notes` and `rejected_readings`): `validate` does not @@ -95,10 +94,6 @@ pub enum Refusal { count: usize, max: usize, }, - /// V13: scene features were given for a packet with no addressee. They - /// state the speaker's relation to the addressee, so with none they - /// describe nobody. - V13SceneWithoutAddressee, /// V14: an addressee consent that no addressee can have given, or that /// names a node that is not a predication. It is what the addressee asked /// for or agreed to, an act, so a referent cannot be its object. @@ -130,7 +125,6 @@ impl Refusal { | Refusal::V11DuplicateNodeRef { .. } | Refusal::V11DuplicateRole { .. } => "V11", Refusal::V12TooManyNodes { .. } => "V12", - Refusal::V13SceneWithoutAddressee => "V13", Refusal::V14ConsentWithoutAddressee | Refusal::V14ConsentNotAPredication { .. } => { "V14" } @@ -157,7 +151,6 @@ impl fmt::Display for Refusal { Refusal::V11DuplicateNodeRef { .. } => "a node id repeats in one list", Refusal::V11DuplicateRole { .. } => "a role id repeats in one predication", Refusal::V12TooManyNodes { .. } => "too many nodes", - Refusal::V13SceneWithoutAddressee => "scene features given without an addressee", Refusal::V14ConsentWithoutAddressee => "consent given without an addressee", Refusal::V14ConsentNotAPredication { .. } => "consent names no predication", Refusal::V15ConsentSource { .. } => "consent set by a source that only read wording", @@ -594,40 +587,6 @@ impl LoweringInput { } } - /// Adds the speaker's relation to the addressee: feature values of slot - /// `Scene`, checked like every feature list (V5 to V8, V11) at this view's - /// inventory revision. They state a relation, so the packet must name an - /// addressee (V13). Typed ids and grades only, never wording, so a scene - /// carries no text to the lowerer. The scene is not part of the packet: a - /// scene document supplies it, and replacing it replaces the earlier one. - pub fn with_scene( - mut self, - inventory: &Inventory, - scene: Vec, - ) -> Result> { - let mut out = Vec::new(); - if !inventory.has_revision(self.inventory_rev) { - out.push(Refusal::V10UnknownRevision { - rev: self.inventory_rev, - }); - } else { - Inventoried { - inv: inventory, - rev: self.inventory_rev, - out: &mut out, - } - .features(&scene, Slot::Scene); - } - if !scene.is_empty() && self.discourse.addressee.is_none() { - out.push(Refusal::V13SceneWithoutAddressee); - } - if !out.is_empty() { - return Err(out); - } - self.scene = scene; - Ok(self) - } - pub fn inventory_rev(&self) -> u32 { self.inventory_rev } diff --git a/crates/weksa/tests/common/mod.rs b/crates/weksa/tests/common/mod.rs index 60ba544..9368227 100644 --- a/crates/weksa/tests/common/mod.rs +++ b/crates/weksa/tests/common/mod.rs @@ -34,7 +34,8 @@ pub const CONCEPT_LATE: &str = "concept:4"; pub const FRAME: &str = "frame:1"; /// An extension entry of value kind Grade, introduced at rev 2. pub const EXT: &str = "extension:1"; -/// A Grade feature introduced at rev 2 that sits in every slot. +/// A Grade feature introduced at rev 2 in every content slot (Discourse, +/// Referent, Predication, Relation). pub const FEAT_ALL: &str = "feature:50"; pub const PACKET_ID: &str = "packet:0123456789abcdef"; @@ -218,7 +219,6 @@ pub fn world() -> World { Slot::Referent, Slot::Predication, Slot::Relation, - Slot::Stance, ], ) }, diff --git a/crates/weksa/tests/display.rs b/crates/weksa/tests/display.rs index 3c1ba63..44063fa 100644 --- a/crates/weksa/tests/display.rs +++ b/crates/weksa/tests/display.rs @@ -117,7 +117,6 @@ fn refusal_display_names_the_rule_and_no_identifier() { count: 4242, max: 4242, }, - Refusal::V13SceneWithoutAddressee, Refusal::V14ConsentWithoutAddressee, Refusal::V14ConsentNotAPredication { id: node() }, Refusal::V15ConsentSource { diff --git a/crates/weksa/tests/scene.rs b/crates/weksa/tests/scene.rs deleted file mode 100644 index 0fc4168..0000000 --- a/crates/weksa/tests/scene.rs +++ /dev/null @@ -1,156 +0,0 @@ -//! The speaker's relation to the addressee as a lowering input: typed feature -//! values of slot `Scene`, checked against the inventory at the view's revision, -//! never wording, and only for a packet that names an addressee. - -mod common; -use std::path::Path; - -use common::{grade, iv, lab}; -use weksa::inventory::{proof_revision, scene_revision, seed}; -use weksa::packet::{FeatValue, FeatureValue, Packet, proof_packet}; -use weksa::{ - CorpusStore, EntityStore, Inventory, InventoryStore, LoweringInput, PacketStore, Refusal, lower, -}; - -fn store() -> (InventoryStore, Inventory, tempfile::TempDir) { - let dir = tempfile::tempdir().unwrap(); - let mut store = InventoryStore::open(&dir.path().join("i.cc")).unwrap(); - for (rev, entries) in [seed(), proof_revision(), scene_revision()] { - store.admit_revision(rev, entries).unwrap(); - } - let inv = store.inventory().unwrap(); - (store, inv, dir) -} - -fn admitted(packet: &Packet, inventory: &InventoryStore) -> LoweringInput { - let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); - let corpus = CorpusStore::open(&root.join("data/corpus/corpus.cc")).unwrap(); - let entities = EntityStore::open(&root.join("data/entity/entity.cc")).unwrap(); - let dir = tempfile::tempdir().unwrap(); - PacketStore::open(&dir.path().join("p.cc")) - .unwrap() - .admit(packet.clone(), inventory, &corpus, &entities) - .unwrap() -} - -fn at_rev(rev: u32) -> Packet { - let mut p = proof_packet(); - p.inventory_rev = rev; - p -} - -fn feat(id: &str, value: FeatValue) -> FeatureValue { - FeatureValue { - feature: iv(id), - value, - } -} - -fn status(inv: &Inventory) -> String { - inv.entries() - .find(|e| e.gloss == "relative_status") - .unwrap() - .id - .to_string() -} - -fn id_of(inv: &Inventory, gloss: &str) -> String { - inv.entries() - .find(|e| e.gloss == gloss) - .unwrap() - .id - .to_string() -} - -fn rules(r: Result>) -> Vec<&'static str> { - r.unwrap_err().iter().map(Refusal::rule).collect() -} - -#[test] -fn a_scene_relation_reaches_the_lowering_view_and_changes_no_english() { - let (store, inv, _dir) = store(); - let plain = admitted(&at_rev(3), &store); - assert!(plain.scene().is_empty()); - let scene = vec![ - feat(&status(&inv), FeatValue::Option(lab("above"))), - feat(&id_of(&inv, "intimacy"), FeatValue::Grade(grade(200))), - feat(&id_of(&inv, "in_group"), FeatValue::Flag(false)), - ]; - let with = plain.clone().with_scene(&inv, scene.clone()).unwrap(); - assert_eq!(with.scene(), scene.as_slice()); - let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); - let entities = EntityStore::open(&root.join("data/entity/entity.cc")).unwrap(); - assert_eq!( - lower(&with, &inv, &entities).unwrap(), - lower(&plain, &inv, &entities).unwrap() - ); -} - -#[test] -fn a_relation_needs_an_addressee() { - let (store, inv, _dir) = store(); - let mut p = at_rev(3); - p.discourse.addressee = None; - let view = admitted(&p, &store); - let scene = vec![feat(&id_of(&inv, "in_group"), FeatValue::Flag(true))]; - assert_eq!(rules(view.clone().with_scene(&inv, scene)), ["V13"]); - assert!(view.with_scene(&inv, vec![]).unwrap().scene().is_empty()); -} - -#[test] -fn a_relation_value_is_checked_like_any_feature() { - let (store, inv, _dir) = store(); - let view = admitted(&at_rev(3), &store); - let status = status(&inv); - let intimacy = id_of(&inv, "intimacy"); - let take = |scene| view.clone().with_scene(&inv, scene); - // A label the feature does not declare. - assert_eq!( - rules(take(vec![feat(&status, FeatValue::Option(lab("lordly")))])), - ["V7"] - ); - // A value of the wrong kind. - assert_eq!( - rules(take(vec![feat(&intimacy, FeatValue::Flag(true))])), - ["V7"] - ); - // A feature declared for another slot: seed feature 1 sits in Predication. - assert_eq!( - rules(take(vec![feat( - "feature:1", - FeatValue::Option(lab("negative")) - )])), - ["V8"] - ); - // An id that is not a feature at all. - assert_eq!( - rules(take(vec![feat("concept:3", FeatValue::Flag(true))])), - ["V6"] - ); - // Twice in one list. - let twice = vec![ - feat(&intimacy, FeatValue::Grade(grade(1))), - feat(&intimacy, FeatValue::Grade(grade(2))), - ]; - assert_eq!(rules(take(twice)), ["V11"]); -} - -#[test] -fn a_view_of_a_revision_before_the_scene_vocabulary_cannot_carry_one() { - let (store, inv, _dir) = store(); - // Revision 2 predates the Scene features, so their ids do not resolve there. - let view = admitted(&at_rev(2), &store); - let scene = vec![feat(&id_of(&inv, "intimacy"), FeatValue::Grade(grade(1)))]; - assert_eq!(rules(view.with_scene(&inv, scene)), ["V5"]); -} - -#[test] -fn a_stance_feature_is_not_a_scene_feature() { - let (store, inv, _dir) = store(); - let view = admitted(&at_rev(3), &store); - let anger = id_of(&inv, "anger"); - assert_eq!( - rules(view.with_scene(&inv, vec![feat(&anger, FeatValue::Grade(grade(1)))])), - ["V8"] - ); -} diff --git a/crates/weksa/tests/stores.rs b/crates/weksa/tests/stores.rs index 22ecb83..fbcce9a 100644 --- a/crates/weksa/tests/stores.rs +++ b/crates/weksa/tests/stores.rs @@ -1093,6 +1093,46 @@ fn revision_change_rules() { assert!(inv.entry(&iv("role:1")).unwrap().retired_rev.is_none()); } +#[test] +fn speaker_state_slots_are_exclusive() { + let shape = "entry shape does not match its family"; + let d = dir(); + let mut store = seeded(&d.path().join("i.cc")); + for slots in [ + &[Slot::Stance, Slot::Discourse][..], + &[Slot::Scene, Slot::Relation], + &[Slot::Discourse, Slot::Scene], + &[Slot::Scene, Slot::Scene], + &[Slot::Stance, Slot::Stance], + ] { + let rev = rev_with(vec![Change::Add(iv("feature:50"))], 2, Some(1)); + let entries = vec![InventoryEntry { + introduced_rev: 2, + ..feature("feature:50", ValueKind::Flag, slots) + }]; + assert_eq!( + refusal(store.admit_revision(rev, entries)), + malformed("feature:50", shape), + "{slots:?}" + ); + } + assert_eq!(store.inventory().unwrap().latest().unwrap().n, 1); + // One slot alone, and several content slots, are admitted. + for (n, slots) in [ + (2, &[Slot::Scene][..]), + (3, &[Slot::Stance]), + (4, &[Slot::Discourse, Slot::Predication]), + ] { + let id = format!("feature:{}", 50 + n); + let rev = rev_with(vec![Change::Add(iv(&id))], n, Some(n - 1)); + let entries = vec![InventoryEntry { + introduced_rev: n, + ..feature(&id, ValueKind::Flag, slots) + }]; + store.admit_revision(rev, entries).unwrap(); + } +} + #[test] fn a_well_formed_extension_entry_is_admitted() { let d = dir(); diff --git a/crates/weksa/tests/validate.rs b/crates/weksa/tests/validate.rs index aaa87bd..c5bc172 100644 --- a/crates/weksa/tests/validate.rs +++ b/crates/weksa/tests/validate.rs @@ -520,9 +520,6 @@ fn v11_duplicate_feature_each_slot() { refusals(&relation), vec![v11(Some(Slot::Relation), FEAT_ALL)] ); - let mut stance = good(); - stance.stance.features = vec![grade_feature(1), grade_feature(2)]; - assert_eq!(refusals(&stance), vec![v11(Some(Slot::Stance), FEAT_ALL)]); let mut felt = good(); felt.stance .features