Struct pns::SimulatedNet

source ·
pub struct SimulatedNet { /* private fields */ }
Expand description

A wrapper type for net to support managing multiple states for simulations.

Implementations§

source§

impl SimulatedNet

source

pub fn new() -> SimulatedNet

Create a new, empty petri net in simulated state.

source

pub fn load(filename: &Path) -> Result<SimulatedNet, Error>

Load a petri net from a file in simulated state.

Examples found in repository?
examples/example.rs (line 24)
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
fn main() -> Result<(), Error> {
    let mut save = false;
    let mut load = false;

    let mut args = std::env::args();
    args.next();
    for arg in args {
        match arg.as_ref() {
            "save" => save = true,
            "load" => load = true,
            _ => eprintln!("Ignore invalid argument '{arg}'"),
        }
    }

    let mut net = SimulatedNet::load("pns/examples/example.pn".as_ref())?;

    let mut names = HashMap::new();
    let file = File::open("pns/examples/example.pnk")?;
    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
        names.insert(tid, line?);
    }

    if save {
        net.save("examples/example_copy.pn".as_ref())?;
    }

    if load {
        net.load_state("examples/example.pns".as_ref())
            .expect("State initialization failed");
    } else {
        net.add_state();
    }
    let mut forward = true;
    for mut state in &mut net {
        loop {
            let fire = if forward {
                state.fire()
            } else {
                state.unfire()
            };

            if fire.transitions.is_empty() {
                println!("Reverse play direction!");
                forward = !forward;
                continue;
            }

            for (i, tid) in fire.transitions.iter().enumerate() {
                println!("{}: {}", i + 1, names[tid]);
            }
            let stdin = io::stdin();
            let mut string = String::new();
            let size = stdin.read_line(&mut string)?;
            if size == 0 {
                continue;
            }
            match string.chars().next().unwrap() {
                'r' => {
                    println!("Reverse play direction!");
                    forward = !forward;
                    continue;
                }
                'q' => break,
                's' => {
                    println!(
                        "{}",
                        if state.save("examples/example.pns".as_ref()).is_ok() {
                            "Saving successful"
                        } else {
                            "Saving failed"
                        }
                    );
                    continue;
                }
                _ => (),
            }
            match usize::from_str(&string[..(string.len() - 1)]) {
                Ok(num) if num != 0 && num <= fire.transitions.len() => {
                    fire.finish(num - 1);
                }
                _ => {
                    println!(
                        "You have to input a valid number from 1 to {}",
                        fire.transitions.len()
                    );
                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
                    continue;
                }
            }
        }
    }

    Ok(())
}
source

pub fn from_net(net: Net) -> SimulatedNet

Convert a Net to a SimulatedNet for advanced simulation.

source

pub fn add_state(&mut self)

Add a new, empty state for simulation of a petri net.

Examples found in repository?
examples/example.rs (line 40)
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
fn main() -> Result<(), Error> {
    let mut save = false;
    let mut load = false;

    let mut args = std::env::args();
    args.next();
    for arg in args {
        match arg.as_ref() {
            "save" => save = true,
            "load" => load = true,
            _ => eprintln!("Ignore invalid argument '{arg}'"),
        }
    }

    let mut net = SimulatedNet::load("pns/examples/example.pn".as_ref())?;

    let mut names = HashMap::new();
    let file = File::open("pns/examples/example.pnk")?;
    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
        names.insert(tid, line?);
    }

    if save {
        net.save("examples/example_copy.pn".as_ref())?;
    }

    if load {
        net.load_state("examples/example.pns".as_ref())
            .expect("State initialization failed");
    } else {
        net.add_state();
    }
    let mut forward = true;
    for mut state in &mut net {
        loop {
            let fire = if forward {
                state.fire()
            } else {
                state.unfire()
            };

            if fire.transitions.is_empty() {
                println!("Reverse play direction!");
                forward = !forward;
                continue;
            }

            for (i, tid) in fire.transitions.iter().enumerate() {
                println!("{}: {}", i + 1, names[tid]);
            }
            let stdin = io::stdin();
            let mut string = String::new();
            let size = stdin.read_line(&mut string)?;
            if size == 0 {
                continue;
            }
            match string.chars().next().unwrap() {
                'r' => {
                    println!("Reverse play direction!");
                    forward = !forward;
                    continue;
                }
                'q' => break,
                's' => {
                    println!(
                        "{}",
                        if state.save("examples/example.pns".as_ref()).is_ok() {
                            "Saving successful"
                        } else {
                            "Saving failed"
                        }
                    );
                    continue;
                }
                _ => (),
            }
            match usize::from_str(&string[..(string.len() - 1)]) {
                Ok(num) if num != 0 && num <= fire.transitions.len() => {
                    fire.finish(num - 1);
                }
                _ => {
                    println!(
                        "You have to input a valid number from 1 to {}",
                        fire.transitions.len()
                    );
                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
                    continue;
                }
            }
        }
    }

    Ok(())
}
source

