1 | #region License Information
|
---|
2 | /* SimSharp - A .NET port of SimPy, discrete event simulation framework
|
---|
3 | Copyright (C) 2002-2016 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 | using System.Collections.Specialized;
|
---|
22 |
|
---|
23 | namespace SimSharp {
|
---|
24 | /// <summary>
|
---|
25 | /// Conditions are events that execute when any or all of its sub-events are executed.
|
---|
26 | /// </summary>
|
---|
27 | public abstract class Condition : Event {
|
---|
28 |
|
---|
29 | public new OrderedDictionary Value {
|
---|
30 | get { return (OrderedDictionary)base.Value; }
|
---|
31 | set { base.Value = value; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | protected List<Event> Events { get; private set; }
|
---|
35 |
|
---|
36 | protected List<Event> FiredEvents { get; private set; }
|
---|
37 |
|
---|
38 | protected Condition(Environment environment, params Event[] events)
|
---|
39 | : this(environment, (IEnumerable<Event>)events) { }
|
---|
40 | protected Condition(Environment environment, IEnumerable<Event> events)
|
---|
41 | : base(environment) {
|
---|
42 | CallbackList.Add(CollectValues);
|
---|
43 | Events = new List<Event>(events);
|
---|
44 | FiredEvents = new List<Event>();
|
---|
45 |
|
---|
46 | foreach (var @event in Events) {
|
---|
47 | if (Environment != @event.Environment)
|
---|
48 | throw new ArgumentException("It is not allowed to mix events from different environments");
|
---|
49 | if (@event.IsProcessed) Check(@event);
|
---|
50 | else @event.AddCallback(Check);
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (IsAlive && Evaluate())
|
---|
54 | Succeed();
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected void Check(Event @event) {
|
---|
58 | if (IsTriggered || IsProcessed) {
|
---|
59 | if (!@event.IsOk) throw new InvalidOperationException(
|
---|
60 | @"Errors that happen after the condition has been triggered will not be
|
---|
61 | handled by the condition and cause the simulation to crash.");
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | FiredEvents.Add(@event);
|
---|
65 |
|
---|
66 | if (!@event.IsOk)
|
---|
67 | Fail(@event.Value);
|
---|
68 | else if (Evaluate()) {
|
---|
69 | Succeed();
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected virtual IEnumerable<KeyValuePair<object, object>> GetValues() {
|
---|
74 | var values = new List<KeyValuePair<object, object>>();
|
---|
75 | foreach (var e in Events) {
|
---|
76 | var condition = e as Condition;
|
---|
77 | if (condition != null) {
|
---|
78 | values.AddRange(condition.GetValues());
|
---|
79 | } else if (e.IsProcessed) {
|
---|
80 | values.Add(new KeyValuePair<object, object>(e, e.Value));
|
---|
81 | }
|
---|
82 | }
|
---|
83 | return values;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected virtual void CollectValues(Event @event) {
|
---|
87 | if (@event.IsOk) {
|
---|
88 | var value = new OrderedDictionary();
|
---|
89 | foreach (var v in GetValues())
|
---|
90 | value.Add(v.Key, v.Value);
|
---|
91 | Value = value;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected abstract bool Evaluate();
|
---|
96 | }
|
---|
97 | }
|
---|