domainforge-core 0.18.0

Rust core library implementing the SEA DSL primitives and validation engine.
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use domainforge_core::parser::{
    parse, parse_to_graph, parse_to_graph_with_options, AstNode, ParseOptions,
};
use serde_json::json;

#[test]
fn test_parse_entity_basic() {
    let source = r#"Entity "Warehouse A""#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_entity_with_domain() {
    let source = r#"Entity "Warehouse A" in logistics"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_resource_basic() {
    let source = r#"Resource "Camera Units""#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_resource_with_units() {
    let source = r#"Resource "Camera Units" units"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_resource_with_units_and_domain() {
    let source = r#"Resource "Camera Units" units in inventory"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_resource_with_domain_only() {
    let source = r#"Resource "USD" in finance"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_flow_basic() {
    let source = r#"Flow "Camera Units" from "Warehouse" to "Factory""#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_flow_with_quantity() {
    let source = r#"Flow "Camera Units" from "Warehouse" to "Factory" quantity 100"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_flow_with_custom_annotations() {
    let source = r#"
        Flow "Chat" @cqrs { "kind": "command" } @tx { "transactional": false } from "Client" to "Server"
    "#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
    match &ast.declarations[0].node {
        AstNode::Flow { annotations, .. } => {
            assert_eq!(annotations.get("cqrs"), Some(&json!({ "kind": "command" })));
            assert_eq!(
                annotations.get("tx"),
                Some(&json!({ "transactional": false }))
            );
        }
        _ => panic!("Expected flow declaration"),
    }
}

#[test]
fn test_parse_policy_simple() {
    let source = r#"Policy check_qty as: Flow.quantity > 0"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_with_colon() {
    let source = r#"Policy check_qty as: Flow.quantity > 0"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
    match &ast.declarations[0].node {
        AstNode::Policy { name, version, .. } => {
            assert_eq!(name, "check_qty");
            assert!(version.is_none());
        }
        _ => panic!("Expected policy declaration"),
    }

    let missing_colon = r#"Policy check_qty as Flow.quantity > 0"#;
    assert!(parse(missing_colon).is_err());
}

#[test]
fn test_parse_policy_and_expression() {
    let source = r#"Policy check as: (A > 0) and (B < 10)"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_or_expression() {
    let source = r#"Policy check as: (A = "yes") or (B = "no")"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_not_expression() {
    let source = r#"Policy check as: not (A = "bad")"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_comparison_operators() {
    let operators = vec![">", "<", ">=", "<=", "=", "!="];

    for op in operators {
        let source = format!(r#"Policy test as: A {} 10"#, op);
        let ast = parse(&source).unwrap();
        assert_eq!(ast.declarations.len(), 1);
    }
}

#[test]
fn test_parse_policy_string_operators() {
    let operators = vec!["contains", "startswith", "endswith"];

    for op in operators {
        let source = format!(r#"Policy test as: name {} "test""#, op);
        let ast = parse(&source).unwrap();
        assert_eq!(ast.declarations.len(), 1);
    }
}

#[test]
fn test_parse_policy_arithmetic() {
    let source = r#"Policy check as: (A + B) > 10"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_nested_parentheses() {
    let source = r#"Policy check as: ((A and B) or (C and D))"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_quantifier_forall() {
    let source = r#"Policy check as: forall f in flows: (f.quantity > 0)"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_quantifier_exists() {
    let source = r#"Policy check as: exists e in entities: (e.name = "Factory")"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_policy_quantifier_exists_unique() {
    let source = r#"Policy check as: exists_unique r in resources: (r.unit = "kg")"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_multiple_declarations() {
    let source = r#"
        Entity "Warehouse" in logistics
        Entity "Factory" in production
        Resource "Camera" units
        Flow "Camera" from "Warehouse" to "Factory" quantity 50
        Policy check as: Flow.quantity > 0
    "#;

    let ast = parse(source).unwrap();
    assert_eq!(ast.declarations.len(), 5);
}

#[test]
fn test_parse_with_comments() {
    let source = r#"
        // This is a warehouse
        Entity "Warehouse" in logistics
        // This is a camera resource
        Resource "Camera" units
        // Flow definition
        Flow "Camera" from "Warehouse" to "Factory"
    "#;

    let ast = parse(source).unwrap();
    assert_eq!(ast.declarations.len(), 3);
}

#[test]
fn test_parse_empty_source() {
    let source = "";
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 0);
}

#[test]
fn test_parse_whitespace_only() {
    let source = "   \n\t  \n  ";
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 0);
}

#[test]
fn test_parse_comments_only() {
    let source = r#"
        // Just comments
        // Nothing else
    "#;

    let ast = parse(source).unwrap();
    assert_eq!(ast.declarations.len(), 0);
}

#[test]
fn test_parse_member_access() {
    let source = r#"Policy check as: Entity.name = "Warehouse""#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_boolean_literals() {
    let source = r#"Policy check as: active = true"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);

    let source2 = r#"Policy check as: active = false"#;
    let ast2 = parse(source2).unwrap();

    assert_eq!(ast2.declarations.len(), 1);
}

#[test]
fn test_parse_numeric_literals() {
    let source = r#"Policy check as: quantity = 42"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);

    let source2 = r#"Policy check as: price = 19.99"#;
    let ast2 = parse(source2).unwrap();

    assert_eq!(ast2.declarations.len(), 1);
}

#[test]
fn test_parse_negative_numbers() {
    let source = r#"Policy check as: temperature > -10"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_parse_unary_negation() {
    let source = r#"Policy check as: result = -value"#;
    let ast = parse(source).unwrap();

    assert_eq!(ast.declarations.len(), 1);
}

