lemma-engine 0.8.13

A language that means business.
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use lemma::parsing::ast::DateTimeValue;
use lemma::Engine;
use std::collections::HashMap;

/// Test cross-spec data references (should work)
#[test]
fn test_cross_spec_data_reference() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base
data price: 100
data quantity: 5
"#;

    let derived_spec = r#"
spec derived
uses base_data: base
rule total: base_data.price * base_data.quantity
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let total = response
        .results
        .values()
        .find(|r| r.rule.name == "total")
        .unwrap();

    assert_eq!(total.result.value().unwrap().to_string(), "500");
}

/// Test cross-spec rule reference
#[test]
fn test_cross_spec_rule_reference() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base
data value: 50
rule doubled: value * 2
"#;

    let derived_spec = r#"
spec derived
uses base_data: base
rule derived_value: base_data.doubled + 10
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let derived_value = response
        .results
        .values()
        .find(|r| r.rule.name == "derived_value")
        .unwrap();

    assert_eq!(derived_value.result.value().unwrap().to_string(), "110");
}

/// Test cross-spec rule reference with dependencies
#[test]
fn test_cross_spec_rule_reference_with_dependencies() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base_employee
data monthly_salary: 5000
rule annual_salary: monthly_salary * 12
rule with_bonus: annual_salary * 1.1
"#;

    let derived_spec = r#"
spec manager
uses employee: base_employee
rule manager_bonus: employee.annual_salary * 0.15
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "manager", Some(&now), HashMap::new(), false)
        .unwrap();
    let bonus = response
        .results
        .values()
        .find(|r| r.rule.name == "manager_bonus")
        .unwrap();

    assert_eq!(bonus.result.value().unwrap().to_string(), "9000");
}

/// Test data binding with cross-spec rule reference
#[test]
fn test_cross_spec_data_binding_with_rule_reference() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base
data price: 100
data quantity: 5
rule total: price * quantity
"#;

    let derived_spec = r#"
spec derived
uses config: base
data config.price: 200
data config.quantity: 3
rule derived_total: config.total
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let total = response
        .results
        .values()
        .find(|r| r.rule.name == "derived_total")
        .unwrap();

    assert_eq!(total.result.value().unwrap().to_string(), "600");
}

/// Test nested cross-spec rule references
#[test]
fn test_nested_cross_spec_rule_reference() {
    let mut engine = Engine::new();

    let config_spec = r#"
spec config
data base_days: 3
rule standard_processing_days: base_days
rule express_processing_days: 1
"#;

    let order_spec = r#"
spec order
data is_express: false
rule processing_days: 5
"#;

    let derived_spec = r#"
spec derived
uses settings: config
uses order_info: order
rule total_days: settings.standard_processing_days + order_info.processing_days
"#;

    engine
        .load(
            config_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            order_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let total = response
        .results
        .values()
        .find(|r| r.rule.name == "total_days")
        .unwrap();

    assert_eq!(total.result.value().unwrap().to_string(), "8");
}

/// Test cross-spec rule reference in unless clause
#[test]
fn test_cross_spec_rule_reference_in_unless_clause() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base
data threshold: 100
data value: 150
rule is_valid: value >= threshold
"#;

    let derived_spec = r#"
spec derived
uses base_data: base
rule status: "invalid"
  unless base_data.is_valid then "valid"
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let status = response
        .results
        .values()
        .find(|r| r.rule.name == "status")
        .unwrap();

    assert_eq!(status.result.value().unwrap().to_string(), "valid");
}

/// Test that we can mix cross-spec data and rule references
#[test]
fn test_cross_spec_mixed_data_and_rule_references() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base
data input: 50
rule calculated: input * 2
"#;

    let derived_spec = r#"
spec derived
uses base_data: base
rule combined: base_data.input + base_data.calculated
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let combined = response
        .results
        .values()
        .find(|r| r.rule.name == "combined")
        .unwrap();

    assert_eq!(combined.result.value().unwrap().to_string(), "150");
}

/// Test cross-spec data binding with multiple levels (should work)
#[test]
fn test_multi_level_data_binding() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base
data x: 10
data y: 20
data z: 30
"#;

    let derived_spec = r#"
spec derived
uses b: base
data b.x: 100
data b.y: 200
rule sum: b.x + b.y + b.z
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let sum = response
        .results
        .values()
        .find(|r| r.rule.name == "sum")
        .unwrap();

    // x=100 (overridden), y=200 (overridden), z=30 (original)
    // 100 + 200 + 30 = 330
    assert_eq!(sum.result.value().unwrap().to_string(), "330");
}

