zamm_yang 0.0.10

A basic, experimental code generator
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use crate::tao::archetype::CodegenFlags;
use crate::tao::form::DefinedMarker;
use crate::tao::{Implement, ImplementConfig};
use heck::KebabCase;
use std::collections::HashMap;
use std::convert::TryFrom;
use yaml_rust::{Yaml, YamlLoader};
use zamm_yin::node_wrappers::CommonNodeTrait;
use zamm_yin::tao::archetype::{Archetype, ArchetypeFormTrait, ArchetypeTrait, AttributeArchetype};
use zamm_yin::tao::attribute::Attribute;
use zamm_yin::tao::{Form, FormTrait, Tao};

fn parse_attr_info(new_subtype: &mut Archetype, entry: &HashMap<String, Yaml>) {
    let mut attr_subtype = AttributeArchetype::from(new_subtype.id());
    if let Some(owner_type_name) = entry.get("owner_archetype").map(|y| y.as_str()).flatten() {
        attr_subtype.set_owner_archetype(
            Archetype::try_from(owner_type_name.to_kebab_case().as_str()).unwrap(),
        );
    }
    if let Some(value_type_name) = entry.get("value_archetype").map(|y| y.as_str()).flatten() {
        attr_subtype.set_value_archetype(
            Archetype::try_from(value_type_name.to_kebab_case().as_str()).unwrap(),
        );
    }
}