pub fn load_state(&mut self, filename: &Path) -> Result<(), Error>

Load a simulation state from a file.

Examples found in repository?
examples/example.rs (line 37)
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
fn main() -> Result<(), Error> {
    let mut save = false;
    let mut load = false;

    let mut args = std::env::args();
    args.next();
    for arg in args {
        match arg.as_ref() {
            "save" => save = true,
            "load" => load = true,
            _ => eprintln!("Ignore invalid argument '{arg}'"),
        }
    }

    let mut net = SimulatedNet::load("pns/examples/example.pn".as_ref())?;

    let mut names = HashMap::new();
    let file = File::open("pns/examples/example.pnk")?;
    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
        names.insert(tid, line?);
    }

    if save {
        net.save("examples/example_copy.pn".as_ref())?;
    }

    if load {
        net.load_state("examples/example.pns".as_ref())
            .expect("State initialization failed");
    } else {
        net.add_state();
    }
    let mut forward = true;
    for mut state in &mut net {
        loop {
            let fire = if forward {
                state.fire()
            } else {
                state.unfire()
            };

            if fire.transitions.is_empty() {
                println!("Reverse play direction!");
                forward = !forward;
                continue;
            }

            for (i, tid) in fire.transitions.iter().enumerate() {
                println!("{}: {}", i + 1, names[tid]);
            }
            let stdin = io::stdin();
            let mut string = String::new();
            let size = stdin.read_line(&mut string)?;
            if size == 0 {
                continue;
            }
            match string.chars().next().unwrap() {
                'r' => {
                    println!("Reverse play direction!");
                    forward = !forward;
                    continue;
                }
                'q' => break,
                's' => {
                    println!(
                        "{}",
                        if state.save("examples/example.pns".as_ref()).is_ok() {
                            "Saving successful"
                        } else {
                            "Saving failed"
                        }
                    );
                    continue;
                }
                _ => (),
            }
            match usize::from_str(&string[..(string.len() - 1)]) {
                Ok(num) if num != 0 && num <= fire.transitions.len() => {
                    fire.finish(num - 1);
                }
                _ => {
                    println!(
                        "You have to input a valid number from 1 to {}",
                        fire.transitions.len()
                    );
                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
                    continue;
                }
            }
        }
    }

    Ok(())
}
source

pub fn add_state_from_values( &mut self, values: &[u32], ) -> Result<(), StateInitializationError>

Add a new state for simulation of a petri net.

source

pub fn state(&self, index: usize) -> GenericSimulationState<'_, &'_ State>

Get the simulation state of a net at the specified index.

source

pub fn state_mut( &mut self, index: usize, ) -> GenericSimulationState<'_, &'_ mut State>

Get the simulation state of a net at the specified index.

source

pub fn states(&self) -> &[State]

Get access to the simulation states.

source

pub fn states_mut(&mut self) -> &mut [State]

Get mutable access to the simulation states.

source

pub fn release(self) -> Net

Release the Net from the SimulatedNet again. Destroys the simulation state.

source

