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
// Copyright (c) 2022 Yegor Bugayenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::edge::Edge;
use crate::hex::Hex;
use crate::Sodg;
use crate::Vertex;
use anyhow::{anyhow, Context, Result};
use log::trace;
use std::collections::VecDeque;
use std::str::FromStr;

impl Sodg {
    /// Add a new vertex `v1` to the Sodg:
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut sodg = Sodg::empty();
    /// sodg.add(0).unwrap();
    /// sodg.add(42).unwrap();
    /// sodg.bind(0, 42, "hello").unwrap();
    /// ```
    pub fn add(&mut self, v1: u32) -> Result<()> {
        if self.vertices.contains_key(&v1) {
            return Err(anyhow!("Vertex ν{} already exists", v1));
        }
        self.vertices.insert(v1, Vertex::empty());
        self.validate(vec![v1])?;
        trace!("#add(ν{}): new vertex added", v1);
        Ok(())
    }

    /// Makes an edge `e1` from vertex `v1` to vertex `v2` and puts `a` label on it. If the
    /// label is not equal to `"ρ"`, makes two backward edges from `v2` to `v1`
    /// and label them as `"ρ"` an `"𝜎"`.
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut sodg = Sodg::empty();
    /// sodg.add(0).unwrap();
    /// sodg.add(42).unwrap();
    /// sodg.bind(0, 42, "forward").unwrap();
    /// sodg.bind(42, 0, "backward").unwrap();
    /// ```
    pub fn bind(&mut self, v1: u32, v2: u32, a: &str) -> Result<()> {
        if v1 == v2 {
            return Err(anyhow!(
                "An edge can't depart from ν{} and arrive to itself",
                v1
            ));
        }
        if a.is_empty() {
            return Err(anyhow!(
                "Edge label can't be empty, from ν{} to ν{}",
                v1,
                v2
            ));
        }
        if !self.vertices.contains_key(&v2) {
            return Err(anyhow!("Can't arrive to ν{}, it's absent", v2));
        }
        let vtx1 = self
            .vertices
            .get_mut(&v1)
            .context(format!("Can't depart from ν{}, it's absent", v1))?;
        vtx1.edges.retain(|e| e.a != a);
        vtx1.edges.push(Edge::new(v2, a));
        self.validate(vec![v1, v2])?;
        trace!("#bind: edge added ν{}-{}->ν{}", v1, a, v2);
        Ok(())
    }

    /// Set vertex data.
    ///
    /// ```
    /// use sodg::hex::Hex;
    /// use sodg::Sodg;
    /// let mut sodg = Sodg::empty();
    /// sodg.add(42).unwrap();
    /// sodg.put(42, Hex::from_str("hello, world!")).unwrap();
    /// ```
    pub fn put(&mut self, v: u32, d: Hex) -> Result<()> {
        let vtx = self
            .vertices
            .get_mut(&v)
            .context(format!("Can't find ν{}", v))?;
        vtx.data = d.clone();
        self.validate(vec![v])?;
        trace!("#data: data of ν{} set to {}", v, d);
        Ok(())
    }

    /// Read vertex data.
    ///
    /// ```
    /// use sodg::hex::Hex;
    /// use sodg::Sodg;
    /// let mut sodg = Sodg::empty();
    /// sodg.add(42).unwrap();
    /// let data = Hex::from_str("hello, world!");
    /// sodg.put(42, data.clone()).unwrap();
    /// assert_eq!(data, sodg.data(42).unwrap());
    /// ```
    pub fn data(&self, v: u32) -> Result<Hex> {
        let vtx = self
            .vertices
            .get(&v)
            .context(format!("Can't find ν{}", v))?;
        Ok(vtx.data.clone())
    }

    /// Find all kids of a vertex.
    pub fn kids(&self, v: u32) -> Result<Vec<(String, u32)>> {
        let vtx = self.vertices.get(&v).context(format!("Can't find ν{v}"))?;
        Ok(vtx.edges.iter().map(|x| (x.a.clone(), x.to)).collect())
    }

    /// Find a kid of a vertex, by its edge name.
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut sodg = Sodg::empty();
    /// sodg.add(0).unwrap();
    /// sodg.add(42).unwrap();
    /// sodg.bind(0, 42, "k").unwrap();
    /// assert_eq!(42, sodg.kid(0, "k").unwrap());
    /// assert!(sodg.kid(0, "another").is_none());
    /// ```
    pub fn kid(&self, v: u32, a: &str) -> Option<u32> {
        self.vertices
            .get(&v)
            .context(format!("Can't find ν{v}"))
            .unwrap()
            .edges
            .iter()
            .find(|e| e.a == a)
            .map(|e| e.to)
    }