/// Parses a YAML string into a list of concepts as represented by the string.
pub fn parse_yaml(yaml: &str) -> Vec<Form> {
    let mut new_concepts = Vec::<Form>::new();
    let docs = YamlLoader::load_from_str(yaml).unwrap();
    let doc = &docs[0];
    let mut entries = HashMap::new();

    // define everything first
    for entry in doc.as_vec().unwrap() {
        if let Some(name) = entry["define"].as_str() {
            let mut new_concept = Tao::individuate();
            // that's all we have to do for now, just ensure that this name now exists for this node
            new_concept.set_internal_name(name.to_kebab_case());
        }
    }

    // then collect all entries for the same node together
    for entry in doc.as_vec().unwrap() {
        let current_concept = if let Some(name) = entry["define"].as_str() {
            // it's already been defined, just find it again
            let mut new_concept = Form::try_from(name.to_kebab_case().as_str()).unwrap();
            new_concept.mark_newly_defined();
            new_concept
        } else if let Some(name) = entry["name"].as_str() {
            Form::try_from(name.to_kebab_case().as_str()).unwrap()
        } else {
            // Not referring to existing node (no "name"), so it must be a new node. Not naming the // new node either (no "define"), so don't bother giving it a name.
            let mut new_concept = Tao::individuate();
            new_concept.mark_newly_defined();
            new_concept
        };

        let existing_entry = entries
            .entry(current_concept.id())
            .or_insert_with(HashMap::<String, Yaml>::new);

        for (k, v) in entry.as_hash().unwrap() {
            existing_entry.insert(k.as_str().unwrap().to_owned(), v.clone());
        }
    }

    // now parse all new nodes
    let mut node_ids = entries.keys().collect::<Vec<&usize>>();
    node_ids.sort(); // for determinism
    for concept_id in node_ids {
        let mut current_concept = Form::from(*concept_id);
        let entry = &entries[concept_id];

        if let Some(parent_name) = entry.get("parent").map(|y| y.as_str()).flatten() {
            let parent = Archetype::try_from(parent_name.to_kebab_case().as_str()).unwrap();
            current_concept.add_parent(parent);
        }

        if let Some(attrs) = entry.get("attributes").map(|y| y.as_vec()).flatten() {
            // attributes can only be specified for new types, not new individuals unless the
            // individual is a singleton
            let mut new_subtype = Archetype::from(*concept_id);
            for attr in attrs {
                let canonical = attr.as_str().unwrap().to_kebab_case();
                let target_attr = AttributeArchetype::try_from(canonical.as_str()).unwrap();
                new_subtype.add_attribute_type(target_attr);
            }
        }

        if current_concept.has_ancestor(Implement::archetype()) {
            let mut implement = Implement::from(*concept_id);
            let target_name = entry["target"].as_str().unwrap().to_kebab_case();
            let mut target = Archetype::try_from(target_name.as_str()).unwrap();
            implement.set_target(target);

            if entry
                .get("attribute_logic")
                .map(|y| y.as_bool())
                .flatten()
                .unwrap_or(false)
            {
                target.activate_attribute_logic();
            }
            // separate if-statement because attribute logic activation gets inherited
            if target.attribute_logic_activated() {
                let target_id = target.id();
                parse_attr_info(&mut target, &entries[&target_id]);
            }

            if entry
                .get("force_own_module")
                .map(|y| y.as_bool())
                .flatten()
                .unwrap_or(false)
            {
                target.mark_own_module();
            }

            let impl_config = ImplementConfig {
                id: entry["output_id"].as_i64().unwrap() as usize,
                doc: entry
                    .get("documentation")
                    .map(|s| s.as_str().unwrap().to_owned()),
            };
            implement.set_config(impl_config);
        } else if current_concept.has_ancestor(Attribute::archetype().as_archetype()) {
            parse_attr_info(&mut Archetype::from(current_concept.id()), entry);
        }
        new_concepts.push(current_concept);
    }
    new_concepts
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tao::{initialize_kb, Implement};
    use indoc::indoc;
    use std::rc::Rc;
    use zamm_yin::tao::attribute::{Attribute, Owner, OwnerArchetype};
    use zamm_yin::tao::{FormTrait, Tao};

    #[test]
    fn test_parse_archetype() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Target
              parent: Attribute
        "});
        assert_eq!(concepts.len(), 1);
        let target = concepts[0];
        assert!(target.has_ancestor(Attribute::archetype().as_archetype()));
        assert_eq!(target.internal_name(), Some(Rc::new("target".to_owned())));
        assert!(target.is_newly_defined());
    }

    #[test]
    fn test_parse_archetype_no_name() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - parent: Attribute
        "});
        assert_eq!(concepts.len(), 1);
        let target = concepts[0];
        assert!(target.has_ancestor(Attribute::archetype().as_archetype()));
        assert_eq!(target.internal_name(), None);
    }

    #[test]
    fn test_parse_attribute_owner_value_archetypes() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Target
              parent: Attribute
              owner_archetype: Implement
              value_archetype: Tao
        "});
        assert_eq!(concepts.len(), 1);
        let target = concepts[0];
        assert!(target.has_ancestor(Attribute::archetype().as_archetype()));
        assert_eq!(target.internal_name(), Some(Rc::new("target".to_owned())));
        let target_as_attr_type = AttributeArchetype::from(*target.essence());
        assert_eq!(
            target_as_attr_type.owner_archetype(),
            Implement::archetype()
        );
        assert_eq!(target_as_attr_type.value_archetype(), Tao::archetype());
    }

    #[test]
    fn test_parse_has_attributes() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Foo
              parent: Tao
              attributes:
                - owner
                - OwnerArchetype
        "});
        assert_eq!(concepts.len(), 1);
        let target = concepts[0];
        assert!(target.has_ancestor(Tao::archetype().as_archetype()));
        assert_eq!(target.internal_name(), Some(Rc::new("foo".to_owned())));
        assert_eq!(
            target.attribute_archetypes(),
            vec![Owner::archetype(), OwnerArchetype::archetype()]
        );
    }

    #[test]
    fn test_parse_implement() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Target
              parent: Attribute
            - parent: Implement
              target: Target
              output_id: 2
              documentation: Howdy, how ya doing?
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        assert!(implement.has_ancestor(Implement::archetype()));
        let target = implement.target().unwrap();
        assert_eq!(target.internal_name(), Some(Rc::new("target".to_owned())));
        assert!(target.is_newly_defined());
        assert!(!target.attribute_logic_activated());
        let cfg = implement.config().unwrap();
        assert_eq!(cfg.id, 2);
        assert_eq!(cfg.doc, Some("Howdy, how ya doing?".to_owned()));
    }

    #[test]
    fn test_parse_implement_attr() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Attribute
              parent: Tao
            - parent: Implement
              target: Attribute
              output_id: 2
              attribute_logic: true
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = implement.target().unwrap();
        assert!(target.attribute_logic_activated());
        let cfg = implement.config().unwrap();
        assert_eq!(cfg.id, 2);
        assert_eq!(cfg.doc, None);
    }

    #[test]
    fn test_parse_implement_attr_archetypes() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Attribute
              parent: Tao
              owner_archetype: Tao
              value_archetype: Form
            - parent: Implement
              target: Attribute
              output_id: 2
              attribute_logic: true
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = AttributeArchetype::from(implement.target().unwrap().id());
        assert!(target.attribute_logic_activated());
        assert_eq!(target.owner_archetype(), Tao::archetype());
        assert_eq!(target.value_archetype(), Form::archetype());
        let cfg = implement.config().unwrap();
        assert_eq!(cfg.id, 2);
        assert_eq!(cfg.doc, None);
    }

    #[test]
    fn test_parse_split_up_definition() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Attribute
              parent: Tao
            - name: Attribute
              owner_archetype: Tao
              value_archetype: Form
            - parent: Implement
              target: Attribute
              output_id: 2
              attribute_logic: true
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = AttributeArchetype::from(implement.target().unwrap().id());
        assert!(target.attribute_logic_activated());
        assert_eq!(target.owner_archetype(), Tao::archetype());
        assert_eq!(target.value_archetype(), Form::archetype());
        let cfg = implement.config().unwrap();
        assert_eq!(cfg.id, 2);
        assert_eq!(cfg.doc, None);
    }

    #[test]
    fn test_parse_define_only() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Attribute
            - name: Attribute
              parent: Tao
            - name: Attribute
              owner_archetype: Tao
              value_archetype: Form
            - parent: Implement
              target: Attribute
              output_id: 2
              attribute_logic: true
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = AttributeArchetype::from(implement.target().unwrap().id());
        assert!(target.attribute_logic_activated());
        assert_eq!(target.owner_archetype(), Tao::archetype());
        assert_eq!(target.value_archetype(), Form::archetype());
        let cfg = implement.config().unwrap();
        assert_eq!(cfg.id, 2);
        assert_eq!(cfg.doc, None);
    }

    #[test]
    fn test_parse_out_of_order() {
        initialize_kb();

        // refer to attribute even before definition
        let concepts = parse_yaml(indoc! {"
            - name: Attribute
              parent: Tao
            - define: Attribute
            - name: Attribute
              owner_archetype: Tao
              value_archetype: Form
            - parent: Implement
              target: Attribute
              output_id: 2
              attribute_logic: true
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = AttributeArchetype::from(implement.target().unwrap().id());
        assert!(target.attribute_logic_activated());
        assert_eq!(target.owner_archetype(), Tao::archetype());
        assert_eq!(target.value_archetype(), Form::archetype());
        let cfg = implement.config().unwrap();
        assert_eq!(cfg.id, 2);
        assert_eq!(cfg.doc, None);
    }

    #[test]
    fn test_parse_implement_inherited_attr_archetypes() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: MyAttr
              parent: Tao
              owner_archetype: Tao
              value_archetype: Tao
            - define: Color
              parent: MyAttr
              value_archetype: Form
            - parent: Implement
              target: MyAttr
              output_id: 2
              attribute_logic: true
            - parent: Implement
              target: Color
              output_id: 3
        "});
        assert_eq!(concepts.len(), 4);
        let target = AttributeArchetype::from(concepts[1].id());
        assert!(target.attribute_logic_activated());
        assert_eq!(
            target
                .ancestry()
                .into_iter()
                .map(|a| a.internal_name().unwrap())
                .collect::<Vec<Rc<String>>>(),
            vec![Rc::new("tao".to_owned()), Rc::new("my-attr".to_owned())]
        );
        assert_eq!(target.owner_archetype(), Tao::archetype());
        assert_eq!(target.value_archetype(), Form::archetype());
    }

    #[test]
    fn test_parse_implement_own_module_unmarked() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: MyType
              parent: Tao
            - parent: Implement
              target: MyType
              output_id: 2
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = implement.target().unwrap();
        assert!(!target.attribute_logic_activated());
        assert!(!target.force_own_module());
    }

    #[test]
    fn test_parse_implement_own_module_marked() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: MyType
              parent: Tao
            - parent: Implement
              target: MyType
              output_id: 2
              force_own_module: true
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = implement.target().unwrap();
        assert!(!target.attribute_logic_activated());
        assert!(target.force_own_module());
    }

    #[test]
    fn test_parse_shadow_old_nodes() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Attribute
              parent: Tao
            - parent: Implement
              target: Attribute
              output_id: 2
        "});
        assert_eq!(concepts.len(), 2);
        let implement = Implement::from(concepts[1].id());
        let target = implement.target().unwrap();
        assert_ne!(target, Attribute::archetype().as_archetype());
        assert_eq!(target.introduced_attribute_archetypes(), vec![]);
    }

    #[test]
    fn test_parse_multiline_string() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Target
              parent: Attribute
            - parent: Implement
              target: Target
              output_id: 2
              documentation: |-
                So much to do.
                So little time.
        "});
        let implement = Implement::from(concepts[1].id());
        let cfg = implement.config().unwrap();
        assert_eq!(cfg.doc, Some("So much to do.\nSo little time.".to_owned()));
    }

    #[test]
    fn test_parse_multiline_string_multiple_breaks() {
        initialize_kb();

        let concepts = parse_yaml(indoc! {"
            - define: Target
              parent: Attribute
            - parent: Implement
              target: Target
              output_id: 2
              documentation: |-
                So much to do.

                So little time.
        "});
        let implement = Implement::from(concepts[1].id());
        let cfg = implement.config().unwrap();
        assert_eq!(
            cfg.doc,
            Some("So much to do.\n\nSo little time.".to_owned())
        );
    }
}