libpetri-core 1.5.0

Core types for libpetri Coloured Time Petri Net engine
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
use std::collections::HashSet;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

use crate::action::{BoxedAction, passthrough};
use crate::arc::{Inhibitor, Read, Reset};
use crate::input::In;
use crate::output::{Out, all_places, find_forward_inputs, find_timeout};
use crate::place::PlaceRef;
use crate::timing::{Timing, immediate};

/// Unique identifier for a transition instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TransitionId(u64);

static NEXT_ID: AtomicU64 = AtomicU64::new(0);

impl TransitionId {
    fn next() -> Self {
        Self(NEXT_ID.fetch_add(1, Ordering::Relaxed))
    }
}

/// A transition in the Time Petri Net that transforms tokens.
///
/// Transitions use identity-based equality (TransitionId) — each instance is unique
/// regardless of name. The name is purely a label for display/debugging/export.
#[derive(Clone)]
pub struct Transition {
    id: TransitionId,
    name: Arc<str>,
    input_specs: Vec<In>,
    output_spec: Option<Out>,
    inhibitors: Vec<Inhibitor>,
    reads: Vec<Read>,
    resets: Vec<Reset>,
    timing: Timing,
    action_timeout: Option<u64>,
    action: BoxedAction,
    priority: i32,
    input_places: HashSet<PlaceRef>,
    read_places: HashSet<PlaceRef>,
    output_places: HashSet<PlaceRef>,
}

impl Transition {
    /// Returns the unique transition ID.
    pub fn id(&self) -> TransitionId {
        self.id
    }

    /// Returns the transition name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the name as Arc<str>.
    pub fn name_arc(&self) -> &Arc<str> {
        &self.name
    }

    /// Returns the input specifications.
    pub fn input_specs(&self) -> &[In] {
        &self.input_specs
    }

    /// Returns the output specification, if any.
    pub fn output_spec(&self) -> Option<&Out> {
        self.output_spec.as_ref()
    }

    /// Returns the inhibitor arcs.
    pub fn inhibitors(&self) -> &[Inhibitor] {
        &self.inhibitors
    }

    /// Returns the read arcs.
    pub fn reads(&self) -> &[Read] {
        &self.reads
    }

    /// Returns the reset arcs.
    pub fn resets(&self) -> &[Reset] {
        &self.resets
    }

    /// Returns the timing specification.
    pub fn timing(&self) -> &Timing {
        &self.timing
    }

    /// Returns the action timeout in ms, if any.
    pub fn action_timeout(&self) -> Option<u64> {
        self.action_timeout
    }

    /// Returns true if this transition has an action timeout.
    pub fn has_action_timeout(&self) -> bool {
        self.action_timeout.is_some()
    }

    /// Returns the transition action.
    pub fn action(&self) -> &BoxedAction {
        &self.action
    }

    /// Returns the priority (higher fires first).
    pub fn priority(&self) -> i32 {
        self.priority
    }

    /// Returns set of input place refs (consumed tokens).
    pub fn input_places(&self) -> &HashSet<PlaceRef> {
        &self.input_places
    }

    /// Returns set of read place refs (context tokens, not consumed).
    pub fn read_places(&self) -> &HashSet<PlaceRef> {
        &self.read_places
    }

    /// Returns set of output place refs (where tokens are produced).
    pub fn output_places(&self) -> &HashSet<PlaceRef> {
        &self.output_places
    }

    /// Creates a new TransitionBuilder.
    pub fn builder(name: impl Into<Arc<str>>) -> TransitionBuilder {
        TransitionBuilder::new(name)
    }
}

impl std::fmt::Debug for Transition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Transition")
            .field("id", &self.id)
            .field("name", &self.name)
            .field("timing", &self.timing)
            .field("priority", &self.priority)
            .finish()
    }
}

impl PartialEq for Transition {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Transition {}

impl std::hash::Hash for Transition {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

impl std::fmt::Display for Transition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Transition[{}]", self.name)
    }
}

/// Builder for constructing Transition instances.
pub struct TransitionBuilder {
    name: Arc<str>,
    input_specs: Vec<In>,
    output_spec: Option<Out>,
    inhibitors: Vec<Inhibitor>,
    reads: Vec<Read>,
    resets: Vec<Reset>,
    timing: Timing,
    action: BoxedAction,
    priority: i32,
}

impl TransitionBuilder {
    pub fn new(name: impl Into<Arc<str>>) -> Self {
        Self {
            name: name.into(),
            input_specs: Vec::new(),
            output_spec: None,
            inhibitors: Vec::new(),
            reads: Vec::new(),
            resets: Vec::new(),
            timing: immediate(),
            action: passthrough(),
            priority: 0,
        }
    }

    /// Add a single input specification.
    pub fn input(mut self, spec: In) -> Self {
        self.input_specs.push(spec);
        self
    }

    /// Add multiple input specifications.
    pub fn inputs(mut self, specs: Vec<In>) -> Self {
        self.input_specs.extend(specs);
        self
    }

    /// Set the output specification.
    pub fn output(mut self, spec: Out) -> Self {
        self.output_spec = Some(spec);
        self
    }

    /// Add an inhibitor arc.
    pub fn inhibitor(mut self, inh: Inhibitor) -> Self {
        self.inhibitors.push(inh);
        self
    }