/// Test simple data binding without rule references (should work)
#[test]
fn test_simple_data_binding() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base
data price: 100
data quantity: 5
"#;

    let derived_spec = r#"
spec derived
uses config: base
data config.price: 200
data config.quantity: 3
rule total: config.price * config.quantity
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "derived", Some(&now), HashMap::new(), false)
        .unwrap();
    let total = response
        .results
        .values()
        .find(|r| r.rule.name == "total")
        .unwrap();

    // Should be 200 * 3 = 600 (using overridden data values)
    assert_eq!(total.result.value().unwrap().to_string(), "600");
}

/// Test that different data paths to the same rule produce different results
/// This is the critical test for the RulePath implementation!
#[test]
fn test_different_data_paths_produce_different_results() {
    let mut engine = Engine::new();

    let example1_spec = r#"
spec example1
data price: 99
rule total: price * 1.21
"#;

    let example2_spec = r#"
spec example2
uses base: example1
"#;

    let example3_spec = r#"
spec example3
uses base: example2
rule total1: base.base.total

uses base2: example2
data base2.base.price: 79
rule total2: base2.base.total
"#;

    engine
        .load(
            example1_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            example2_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            example3_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "example3", Some(&now), HashMap::new(), false)
        .unwrap();

    let total1 = response
        .results
        .values()
        .find(|r| r.rule.name == "total1")
        .unwrap();

    let total2 = response
        .results
        .values()
        .find(|r| r.rule.name == "total2")
        .unwrap();

    // total1 uses original price: 99 * 1.21 = 119.79
    assert_eq!(total1.result.value().unwrap().to_string(), "119.79");

    // total2 uses overridden price: 79 * 1.21 = 95.59
    assert_eq!(total2.result.value().unwrap().to_string(), "95.59");
}

#[test]
fn spec_ref_evaluates_to_referenced_spec() {
    let mut engine = Engine::new();

    let code = r#"
spec pricing
data base_price: 200

spec order
uses p: pricing
rule total: p.base_price
"#;

    engine
        .load(
            code,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "order", Some(&now), HashMap::new(), false)
        .unwrap();
    let total = response
        .results
        .values()
        .find(|r| r.rule.name == "total")
        .unwrap();

    assert_eq!(
        total.result.value().unwrap().to_string(),
        "200",
        "Spec ref should evaluate against the referenced pricing spec"
    );
}

#[test]
fn cross_spec_dependency_rules_excluded_from_results() {
    let mut engine = Engine::new();

    let base_spec = r#"
spec base_employee
data monthly_salary: 5000
data employment_duration: 3 years
rule annual_salary: monthly_salary * 12
rule is_eligible_for_bonus: false
  unless employment_duration >= 1 years then true
"#;

    let derived_spec = r#"
spec specific_employee
uses employee: base_employee
rule salary_with_bonus: employee.annual_salary
  unless employee.is_eligible_for_bonus then employee.annual_salary * 1.1
rule employee_summary: employee.monthly_salary
"#;

    engine
        .load(
            base_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();
    engine
        .load(
            derived_spec,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "specific_employee", Some(&now), HashMap::new(), false)
        .unwrap();

    let mut result_names: Vec<&str> = response.results.keys().map(|k| k.as_str()).collect();
    result_names.sort();
    assert_eq!(
        result_names,
        vec!["employee_summary", "salary_with_bonus"],
        "Only local rules should appear in results; cross-spec dependencies \
         (annual_salary, is_eligible_for_bonus) must be excluded"
    );

    assert_eq!(
        response
            .results
            .get("salary_with_bonus")
            .unwrap()
            .result
            .value()
            .unwrap()
            .to_string(),
        "66000"
    );
}

#[test]
fn spec_ref_from_order_to_pricing_evaluates_correctly() {
    let mut engine = Engine::new();

    let code = r#"
spec pricing
data base_price: 150

spec order
uses p: pricing
rule total: p.base_price
"#;

    engine
        .load(
            code,
            lemma::SourceType::Path(std::sync::Arc::new(std::path::PathBuf::from("test.lemma"))),
        )
        .unwrap();

    let now = DateTimeValue::now();
    let response = engine
        .run(None, "order", Some(&now), HashMap::new(), false)
        .unwrap();
    let total = response
        .results
        .values()
        .find(|r| r.rule.name == "total")
        .unwrap();

    assert_eq!(
        total.result.value().unwrap().to_string(),
        "150",
        "Spec ref should evaluate against the referenced pricing spec"
    );
}