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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// Modified from: logica/compiler/universe.py
// Original authors: Evgeny Skvortsov et al. (Logica Team, Google LLC)
// Original work: Copyright 2020 Google LLC, licensed under the Apache License, Version 2.0.
// Modifications: Copyright 2025-2026 Yoan Sallami (Synalinks Team), licensed under the Apache License, Version 2.0.
use std::collections::{HashMap, HashSet};
use crate::parser::Json;
use crate::compiler::CompileResult;
use crate::compiler::CompileError;
use crate::compiler::universe::IterationDef;
use crate::compiler::{home_schema, test_schema};
/// Known annotating predicates (matches Python's ANNOTATING_PREDICATES).
const ANNOTATING_PREDICATES: &[&str] = &[
"@Limit", "@OrderBy", "@Ground", "@Flag", "@DefineFlag",
"@NoInject", "@Make", "@CompileAsTvf", "@With", "@NoWith",
"@CompileAsUdf", "@ResetFlagValue", "@Dataset", "@AttachDatabase",
"@Engine", "@Recursive", "@Iteration", "@BareAggregation",
];
/// Parsed annotations from a Logica program.
#[derive(Clone)]
pub struct Annotations {
/// Raw annotation values per predicate: predicate_name → { annotation_key → value }
pub annotations: HashMap<String, HashMap<String, Json>>,
/// Compiled flag values (defaults merged with user overrides).
pub flag_values: HashMap<String, String>,
/// Default engine name.
pub engine: String,
/// User-defined @AttachDatabase entries: db_name → path.
pub user_attached_databases: HashMap<String, String>,
/// @Dataset override (None = use engine default).
pub dataset_override: Option<String>,
/// @CompileAsUdf predicate names.
pub compile_as_udf: HashSet<String>,
/// @CompileAsTvf entries: predicate_name → list of argument names.
pub compile_as_tvf: HashMap<String, Vec<String>>,
/// @BareAggregation entries: predicate_name → semigroup name.
pub bare_aggregation: HashMap<String, String>,
/// Engine sub-keys (motherduck, threads, type_checking, clingo).
pub engine_options: HashMap<String, Json>,
}
#[derive(Debug, Clone)]
pub struct Ground {
pub table_name: String,
pub overwrite: bool,
pub copy_to_file: Option<String>,
}
/// Extract a string value from a Logica expression literal.
/// Handles the nested structure: {"literal": {"the_string": {"the_string": "value"}}}
fn extract_string_literal(expr: &Json) -> Option<String> {
if !expr.is_object() {
return None;
}
let o = expr.as_object();
if let Some(lit) = o.get("literal") {
if let Some(ts) = lit.as_object().get("the_string") {
if ts.is_string() {
return Some(ts.as_str().to_string());
}
if ts.is_object() {
if let Some(inner) = ts.as_object().get("the_string") {
return Some(inner.as_str().to_string());
}
}
}
}
None
}
impl Annotations {
/// Extract annotations from parsed rules.
/// Follows Python's two-pass approach: first @DefineFlag/@ResetFlagValue, then the rest.
pub fn extract(rules: &[(String, Json)]) -> CompileResult<Self> {
let mut engine = "duckdb".to_string();
let mut engine_options: HashMap<String, Json> = HashMap::new();
let mut flag_defaults: HashMap<String, String> = HashMap::new();
let mut flag_resets: HashMap<String, String> = HashMap::new();
// ── Pass 1: Extract @DefineFlag, @ResetFlagValue, and @Engine ──
for (_pred_name, rule) in rules {
let ro = rule.as_object();
if !ro.contains_key("head") { continue; }
let head = ro["head"].as_object();
let name = head["predicate_name"].as_str();
if !name.starts_with('@') { continue; }
// Validate annotation name
if !ANNOTATING_PREDICATES.contains(&name) {
let full_text = ro.get("full_text")
.map(|ft| ft.as_str().to_string())
.unwrap_or_default();
return Err(CompileError::new(
format!(
"Only {} and {} special predicates are allowed.",
ANNOTATING_PREDICATES[..ANNOTATING_PREDICATES.len()-1].join(", "),
ANNOTATING_PREDICATES[ANNOTATING_PREDICATES.len()-1]
),
&full_text,
));
}
let ann_name = &name[1..];
let fvs = Self::field_values(head);
match ann_name {
"Engine" => {
if let Some((_, val)) = fvs.iter().find(|(k, _)| k == "0") {
if let Some(s) = extract_string_literal(val) {
engine = s;
}
}
// Collect engine sub-keys (motherduck, threads, type_checking, clingo)
for (k, v) in &fvs {
if k != "0" {
engine_options.insert(k.clone(), v.clone());
}
}
}
"DefineFlag" => {
let flag_name = fvs.iter()
.find(|(k, _)| k == "0")
.and_then(|(_, v)| extract_string_literal(v));
let default = fvs.iter()
.find(|(k, _)| k == "1")
.and_then(|(_, v)| extract_string_literal(v))
.unwrap_or_default();
if let Some(name) = flag_name {
flag_defaults.entry(name).or_insert(default);
}
}
"ResetFlagValue" => {
let flag_name = fvs.iter()
.find(|(k, _)| k == "0")
.and_then(|(_, v)| extract_string_literal(v));
let value = fvs.iter()
.find(|(k, _)| k == "1")
.and_then(|(_, v)| extract_string_literal(v))
.unwrap_or_default();
if let Some(name) = flag_name {
flag_resets.insert(name, value);
}
}
_ => {}
}
}
// Build flag values: defaults, then programmatic resets, then user flags
let mut flag_values = flag_defaults;
for (k, v) in flag_resets {
flag_values.insert(k, v);
}
// ── Pass 2: Extract all other annotations ──
let mut per_predicate: HashMap<String, HashMap<String, Json>> = HashMap::new();
let mut user_attached_databases: HashMap<String, String> = HashMap::new();
let mut dataset_override: Option<String> = None;
let mut compile_as_udf: HashSet<String> = HashSet::new();
let mut compile_as_tvf: HashMap<String, Vec<String>> = HashMap::new();
let mut bare_aggregation: HashMap<String, String> = HashMap::new();
for (_pred_name, rule) in rules {
let ro = rule.as_object();
if !ro.contains_key("head") { continue; }
let head = ro["head"].as_object();
let name = head["predicate_name"].as_str();
if !name.starts_with('@') { continue; }
let ann_name = &name[1..];
let fvs = Self::field_values(head);
match ann_name {
"Engine" | "DefineFlag" | "ResetFlagValue" => {
// Already handled in pass 1
}
"OrderBy" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
let entry = per_predicate.entry(target).or_default();
entry.insert("order_by".into(), Json::Array(
fvs.iter()
.filter(|(k, _)| k != "0")
.map(|(_, v)| v.clone())
.collect()
));
}
}
"Limit" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
if let Some((_, limit_val)) = fvs.iter().find(|(k, _)| k == "1") {
let entry = per_predicate.entry(target).or_default();
entry.insert("limit".into(), limit_val.clone());
}
}
}
"Ground" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
let table = if let Some((_, t)) = fvs.iter().find(|(k, _)| k == "1") {
Self::extract_predicate_name(t).unwrap_or_else(|| target.clone())
} else {
target.clone()
};
let entry = per_predicate.entry(target.clone()).or_default();
entry.insert("ground".into(), Json::Str(table));
// Store overwrite and copy_to_file if present
if let Some((_, ow)) = fvs.iter().find(|(k, _)| k == "overwrite") {
entry.insert("ground_overwrite".into(), ow.clone());
}
if let Some((_, cf)) = fvs.iter().find(|(k, _)| k == "copy_to_file") {
entry.insert("ground_copy_to_file".into(), cf.clone());
}
}
}
"With" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
let entry = per_predicate.entry(target).or_default();
entry.insert("with".into(), Json::Bool(true));
}
}
"NoWith" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
let entry = per_predicate.entry(target).or_default();
entry.insert("with".into(), Json::Bool(false));
}
}
"NoInject" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
let entry = per_predicate.entry(target).or_default();
entry.insert("no_inject".into(), Json::Bool(true));
}
}
"Dataset" => {
// @Dataset("name") — singleton, first positional arg is the dataset name
if let Some((_, val)) = fvs.iter().find(|(k, _)| k == "0") {
if let Some(s) = extract_string_literal(val) {
dataset_override = Some(s);
} else if let Some(name) = Self::extract_predicate_name(val) {
dataset_override = Some(name);
}
}
}
"AttachDatabase" => {
// @AttachDatabase(db_name, "path")
if let Some(db_name) = Self::predicate_name_from_field(&fvs, "0") {
if let Some((_, path_val)) = fvs.iter().find(|(k, _)| k == "1") {
if let Some(path) = extract_string_literal(path_val) {
user_attached_databases.insert(db_name, path);
}
}
}
}
"CompileAsUdf" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
compile_as_udf.insert(target);
}
}
"CompileAsTvf" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
// Second arg is a list of argument predicate names
let mut args = Vec::new();
if let Some((_, list_val)) = fvs.iter().find(|(k, _)| k == "1") {
if list_val.is_array() {
for item in list_val.as_array() {
if let Some(pname) = Self::extract_predicate_name(item) {
args.push(pname);
}
}
}
}
compile_as_tvf.insert(target, args);
}
}
"BareAggregation" => {
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
if let Some((_, sg_val)) = fvs.iter().find(|(k, _)| k == "semigroup") {
if let Some(sg_name) = Self::extract_predicate_name(sg_val) {
bare_aggregation.insert(target, sg_name);
}
}
}
}
"Iteration" => {
// @Iteration(name, predicates: [...], repetitions: N, stop_signal: Pred)
if let Some(target) = Self::predicate_name_from_field(&fvs, "0") {
let entry = per_predicate.entry(format!("@Iteration_{}", target)).or_default();
for (k, v) in &fvs {
if k != "0" {
entry.insert(k.clone(), v.clone());
}
}
}
}
// @Make, @Recursive are handled by functors.rs directly from raw rules
// @Flag is just a marker
_ => {}
}
}
Ok(Annotations {
annotations: per_predicate,
flag_values,
engine,
user_attached_databases,
dataset_override,
compile_as_udf,
compile_as_tvf,
bare_aggregation,
engine_options,
})
}
fn field_values(head: &crate::parser::JsonObject) -> Vec<(String, Json)> {
let mut result = Vec::new();
if let Some(record) = head.get("record") {
if let Some(fvs) = record.as_object().get("field_value") {
for fv in fvs.as_array() {
let field = &fv.as_object()["field"];
let value = &fv.as_object()["value"];
let field_str = if field.is_int() {
field.as_int().to_string()
} else {
field.as_str().to_string()
};
let val = if let Some(expr) = value.as_object().get("expression") {
expr.clone()
} else {
value.clone()
};
result.push((field_str, val));
}
}
}
result
}
fn extract_predicate_name(val: &Json) -> Option<String> {
if val.is_object() {
if let Some(var) = val.as_object().get("variable") {
return Some(var.as_object()["var_name"].as_var_name());
}
if let Some(lit) = val.as_object().get("literal") {
if let Some(pred) = lit.as_object().get("the_predicate") {
return Some(pred.as_object()["predicate_name"].as_str().to_string());
}
}
}
None
}
fn predicate_name_from_field(fvs: &[(String, Json)], field: &str) -> Option<String> {
fvs.iter()
.find(|(k, _)| k == field)
.and_then(|(_, v)| Self::extract_predicate_name(v))
}
/// Get the target engine name.
pub fn engine(&self) -> &str {
&self.engine
}
/// Whether the engine is DuckDB with motherduck enabled.
pub fn is_motherduck(&self) -> bool {
self.engine_options.get("motherduck")
.map(|v| !v.is_null())
.unwrap_or(false)
}
/// Whether Clingo integration is requested.
pub fn needs_clingo(&self) -> bool {
self.engine_options.get("clingo")
.map(|v| !v.is_null())
.unwrap_or(false)
}
/// Whether type checking is enabled for the current engine.
/// Matches Python's ShouldTypecheck().
pub fn should_typecheck(&self) -> bool {
let typechecks_by_default = self.engine_typechecks_by_default();
// Check @Engine annotation for explicit type_checking setting
if let Some(type_checking) = self.engine_options.get("type_checking") {
if type_checking.is_bool() {
return match type_checking {
Json::Bool(b) => *b,
_ => typechecks_by_default,
};
}
// If type_checking is present but not a bool, treat as enabled
return true;
}
typechecks_by_default
}
/// Whether the engine typechecks by default.
fn engine_typechecks_by_default(&self) -> bool {
// All engines typecheck by default
true
}
/// Get the default dataset name based on engine and @Dataset annotation.
/// Matches Python's `Annotations.Dataset()`.
pub fn dataset(&self) -> String {
if let Some(ref ds) = self.dataset_override {
return ds.clone();
}
let home = home_schema();
let test = test_schema();
match self.engine.as_str() {
"psql" | "duckdb" => home.to_string(),
"sqlite" if self.user_attached_databases.contains_key(home) => home.to_string(),
_ => test.to_string(),
}
}
/// Get attached databases (user-defined + auto-attach for SQLite).
/// Matches Python's `Annotations.AttachedDatabases()`.
pub fn attached_databases(&self) -> Vec<(String, String)> {
let mut result: Vec<(String, String)> = self.user_attached_databases
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
// Auto-attach test schema for SQLite when @Ground is used and not user-attached
let test = test_schema();
if self.engine == "sqlite"
&& !self.user_attached_databases.contains_key(test)
&& !self.grounded_predicates().is_empty()
{
result.push((test.to_string(), ":memory:".to_string()));
}
result
}
/// Generate ATTACH DATABASE statements.
/// Matches Python's `Annotations.AttachDatabaseStatements()`.
pub fn attach_database_statements(&self) -> String {
let dbs = self.attached_databases();
if dbs.is_empty() {
return String::new();
}
let mut lines = Vec::new();
for (k, v) in &dbs {
// DuckDB: detach first, detect .sqlite files
if self.engine == "duckdb" {
lines.push(format!("DETACH DATABASE IF EXISTS {};", k));
}
let type_sqlite = if self.engine == "duckdb" && v.ends_with(".sqlite") {
" (TYPE SQLITE)"
} else {
""
};
lines.push(format!("ATTACH DATABASE '{}' AS {}{};", v, k, type_sqlite));
}
lines.join("\n")
}
/// Generate the full preamble for the current engine.
/// Matches Python's `Annotations.Preamble()`.
pub fn preamble(&self) -> String {
let mut preamble = String::new();
let attach = self.attach_database_statements();
if !attach.is_empty() {
preamble.push_str(&attach);
preamble.push_str("\n\n");
}
let home = home_schema();
match self.engine.as_str() {
"psql" => {
preamble.push_str(&format!(
"-- Initializing PostgreSQL environment.\n\
set client_min_messages to warning;\n\
create schema if not exists {};\n\
-- Empty logica type: logicarecord893574736;\n\
DO $$ BEGIN if not exists (select 'I(am) :- I(think)' from pg_type \
where typname = 'logicarecord893574736') then \
create type logicarecord893574736 as (nirvana numeric); \
end if; END $$;\n\n",
home
));
}
"duckdb" => {
let home_attachment = if self.user_attached_databases.contains_key(home) {
format!("-- {} attached by user.\n", home)
} else {
format!("create schema if not exists {};\n", home)
};
preamble.push_str("-- Initializing DuckDB environment.\n");
preamble.push_str(&home_attachment);
preamble.push_str(
"-- Empty record, has to have a field by DuckDB syntax.\n\
drop type if exists logicarecord893574736 cascade; \
create type logicarecord893574736 as struct(nirvana numeric);\n"
);
if self.is_motherduck() {
preamble.push('\n'); // Sequences not supported in MotherDuck
} else {
preamble.push_str("create sequence if not exists eternal_logical_sequence;\n\n");
}
// Thread count
if let Some(threads_val) = self.engine_options.get("threads") {
if let Some(s) = extract_string_literal(threads_val) {
if let Ok(n) = s.parse::<i64>() {
preamble.push_str(&format!("set threads to {};\n", n));
}
} else if threads_val.is_int() {
preamble.push_str(&format!("set threads to {};\n", threads_val.as_int()));
}
}
}
_ => {}
}
preamble
}
/// Get ground table name for a predicate, if any.
pub fn ground(&self, pred_name: &str) -> Option<Ground> {
let a = self.annotations.get(pred_name)?;
let v = a.get("ground")?;
let raw = v.as_str().to_string();
// If the stored ground name equals the predicate name (no explicit table given),
// prepend the default dataset. Matches Python:
// table_name = annotation.get('1', self.Dataset() + '.' + predicate_name)
let table_name = if raw == pred_name {
format!("{}.{}", self.dataset(), raw)
} else {
// Could be another predicate reference — check if it's grounded
if let Some(other_ground) = self.annotations
.get(&raw)
.and_then(|ann| ann.get("ground"))
{
let other_raw = other_ground.as_str().to_string();
if other_raw == raw {
format!("{}.{}", self.dataset(), other_raw)
} else {
other_raw
}
} else {
raw
}
};
// Read overwrite flag (defaults to true)
let overwrite = a.get("ground_overwrite")
.and_then(|ow| {
if let Json::Bool(b) = ow { Some(*b) }
else { extract_string_literal(ow).map(|s| s != "false") }
})
.unwrap_or(true);
// Read copy_to_file
let copy_to_file = a.get("ground_copy_to_file")
.and_then(|cf| extract_string_literal(cf));
Some(Ground { table_name, overwrite, copy_to_file })
}
/// Get ORDER BY columns for a predicate, if any.
pub fn order_by(&self, pred_name: &str) -> Option<Vec<String>> {
self.annotations
.get(pred_name)
.and_then(|a| a.get("order_by"))
.map(|v| {
v.as_array()
.iter()
.filter_map(|item| {
// Try predicate name first, then string literal
Self::extract_predicate_name(item)
.or_else(|| extract_string_literal(item))
})
.collect()
})
}
/// Generate ORDER BY SQL clause for a predicate.
/// Handles DESC syntax: @OrderBy(Pred, "col1", "DESC") → ORDER BY col1 DESC
pub fn order_by_clause(&self, pred_name: &str) -> String {
match self.order_by(pred_name) {
Some(order_by) if !order_by.is_empty() => {
let mut parts = Vec::new();
let len = order_by.len();
for i in 0..len {
if order_by[i] == "DESC" {
continue; // DESC is handled when processing the previous item
}
if i + 1 < len && order_by[i + 1] == "DESC" {
parts.push(format!("{} DESC", order_by[i]));
} else {
parts.push(order_by[i].clone());
}
}
format!(" ORDER BY {}", parts.join(", "))
}
_ => String::new(),
}
}
/// Get LIMIT for a predicate, if any.
pub fn limit_of(&self, pred_name: &str) -> Option<i64> {
self.annotations
.get(pred_name)
.and_then(|a| a.get("limit"))
.and_then(|v| {
if !v.is_object() {
return None;
}
let lit = v.as_object().get("literal")?;
let num = lit.as_object().get("the_number")?;
if num.is_int() {
Some(num.as_int())
} else if num.is_object() {
// Parser stores numbers as {"number": "10"}
num.as_object().get("number")
.and_then(|s| s.as_str().parse::<i64>().ok())
} else {
None
}
})
}
/// Generate LIMIT SQL clause for a predicate.
pub fn limit_clause(&self, pred_name: &str) -> String {
match self.limit_of(pred_name) {
Some(n) => format!(" LIMIT {}", n),
None => String::new(),
}
}
/// Whether predicate should use WITH (CTE).
pub fn use_with(&self, pred_name: &str) -> bool {
self.annotations
.get(pred_name)
.and_then(|a| a.get("with"))
.map(|v| match v {
Json::Bool(b) => *b,
_ => true,
})
.unwrap_or(true)
}
/// Whether a predicate is explicitly marked @NoInject.
pub fn no_inject(&self, pred_name: &str) -> bool {
self.annotations
.get(pred_name)
.and_then(|a| a.get("no_inject"))
.map(|v| match v { Json::Bool(b) => *b, _ => false })
.unwrap_or(false)
}
/// Whether a predicate can be injected (inlined into callers).
/// Like Python's OkInjection: false if OrderBy, Limit, Ground, NoInject, or ForceWith.
pub fn ok_injection(&self, pred_name: &str) -> bool {
if self.order_by(pred_name).is_some()
|| self.limit_of(pred_name).is_some()
|| self.ground(pred_name).is_some()
|| self.no_inject(pred_name)
|| self.force_with(pred_name)
{
return false;
}
true
}
/// Whether @With is explicitly set (not just the default).
pub fn force_with(&self, pred_name: &str) -> bool {
self.annotations
.get(pred_name)
.and_then(|a| a.get("with"))
.map(|v| match v { Json::Bool(b) => *b, _ => false })
.unwrap_or(false)
}
/// Get all raw rules for a given annotation name (e.g. "@Recursive", "@Make").
/// Returns None if the annotation doesn't exist.
pub fn get_annotation_rules(&self, _annotation: &str) -> Option<Vec<Json>> {
// This method is called from functors, but the Annotations struct
// doesn't store raw rules anymore. We need to provide this differently.
// For now, return None - the caller should use alternative methods.
None
}
/// Get iteration definitions from @Iteration annotations.
/// Get iteration definitions from @Iteration annotations.
/// Matches Python's Annotations.Iterations().
pub fn iterations(&self) -> CompileResult<HashMap<String, IterationDef>> {
let mut result = HashMap::new();
let prefix = "@Iteration_";
for (key, annot) in &self.annotations {
if !key.starts_with(prefix) {
continue;
}
let iteration_name = key[prefix.len()..].to_string();
// Extract predicates list
let predicates = if let Some(preds_json) = annot.get("predicates") {
// predicates is a list of predicate symbols
match preds_json {
Json::Array(arr) => {
arr.iter().filter_map(|p| {
p.as_object().get("predicate_name")
.map(|pn| pn.as_str().to_string())
}).collect()
}
Json::Object(o) => {
// Could be a single predicate or nested
if let Some(pn) = o.get("predicate_name") {
vec![pn.as_str().to_string()]
} else {
// Try to extract from the_list structure
if let Some(the_list) = o.get("the_list") {
if let Some(elements) = the_list.as_object().get("element") {
elements.as_array().iter().filter_map(|e| {
e.as_object().get("literal")
.and_then(|l| l.as_object().get("the_predicate"))
.and_then(|p| p.as_object().get("predicate_name"))
.map(|pn| pn.as_str().to_string())
}).collect()
} else { vec![] }
} else { vec![] }
}
}
_ => vec![],
}
} else {
return Err(CompileError::new(
"Iteration must specify list of predicates.".to_string(), &iteration_name));
};
// Extract repetitions
let repetitions = if let Some(rep) = annot.get("repetitions") {
if rep.is_int() {
rep.as_int()
} else if rep.is_object() {
rep.as_object().get("number")
.and_then(|n| n.as_str().parse::<i64>().ok())
.unwrap_or(10)
} else { 10 }
} else {
return Err(CompileError::new(
"Iteration must specify number of repetitions.".to_string(), &iteration_name));
};
// Extract optional stop_signal
let stop_signal = annot.get("stop_signal").and_then(|ss| {
ss.as_object().get("predicate_name")
.map(|pn| pn.as_str().to_string())
});
result.insert(iteration_name, IterationDef {
predicates,
repetitions,
stop_signal,
});
}
Ok(result)
}
/// Get the set of grounded predicates.
pub fn grounded_predicates(&self) -> HashSet<String> {
self.annotations
.iter()
.filter(|(_, v)| v.contains_key("ground"))
.map(|(k, _)| k.clone())
.collect()
}
}
#[cfg(test)]
#[path = "annotations_test.rs"]
mod annotations_test;