pub fn maximum_fire_count(&self, tid: Tid) -> Option<usize>

Calculate how often a transaction could theoretically be fired.

source

pub fn maximum_unfire_count(&self, tid: Tid) -> Option<usize>

Calculate how often a transaction could theoretically be unfired.

source§

impl SimulatedNet

source

pub fn add_place(&mut self) -> Pid

Add a new place to the petri net and get the index and refresh states.

source

pub fn add_transition(&mut self) -> Tid

Add a new transition to the petri net and get the index and refresh states.

source

pub fn add_connected_transition( &mut self, in_pids: &[Pid], out_pids: &[Pid], ) -> Tid

Add a new transition to the petri net, connct it to the specified places and get the index and refresh states.

source

pub fn remove_place(&mut self, pid: Pid)

Remove a place at index pid from petri net and refresh states.

source

pub fn connect_in(&mut self, tid: Tid, pid: Pid) -> bool

Make a connection in to the transition with index tid from place with index pid and refresh states. Result represents success.

source

pub fn connect_out(&mut self, tid: Tid, pid: Pid) -> bool

Make a connection out from the transition with index tid to place with index pid and refresh states. Result represents success.

source

pub fn duplicate_transition(&mut self, tid: Tid) -> Tid

Duplicate the transition and get the index of the clone and refresh states.

source

pub fn duplicate_place(&mut self, pid: Pid) -> Pid

Duplicate the place and get the index of the clone and refresh states.

source

pub fn start(&mut self, pid: Pid, count: usize) -> usize

Increase the initial token count in place indexed by pid and refresh states.

Methods from Deref<Target = Net>§

source

pub fn save(&self, filename: &Path) -> Result<(), Error>

Save the petri net to a file. Result represents success.

Examples found in repository?
examples/example.rs (line 33)
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
fn main() -> Result<(), Error> {
    let mut save = false;
    let mut load = false;

    let mut args = std::env::args();
    args.next();
    for arg in args {
        match arg.as_ref() {
            "save" => save = true,
            "load" => load = true,
            _ => eprintln!("Ignore invalid argument '{arg}'"),
        }
    }

    let mut net = SimulatedNet::load("pns/examples/example.pn".as_ref())?;

    let mut names = HashMap::new();
    let file = File::open("pns/examples/example.pnk")?;
    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
        names.insert(tid, line?);
    }

    if save {
        net.save("examples/example_copy.pn".as_ref())?;
    }

    if load {
        net.load_state("examples/example.pns".as_ref())
            .expect("State initialization failed");
    } else {
        net.add_state();
    }
    let mut forward = true;
    for mut state in &mut net {
        loop {
            let fire = if forward {
                state.fire()
            } else {
                state.unfire()
            };

            if fire.transitions.is_empty() {
                println!("Reverse play direction!");
                forward = !forward;
                continue;
            }

            for (i, tid) in fire.transitions.iter().enumerate() {
                println!("{}: {}", i + 1, names[tid]);
            }
            let stdin = io::stdin();
            let mut string = String::new();
            let size = stdin.read_line(&mut string)?;
            if size == 0 {
                continue;
            }
            match string.chars().next().unwrap() {
                'r' => {
                    println!("Reverse play direction!");
                    forward = !forward;
                    continue;
                }
                'q' => break,
                's' => {
                    println!(
                        "{}",
                        if state.save("examples/example.pns".as_ref()).is_ok() {
                            "Saving successful"
                        } else {
                            "Saving failed"
                        }
                    );
                    continue;
                }
                _ => (),
            }
            match usize::from_str(&string[..(string.len() - 1)]) {
                Ok(num) if num != 0 && num <= fire.transitions.len() => {
                    fire.finish(num - 1);
                }
                _ => {
                    println!(
                        "You have to input a valid number from 1 to {}",
                        fire.transitions.len()
                    );
                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
                    continue;
                }
            }
        }
    }

    Ok(())
}
source

pub fn transition_count(&self) -> usize

The count of transitions of the petri net.

source

pub fn transition_ids(&self) -> Ids<Transition>

