Exemple #1
0
        public DesTask Transfer(DesTask owner, DesTask receiver)
        {
            if (owner == receiver)
            {
                throw new ArgumentException(
                          "'owner' was the same as 'receiver'.");
            }
            if (owner == null)
            {
                throw new ArgumentNullException("'owner' was null.");
            }
            if (receiver == null)
            {
                throw new ArgumentNullException("'receiver' was null.");
            }
            if (!IsOwner(owner))
            {
                throw new InvalidOperationException(
                          "DesTask is not a DESResource owner.");
            }
            if (!AllowOwnMany && IsOwner(receiver))
            {
                throw new InvalidOperationException(
                          "'receiver' already owns a DESResource from this pool.");
            }

            ResourceEntry entry = _owners[owner];
            object        item  = SelectItemToRelease(owner, entry.Items);

            return(new TransferResource(owner.Simulation, this, receiver, item));
        }
Exemple #2
0
 /// <summary>
 /// Run the <see cref="Simulation"/> using the given generator
 /// <see cref="DesTask"/> instance.
 /// </summary>
 /// <exception cref="ArgumentNullException">
 /// If <paramref name="generator"/> is <see langword="null"/>.
 /// </exception>
 /// <param name="generator">
 /// The <see cref="DesTask"/> which will serve as the sole generator.
 /// </param>
 /// <returns>
 /// The number of <see cref="ActivationEvent"/> instances that
 /// remained in the event queue at the time the
 /// <see cref="Simulation"/> stopped.
 /// </returns>
 public int Run(DesTask generator)
 {
     if (generator == null)
     {
         throw new ArgumentNullException("generator");
     }
     return(Run(new DesTask [] { generator }));
 }
 public DesTask Consume(DesTask task, decimal consumption)
 {
     while (!Consume(ref consumption))
     {
         return(waitForMoreProducts.Block(task));
     }
     return(task);
 }
Exemple #4
0
        public DesTask Resupply(DesTask desTask, int quantity)
        {
            if (desTask == null)
            {
                throw new ArgumentNullException("desTask");
            }

            return(new ResupplyConsumable(desTask.Simulation, this, quantity));
        }
Exemple #5
0
        public DesTask Acquire(DesTask requestor, int quantity)
        {
            if (requestor == null)
            {
                throw new ArgumentNullException("requestor");
            }

            return(new AcquireConsumable(requestor.Simulation, this, quantity));
        }
Exemple #6
0
 /// <summary>
 /// Invoked by a <see cref="TransferResource"/> DesTask to transfer
 /// ownership of a DESResource from one <see cref="DesTask"/> to another.
 /// </summary>
 /// <param name="owner">
 /// The <see cref="DesTask"/> that is the actual DESResource owner.  This
 /// is <b>not</b> the <see cref="TransferResource"/> DesTask.
 /// </param>
 /// <param name="receiver">
 /// The <see cref="DesTask"/> that will be granted ownership of the
 /// DESResource.
 /// </param>
 /// <param name="item">
 /// The DESResource item.  This will be <see langword="null"/> for
 /// anonymous resources.
 /// </param>
 internal void TransferResource(DesTask owner, DesTask receiver, object item)
 {
     if (receiver != null)
     {
         ClearOwner(owner, item);
         SetOwner(receiver, item);
     }
     else
     {
         ReturnResource(owner, item);
     }
 }
Exemple #7
0
        public virtual DesTask Acquire(DesTask requestor)
        {
            if (requestor == null)
            {
                throw new ArgumentNullException("requestor", "cannot be null");
            }

            if (!AllowOwnMany && IsOwner(requestor))
            {
                throw new InvalidOperationException(
                          "'requestor' already owns a DESResource from this pool.");
            }
            return(new AcquireResource(requestor.Simulation, this));
        }
        /// <summary>
        /// Invoked by the <see cref="Simulation"/> to fire the
        /// <see cref="ActivationEvent"/>.
        /// </summary>
        /// <remarks>
        /// Only a <see cref="Simulation"/> should invoke this method and then
        /// only after scheduling the <see cref="ActivationEvent"/>.
        /// </remarks>
        /// <param name="sim">
        /// The <see cref="Simulation"/> firing the
        /// <see cref="ActivationEvent"/>.
        /// </param>
        internal void Fire(Simulation sim)
        {
            if (!IsPending)
            {
                throw new InvalidOperationException("Event was not pending.");
            }

            // Obtained the deferred data.  This call must come before the
            // call to base.Fire(sim), otherwise the DeferredDataCallback
            // will treat this event as canceled.
            PrepareDeferredData();

            _evtTime = NotScheduled;
            DesTask.RunFromActivationEvent(this);
        }
