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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use super::{
    debug_wrapper, BaseNode, BaseNodeTrait, CommonNodeTrait, InheritanceNode, InheritanceNodeTrait,
};
use crate::graph::value_wrappers::KBValue;
use std::cmp::{Eq, PartialEq};
use std::convert::TryFrom;
use std::fmt;
use std::fmt::{Debug, Formatter};
use std::hash::Hash;
use std::rc::Rc;

/// Final node wrapper that offers a stable API for all concept abstractions dependent on it.
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct FinalNode {
    inode: InheritanceNode,
}

#[allow(clippy::new_without_default)]
impl FinalNode {
    /// Create a new node.
    pub fn new() -> Self {
        FinalNode {
            inode: InheritanceNode::new(),
        }
    }

    /// Create a new node with an inheritance relation.
    pub fn new_with_inheritance(type_id: usize) -> Self {
        Self::from(InheritanceNode::new_with_inheritance(type_id))
    }

    /// Leak inheritance-level functionality.
    pub fn inheritance_wrapper(&self) -> &InheritanceNode {
        &self.inode
    }

    /// Leak base-level functionality.
    pub fn base_wrapper(&self) -> &BaseNode {
        &self.inode.base_wrapper()
    }
}

impl From<usize> for FinalNode {
    fn from(id: usize) -> Self {
        FinalNode {
            inode: InheritanceNode::from(id),
        }
    }
}

impl From<BaseNode> for FinalNode {
    fn from(b: BaseNode) -> Self {
        FinalNode {
            inode: InheritanceNode::from(b),
        }
    }
}

impl From<InheritanceNode> for FinalNode {
    fn from(b: InheritanceNode) -> Self {
        FinalNode { inode: b }
    }
}

impl<'a> TryFrom<&'a str> for FinalNode {
    type Error = String;

    fn try_from(name: &'a str) -> Result<Self, Self::Error> {
        InheritanceNode::try_from(name).map(|n| FinalNode { inode: n })
    }
}

impl Debug for FinalNode {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        debug_wrapper("FWrapper", self, f)
    }
}

impl CommonNodeTrait for FinalNode {
    fn id(&self) -> usize {
        self.inode.id()
    }

    fn set_internal_name(&mut self, name: String) {
        self.inode.set_internal_name(name);
    }

    fn internal_name(&self) -> Option<Rc<String>> {
        self.inode.internal_name()
    }
}

impl BaseNodeTrait<FinalNode> for FinalNode {
    fn set_value(&mut self, value: Rc<dyn KBValue>) {
        self.inode.set_value(value)
    }

    fn value(&self) -> Option<Rc<dyn KBValue>> {
        self.inode.value()
    }

    fn add_outgoing(&mut self, edge_type: usize, to: &FinalNode) {
        self.inode.add_outgoing(edge_type, &to.inode)
    }

    fn add_incoming(&mut self, edge_type: usize, from: &FinalNode) {
        self.inode.add_incoming(edge_type, &from.inode)
    }

    fn has_outgoing(&self, edge_type: usize, to: &FinalNode) -> bool {
        self.inode.has_outgoing(edge_type, &to.inode)
    }

    fn has_incoming(&self, edge_type: usize, from: &FinalNode) -> bool {
        self.inode.has_incoming(edge_type, &from.inode)
    }

    fn outgoing_nodes(&self, edge_type: usize) -> Vec<FinalNode> {
        self.inode
            .outgoing_nodes(edge_type)
            .into_iter()
            .map(FinalNode::from)
            .collect()
    }

    fn incoming_nodes(&self, edge_type: usize) -> Vec<FinalNode> {
        self.inode
            .incoming_nodes(edge_type)
            .into_iter()
            .map(FinalNode::from)
            .collect()
    }
}

