Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.0.9/SimSharp-3.0.9/Core/Events/Condition.cs @ 15584

Last change on this file since 15584 was 15584, checked in by swagner, 6 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 3.2 KB
Line 
1#region License Information
2/* SimSharp - A .NET port of SimPy, discrete event simulation framework
3Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Collections.Specialized;
22
23namespace 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
61handled 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}
Note: See TracBrowser for help on using the repository browser.