ptnet_core/
net.rs

1/*!
2This module provides the basic structural traits for a net.
3
4
5## Token Types (Color)
6
7In some cases it is desirable to be able to distinguish between *token types* or token *colors*. In this case the
8definition of a Colored Petri Net (CPN) adds the set of colors \\(C\\) to the tuple.
9
10$$\tag{Colored Petri Net} CPN = \left\langle P,T,A,C \right\rangle$$
11
12$$\tag{Colored Marking Function} M: P \mapsto C \times \mathbb{N}$$
13
14*/
15
16use crate::{HasIdentity, HasLabel, NodeId};
17use std::fmt::Debug;
18
19// ------------------------------------------------------------------------------------------------
20// Public Types  Net
21// ------------------------------------------------------------------------------------------------
22
23///
24/// This is the core trait for a place within a place/transition net. Given a net
25/// \\(N=\left\langle P,T,A  \right\rangle\\) a place \\(p\\) is a member of the set \\(P\\) such
26/// that \\(p\in P\\)
27///
28pub trait Place: Debug + HasIdentity + HasLabel {}
29
30///
31/// This is the core trait for a transition within a place/transition net. Given a net
32/// \\(N=\left\langle P,T,A  \right\rangle\\) a transition \\(t\\) is a member of the set \\(T\\) such
33/// that \\(t\in T\\)
34///
35pub trait Transition: Debug + HasIdentity + HasLabel {}
36
37///
38/// This is the core trait for an arc within a place/transition net. Given a net
39/// \\(N=\left\langle P,T,A  \right\rangle\\) an arc \\(a\\) is a member of the set \\(A\\) such
40/// that \\(a\in A\\)
41///
42pub trait Arc: Debug {
43    fn source(&self) -> NodeId;
44    fn target(&self) -> NodeId;
45}
46
47///
48/// This is the core trait for a Net \\(N\\) comprising a set of places \\(P\\), a set of transitions
49/// \\(T\\), and a set of arcs \\(A\\) such that \\(N=\left\langle P,T,A  \right\rangle\\)
50///
51pub trait Net: Debug {
52    type Place: Place;
53    type Transition: Transition;
54    type Arc: Arc;
55
56    fn places(&self) -> Vec<&Self::Place>;
57    fn place(&self, id: &NodeId) -> Option<&Self::Place>;
58    fn place_mut(&mut self, id: &NodeId) -> Option<&mut Self::Place>;
59    fn add_place(&mut self) -> NodeId;
60    fn add_labeled_place<S>(&mut self, label: S) -> NodeId
61    where
62        S: Into<String>;
63
64    fn transitions(&self) -> Vec<&Self::Transition>;
65    fn transition(&self, id: &NodeId) -> Option<&Self::Transition>;
66    fn transition_mut(&mut self, id: &NodeId) -> Option<&mut Self::Transition>;
67    fn add_transition(&mut self) -> NodeId;
68    fn add_labeled_transition<S>(&mut self, label: S) -> NodeId
69    where
70        S: Into<String>;
71
72    fn arcs(&self) -> Vec<&Self::Arc>;
73    fn add_arc(&mut self, source: NodeId, target: NodeId);
74
75    fn inputs(&self, id: &NodeId) -> Vec<&NodeId>;
76    fn outputs(&self, id: &NodeId) -> Vec<&NodeId>;
77}
78
79// ------------------------------------------------------------------------------------------------
80// Public Types  Builders
81// ------------------------------------------------------------------------------------------------
82
83///
84/// A builder trait for construction of [`Net`] objects.
85///
86pub trait NetBuilder {
87    type Place: Place;
88    type Transition: Transition;
89    type Arc: Arc;
90    type Net: Net<Place = Self::Place, Transition = Self::Transition, Arc = Self::Arc>;
91    type PlaceBuilder: PlaceBuilder<TransitionBuilder = Self::TransitionBuilder>;
92    type TransitionBuilder: TransitionBuilder<PlaceBuilder = Self::PlaceBuilder>;
93
94    fn place(&mut self) -> Self::PlaceBuilder;
95    fn place_with_id(&mut self, id: &NodeId) -> Self::PlaceBuilder;
96    fn recall_place(&mut self, tag: &'static str) -> Self::PlaceBuilder;
97
98    fn transition(&mut self) -> Self::TransitionBuilder;
99    fn transition_with_id(&mut self, id: &NodeId) -> Self::TransitionBuilder;
100    fn recall_transition(&mut self, tag: &'static str) -> Self::TransitionBuilder;
101
102    fn arc(&mut self, source: NodeId, target: NodeId) -> &mut Self;
103
104    fn recall(&self, tag: &'static str) -> Option<NodeId>;
105
106    fn build(self) -> Self::Net;
107}
108
109///
110/// A builder trait for construction of [`Place`] objects.
111///
112pub trait PlaceBuilder {
113    type TransitionBuilder: TransitionBuilder;
114
115    fn with_label<S>(self, _label: S) -> Self
116    where
117        S: Into<String>;
118    fn id(&self) -> NodeId;
119
120    fn remember_as(self, tag: &'static str) -> Self;
121
122    fn to_transition(self) -> Self::TransitionBuilder;
123    fn to_id(self, id: &NodeId) -> Self::TransitionBuilder;
124    fn to_remembered(self, tag: &'static str) -> Self::TransitionBuilder;
125
126    #[allow(clippy::wrong_self_convention)]
127    fn from_transition(self) -> Self::TransitionBuilder;
128    #[allow(clippy::wrong_self_convention)]
129    fn from_id(self, id: &NodeId) -> Self::TransitionBuilder;
130    #[allow(clippy::wrong_self_convention)]
131    fn from_remembered(self, tag: &'static str) -> Self::TransitionBuilder;
132}
133
134///
135/// A builder trait for construction of [`Transition`] objects.
136///
137pub trait TransitionBuilder {
138    type PlaceBuilder: PlaceBuilder;
139
140    fn with_label<S>(self, _label: S) -> Self
141    where
142        S: Into<String>;
143    fn id(&self) -> NodeId;
144
145    fn remember_as(self, tag: &'static str) -> Self;
146
147    fn to_place(self) -> Self::PlaceBuilder;
148    fn to_id(self, id: &NodeId) -> Self::PlaceBuilder;
149    fn to_remembered(self, tag: &'static str) -> Self::PlaceBuilder;
150
151    #[allow(clippy::wrong_self_convention)]
152    fn from_place(self) -> Self::PlaceBuilder;
153    #[allow(clippy::wrong_self_convention)]
154    fn from_id(self, id: &NodeId) -> Self::PlaceBuilder;
155    #[allow(clippy::wrong_self_convention)]
156    fn from_remembered(self, tag: &'static str) -> Self::PlaceBuilder;
157}