petri-net-simulation 0.4.0

A library for simulating petri nets
Documentation
use std::ops::Deref;

use pns::{Pid, Tid};

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

/// 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)
    }
}