Exemple #9
0
        /// <summary>
        /// Gets the number of resources owned by the specified
        /// <see cref="DesTask"/> (if any).
        /// </summary>
        /// <param name="desTask">
        /// The <see cref="DesTask"/> whose DESResource ownership count will be
        /// computed.
        /// </param>
        /// <returns>
        /// The number of DESResource from the pool owned by
        /// <paramref name="desTask"/>.  If <paramref name="desTask"/> does not
        /// own any resources from the pool, the returned value will be zero
        /// (0).
        /// </returns>
        public int OwnershipCount(DesTask desTask)
        {
            int count;

            if (IsOwner(desTask))
            {
                ResourceEntry entry = _owners[desTask];
                count = entry.Count;
                System.Diagnostics.Debug.Assert(count > 0);
            }
            else
            {
                count = 0;
            }
            return(count);
        }
Exemple #10
0
        /// <summary>
        /// Select and return a particular DESResource item to release.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method will return <see langword="null"/> if
        /// <see cref="AutoSelect"/> is set to <b>false</b>.  If
        /// <see cref="AutoSelect"/> is <b>true</b> (the default), this method
        /// will select an DESResource item from <paramref name="items"/> to
        /// release. If only one item is owned by <paramref name="owner"/>,
        /// that item is selected for release.  If <paramref name="owner"/>
        /// owns multiple items belonging to this <see cref="TrackedDesResource"/>
        /// then the first item acquired is selected for release.
        /// </para>
        /// <para>
        /// This method must be overridden to change the selection policy.
        /// </para>
        /// </remarks>
        /// <param name="owner">
        /// The <see cref="DesTask"/> that is the actual DESResource owner.
        /// </param>
        /// <param name="items">
        /// An immutable <see cref="IList"/> of DESResource items owned by
        /// <paramref name="owner"/>.  This should never be will be <see langword="null"/>
        /// if the <see cref="DESResource"/> is not tracking individual items.
        /// </param>
        /// <returns>A DESResource item to release.</returns>
        protected override object SelectItemToRelease(DesTask owner, IList items)
        {
            object item;

            if (AutoSelect)
            {
                System.Diagnostics.Debug.Assert(items != null);
                System.Diagnostics.Debug.Assert(items.Count > 0);
                item = items[0];
            }
            else
            {
                item = null;
            }

            return(item);
        }
Exemple #11
0
        public virtual DesTask Release(DesTask owner)
        {
            if (owner == null)
            {
                throw new ArgumentNullException("owner", "cannot be null");
            }

            if (!IsOwner(owner))
            {
                throw new InvalidOperationException(
                          "DesTask is not a DESResource owner.");
            }

            ResourceEntry entry = _owners[owner];
            object        item  = SelectItemToRelease(owner, entry.Items);

            return(new ReleaseResource(owner.Simulation, this, item));
        }
Exemple #12
0
 /// <summary>
 /// Create a new <see cref="ActivationEvent"/> that will run the
 /// specified <see cref="DesTask"/> on behalf of the given activator.
 /// </summary>
 /// <param name="desTask">
 /// The <see cref="DesTask"/> this <see cref="ActivationEvent"/> will run
 /// when it is fired.
 /// </param>
 /// <param name="activator">
 /// The object which is activating <paramref name="desTask"/>.
 /// </param>
 /// <param name="relTime">
 /// The relative time when <paramref name="desTask"/> should be executed.
 /// </param>
 public ActivationEvent(DesTask desTask, object activator, ulong relTime)
 {
     if (desTask == null)
     {
         throw new ArgumentNullException("desTask");
     }
     if (desTask.Canceled)
     {
         throw new ArgumentException("Cannot be canceled.", "desTask");
     }
     if (relTime == 0)
     {
         ;
     }
     _evtTime   = desTask.Simulation.Now + relTime;
     _desTask   = desTask;
     _activator = activator;
     _priority  = desTask.Priority;
 }