impl InheritanceNodeTrait<FinalNode> for FinalNode {
    fn inheritance_nodes(&self) -> Vec<FinalNode> {
        self.inode
            .inheritance_nodes()
            .into_iter()
            .map(FinalNode::from)
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::concepts::attributes::{Inherits, Owner};
    use crate::concepts::{initialize_kb, ArchetypeTrait};
    use crate::graph::value_wrappers::{unwrap_weak, WeakValue};

    #[test]
    fn create_and_retrieve_node_id() {
        initialize_kb();
        let node1 = FinalNode::new();
        let node2 = FinalNode::new();
        assert_eq!(node1.id() + 1, node2.id());
    }

    #[test]
    fn from_node_id() {
        initialize_kb();
        let node = FinalNode::new();
        let node_copy = FinalNode::from(node.id());
        assert_eq!(node.id(), node_copy.id());
    }

    #[test]
    fn from_name() {
        initialize_kb();
        let mut node = FinalNode::new();
        node.set_internal_name("A".to_string());
        assert_eq!(FinalNode::try_from("A"), Ok(node));
        assert!(FinalNode::try_from("B").is_err());
    }

    #[test]
    fn create_and_retrieve_node_name() {
        initialize_kb();
        let mut node = FinalNode::new();
        node.set_internal_name("A".to_string());
        assert_eq!(node.internal_name(), Some(Rc::new("A".to_string())));
    }

    #[test]
    fn retrieve_node_value() {
        initialize_kb();
        let mut node = FinalNode::new();
        let v = Rc::new(5);
        node.set_value(Rc::new(WeakValue::new(&v)));
        assert_eq!(unwrap_weak::<i32>(node.value()), Some(v));
    }

    #[test]
    fn create_with_inheritance() {
        initialize_kb();
        let owner = FinalNode::new();
        let mut type1 = FinalNode::new();
        type1.add_outgoing(Owner::TYPE_ID, &owner);
        let node = FinalNode::new_with_inheritance(type1.id());
        assert!(node.has_outgoing(Owner::TYPE_ID, &owner));
    }

    #[test]
    fn check_inheritance_nodes() {
        initialize_kb();
        let type1 = InheritanceNode::new();
        let mut type2 = InheritanceNode::new();
        let mut a = InheritanceNode::new();
        type2.add_outgoing(Inherits::TYPE_ID, &type1);
        a.add_outgoing(Inherits::TYPE_ID, &type2);
        assert_eq!(a.inheritance_nodes(), vec![type1, type2, a]);
        assert_eq!(type2.inheritance_nodes(), vec![type1, type2]);
        assert_eq!(type1.inheritance_nodes(), vec![type1]);
    }

    #[test]
    fn no_outgoing_nodes() {
        initialize_kb();
        let a = FinalNode::new();
        assert_eq!(a.outgoing_nodes(a.id()), Vec::new());
    }

    #[allow(clippy::many_single_char_names)]
    #[test]
    fn outgoing_nodes() {
        initialize_kb();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let c = FinalNode::new();
        let d = FinalNode::new();
        let mut e = FinalNode::new();
        let edge_type1 = FinalNode::new();
        let edge_type2 = FinalNode::new();
        a.add_outgoing(edge_type1.id(), &b);
        a.add_outgoing(edge_type2.id(), &c);
        a.add_outgoing(edge_type1.id(), &d);
        e.add_outgoing(edge_type1.id(), &a);
        assert_eq!(a.outgoing_nodes(edge_type1.id()), vec![b, d]);
    }

    #[test]
    fn inherited_outgoing_nodes() {
        initialize_kb();
        let mut type1 = FinalNode::new();
        let mut type2 = FinalNode::new();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let c = FinalNode::new();
        let mut d = FinalNode::new();
        let edge_type = FinalNode::new();
        a.add_outgoing(edge_type.id(), &b);
        type1.add_outgoing(edge_type.id(), &c);
        type2.add_outgoing(edge_type.id(), &c);
        d.add_outgoing(edge_type.id(), &a);

        type2.add_outgoing(Inherits::TYPE_ID, &type1);
        a.add_outgoing(Inherits::TYPE_ID, &type2);
        assert_eq!(a.outgoing_nodes(edge_type.id()), vec![b, c]);
        assert_eq!(type1.outgoing_nodes(edge_type.id()), vec![c]);
    }

    #[test]
    fn no_incoming_nodes() {
        initialize_kb();
        let a = FinalNode::new();
        assert_eq!(a.incoming_nodes(a.id()), Vec::new());
    }

    #[allow(clippy::many_single_char_names)]
    #[test]
    fn incoming_nodes() {
        initialize_kb();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let c = FinalNode::new();
        let d = FinalNode::new();
        let mut e = FinalNode::new();
        let edge_type1 = FinalNode::new();
        let edge_type2 = FinalNode::new();
        a.add_incoming(edge_type1.id(), &b);
        a.add_incoming(edge_type2.id(), &c);
        a.add_incoming(edge_type1.id(), &d);
        e.add_incoming(edge_type1.id(), &a);
        assert_eq!(a.incoming_nodes(edge_type1.id()), vec![b, d]);
    }

    #[test]
    fn inherited_incoming_nodes() {
        initialize_kb();
        let mut type1 = FinalNode::new();
        let mut type2 = FinalNode::new();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let c = FinalNode::new();
        let mut d = FinalNode::new();
        let edge_type = FinalNode::new();
        a.add_incoming(edge_type.id(), &b);
        type1.add_incoming(edge_type.id(), &c);
        type2.add_incoming(edge_type.id(), &c);
        d.add_incoming(edge_type.id(), &a);

        // inherit links are always outgoing
        type2.add_outgoing(Inherits::TYPE_ID, &type1);
        a.add_outgoing(Inherits::TYPE_ID, &type2);
        assert_eq!(a.incoming_nodes(edge_type.id()), vec![b, c]);
        assert_eq!(type1.incoming_nodes(edge_type.id()), vec![c]);
    }

    #[test]
    fn test_has_outgoing() {
        initialize_kb();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let edge_type1 = FinalNode::new();
        let edge_type2 = FinalNode::new();
        a.add_outgoing(edge_type1.id(), &b);
        assert!(a.has_outgoing(edge_type1.id(), &b));
        assert!(!a.has_outgoing(edge_type2.id(), &b));
        assert!(!b.has_outgoing(edge_type1.id(), &a));
    }

    #[test]
    fn test_has_incoming() {
        initialize_kb();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let edge_type1 = FinalNode::new();
        let edge_type2 = FinalNode::new();
        a.add_incoming(edge_type1.id(), &b);
        assert!(a.has_incoming(edge_type1.id(), &b));
        assert!(!a.has_incoming(edge_type2.id(), &b));
        assert!(!b.has_incoming(edge_type1.id(), &a));
    }

    #[test]
    fn inherited_has_outgoing() {
        initialize_kb();
        let mut type1 = FinalNode::new();
        let mut type2 = FinalNode::new();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let c = FinalNode::new();
        let edge_type = FinalNode::new();
        type1.add_outgoing(edge_type.id(), &b);
        type1.add_incoming(edge_type.id(), &c);

        // inherit links are always outgoing
        type2.add_outgoing(Inherits::TYPE_ID, &type1);
        a.add_outgoing(Inherits::TYPE_ID, &type2);
        assert!(a.has_outgoing(edge_type.id(), &b));
        assert!(!a.has_outgoing(edge_type.id(), &c));
    }

    #[test]
    fn inherited_has_incoming() {
        initialize_kb();
        let mut type1 = FinalNode::new();
        let mut type2 = FinalNode::new();
        let mut a = FinalNode::new();
        let b = FinalNode::new();
        let c = FinalNode::new();
        let edge_type = FinalNode::new();
        type1.add_outgoing(edge_type.id(), &b);
        type1.add_incoming(edge_type.id(), &c);

        // inherit links are always outgoing
        type2.add_outgoing(Inherits::TYPE_ID, &type1);
        a.add_outgoing(Inherits::TYPE_ID, &type2);
        assert!(!a.has_incoming(edge_type.id(), &b));
        assert!(a.has_incoming(edge_type.id(), &c));
    }
}