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
use super::Form;
use crate::node_wrappers::{BaseNodeTrait, FinalNode, InheritanceNodeTrait};
use crate::tao::archetype::{Archetype, ArchetypeTrait, AttributeArchetype};
use crate::tao::relation::attribute::{HasProperty, Inherits};
use crate::tao::Tao;
use crate::Wrapper;
use std::collections::{HashMap, VecDeque};

/// All forms are derived from archetypes. All forms, by their very existence, are capable of the
/// following interactions.
pub trait FormTrait: Wrapper<BaseType = FinalNode> {
    /// Jung called, and you answered. It is time to let go of your individuality and return to
    /// the Oneness from which you once came. There is no life or death, there is no existence or
    /// non-existence, there is no form or abstraction. Forget all preconceptions, blur all
    /// boundaries, be at peace with the universe again.
    fn ego_death(&self) -> Tao {
        Tao::from(*self.essence())
    }

    /// A less severe form of ego-death, where you still remember that you exist.
    fn as_form(&self) -> Form {
        Form::from(*self.essence())
    }

    /// Set a parent archetype. The current archetype will inherit all attributes of the parent
    /// archetype.
    fn add_parent(&mut self, parent: Archetype) {
        self.essence_mut()
            .add_outgoing(Inherits::TYPE_ID, parent.essence());
    }

    /// Get all direct parent archetypes of this concept.
    fn parents(&self) -> Vec<Archetype> {
        let direct_parents: Vec<Archetype> = self
            .essence()
            .outgoing_nodes(Inherits::TYPE_ID)
            .into_iter()
            .filter(|p| p != self.essence())
            .map(Archetype::from)
            .collect();
        let mut specific_parents = Vec::<Archetype>::new();
        for parent in direct_parents {
            if specific_parents.iter().any(|sp| sp.has_ancestor(parent)) {
                continue; // this is not the most specific parent
            }
            specific_parents.retain(|sp| !parent.has_ancestor(*sp));
            specific_parents.push(parent);
        }
        specific_parents
    }

    /// Get the shortest chain of ancestors that leads back to Tao, starting with Tao itself.
    fn ancestry(&self) -> Vec<Archetype> {
        let mut to_be_visited = VecDeque::<Form>::new();
        let mut backpointers = HashMap::<Form, Form>::new();
        to_be_visited.push_back(self.as_form());

        while let Some(next_node) = to_be_visited.pop_front() {
            for parent in next_node.parents() {
                let parent_tao = parent.as_form();
                #[allow(clippy::map_entry)]
                if !backpointers.contains_key(&parent_tao) {
                    backpointers.insert(parent_tao, next_node);
                    to_be_visited.push_back(parent_tao);
                    if parent == Tao::archetype() {
                        break;
                    }
                }
            }
        }

        let mut ancestry = Vec::new();
        let mut next_node = Tao::archetype().as_form();
        let selfless_ego = self.as_form();
        while next_node != selfless_ego {
            ancestry.push(Archetype::from(*next_node.essence()));
            next_node = *backpointers.get(&next_node).unwrap();
        }
        ancestry
    }

    /// Checks to see if another archetype is a direct parent of this one.
    fn has_parent(&self, possible_ancestor: Archetype) -> bool {
        self.essence()
            .outgoing_nodes(Inherits::TYPE_ID)
            .contains(possible_ancestor.essence())
    }

    /// Checks to see if another archetype is an ancestor of this one. If so, the current archetype
    /// will inherit all attributes of the ancestor.
    fn has_ancestor(&self, possible_ancestor: Archetype) -> bool {
        self.essence()
            .inheritance_nodes()
            .contains(possible_ancestor.essence())
    }

    /// Get all the types of attributes that this concept is predefined to potentially have.
    fn attribute_archetypes(&self) -> Vec<AttributeArchetype> {
        self.essence()
            .outgoing_nodes(HasProperty::TYPE_ID)
            .into_iter()
            .map(AttributeArchetype::from)
            .collect()
    }