Exemple #13
0
        /// <summary>
        /// Removes the specified <see cref="DesTask"/> as a DESResource item owner.
        /// </summary>
        /// <param name="owner">
        /// The <see cref="DesTask"/> that owns <paramref name="item"/>.
        /// </param>
        /// <param name="item">
        /// A reference to the DESResource item previously obtained via a call to
        /// <see cref="AllocateResource"/> (for tracked resources); or
        /// <see langword="null"/> (for anonymous resources).
        /// </param>
        private void ClearOwner(DesTask owner, object item)
        {
            if (owner != null && _owners != null)
            {
                ResourceEntry entry = _owners[owner];
                if (item == null)
                {
                    entry.Remove();
                }
                else
                {
                    entry.Remove(item);
                }

                if (entry.Count < 1)
                {
                    _owners.Remove(owner);
                }
            }
        }
Exemple #14
0
        /// <summary>
        /// Executes each process step <see cref="DesTask"/> obtained from the
        /// generator created by <see cref="GetProcessSteps"/>.
        /// </summary>
        /// <remarks>
        /// Normally, <see cref="Process"/> implementors will not need to
        /// override this method; override <see cref="GetProcessSteps"/>
        /// instead.
        /// </remarks>
        /// <param name="activator">
        /// The object that activated this <see cref="Process"/>.
        /// </param>
        /// <param name="data">Per-activation event data.</param>
        protected override void ExecuteTask(object activator, object data)
        {
            if (_steps == null)
            {
                _steps = GetProcessSteps();
            }

            _activator      = activator;
            _activationData = data;

            if (_steps.MoveNext())
            {
                DesTask desTask = _steps.Current;
                if (desTask == null)
                {
                    throw new SimulationException("Process DesTask step was null.");
                }
                if (desTask != this)
                {
                    // Clear interrupt flag before calling WaitOnTask,
                    // otherwise the call to Activate in WaitOnTask will
                    // be ignored.
                    ClearInterrupt();
                    WaitOnTask(desTask);
                }
            }
            else
            {
                _steps = null;
            }

            _activationData = null;
            _activator      = null;

            if (_steps == null)
            {
                ResumeAll();
            }
        }
Exemple #15
0
        /// <summary>
        /// Record the given <see cref="DesTask"/> as a DESResource item owner.
        /// </summary>
        /// <param name="desTask">
        /// The <see cref="DesTask"/> that owns <paramref name="item"/>.
        /// </param>
        /// <param name="item">
        /// The reference to a DESResource item for tracked resources; or
        /// <see langword="null"/> for anonymous resources.
        /// </param>
        internal void SetOwner(DesTask desTask, object item)
        {
            if (desTask != null)
            {
                ResourceEntry entry;
                if (_owners == null)
                {
                    _owners = new Dictionary <DesTask, ResourceEntry>();
                }

                if (!IsOwner(desTask))
                {
                    if (item == null)
                    {
                        entry = new ResourceEntry();
                    }
                    else
                    {
                        entry = new ResourceEntry(item);
                    }
                    _owners.Add(desTask, entry);
                }
                else
                {
                    entry = _owners[desTask];
                    if (item == null)
                    {
                        entry.Add();
                    }
                    else
                    {
                        entry.Add(item);
                    }
                }
            }
        }
 /// <summary>
 /// Select and return a particular DESResource item to release.
 /// </summary>
 /// <remarks>
 /// This method always returns <see langword="null"/> as an
 /// <see cref="AnonymousDesResource"/> does not keep track of
 /// individual DESResource items.
 /// </remarks>
 /// <param name="owner">
 /// The <see cref="DesTask"/> that is the actual DESResource owner.
 /// </param>
 /// <param name="items">
 /// An immutable <see cref="IList"/> of DESResource items owned by
 /// <paramref name="owner"/>.  This will be <see langword="null"/>
 /// if the <see cref="DESResource"/> is not tracking individual items.
 /// </param>
 /// <returns>Always returns <see langword="null"/>.</returns>
 protected override object SelectItemToRelease(DesTask owner, IList items)
 {
     return(null);
 }
