1 | #region License Information
|
---|
2 | /* SimSharp - A .NET port of SimPy, discrete event simulation framework
|
---|
3 | Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 |
|
---|
5 | This program is free software: you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation, either version 3 of the License, or
|
---|
8 | (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program. If not, see <http://www.gnu.org/licenses/>.*/
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | using System;
|
---|
20 | using System.Collections.Generic;
|
---|
21 |
|
---|
22 | namespace SimSharp {
|
---|
23 | /// <summary>
|
---|
24 | /// A Process handles the iteration of events. Processes may define steps that
|
---|
25 | /// a certain entity in the simulation has to perform. Each time the process
|
---|
26 | /// should wait it yields an event and will be resumed when that event is processed.
|
---|
27 | /// </summary>
|
---|
28 | /// <remarks>
|
---|
29 | /// Since an iterator method does not have access to its process, the method can
|
---|
30 | /// retrieve the associated Process through the ActiveProcess property of the
|
---|
31 | /// environment. Each Process sets and resets that property during Resume.
|
---|
32 | /// </remarks>
|
---|
33 | public class Process : Event {
|
---|
34 | private readonly IEnumerator<Event> generator;
|
---|
35 | private Event target;
|
---|
36 | /// <summary>
|
---|
37 | /// Target is the event that is expected to be executed next in the process.
|
---|
38 | /// </summary>
|
---|
39 | public Event Target {
|
---|
40 | get { return target; }
|
---|
41 | protected set { target = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Sets up a new process.
|
---|
46 | /// The process places an initialize event into the event queue which starts
|
---|
47 | /// the process by retrieving events from the generator.
|
---|
48 | /// </summary>
|
---|
49 | /// <param name="environment">The environment in which the process lives.</param>
|
---|
50 | /// <param name="generator">The generator function of the process.</param>
|
---|
51 | /// <param name="priority">The priority if multiple processes are started at the same time.</param>
|
---|
52 | public Process(Simulation environment, IEnumerable<Event> generator, int priority = 0)
|
---|
53 | : base(environment) {
|
---|
54 | this.generator = generator.GetEnumerator();
|
---|
55 | IsOk = true;
|
---|
56 | target = new Initialize(environment, this, priority);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /// <summary>
|
---|
60 | /// This interrupts a process and causes the IsOk flag to be set to false.
|
---|
61 | /// If a process is interrupted the iterator method needs to call HandleFault()
|
---|
62 | /// before continuing to yield further events.
|
---|
63 | /// </summary>
|
---|
64 | /// <exception cref="InvalidOperationException">This is thrown in three conditions:
|
---|
65 | /// - If the process has already been triggered.
|
---|
66 | /// - If the process attempts to interrupt itself.
|
---|
67 | /// - If the process continues to yield events despite being faulted.</exception>
|
---|
68 | /// <param name="cause">The cause of the interrupt.</param>
|
---|
69 | /// <param name="priority">The priority to rank events at the same time (smaller value = higher priority).</param>
|
---|
70 | public virtual void Interrupt(object cause = null, int priority = 0) {
|
---|
71 | if (IsTriggered) throw new InvalidOperationException("The process has terminated and cannot be interrupted.");
|
---|
72 | if (Environment.ActiveProcess == this) throw new InvalidOperationException("A process is not allowed to interrupt itself.");
|
---|
73 |
|
---|
74 | var interruptEvent = new Event(Environment);
|
---|
75 | interruptEvent.AddCallback(Resume);
|
---|
76 | interruptEvent.Fail(cause, priority);
|
---|
77 |
|
---|
78 | if (Target != null)
|
---|
79 | Target.RemoveCallback(Resume);
|
---|
80 | }
|
---|
81 |
|
---|
82 | protected virtual void Resume(Event @event) {
|
---|
83 | Environment.ActiveProcess = this;
|
---|
84 | while (true) {
|
---|
85 | if (@event.IsOk) {
|
---|
86 | if (generator.MoveNext()) {
|
---|
87 | if (IsTriggered) {
|
---|
88 | // the generator called e.g. Environment.ActiveProcess.Fail
|
---|
89 | Environment.ActiveProcess = null;
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | if (!ProceedToEvent()) {
|
---|
93 | @event = target;
|
---|
94 | continue;
|
---|
95 | } else break;
|
---|
96 | } else if (!IsTriggered) {
|
---|
97 | Succeed(@event.Value);
|
---|
98 | break;
|
---|
99 | } else break;
|
---|
100 | } else {
|
---|
101 | /* Fault handling differs from SimPy as in .NET it is not possible to inject an
|
---|
102 | * exception into an enumerator and it is impossible to put a yield return inside
|
---|
103 | * a try-catch block. In SimSharp the Process will set IsOk and will then move to
|
---|
104 | * the next yield in the generator. However, if after this move IsOk is still false
|
---|
105 | * we know that the error was not handled. It is assumed the error is handled if
|
---|
106 | * HandleFault() is called on the environment's ActiveProcess which will reset the
|
---|
107 | * flag. */
|
---|
108 | IsOk = false;
|
---|
109 | Value = @event.Value;
|
---|
110 |
|
---|
111 | if (generator.MoveNext()) {
|
---|
112 | if (IsTriggered) {
|
---|
113 | // the generator called e.g. Environment.ActiveProcess.Fail
|
---|
114 | Environment.ActiveProcess = null;
|
---|
115 | return;
|
---|
116 | }
|
---|
117 | // if we move next, but IsOk is still false
|
---|
118 | if (!IsOk) throw new InvalidOperationException("The process did not react to being faulted.");
|
---|
119 | // otherwise HandleFault was called and the fault was handled
|
---|
120 | if (ProceedToEvent()) break;
|
---|
121 | } else if (!IsTriggered) {
|
---|
122 | if (!IsOk) Fail(@event.Value);
|
---|
123 | else Succeed(@event.Value);
|
---|
124 | break;
|
---|
125 | } else break;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | Environment.ActiveProcess = null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected virtual bool ProceedToEvent() {
|
---|
132 | target = generator.Current;
|
---|
133 | Value = target.Value;
|
---|
134 | if (target.IsProcessed) return false;
|
---|
135 | target.AddCallback(Resume);
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /// <summary>
|
---|
140 | /// This method must be called to reset the IsOk flag of the process back to true.
|
---|
141 | /// The IsOk flag may be set to false if the process waited on an event that failed.
|
---|
142 | /// </summary>
|
---|
143 | /// <remarks>
|
---|
144 | /// In SimPy a faulting process would throw an exception which is then catched and
|
---|
145 | /// chained. In SimSharp catching exceptions from a yield is not possible as a yield
|
---|
146 | /// return statement may not throw an exception.
|
---|
147 | /// If a processes faulted the Value property may indicate a cause for the fault.
|
---|
148 | /// </remarks>
|
---|
149 | /// <returns>True if a faulting situation needs to be handled, false if the process
|
---|
150 | /// is okay and the last yielded event succeeded.</returns>
|
---|
151 | public virtual bool HandleFault() {
|
---|
152 | if (IsOk) return false;
|
---|
153 | IsOk = true;
|
---|
154 | return true;
|
---|
155 | }
|
---|
156 |
|
---|
157 | private class Initialize : Event {
|
---|
158 | public Initialize(Simulation environment, Process process, int priority)
|
---|
159 | : base(environment) {
|
---|
160 | CallbackList.Add(process.Resume);
|
---|
161 | IsOk = true;
|
---|
162 | IsTriggered = true;
|
---|
163 | environment.Schedule(this, priority);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|