qualia_core_db/modalities/argumentation/
bipolar.rs1use super::{ArgumentationFramework, Attack, AttackType};
8use std::collections::HashSet;
9
10#[derive(Debug, Clone)]
12pub struct BipolarFramework {
13 pub af: ArgumentationFramework,
14 pub supports: Vec<(u64, u64)>,
16}
17
18impl BipolarFramework {
19 pub fn new(af: ArgumentationFramework) -> Self {
21 Self {
22 af,
23 supports: Vec::new(),
24 }
25 }
26
27 pub fn add_support(&mut self, supporter: u64, supported: u64) {
29 self.supports.push((supporter, supported));
30 }
31
32 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 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 for &a in &ids {
74 if a != b && self.support_reaches(a, b) {
75 out.add_attack(mk(a, c));
76 }
77 }
78 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 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 let mut af = ArgumentationFramework::new();
109 af.add_argument(arg(1)); af.add_argument(arg(2)); af.add_argument(arg(3)); 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); assert!(bf.support_reaches(1, 2));
122
123 let dung = bf.to_dung();
124 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 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}