Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.3.1/SimSharp-3.3.1/Core/Events/Condition.cs @ 17487

Last change on this file since 17487 was 17487, checked in by abeham, 4 years ago

#3065: update Sim# to 3.3.1

File size: 2.6 KB
Line 
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
8using System;
9using System.Collections.Generic;
10using System.Collections.Specialized;
11
12namespace 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
50handled 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}
Note: See TracBrowser for help on using the repository browser.