Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/12/19 13:45:11 (5 years ago)
Author:
abeham
Message:

#2975: Updated Sim# to 3.1.1

Location:
trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.1.1
Files:
1 edited
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.ExtLibs/HeuristicLab.SimSharp/3.1.1/SimSharp-3.1.1/Core/Resources/Container.cs

    r15972 r16779  
    11#region License Information
    22/* SimSharp - A .NET port of SimPy, discrete event simulation framework
    3 Copyright (C) 2016  Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3Copyright (C) 2019  Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44
    55This program is free software: you can redistribute it and/or modify
     
    1919using System;
    2020using System.Collections.Generic;
    21 using System.Linq;
    2221
    2322namespace SimSharp {
     23  /// <summary>
     24  /// A container holds a variable amount of a single continuous entity, e.g. water, coal, grain, etc.
     25  ///
     26  /// Put and Get are in FIFO order only when they can be satisfied.
     27  /// Any put or get that can be satisfied takes precedence.
     28  /// Put events that attempt to add more to the Container than there is capacity for and
     29  /// Get events that remove more than there is are backlogged.
     30  /// </summary>
    2431  public class Container {
     32
     33    public double Capacity { get; protected set; }
     34
    2535    public double Level { get; protected set; }
    26     public double Capacity { get; protected set; }
    27     protected Environment Environment { get; private set; }
     36
     37    protected Simulation Environment { get; private set; }
    2838
    2939    protected Queue<ContainerPut> PutQueue { get; private set; }
    3040    protected Queue<ContainerGet> GetQueue { get; private set; }
     41    protected SimplePriorityQueue<Event, double> WhenAtLeastQueue { get; private set; }
     42    protected SimplePriorityQueue<Event, double> WhenAtMostQueue { get; private set; }
     43    protected List<Event> WhenChangeQueue { get; private set; }
    3144
    32     public Container(Environment environment, double capacity = double.MaxValue, double initial = 0) {
     45    public Container(Simulation environment, double capacity = double.MaxValue, double initial = 0) {
    3346      if (capacity <= 0) throw new ArgumentException("Capacity must be > 0", "capacity");
    3447      if (initial < 0) throw new ArgumentException("Initial must be >= 0", "initial");
     
    3952      PutQueue = new Queue<ContainerPut>();
    4053      GetQueue = new Queue<ContainerGet>();
     54      WhenAtLeastQueue = new SimplePriorityQueue<Event, double>();
     55      WhenAtMostQueue = new SimplePriorityQueue<Event, double>(new ReverseComparer<double>());
     56      WhenChangeQueue = new List<Event>();
    4157    }
    4258
     
    5571      TriggerGet();
    5672      return get;
     73    }
     74
     75    public virtual Event WhenAtLeast(double level) {
     76      var whenAtLeast = new Event(Environment);
     77      WhenAtLeastQueue.Enqueue(whenAtLeast, level);
     78      TriggerWhenAtLeast();
     79      return whenAtLeast;
     80    }
     81
     82    public virtual Event WhenFull() {
     83      return WhenAtLeast(Capacity);
     84    }
     85
     86    public virtual Event WhenAtMost(double level) {
     87      var whenAtMost = new Event(Environment);
     88      WhenAtMostQueue.Enqueue(whenAtMost, level);
     89      TriggerWhenAtMost();
     90      return whenAtMost;
     91    }
     92
     93    public virtual Event WhenEmpty() {
     94      return WhenAtMost(0);
     95    }
     96
     97    public virtual Event WhenChange() {
     98      var whenChange = new Event(Environment);
     99      WhenChangeQueue.Add(whenChange);
     100      return whenChange;
    57101    }
    58102
     
    77121        if (put.IsTriggered) {
    78122          PutQueue.Dequeue();
     123          TriggerWhenAtLeast();
     124          TriggerWhenChange();
    79125        } else break;
    80126      }
     
    87133        if (get.IsTriggered) {
    88134          GetQueue.Dequeue();
     135          TriggerWhenAtMost();
     136          TriggerWhenChange();
    89137        } else break;
    90138      }
    91139    }
     140
     141    protected virtual void TriggerWhenAtLeast() {
     142      while (WhenAtLeastQueue.Count > 0 && Level >= WhenAtLeastQueue.Peek) {
     143        var whenAtLeast = WhenAtLeastQueue.Dequeue();
     144        whenAtLeast.Succeed();
     145      }
     146    }
     147
     148    protected virtual void TriggerWhenAtMost() {
     149      while (WhenAtMostQueue.Count > 0 && Level <= WhenAtMostQueue.Peek) {
     150        var whenAtMost = WhenAtMostQueue.Dequeue();
     151        whenAtMost.Succeed();
     152      }
     153    }
     154
     155    protected virtual void TriggerWhenChange() {
     156      if (WhenChangeQueue.Count == 0) return;
     157      foreach (var evt in WhenChangeQueue)
     158        evt.Succeed();
     159      WhenChangeQueue.Clear();
     160    }
    92161  }
    93162}
Note: See TracChangeset for help on using the changeset viewer.