Skip to main content

qualia_core_db/modalities/argumentation/
bipolar.rs

1//! Bipolar Argumentation (Cayrol & Lagasquie-Schiex) — adds a **support** relation alongside
2//! attack and derives the *complex attacks* it induces (deductive-support semantics):
3//!   * **supported attack:**  a supports* b  ∧  b attacks c   ⇒   a attacks c
4//!   * **secondary attack:**  a attacks b    ∧  c supports* b  ⇒   a attacks c
5//! Projecting these derived attacks back into a Dung framework lets the standard semantics apply.
6
7use super::{ArgumentationFramework, Attack, AttackType};
8use std::collections::HashSet;
9
10/// A Dung framework augmented with a binary support relation.
11#[derive(Debug, Clone)]
12pub struct BipolarFramework {
13    pub af: ArgumentationFramework,
14    /// `(supporter, supported)` edges.
15    pub supports: Vec<(u64, u64)>,
16}
17
18impl BipolarFramework {
19    /// Creates `BipolarFramework` with the given state.
20    pub fn new(af: ArgumentationFramework) -> Self {
21        Self {
22            af,
23            supports: Vec::new(),
24        }
25    }
26
27    /// Add a support edge `supporter → supported`.
28    pub fn add_support(&mut self, supporter: u64, supported: u64) {
29        self.supports.push((supporter, supported));
30    }
31
32    /// Is there a support path `from →…→ to` of length ≥ 1?
33    pub fn support_reaches(&self, from: u64, to: u64) -> bool {
34        let mut stack = vec![from];
35        let mut seen = HashSet::new();
36        seen.insert(from);
37        while let Some(x) = stack.pop() {
38            for &(s, t) in &self.supports {
39                if s == x {
40                    if t == to {
41                        return true;
42                    }
43                    if seen.insert(t) {
44                        stack.push(t);
45                    }
46                }
47            }
48        }
49        false
50    }
51
52    /// Project to a Dung framework whose attacks are the original attacks PLUS the derived
53    /// supported and secondary attacks. Duplicate edges are harmless to the semantics.
54    pub fn to_dung(&self) -> ArgumentationFramework {
55        let mk = |a: u64, b: u64| Attack {
56            attacker: a,
57            target: b,
58            attack_type: AttackType::Rebuttal,
59            strength: 1.0,
60        };
61        let mut out = ArgumentationFramework::new();
62        for arg in self.af.arguments.values() {
63            out.add_argument(arg.clone());
64        }
65        for atk in &self.af.attacks {
66            out.add_attack(atk.clone());
67        }
68        let ids: Vec<u64> = self.af.arguments.keys().copied().collect();
69
70        for atk in &self.af.attacks {
71            let (b, c) = (atk.attacker, atk.target);
72            // supported attack: a supports* b ∧ b attacks c ⇒ a attacks c
73            for &a in &ids {
74                if a != b && self.support_reaches(a, b) {
75                    out.add_attack(mk(a, c));
76                }
77            }
78            // secondary attack: a attacks b ∧ c supports* b ⇒ a attacks c
79            let (a, bb) = (atk.attacker, atk.target);
80            for &c in &ids {
81                if c != bb && self.support_reaches(c, bb) {
82                    out.add_attack(mk(a, c));
83                }
84            }
85        }
86        out
87    }
88
89    /// Grounded extension over the derived (complex-attack) Dung framework.
90    pub fn grounded_extension(&self) -> HashSet<u64> {
91        self.to_dung().grounded_extension()
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::super::{Argument, Attack, AttackType};
98    use super::*;
99    use crate::NQuin;
100
101    fn arg(id: u64) -> Argument {
102        Argument::new(id, String::new(), Vec::new(), NQuin::default())
103    }
104
105    #[test]
106    fn support_induces_a_supported_attack() {
107        // a supports b; b attacks c. Deductive support ⇒ a attacks c.
108        let mut af = ArgumentationFramework::new();
109        af.add_argument(arg(1)); // a
110        af.add_argument(arg(2)); // b
111        af.add_argument(arg(3)); // c
112        af.add_attack(Attack {
113            attacker: 2,
114            target: 3,
115            attack_type: AttackType::Rebuttal,
116            strength: 1.0,
117        });
118
119        let mut bf = BipolarFramework::new(af);
120        bf.add_support(1, 2); // a supports b
121        assert!(bf.support_reaches(1, 2));
122
123        let dung = bf.to_dung();
124        // The derived framework contains a→c (the supported attack).
125        assert!(
126            dung.attacks
127                .iter()
128                .any(|atk| atk.attacker == 1 && atk.target == 3),
129            "a supports b, b attacks c ⇒ a attacks c"
130        );
131        // c has attackers {2,3-side}; with a supporting b, c is not accepted.
132        let g = bf.grounded_extension();
133        assert!(
134            !g.contains(&3),
135            "c is defeated through the support-backed attack"
136        );
137    }
138
139    #[test]
140    fn support_paths_are_transitive() {
141        let af = {
142            let mut a = ArgumentationFramework::new();
143            a.add_argument(arg(1));
144            a.add_argument(arg(2));
145            a.add_argument(arg(3));
146            a
147        };
148        let mut bf = BipolarFramework::new(af);
149        bf.add_support(1, 2);
150        bf.add_support(2, 3);
151        assert!(bf.support_reaches(1, 3), "support is transitive (1→2→3)");
152        assert!(!bf.support_reaches(3, 1));
153    }
154}