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
//! OCEL Process Evidence Tests
//!
//! Van der Aalst-inspired validation of object-centric event log structure.
//! Tests verify that OCEL representation of autonomic cycles follows lawful
//! object lifecycle patterns: no orphaned objects, proper phase sequencing,
//! correct event-object relationships, and temporal monotonicity.
//!
//! Oracle types:
//! - Rank 1: Mathematical invariants (cardinality, ordering, finiteness)
//! - Rank 2: Domain contracts (4 phases per cycle, no orphans)
//! - Rank 3: Metamorphic (relationships between run count and event count)
use chrono::{Duration, Utc};
use std::collections::{BTreeMap, HashMap, HashSet};
use wasm4pm::models::{OCELEvent, OCELEventObjectRef, OCELObject, OCEL};
// ============================================================================
// Test Helper
// ============================================================================
/// Build a test OCEL with N autonomic cycles.
/// Each cycle has 4 phases (Perception, Decision, Protection, Optimization),
/// represented as 4 events with monotonically increasing timestamps.
fn build_test_ocel(cycles: usize) -> OCEL {
let phases = ["Perception", "Decision", "Protection", "Optimization"];
let base_time = Utc::now();
let mut events = Vec::new();
let mut objects = Vec::new();
for i in 0..cycles {
let obj_id = format!("run_{}", i);
// Create cycle_run object
objects.push(OCELObject {
id: obj_id.clone(),
object_type: "cycle_run".to_string(),
attributes: BTreeMap::new(),
changes: Vec::new(),
embedded_relations: Vec::new(),
});
// Create 4 phase events for this cycle
for (j, phase) in phases.iter().enumerate() {
let event_id = format!("{}_{}", obj_id, phase.to_lowercase());
let timestamp = (base_time + Duration::seconds((i * 4 + j) as i64))
.format("%Y-%m-%dT%H:%M:%S%.3fZ")
.to_string();
events.push(OCELEvent {
id: event_id,
event_type: phase.to_string(),
timestamp,
attributes: BTreeMap::new(),
object_ids: vec![obj_id.clone()],
object_refs: vec![OCELEventObjectRef {
object_id: obj_id.clone(),
qualifier: "".to_string(),
}],
});
}
}
OCEL {
event_types: vec![
"Perception".to_string(),
"Decision".to_string(),
"Protection".to_string(),
"Optimization".to_string(),
],
object_types: vec!["cycle_run".to_string()],
events,
objects,
object_relations: Vec::new(),
}
}
// ============================================================================
// Test 1: Cardinality — Rank 1 Mathematical Invariant
// ============================================================================
#[test]
fn test_ocel_event_count_equals_four_times_cycle_count() {
// JTBD: "Every autonomic cycle produces exactly 4 events"
// Oracle Rank 1: Mathematical theorem — 4 phases per cycle
for cycles in [1, 2, 3, 5, 10] {
let ocel = build_test_ocel(cycles);
assert_eq!(
ocel.events.len(),
cycles * 4,
"OCEL with {} cycles should have {} events, got {}",
cycles,
cycles * 4,
ocel.events.len()
);
}
}
// ============================================================================
// Test 2: Phase Inventory — Rank 2 Domain Contract
// ============================================================================
#[test]
fn test_all_four_phases_present_in_ocel() {
// JTBD: "All 4 declared phases appear in the event log"
// Oracle Rank 2: Domain contract — autonomic cycle has 4 stages
let ocel = build_test_ocel(3);
let phases: HashSet<&str> = ocel.events.iter().map(|e| e.event_type.as_str()).collect();
let expected: HashSet<&str> = ["Perception", "Decision", "Protection", "Optimization"]
.iter()
.copied()
.collect();
assert_eq!(
phases, expected,
"OCEL must contain all 4 phases. Got: {:?}",
phases
);
}
// ============================================================================
// Test 3: Object Lifecycle — Rank 2 Domain Contract
// ============================================================================
#[test]
fn test_each_object_has_exactly_four_phase_events() {
// JTBD: "Every cycle_run object is referenced by exactly 4 events (no orphans)"
// Oracle Rank 2: Domain contract — lawful object lifecycle
let ocel = build_test_ocel(5);
// Count events per object
let mut events_per_object: HashMap<String, usize> = HashMap::new();
for event in &ocel.events {
for obj_id in &event.object_ids {
*events_per_object.entry(obj_id.clone()).or_insert(0) += 1;
}
}
// Verify every object has exactly 4 events
for (obj_id, count) in &events_per_object {
assert_eq!(
*count, 4,
"Object '{}' should have exactly 4 events, got {}",
obj_id, count
);
}
// Verify count matches object count
assert_eq!(
events_per_object.len(),
5,
"Should have 5 objects (one per cycle), got {}",
events_per_object.len()
);
}
// ============================================================================
// Test 4: No Orphaned Objects — Rank 2 Domain Contract
// ============================================================================
#[test]
fn test_no_orphaned_objects() {
// JTBD: "Every object is referenced by at least one event"
// Oracle Rank 2: Domain contract — no dangling references
let ocel = build_test_ocel(3);
let object_ids: HashSet<&str> = ocel.objects.iter().map(|o| o.id.as_str()).collect();
let referenced_ids: HashSet<&str> = ocel
.events
.iter()
.flat_map(|e| e.object_ids.iter().map(|id| id.as_str()))
.collect();
for obj_id in &object_ids {
assert!(
referenced_ids.contains(obj_id),
"Object '{}' is not referenced by any event",
obj_id
);
}
}
// ============================================================================
// Test 5: Phase Sequence — Rank 1 Mathematical Invariant
// ============================================================================
#[test]
fn test_phase_sequence_is_perception_decision_protection_optimization() {
// JTBD: "Phases execute in declared order: Perception → Decision → Protection → Optimization"
// Oracle Rank 1: Mathematical theorem — cycle phases are ordered
let ocel = build_test_ocel(3);
let phases = ["Perception", "Decision", "Protection", "Optimization"];
// For each object, collect its events in timestamp order
let mut events_by_object: HashMap<String, Vec<&OCELEvent>> = HashMap::new();
for event in &ocel.events {
for obj_id in &event.object_ids {
events_by_object
.entry(obj_id.clone())
.or_insert_with(Vec::new)
.push(event);
}
}
// Sort events within each object by timestamp
for events in events_by_object.values_mut() {
events.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
}
// Verify phase sequence per object
for (obj_id, events) in &events_by_object {
assert_eq!(events.len(), 4, "Object '{}' should have 4 events", obj_id);
for (i, event) in events.iter().enumerate() {
assert_eq!(
event.event_type, phases[i],
"Event {} for object '{}' should be '{}', got '{}'",
i, obj_id, phases[i], event.event_type
);
}
}
}
// ============================================================================
// Bonus: Temporal Monotonicity — Rank 1 Mathematical Invariant
// ============================================================================
#[test]
fn test_timestamps_are_monotonically_increasing() {
// JTBD: "Phase timestamps must increase over time (no time travel)"
// Oracle Rank 1: Mathematical invariant — temporal ordering property
let ocel = build_test_ocel(3);
// For each object, verify timestamps are increasing
let mut events_by_object: HashMap<String, Vec<&OCELEvent>> = HashMap::new();
for event in &ocel.events {
for obj_id in &event.object_ids {
events_by_object
.entry(obj_id.clone())
.or_insert_with(Vec::new)
.push(event);
}
}
for (obj_id, events) in events_by_object {
let mut sorted = events.clone();
sorted.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
for i in 1..sorted.len() {
assert!(
sorted[i].timestamp >= sorted[i - 1].timestamp,
"Object '{}': Event {} timestamp should be >= Event {} timestamp",
obj_id,
i,
i - 1
);
}
}
}