#[test]
fn test_ast_to_graph_entities() {
    let source = r#"
        Entity "Warehouse" in logistics
        Entity "Factory" in production
    "#;

    let graph = parse_to_graph(source).unwrap();
    assert_eq!(graph.all_entities().len(), 2);
}

#[test]
fn test_ast_to_graph_resources() {
    let source = r#"
        Resource "Camera" units
        Resource "USD" currency in finance
    "#;

    let graph = parse_to_graph(source).unwrap();
    assert_eq!(graph.all_resources().len(), 2);
}

#[test]
fn test_ast_to_graph_flows() {
    let source = r#"
        Entity "Warehouse"
        Entity "Factory"
        Resource "Camera" units
        Flow "Camera" from "Warehouse" to "Factory" quantity 100
    "#;

    let graph = parse_to_graph(source).unwrap();
    assert_eq!(graph.all_entities().len(), 2);
    assert_eq!(graph.all_resources().len(), 1);
    assert_eq!(graph.all_flows().len(), 1);
}

#[test]
fn test_ast_to_graph_undefined_entity_error() {
    let source = r#"
        Resource "Camera" units
        Flow "Camera" from "Warehouse" to "Factory"
    "#;

    let result = parse_to_graph(source);
    assert!(result.is_err());
}

#[test]
fn test_ast_to_graph_undefined_resource_error() {
    let source = r#"
        Entity "Warehouse"
        Entity "Factory"
        Flow "Camera" from "Warehouse" to "Factory"
    "#;

    let result = parse_to_graph(source);
    assert!(result.is_err());
}

#[test]
fn test_ast_to_graph_duplicate_entity_error() {
    let source = r#"
        Entity "Warehouse"
        Entity "Warehouse"
    "#;

    let result = parse_to_graph(source);
    assert!(result.is_err());
}

#[test]
fn test_ast_to_graph_duplicate_resource_error() {
    let source = r#"
        Resource "Camera" units
        Resource "Camera" units
    "#;

    let result = parse_to_graph(source);
    assert!(result.is_err());
}

#[test]
fn test_parse_to_graph_reports_ambiguous_unqualified_entity_reference() {
    let source = r#"
        Entity "Warehouse" in east
        Entity "Warehouse" in west
        Resource "Camera" units
        Flow "Camera" from "Warehouse" to "Warehouse"
    "#;

    let error = parse_to_graph(source).expect_err("expected ambiguous reference error");
    assert_eq!(
        error.to_string(),
        "Validation error: Ambiguous reference 'Warehouse' found in namespaces: east, west"
    );
}

#[test]
fn test_parse_to_graph_with_options_overrides_namespace() {
    let source = r#"
        Entity "Warehouse"
        Resource "Camera" units
    "#;

    let options = ParseOptions {
        default_namespace: Some("logistics".to_string()),
        namespace_registry: None,
        entry_path: None,
        ..Default::default()
    };

    let graph = parse_to_graph_with_options(source, &options).unwrap();
    let entity = graph.all_entities().into_iter().next().expect("entity");

    assert_eq!(entity.namespace(), "logistics");
}

#[test]
fn test_parse_to_graph_with_options_preserves_explicit_namespace() {
    let source = r#"
        Entity "Warehouse" in production
        Resource "Camera" units
    "#;

    let options = ParseOptions {
        default_namespace: Some("logistics".to_string()),
        namespace_registry: None,
        entry_path: None,
        ..Default::default()
    };

    let graph = parse_to_graph_with_options(source, &options).unwrap();
    let entity = graph.all_entities().into_iter().next().expect("entity");

    assert_eq!(entity.namespace(), "production");
}

#[test]
fn test_parse_case_insensitive_keywords() {
    let sources = vec![
        r#"ENTITY "Test""#,
        r#"Entity "Test""#,
        r#"entity "Test""#,
        r#"RESOURCE "Test" units"#,
        r#"Resource "Test" units"#,
        r#"resource "Test" units"#,
    ];

    for source in sources {
        let result = parse(source);
        assert!(result.is_ok(), "Failed to parse: {}", source);
    }
}

#[test]
fn test_parse_complex_supply_chain() {
    let source = r#"
        // Entities
        Entity "Supplier A" in sourcing
        Entity "Warehouse B" in logistics
        Entity "Factory C" in production
        Entity "Retailer D" in sales

        // Resources
        Resource "Raw Materials" kg in sourcing
        Resource "Components" units in production
        Resource "Finished Product" units in sales

        // Flows
        Flow "Raw Materials" from "Supplier A" to "Warehouse B" quantity 1000
        Flow "Raw Materials" from "Warehouse B" to "Factory C" quantity 800
        Flow "Components" from "Factory C" to "Retailer D" quantity 500

        // Policies
        Policy min_stock as: forall f in flows: (f.quantity > 0)
        Policy max_capacity as: forall e in entities: (e.capacity < 10000)
    "#;

    let graph = parse_to_graph(source).unwrap();
    assert_eq!(graph.all_entities().len(), 4);
    assert_eq!(graph.all_resources().len(), 3);
    assert_eq!(graph.all_flows().len(), 3);
}

#[test]
fn test_parse_error_invalid_syntax() {
    let source = r#"Entity"#; // Missing name
    let result = parse(source);
    assert!(result.is_err());
}

#[test]
fn test_parse_error_unclosed_string() {
    let source = r#"Entity "Warehouse"#; // Unclosed quote
    let result = parse(source);
    assert!(result.is_err());
}

#[test]
fn test_parse_error_missing_from() {
    let source = r#"Flow "Camera" to "Factory""#; // Missing 'from'
    let result = parse(source);
    assert!(result.is_err());
}