- Timestamp:
- 02/13/14 17:28:30 (11 years ago)
- Location:
- branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent
- Files:
-
- 1 added
- 1 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Action.cs
r10450 r10454 6 6 [StorableClass] 7 7 public abstract class Action<TModel> : IAction<TModel> where TModel : IModel { 8 public abstract Guid ActionId { get; }9 8 public IActivity<TModel> Mandate { get; protected set; } 10 9 -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Activity.cs
r10450 r10454 15 15 } 16 16 17 public virtual IEnumerable< Guid> MonitoredActionIds {18 get { return new Guid[0]; }17 public virtual IEnumerable<Type> MonitoredActions { 18 get { return new Type[0]; } 19 19 } 20 20 -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/CompositeEventQueue.cs
r10450 r10454 41 41 } 42 42 43 public void Clear() { 44 foreach (var queue in queues) { 45 queue.Clear(); 46 } 47 } 48 43 49 [StorableConstructor] 44 50 private CompositeEventQueue(bool deserializing) : base(deserializing) { } -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/DiscreteEventSimulation.cs
r10450 r10454 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 using System.Text; 4 5 using System.Threading; 5 6 using HeuristicLab.Common; … … 32 33 } 33 34 35 [StorableConstructor] 36 protected DiscreteEventSimulation(bool deserializing) : base(deserializing) { } 34 37 protected DiscreteEventSimulation(DiscreteEventSimulation<TModel> original, Cloner cloner) 35 38 : base(original, cloner) { … … 39 42 Model = cloner.Clone(original.Model); 40 43 } 41 42 44 protected DiscreteEventSimulation() { 43 45 Parameters.Add(new FixedValueParameter<DoubleValue>("MaximumTime", "The simulation stops after reaching this time.", new DoubleValue(1000))); 46 } 47 48 protected override void OnPrepared() { 49 base.OnPrepared(); 50 if (CompositeEventQueue != null) 51 CompositeEventQueue.Clear(); 44 52 } 45 53 … … 47 55 CompositeEventQueue = new CompositeEventQueue<TModel>(Activities.Select(x => x.EventQueue)); 48 56 49 var actionMonitor = new Dictionary<Guid, List<IActivity<TModel>>>(); 57 var log = new StringBuilder(); 58 59 var actionMonitor = new Dictionary<Type, List<IActivity<TModel>>>(); 50 60 foreach (var activity in Activities) { 51 foreach (var actionId in activity.MonitoredAction Ids) {61 foreach (var actionId in activity.MonitoredActions) { 52 62 if (!actionMonitor.ContainsKey(actionId)) actionMonitor[actionId] = new List<IActivity<TModel>>(); 53 63 actionMonitor[actionId].Add(activity); … … 55 65 } 56 66 57 if ( InitialAction != null) {67 if (CompositeEventQueue.Empty && InitialAction != null) { 58 68 InitialAction.Execute(Model); 59 69 CallActivities(actionMonitor, InitialAction); … … 62 72 while (!CompositeEventQueue.Empty && Model.CurrentTime < MaximumTime) { 63 73 var @event = CompositeEventQueue.Pop(); 74 if (@event.Time < Model.CurrentTime) continue; 75 log.AppendLine(@event.Action.GetType().Name); 76 Model.CurrentTime = @event.Time; 77 64 78 var nextEvent = CompositeEventQueue.Empty ? null : CompositeEventQueue.Peek(); 65 79 var action = @event.Action; 66 80 67 81 action.Execute(Model); 68 CallActivities(actionMonitor, action);69 82 70 if (@event.Time > Model.CurrentTime && (nextEvent == null || @event.Time < nextEvent.Time)) { 71 var timeUpdateAction = new ModelTimeUpdateAction<TModel>(null, @event.Time); 72 timeUpdateAction.Execute(Model); 73 74 if (actionMonitor.ContainsKey(timeUpdateAction.ActionId)) 75 foreach (var activity in actionMonitor[timeUpdateAction.ActionId]) 76 activity.ManageEvents(Model, timeUpdateAction); 77 83 if (nextEvent == null || @event.Time < nextEvent.Time) { 78 84 foreach (var reporter in Reporters) 79 85 reporter.UpdateReporting(Model, Results); 80 86 } 87 88 CallActivities(actionMonitor, action); 89 81 90 cancellationToken.ThrowIfCancellationRequested(); 82 91 }; 83 92 } 84 93 85 private void CallActivities(Dictionary<Guid, List<IActivity<TModel>>> actionMonitor, IAction<TModel> action) { 86 if (actionMonitor.ContainsKey(action.ActionId)) { 87 foreach (var activity in actionMonitor[action.ActionId]) 94 private void CallActivities(Dictionary<Type, List<IActivity<TModel>>> actionMonitor, IAction<TModel> action) { 95 if (actionMonitor.ContainsKey(action.GetType())) { 96 foreach (var activity in actionMonitor[action.GetType()]) 97 activity.ManageEvents(Model, action); 98 } 99 100 if (actionMonitor.ContainsKey(typeof(object))) { 101 foreach (var activity in actionMonitor[typeof(object)]) 88 102 activity.ManageEvents(Model, action); 89 103 } -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Event.cs
r10450 r10454 11 11 12 12 #region Storable Properties 13 [Storable]14 13 // ReSharper disable UnusedMember.Local 15 14 // ReSharper disable UnusedParameter.Local 15 [Storable] 16 16 private double StorableTime { 17 17 get { return time; } -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Interfaces/IAction.cs
r10450 r10454 1 using System; 2 using HeuristicLab.Common; 1 using HeuristicLab.Common; 3 2 4 3 namespace HeuristicLab.SimulationCore { 5 4 public interface IAction<TModel> : IDeepCloneable, IContent where TModel : IModel { 6 Guid ActionId { get; }7 5 IActivity<TModel> Mandate { get; } 8 6 -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Interfaces/IActivity.cs
r10450 r10454 7 7 where TModel : IModel { 8 8 IEventQueue<TModel> EventQueue { get; } 9 IEnumerable< Guid> MonitoredActionIds { get; }9 IEnumerable<Type> MonitoredActions { get; } 10 10 11 11 void ManageEvents(TModel model, IAction<TModel> lastAction); -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Interfaces/IEventQueue.cs
r10450 r10454 8 8 IEvent<TModel> Peek(); 9 9 IEvent<TModel> Pop(); 10 void Push( IEvent<TModel> @event);10 void Push(double time, IAction<TModel> action); 11 11 12 12 IEvent<TModel> ChangeTime(IEvent<TModel> @event, double newTime); -
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/SortedListEventQueue.cs
r10450 r10454 26 26 } 27 27 28 public void Push(IEvent<TModel> @event) { 29 if (queue.ContainsKey(@event.Time)) 30 queue[@event.Time].Add(@event); 31 else queue.Add(@event.Time, new List<IEvent<TModel>>() { @event }); 28 public void Push(double time, IAction<TModel> action) { 29 PushEvent(time, action); 30 } 31 32 private IEvent<TModel> PushEvent(double time, IAction<TModel> action) { 33 IEvent<TModel> result = new Event<TModel>(time, action); 34 if (queue.ContainsKey(time)) 35 queue[time].Add(result); 36 else queue.Add(time, new List<IEvent<TModel>>() { result }); 37 return result; 32 38 } 33 39 34 40 public IEvent<TModel> ChangeTime(IEvent<TModel> @event, double newTime) { 35 41 Remove(@event); 36 var newEvent = new Event<TModel>(newTime, @event.Action); 37 Push(newEvent); 38 return newEvent; 42 return PushEvent(newTime, @event.Action); 39 43 } 40 44
Note: See TracChangeset
for help on using the changeset viewer.