An iterator over the ids of existing transitions.

Examples found in repository?
examples/example.rs (line 28)
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
fn main() -> Result<(), Error> {
    let mut save = false;
    let mut load = false;

    let mut args = std::env::args();
    args.next();
    for arg in args {
        match arg.as_ref() {
            "save" => save = true,
            "load" => load = true,
            _ => eprintln!("Ignore invalid argument '{arg}'"),
        }
    }

    let mut net = SimulatedNet::load("pns/examples/example.pn".as_ref())?;

    let mut names = HashMap::new();
    let file = File::open("pns/examples/example.pnk")?;
    for (tid, line) in net.transition_ids().zip(BufReader::new(file).lines()) {
        names.insert(tid, line?);
    }

    if save {
        net.save("examples/example_copy.pn".as_ref())?;
    }

    if load {
        net.load_state("examples/example.pns".as_ref())
            .expect("State initialization failed");
    } else {
        net.add_state();
    }
    let mut forward = true;
    for mut state in &mut net {
        loop {
            let fire = if forward {
                state.fire()
            } else {
                state.unfire()
            };

            if fire.transitions.is_empty() {
                println!("Reverse play direction!");
                forward = !forward;
                continue;
            }

            for (i, tid) in fire.transitions.iter().enumerate() {
                println!("{}: {}", i + 1, names[tid]);
            }
            let stdin = io::stdin();
            let mut string = String::new();
            let size = stdin.read_line(&mut string)?;
            if size == 0 {
                continue;
            }
            match string.chars().next().unwrap() {
                'r' => {
                    println!("Reverse play direction!");
                    forward = !forward;
                    continue;
                }
                'q' => break,
                's' => {
                    println!(
                        "{}",
                        if state.save("examples/example.pns".as_ref()).is_ok() {
                            "Saving successful"
                        } else {
                            "Saving failed"
                        }
                    );
                    continue;
                }
                _ => (),
            }
            match usize::from_str(&string[..(string.len() - 1)]) {
                Ok(num) if num != 0 && num <= fire.transitions.len() => {
                    fire.finish(num - 1);
                }
                _ => {
                    println!(
                        "You have to input a valid number from 1 to {}",
                        fire.transitions.len()
                    );
                    println!("You can also quit by writing \"q\", save the current state by writing \"s\" or reverse by writing \"r\"");
                    continue;
                }
            }
        }
    }

    Ok(())
}
source

pub fn transition(&self, tid: Tid) -> &Node<Pid>

A node representing a transition of the petri net.

source

pub fn reusable_transition(&self) -> Option<Tid>

Returns the index of the next reusable transition.

source

pub fn place_count(&self) -> usize

The count of places of the petri net.

source

pub fn place_ids(&self) -> Ids<Place>

An iterator over the ids of existing places.

source

pub fn place(&self, pid: Pid) -> &Node<Tid>

A node representing a place of the petri net.

source

pub fn reusable_place(&self) -> Option<Pid>

Returns the index of the next reusable place.

source

pub fn initial_token_count(&self, pid: Pid) -> usize

The initial token count for a place of the petri net.

Trait Implementations§

source§

impl Clone for SimulatedNet

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for SimulatedNet

source§

fn default() -> SimulatedNet

Returns the “default value” for a type. Read more
source§

impl Deref for SimulatedNet

§

type Target = Net

The resulting type after dereferencing.
source§

fn deref(&self) -> &Net

Dereferences the value.
source§

impl From<SimulatedNet> for DynamicNet

source§

fn from(net: SimulatedNet) -> Self

Converts to this type from the input type.
source§

impl<'net> IntoIterator for &'net SimulatedNet

§

type Item = GenericSimulationState<'net, &'net State>

The type of the elements being iterated over.
§

type IntoIter = Iter<'net>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'net> IntoIterator for &'net mut SimulatedNet

§

type Item = GenericSimulationState<'net, &'net mut State>

The type of the elements being iterated over.
§

type IntoIter = IterMut<'net>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.