    /// Checks to see if an archetype is one of the possible attribute types this concept could
    /// have.
    fn has_attribute_type(&self, possible_type: AttributeArchetype) -> bool {
        self.essence()
            .has_outgoing(HasProperty::TYPE_ID, possible_type.essence())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tao::archetype::{Archetype, ArchetypeFormTrait};
    use crate::tao::initialize_kb;
    use crate::tao::relation::attribute::{Attribute, Owner, Value};

    #[test]
    fn test_parents() {
        initialize_kb();
        let owner = Owner::new();
        assert_eq!(owner.parents(), vec![Owner::archetype().as_archetype()]);
    }

    #[test]
    fn test_multiple_parents() {
        initialize_kb();
        let mut owner = Owner::new();
        owner.add_parent(Value::archetype().as_archetype()); // nonsensical, but okay for tests
        assert_eq!(
            owner.parents(),
            vec![
                Owner::archetype().as_archetype(),
                Value::archetype().as_archetype()
            ]
        );
    }

    #[test]
    fn test_ancestry_archetype() {
        initialize_kb();
        let type1 = Tao::archetype().individuate_as_archetype();
        let type2 = type1.individuate_as_archetype();
        assert_eq!(type2.ancestry(), vec![Tao::archetype(), type1]);
    }

    #[test]
    fn test_ancestry_individual() {
        initialize_kb();
        let type1 = Tao::archetype().individuate_as_archetype();
        let type2 = type1.individuate_as_archetype();
        let form = type2.individuate_as_form();
        assert_eq!(form.ancestry(), vec![Tao::archetype(), type1, type2]);
    }

    #[test]
    fn test_tao_ancestry() {
        initialize_kb();
        assert_eq!(Tao::archetype().ancestry(), Vec::<Archetype>::new());
    }

    #[test]
    fn test_looped_ancestry() {
        initialize_kb();
        let mut type1 = Tao::archetype().individuate_as_archetype();
        type1.add_parent(type1);
        assert_eq!(type1.ancestry(), vec![Tao::archetype()]);
    }

    #[test]
    fn test_looped_child_ancestry() {
        initialize_kb();
        let mut type1 = Tao::archetype().individuate_as_archetype();
        type1.add_parent(type1);
        let type2 = type1.individuate_as_archetype();
        assert_eq!(type2.ancestry(), vec![Tao::archetype(), type1]);
    }

    #[test]
    fn test_parenthood() {
        initialize_kb();
        let owner = Owner::new();
        assert!(owner.has_parent(Owner::archetype().as_archetype()));
        assert!(!owner.has_parent(Tao::archetype()));
        assert!(!owner.has_parent(Value::archetype().as_archetype()));
    }

    #[test]
    fn test_multiple_parenthood() {
        initialize_kb();
        let mut owner = Owner::new();
        owner.add_parent(Value::archetype().as_archetype()); // nonsensical, but okay for tests
        assert!(owner.has_parent(Owner::archetype().as_archetype()));
        assert!(!owner.has_parent(Tao::archetype()));
        assert!(owner.has_parent(Value::archetype().as_archetype()));
    }

    #[test]
    fn test_self_parenthood_ignored() {
        initialize_kb();
        let mut new_type = Tao::archetype().individuate_as_archetype();
        new_type.add_parent(new_type);
        assert_eq!(new_type.parents(), vec![Tao::archetype()]);
    }

    #[test]
    fn specific_to_generic_parenthood() {
        initialize_kb();
        let mut form = Form::new();
        form.add_parent(Tao::archetype());
        assert_eq!(form.parents(), vec![Form::archetype()]);
    }

    #[test]
    fn generic_to_specific_parenthood() {
        initialize_kb();
        let mut form = Tao::new();
        form.add_parent(Form::archetype());
        assert_eq!(form.parents(), vec![Form::archetype()]);
    }

    #[test]
    fn test_has_ancestor() {
        initialize_kb();
        let owner = Owner::new();
        assert!(owner.has_ancestor(Owner::archetype().as_archetype()));
        assert!(owner.has_ancestor(Tao::archetype()));
        assert!(!owner.has_ancestor(Value::archetype().as_archetype()));
    }

    #[test]
    fn test_attribute_types() {
        initialize_kb();
        let mut type1 = Attribute::archetype().individuate_as_archetype();
        let type2 = Attribute::archetype().individuate_as_archetype();
        type1.add_attribute_type(type2);
        let instance = type1.individuate_as_form();

        assert_eq!(
            instance.attribute_archetypes(),
            vec![Owner::archetype(), Value::archetype(), type2]
        );
        assert!(!instance.has_attribute_type(type1));
        assert!(instance.has_attribute_type(type2));
    }

    #[test]
    fn test_attribute_types_inherited() {
        initialize_kb();
        let mut type1 = Attribute::archetype().individuate_as_archetype();
        let type2 = Attribute::archetype().individuate_as_archetype();
        let type3 = type1.individuate_as_archetype();
        type1.add_attribute_type(type2);
        let instance = type3.individuate_as_form();

        assert_eq!(
            instance.attribute_archetypes(),
            vec![Owner::archetype(), Value::archetype(), type2]
        );
        assert!(!instance.has_attribute_type(type1));
        assert!(instance.has_attribute_type(type2));
    }
}