antecedent_core/query/
counterfactual.rs1use std::sync::Arc;
6
7use crate::ids::VariableId;
8use crate::intervention::Intervention;
9
10use super::error::QueryError;
11
12#[derive(Clone, Debug, PartialEq)]
13pub struct CounterfactualQuery {
15 pub outcomes: Arc<[VariableId]>,
17 pub interventions: Arc<[Intervention]>,
19 pub allow_nested: bool,
21}
22
23impl CounterfactualQuery {
24 #[must_use]
26 pub fn new(outcome: VariableId, interventions: impl Into<Arc<[Intervention]>>) -> Self {
27 Self {
28 outcomes: Arc::from([outcome]),
29 interventions: interventions.into(),
30 allow_nested: false,
31 }
32 }
33
34 #[must_use]
36 pub const fn with_nested(mut self, allow_nested: bool) -> Self {
37 self.allow_nested = allow_nested;
38 self
39 }
40
41 pub fn validate(&self) -> Result<(), QueryError> {
47 if self.outcomes.is_empty() {
48 return Err(QueryError::EmptyCounterfactualOutcomes);
49 }
50 for iv in self.interventions.iter() {
51 iv.validate().map_err(|e| QueryError::InvalidIntervention(e.to_string()))?;
52 }
53 Ok(())
54 }
55}