    /// Find a vertex in the Sodg by its locator.
    ///
    /// ```
    /// use sodg::Sodg;
    /// let mut sodg = Sodg::empty();
    /// sodg.add(0).unwrap();
    /// sodg.add(1).unwrap();
    /// sodg.bind(0, 1, "a").unwrap();
    /// sodg.add(2).unwrap();
    /// sodg.bind(1, 2, "b").unwrap();
    /// assert_eq!(2, sodg.find(0, "a.b").unwrap());
    /// ```
    pub fn find(&self, v1: u32, loc: &str) -> Result<u32> {
        let mut v = v1;
        let mut locator: VecDeque<String> = VecDeque::new();
        loc.split('.')
            .filter(|k| !k.is_empty())
            .for_each(|k| locator.push_back(k.to_string()));
        loop {
            let next = locator.pop_front();
            if next.is_none() {
                trace!("#find: end of locator, we are at ν{v}");
                break;
            }
            let k = next.unwrap().to_string();
            if k.is_empty() {
                return Err(anyhow!("System error, the locator is empty"));
            }
            if k.starts_with('ν') {
                let num: String = k.chars().skip(1).collect::<Vec<_>>().into_iter().collect();
                v = u32::from_str(num.as_str())?;
                trace!("#find: jumping directly to ν{v}");
                continue;
            }
            if let Some(to) = self.kid(v, k.as_str()) {
                trace!("#find: ν{v}.{k} -> ν{to}");
                v = to;
                continue;
            };
            let others: Vec<String> = self
                .vertices
                .get(&v)
                .context(format!("Can't find ν{v}"))
                .unwrap()
                .edges
                .iter()
                .map(|e| e.a.clone())
                .collect();
            return Err(anyhow!(
                "Can't find .{} in ν{} among other {} attribute{}: {}",
                k,
                v,
                others.len(),
                if others.len() == 1 { "" } else { "s" },
                others.join(", ")
            ));
        }
        trace!("#find: found ν{v1} by '{loc}'");
        Ok(v)
    }

    /// Check all alerts.
    fn validate(&self, vx: Vec<u32>) -> Result<()> {
        if self.alerts_active {
            for a in self.alerts.iter() {
                let msgs = a(self, vx.clone());
                if !msgs.is_empty() {
                    return Err(anyhow!("{}", msgs.join("; ")));
                }
            }
        }
        Ok(())
    }
}

#[test]
fn adds_simple_vertex() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    assert_eq!(1, g.find(1, "")?);
    Ok(())
}

#[test]
fn binds_simple_vertices() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    let k = "hello";
    g.bind(1, 2, k)?;
    assert_eq!(2, g.find(1, k)?);
    Ok(())
}

#[test]
fn pre_defined_ids() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    let k = "a-привет";
    g.bind(1, 2, k)?;
    assert_eq!(2, g.find(1, k)?);
    Ok(())
}

#[test]
fn binds_two_names() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    g.bind(1, 2, "first")?;
    g.bind(1, 2, "second")?;
    assert_eq!(2, g.find(1, "first")?);
    Ok(())
}

#[test]
fn overwrites_edge() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(1)?;
    g.add(2)?;
    let label = "hello";
    g.bind(1, 2, label)?;
    g.add(3)?;
    g.bind(1, 3, label)?;
    assert_eq!(3, g.find(1, label)?);
    Ok(())
}

#[test]
fn binds_to_root() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "x")?;
    assert!(g.kid(0, "ρ").is_none());
    assert!(g.kid(0, "σ").is_none());
    Ok(())
}

#[test]
fn sets_simple_data() -> Result<()> {
    let mut g = Sodg::empty();
    let data = Hex::from_str("hello");
    g.add(0)?;
    g.put(0, data.clone())?;
    assert_eq!(data, g.data(0)?);
    Ok(())
}

#[test]
fn finds_root() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    assert_eq!(0, g.find(0, "")?);
    Ok(())
}

#[test]
fn finds_all_kids() -> Result<()> {
    let mut g = Sodg::empty();
    g.add(0)?;
    g.add(1)?;
    g.bind(0, 1, "one")?;
    g.bind(0, 1, "two")?;
    assert_eq!(2, g.kids(0)?.iter().count());
    Ok(())
}

#[test]
fn panic_on_simple_alert() -> Result<()> {
    let mut g = Sodg::empty();
    g.alert_on(|_, _| vec![format!("{}", "oops")]);
    assert!(g.add(0).is_err());
    Ok(())
}

#[test]
fn dont_panic_when_alerts_disabled() -> Result<()> {
    let mut g = Sodg::empty();
    g.alert_on(|_, _| vec!["should never happen".to_string()]);
    g.alerts_off();
    assert!(!g.add(0).is_err());
    Ok(())
}

#[test]
fn panic_on_complex_alert() -> Result<()> {
    let mut g = Sodg::empty();
    g.alert_on(|_, vx| {
        let v = 42;
        if vx.contains(&v) {
            vec![format!("Vertex no.{v} is not allowed")]
        } else {
            vec![]
        }
    });
    assert!(g.add(42).is_err());
    Ok(())
}