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
//! AssociationModel evaluation — antecedent → consequent rule firing.
//!
//! Implements `AssociationModel` where each transaction is a single active-field
//! discrete item (e.g., `"milk"`). The evaluator:
//!
//! 1. Resolves the input `Discrete` symbol to its `Item/@id` via `items`.
//! 2. Treats the single item as an antecedent `Itemset` and scans `AssociationRule`s
//! for one whose `antecedent` `Itemset` contains that `Item` id.
//! 3. Returns the first item of the `consequent` `Itemset`'s value.
//!
//! The PMML `InputTable` / `JoinTable` host factor for transactional inputs is not yet
//! implemented; the path handles single-item transactions as used by fixtures.
//!
//! # What belongs here
//!
//! - [`evaluate_association`] — the single public entry point.
//!
//! # Performance
//!
//! `O(items + itemsets + rules * antecedent_size)`; linear scan, no heap beyond two small `HashMap`s.
//! Typically `rules < 100`.
use crateValue;
use crateAssociationIr;
/// Evaluate an [`AssociationIr`] against a dense `values` array.
///
/// Single-item transactional path: `mining_schema.active_fields[0]` holds the input item
/// as `Discrete`. The function reverse-looks up its `Item.id`, then finds the first rule
/// whose antecedent itemset contains that id and returns the consequent's first item value.
/// When the input is `Missing`/`Continuous`, or no item/rule matches, `Missing` is returned.
///
/// # Parameters
///
/// - `assoc`: Lowered association model (`AssociationIr`) with `items`, `itemsets`, `rules`, `mining_schema`.
/// - `values`: Dense `&[Value]` indexed by [`FieldId`](crate::base::FieldId). Only `active_fields[0]` is read; out-of-bounds → `Missing`.
///
/// # Returns
///
/// `Discrete(consequent_value)` when a rule fires, otherwise `Missing`.
///
/// # Panics
///
/// Never panics. All `FieldId` indexing is bounds-checked.
///
/// # Performance
///
/// `O(items + itemsets + rules * antecedent_size)` scans; small constants for fixture sizes.
///
/// # Examples
///
/// ```
/// use pmmlruntime::base::{FieldId, SymbolId, Value};
/// use pmmlruntime::ir::*;
/// use pmmlruntime::engine::models::evaluate_association;
///
/// let fid = FieldId(0);
/// let sid_milk = SymbolId(1);
/// let sid_bread = SymbolId(2);
/// let model = AssociationIr {
/// function_name: "associationRules".into(),
/// mining_schema: MiningSchemaIr { active_fields: vec![fid], target_field: None, field_metas: vec![], missing_value_replacement: None },
/// output: vec![],
/// items: vec![ItemIr { id: "1".into(), value: sid_milk }, ItemIr { id: "2".into(), value: sid_bread }],
/// itemsets: vec![ItemsetIr { id: "a".into(), item_ids: vec!["1".into()] }, ItemsetIr { id: "c".into(), item_ids: vec!["2".into()] }],
/// rules: vec![AssociationRuleIr { antecedent: "a".into(), consequent: "c".into(), support: 0.1, confidence: 0.9, lift: 1.2 }],
/// };
/// let pred = evaluate_association(&model, &[Value::Discrete(sid_milk)]);
/// assert_eq!(pred, Value::Discrete(sid_bread));
/// assert_eq!(evaluate_association(&model, &[Value::Discrete(SymbolId(99))]), Value::Missing);
/// ```