    /// Add multiple inhibitor arcs.
    pub fn inhibitors(mut self, inhs: Vec<Inhibitor>) -> Self {
        self.inhibitors.extend(inhs);
        self
    }

    /// Add a read arc.
    pub fn read(mut self, r: Read) -> Self {
        self.reads.push(r);
        self
    }

    /// Add multiple read arcs.
    pub fn reads(mut self, rs: Vec<Read>) -> Self {
        self.reads.extend(rs);
        self
    }

    /// Add a reset arc.
    pub fn reset(mut self, r: Reset) -> Self {
        self.resets.push(r);
        self
    }

    /// Add multiple reset arcs.
    pub fn resets(mut self, rs: Vec<Reset>) -> Self {
        self.resets.extend(rs);
        self
    }

    /// Set timing specification.
    pub fn timing(mut self, timing: Timing) -> Self {
        self.timing = timing;
        self
    }

    /// Set the transition action.
    pub fn action(mut self, action: BoxedAction) -> Self {
        self.action = action;
        self
    }

    /// Set priority (higher fires first).
    pub fn priority(mut self, priority: i32) -> Self {
        self.priority = priority;
        self
    }

    /// Build the transition.
    ///
    /// # Panics
    /// Panics if ForwardInput references a non-input place.
    pub fn build(self) -> Transition {
        // Validate ForwardInput references
        if let Some(ref out) = self.output_spec {
            let input_place_names: HashSet<_> =
                self.input_specs.iter().map(|s| s.place_name()).collect();
            for (from, _) in find_forward_inputs(out) {
                assert!(
                    input_place_names.contains(from.name()),
                    "Transition '{}': ForwardInput references non-input place '{}'",
                    self.name,
                    from.name()
                );
            }
        }

        let action_timeout = self
            .output_spec
            .as_ref()
            .and_then(|o| find_timeout(o).map(|(ms, _)| ms));

        // Precompute place sets
        let input_places: HashSet<PlaceRef> =
            self.input_specs.iter().map(|s| s.place().clone()).collect();

        let read_places: HashSet<PlaceRef> = self.reads.iter().map(|r| r.place.clone()).collect();

        let output_places: HashSet<PlaceRef> = self
            .output_spec
            .as_ref()
            .map(all_places)
            .unwrap_or_default();

        Transition {
            id: TransitionId::next(),
            name: self.name,
            input_specs: self.input_specs,
            output_spec: self.output_spec,
            inhibitors: self.inhibitors,
            reads: self.reads,
            resets: self.resets,
            timing: self.timing,
            action_timeout,
            action: self.action,
            priority: self.priority,
            input_places,
            read_places,
            output_places,
        }
    }
}

/// Creates a new transition with a different action while preserving all arc specs.
pub(crate) fn rebuild_with_action(t: &Transition, action: BoxedAction) -> Transition {
    let mut builder = Transition::builder(Arc::clone(&t.name))
        .timing(t.timing)
        .priority(t.priority)
        .action(action)
        .inputs(t.input_specs.clone())
        .inhibitors(t.inhibitors.clone())
        .reads(t.reads.clone())
        .resets(t.resets.clone());

    if let Some(ref out) = t.output_spec {
        builder = builder.output(out.clone());
    }

    builder.build()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::input::one;
    use crate::output::out_place;
    use crate::place::Place;

    #[test]
    fn transition_builder_basic() {
        let p_in = Place::<i32>::new("in");
        let p_out = Place::<i32>::new("out");

        let t = Transition::builder("test")
            .input(one(&p_in))
            .output(out_place(&p_out))
            .build();

        assert_eq!(t.name(), "test");
        assert_eq!(t.input_specs().len(), 1);
        assert!(t.output_spec().is_some());
        assert_eq!(t.timing(), &Timing::Immediate);
        assert_eq!(t.priority(), 0);
    }

    #[test]
    fn transition_identity() {
        let t1 = Transition::builder("test").build();
        let t2 = Transition::builder("test").build();
        assert_ne!(t1, t2); // different IDs
    }

    #[test]
    fn transition_places_computed() {
        let p_in = Place::<i32>::new("in");
        let p_out = Place::<i32>::new("out");
        let p_read = Place::<String>::new("ctx");

        let t = Transition::builder("test")
            .input(one(&p_in))
            .output(out_place(&p_out))
            .read(crate::arc::read(&p_read))
            .build();

        assert!(t.input_places().contains(&PlaceRef::new("in")));
        assert!(t.output_places().contains(&PlaceRef::new("out")));
        assert!(t.read_places().contains(&PlaceRef::new("ctx")));
    }

    #[test]
    #[should_panic(expected = "ForwardInput references non-input place")]
    fn forward_input_validation() {
        let from = Place::<i32>::new("not-an-input");
        let to = Place::<i32>::new("to");

        Transition::builder("test")
            .output(crate::output::forward_input(&from, &to))
            .build();
    }

    #[test]
    fn action_timeout_detected() {
        let p = Place::<i32>::new("timeout");
        let t = Transition::builder("test")
            .output(crate::output::timeout_place(5000, &p))
            .build();
        assert_eq!(t.action_timeout(), Some(5000));
    }

    #[test]
    fn no_action_timeout() {
        let p = Place::<i32>::new("out");
        let t = Transition::builder("test").output(out_place(&p)).build();
        assert_eq!(t.action_timeout(), None);
    }
}