1 | #region License Information
|
---|
2 | /*
|
---|
3 | * This file is part of SimSharp which is licensed under the MIT license.
|
---|
4 | * See the LICENSE file in the project root for more information.
|
---|
5 | */
|
---|
6 | #endregion
|
---|
7 |
|
---|
8 | using System;
|
---|
9 | using System.Collections.Generic;
|
---|
10 | using System.Collections.Specialized;
|
---|
11 |
|
---|
12 | namespace SimSharp {
|
---|
13 | /// <summary>
|
---|
14 | /// Conditions are events that execute when any or all of its sub-events are executed.
|
---|
15 | /// </summary>
|
---|
16 | public abstract class Condition : Event {
|
---|
17 |
|
---|
18 | public new OrderedDictionary Value {
|
---|
19 | get { return (OrderedDictionary)base.Value; }
|
---|
20 | set { base.Value = value; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected List<Event> Events { get; private set; }
|
---|
24 |
|
---|
25 | protected List<Event> FiredEvents { get; private set; }
|
---|
26 |
|
---|
27 | protected Condition(Simulation environment, params Event[] events)
|
---|
28 | : this(environment, (IEnumerable<Event>)events) { }
|
---|
29 | protected Condition(Simulation environment, IEnumerable<Event> events)
|
---|
30 | : base(environment) {
|
---|
31 | CallbackList.Add(CollectValues);
|
---|
32 | Events = new List<Event>(events);
|
---|
33 | FiredEvents = new List<Event>();
|
---|
34 |
|
---|
35 | foreach (var @event in Events) {
|
---|
36 | if (Environment != @event.Environment)
|
---|
37 | throw new ArgumentException("It is not allowed to mix events from different environments");
|
---|
38 | if (@event.IsProcessed) Check(@event);
|
---|
39 | else @event.AddCallback(Check);
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (IsAlive && Evaluate())
|
---|
43 | Succeed();
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected void Check(Event @event) {
|
---|
47 | if (IsTriggered || IsProcessed) {
|
---|
48 | if (!@event.IsOk) throw new InvalidOperationException(
|
---|
49 | @"Errors that happen after the condition has been triggered will not be
|
---|
50 | handled by the condition and cause the simulation to crash.");
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | FiredEvents.Add(@event);
|
---|
54 |
|
---|
55 | if (!@event.IsOk)
|
---|
56 | Fail(@event.Value);
|
---|
57 | else if (Evaluate()) {
|
---|
58 | Succeed();
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected virtual IEnumerable<KeyValuePair<object, object>> GetValues() {
|
---|
63 | var values = new List<KeyValuePair<object, object>>();
|
---|
64 | foreach (var e in Events) {
|
---|
65 | var condition = e as Condition;
|
---|
66 | if (condition != null) {
|
---|
67 | values.AddRange(condition.GetValues());
|
---|
68 | } else if (e.IsProcessed) {
|
---|
69 | values.Add(new KeyValuePair<object, object>(e, e.Value));
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return values;
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected virtual void CollectValues(Event @event) {
|
---|
76 | if (@event.IsOk) {
|
---|
77 | var value = new OrderedDictionary();
|
---|
78 | foreach (var v in GetValues())
|
---|
79 | value.Add(v.Key, v.Value);
|
---|
80 | Value = value;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected abstract bool Evaluate();
|
---|
85 | }
|
---|
86 | }
|
---|