Exemple #17
0
 /// <summary>
 /// Cancels the <see cref="ActivationEvent"/>.
 /// </summary>
 /// <remarks>
 /// <para>
 /// After cancelling the <see cref="ActivationEvent"/>,
 /// <see cref="IsPending"/> will be <b>false</b> and the event will
 /// not be executed (fired) by the <see cref="Simulation"/>.
 /// </para>
 /// <para>
 /// Note that cancelling an <see cref="ActivationEvent"/> does not cancel
 /// its associated <see cref="DesTask"/>.
 /// </para>
 /// </remarks>
 public void Cancel()
 {
     _cancelFlag = true;
     DesTask.CancelPending(this);
     PrepareDeferredData();
 }
Exemple #18
0
 /// <summary>
 /// Creates a new <see cref="ActivationEvent"/> that will run the specified
 /// <see cref="DesTask"/>.
 /// </summary>
 /// <exception cref="ArgumentNullException">
 /// If <paramref name="desTask"/> is <see langword="null"/>.
 /// </exception>
 /// <param name="desTask">
 /// The <see cref="DesTask"/> this <see cref="ActivationEvent"/> will run
 /// when it is fired.
 /// </param>
 /// <param name="relTime">
 /// The time relative to the current simulation time when the
 /// <see cref="ActivationEvent"/> should be fired.
 /// </param>
 public ActivationEvent(DesTask desTask, ulong relTime)
     : this(desTask, null, relTime)
 {
 }
Exemple #19
0
 public DesTask Acquire(DesTask requestor)
 {
     return(Acquire(requestor, 1));
 }
Exemple #20
0
 /// <summary>
 /// Invoked by a <see cref="ReleaseResource"/> DesTask to return a
 /// DESResource to the pool.
 /// </summary>
 /// <param name="owner">
 /// The <see cref="DesTask"/> that is the actual DESResource owner.  This
 /// is <b>not</b> the <see cref="ReleaseResource"/> DesTask.
 /// </param>
 /// <param name="item">
 /// The DESResource item.  This will be <see langword="null"/> for
 /// anonymous resources.
 /// </param>
 internal void ReturnResource(DesTask owner, object item)
 {
     DeallocateResource(item);
     ClearOwner(owner, item);
     ResumeWaiting();
 }
Exemple #21
0
 /// <summary>
 /// Select and return a particular DESResource item to release.
 /// </summary>
 /// <remarks>
 /// This method may simply return <see langword="null"/> if the
 /// <see cref="DESResource"/> implementation does not track individual
 /// DESResource items; otherwise it should select an item from the
 /// <see cref="IList"/> to release.
 /// </remarks>
 /// <param name="owner">
 /// The <see cref="DesTask"/> that is the actual DESResource owner.
 /// </param>
 /// <param name="items">
 /// An immutable <see cref="IList"/> of DESResource items owned by
 /// <paramref name="owner"/>.  This will be <see langword="null"/>
 /// if the <see cref="DESResource"/> is not tracking individual items.
 /// </param>
 /// <returns>A DESResource item to release.</returns>
 protected abstract object SelectItemToRelease(DesTask owner, IList items);
 public DesTask Fill(DesTask task)
 {
     consumabel++;
     waitForMoreProducts.Signal();
     return(Block(task));
 }
Exemple #23
0
 /// <summary>
 /// Checks if the given <see cref="DesTask"/> owns any resources in the
 /// pool.
 /// </summary>
 /// <param name="desTask">
 /// The <see cref="DesTask"/> which may own one or more resources in the
 /// DESResource pool.
 /// </param>
 /// <returns>
 /// <b>true</b> if <paramref name="desTask"/> owns at least one DESResource
 /// from this DESResource pool.
 /// </returns>
 public bool IsOwner(DesTask desTask)
 {
     return(_owners != null?_owners.ContainsKey(desTask) : false);
 }