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
use std::ops::Deref;

use pns::{Pid, Tid};

use super::{maximum_fire_count, maximum_unfire_count, PetriNetInfo};

/// A wrapper type for net which only supports safe edits.
pub struct EditNet<'a> {
    net: &'a mut PetriNetInfo,
}

impl Deref for EditNet<'_> {
    type Target = PetriNetInfo;
    fn deref(&self) -> &PetriNetInfo {
        self.net
    }
}

impl<'a> EditNet<'a> {
    pub(crate) fn new(net: &'a mut PetriNetInfo) -> Self {
        Self { net }
    }
}

impl EditNet<'_> {
    /// Add a new place to the petri net and get the index and refresh states.
    #[inline]
    pub fn add_place(&mut self) -> Pid {
        self.net.add_place()
    }

    /// Add a new transition to the petri net and get the index and refresh states.
    #[inline]
    pub fn add_transition(&mut self) -> Tid {
        self.net.add_transition()
    }

    /// Add a new transition to the petri net, connct it to the specified places and get the index and refresh states.
    #[inline]
    pub fn add_connected_transition(&mut self, in_pids: &[Pid], out_pids: &[Pid]) -> Tid {
        self.net.add_connected_transition(in_pids, out_pids)
    }

    /// Remove a place at index `pid` from petri net and refresh states.
    #[inline]
    pub fn remove_place(&mut self, pid: Pid) {
        self.net.remove_place(pid)
    }

    /// Make a connection in to the transition with index `tid` from place with index `pid` and refresh states.
    /// Result represents success.
    #[inline]
    pub fn connect_place_to_transition(&mut self, pid: Pid, tid: Tid) -> bool {
        let minimal_count = maximum_fire_count(self.net, tid);

        let count = self.net.initial_token_count(pid);
        let Some(minimal_count) = minimal_count else {
            return false;
        };
        if count < minimal_count {
            return false;
        }

        self.net.connect_place_to_transition(pid, tid)
    }

    /// Make a connection out from the transition with index `tid` to place with index `pid` and refresh states.
    /// Result represents success.
    #[inline]
    pub fn connect_transition_to_place(&mut self, tid: Tid, pid: Pid) -> bool {
        let minimal_count = maximum_unfire_count(self.net, tid);

        let count = self.net.initial_token_count(pid);
        let Some(minimal_count) = minimal_count else {
            return false;
        };
        if count < minimal_count {
            return false;
        }

        self.net.connect_transition_to_place(tid, pid)
    }

    /// Duplicate the transition and get the index of the clone and refresh states.
    #[inline]
    pub fn duplicate_transition(&mut self, tid: Tid) -> Tid {
        self.net.duplicate_transition(tid)
    }

    /// Duplicate the place and get the index of the clone and refresh states.
    #[inline]
    pub fn duplicate_place(&mut self, pid: Pid) -> Pid {
        self.net.duplicate_place(pid)
    }

    /// Increase the initial token count in place indexed by `pid` and refresh states.
    #[inline]
    pub fn add_initial_tokens(&mut self, pid: Pid, count: usize) -> usize {
        self.net.add_initial_tokens(pid, count)
    }
}