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
/* Macros for reducing boilerplate in N-ary constraint implementations.
The `impl_get_matches_nary!` macro generates `get_matches()` implementations
for self-join constraints with identical structure but varying arity.
*/
/* Generates `get_matches()` implementation for N-ary self-join constraints.
All N-ary constraints share the same pattern:
1. Extract entities and build key index
2. Iterate over N-tuples within each key group
3. Filter and collect DetailedConstraintMatch with EntityRefs
# Usage
This macro is used internally in constraint implementations:
```text
fn get_matches<'a>(&'a self, solution: &S) -> Vec<DetailedConstraintMatch<'a, Sc>> {
impl_get_matches_nary!(bi: self, solution)
}
```
Available arities: `bi`, `tri`, `quad`, `penta`
*/
#[macro_export]
macro_rules! impl_get_matches_nary {
// Bi-constraint: 2 entities
(bi: $self:expr, $solution:expr) => {{
use std::collections::HashMap;
use $crate::api::analysis::{ConstraintJustification, DetailedConstraintMatch, EntityRef};
let entities = $crate::stream::collection_extract::CollectionExtract::extract(
&$self.extractor,
$solution,
);
let cref = $self.constraint_ref();
let mut temp_index: HashMap<_, Vec<usize>> = HashMap::new();
for (i, entity) in entities.iter().enumerate() {
let key = $crate::stream::key_extract::KeyExtract::extract(
&$self.key_extractor,
$solution,
entity,
i,
);
temp_index.entry(key).or_default().push(i);
}
let mut matches = Vec::new();
for indices in temp_index.values() {
for i in 0..indices.len() {
for j in (i + 1)..indices.len() {
let idx_a = indices[i];
let idx_b = indices[j];
let a = &entities[idx_a];
let b = &entities[idx_b];
if ($self.filter)($solution, a, b, idx_a, idx_b) {
let justification = ConstraintJustification::new(vec![
EntityRef::new(a),
EntityRef::new(b),
]);
let score = $self.compute_score($solution, idx_a, idx_b);
matches.push(DetailedConstraintMatch::new(cref, score, justification));
}
}
}
}
matches
}};
// Tri-constraint: 3 entities
(tri: $self:expr, $solution:expr) => {{
use std::collections::HashMap;
use $crate::api::analysis::{ConstraintJustification, DetailedConstraintMatch, EntityRef};
let entities = $crate::stream::collection_extract::CollectionExtract::extract(
&$self.extractor,
$solution,
);
let cref = $self.constraint_ref();
let mut temp_index: HashMap<_, Vec<usize>> = HashMap::new();
for (i, entity) in entities.iter().enumerate() {
let key = $crate::stream::key_extract::KeyExtract::extract(
&$self.key_extractor,
$solution,
entity,
i,
);
temp_index.entry(key).or_default().push(i);
}
let mut matches = Vec::new();
for indices in temp_index.values() {
for pos_i in 0..indices.len() {
for pos_j in (pos_i + 1)..indices.len() {
for pos_k in (pos_j + 1)..indices.len() {
let i = indices[pos_i];
let j = indices[pos_j];
let k = indices[pos_k];
let a = &entities[i];
let b = &entities[j];
let c = &entities[k];
if ($self.filter)($solution, a, b, c) {
let justification = ConstraintJustification::new(vec![
EntityRef::new(a),
EntityRef::new(b),
EntityRef::new(c),
]);
let score = $self.compute_score($solution, i, j, k);
matches.push(DetailedConstraintMatch::new(cref, score, justification));
}
}
}
}
}
matches
}};
// Quad-constraint: 4 entities
(quad: $self:expr, $solution:expr) => {{
use std::collections::HashMap;
use $crate::api::analysis::{ConstraintJustification, DetailedConstraintMatch, EntityRef};
let entities = $crate::stream::collection_extract::CollectionExtract::extract(
&$self.extractor,
$solution,
);
let cref = $self.constraint_ref();
let mut temp_index: HashMap<_, Vec<usize>> = HashMap::new();
for (i, entity) in entities.iter().enumerate() {
let key = $crate::stream::key_extract::KeyExtract::extract(
&$self.key_extractor,
$solution,
entity,
i,
);
temp_index.entry(key).or_default().push(i);
}
let mut matches = Vec::new();
for indices in temp_index.values() {
for pos_i in 0..indices.len() {
for pos_j in (pos_i + 1)..indices.len() {
for pos_k in (pos_j + 1)..indices.len() {
for pos_l in (pos_k + 1)..indices.len() {
let i = indices[pos_i];
let j = indices[pos_j];
let k = indices[pos_k];
let l = indices[pos_l];
let a = &entities[i];
let b = &entities[j];
let c = &entities[k];
let d = &entities[l];
if ($self.filter)($solution, a, b, c, d) {
let justification = ConstraintJustification::new(vec![
EntityRef::new(a),
EntityRef::new(b),
EntityRef::new(c),
EntityRef::new(d),
]);
let score = $self.compute_score($solution, i, j, k, l);
matches.push(DetailedConstraintMatch::new(
cref,
score,
justification,
));
}
}
}
}
}
}
matches
}};
// Penta-constraint: 5 entities
(penta: $self:expr, $solution:expr) => {{
use std::collections::HashMap;
use $crate::api::analysis::{ConstraintJustification, DetailedConstraintMatch, EntityRef};
let entities = $crate::stream::collection_extract::CollectionExtract::extract(
&$self.extractor,
$solution,
);
let cref = $self.constraint_ref();
let mut temp_index: HashMap<_, Vec<usize>> = HashMap::new();
for (i, entity) in entities.iter().enumerate() {
let key = $crate::stream::key_extract::KeyExtract::extract(
&$self.key_extractor,
$solution,
entity,
i,
);
temp_index.entry(key).or_default().push(i);
}
let mut matches = Vec::new();
for indices in temp_index.values() {
for pos_i in 0..indices.len() {
for pos_j in (pos_i + 1)..indices.len() {
for pos_k in (pos_j + 1)..indices.len() {
for pos_l in (pos_k + 1)..indices.len() {
for pos_m in (pos_l + 1)..indices.len() {
let i = indices[pos_i];
let j = indices[pos_j];
let k = indices[pos_k];
let l = indices[pos_l];
let m = indices[pos_m];
let a = &entities[i];
let b = &entities[j];
let c = &entities[k];
let d = &entities[l];
let e = &entities[m];
if ($self.filter)($solution, a, b, c, d, e) {
let justification = ConstraintJustification::new(vec![
EntityRef::new(a),
EntityRef::new(b),
EntityRef::new(c),
EntityRef::new(d),
EntityRef::new(e),
]);
let score = $self.compute_score($solution, i, j, k, l, m);
matches.push(DetailedConstraintMatch::new(
cref,
score,
justification,
));
}
}
}
}
}
}
}
matches
}};
}
pub use impl_get_matches_nary;