aivika-transformers-2.0: Transformers for the Aivika simulation library

CopyrightCopyright (c) 2009-2014, David Sorokin <david.sorokin@gmail.com>
LicenseBSD3
MaintainerDavid Sorokin <david.sorokin@gmail.com>
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Simulation.Aivika.Trans.Process

Contents

Description

Tested with: GHC 7.8.3

A value in the Process monad represents a discontinuous process that can suspend in any simulation time point and then resume later in the same or another time point.

The process of this type can involve the Event, Dynamics and Simulation computations. Moreover, a value in the Process monad can be run within the Event computation.

A value of the ProcessId type is just an identifier of such a process.

The characteristic property of the Process type is function holdProcess that suspends the current process for the specified time interval.

Synopsis

Process Monad

data ProcessId m Source

Represents a process identifier.

Instances

data Process m a Source

Specifies a discontinuous process that can suspend at any time and then resume later.

class ProcessLift t where Source

A type class to lift the Process computation into other computations.

Methods

liftProcess :: MonadComp m => Process m a -> t m a Source

Lift the specified Process computation into another computation.

Instances

Running Process

runProcess :: MonadComp m => Process m () -> Event m () Source

Run immediately the process. A new ProcessId identifier will be assigned to the process.

To run the process at the specified time, you can use the enqueueProcess function.

runProcessUsingId :: MonadComp m => ProcessId m -> Process m () -> Event m () Source

Run immediately the process with the specified identifier. It will be more efficient than as you would specify the process identifier with help of the processUsingId combinator and then would call runProcess.

To run the process at the specified time, you can use the enqueueProcessUsingId function.

runProcessInStartTime :: MonadComp m => Process m () -> Simulation m () Source

Run the process in the start time immediately involving all pending CurrentEvents in the computation too.

runProcessInStartTimeUsingId :: MonadComp m => ProcessId m -> Process m () -> Simulation m () Source

Run the process in the start time immediately using the specified identifier and involving all pending CurrentEvents in the computation too.

runProcessInStopTime :: MonadComp m => Process m () -> Simulation m () Source

Run the process in the final simulation time immediately involving all pending CurrentEvents in the computation too.

runProcessInStopTimeUsingId :: MonadComp m => ProcessId m -> Process m () -> Simulation m () Source

Run the process in the final simulation time immediately using the specified identifier and involving all pending CurrentEvents in the computation too.

Spawning Processes

spawnProcess :: MonadComp m => ContCancellation -> Process m () -> Process m () Source

Spawn the child process specifying how the child and parent processes should be cancelled in case of need.

spawnProcessUsingId :: MonadComp m => ContCancellation -> ProcessId m -> Process m () -> Process m () Source

Spawn the child process specifying how the child and parent processes should be cancelled in case of need.

Enqueueing Process

enqueueProcess :: MonadComp m => Double -> Process m () -> Event m () Source

Enqueue the process that will be then started at the specified time from the event queue.

enqueueProcessUsingId :: MonadComp m => Double -> ProcessId m -> Process m () -> Event m () Source

Enqueue the process that will be then started at the specified time from the event queue.

Creating Process Identifier

newProcessId :: MonadComp m => Simulation m (ProcessId m) Source

Create a new process identifier.

processId :: MonadComp m => Process m (ProcessId m) Source

Return the current process identifier.

processUsingId :: MonadComp m => ProcessId m -> Process m a -> Process m a Source

Allow calling the process with the specified identifier. It creates a nested process when canceling any of two, or raising an IO exception in any of the both, affects the Process computation.

At the same time, the interruption has no such effect as it requires explicit specifying the ProcessId identifier of the nested process itself, that is the nested process cannot be interrupted using only the parent process identifier.

Holding, Interrupting, Passivating and Canceling Process

holdProcess :: MonadComp m => Double -> Process m () Source

Hold the process for the specified time period.

interruptProcess :: MonadComp m => ProcessId m -> Event m () Source

Interrupt a process with the specified identifier if the process is held by computation holdProcess.

processInterrupted :: MonadComp m => ProcessId m -> Event m Bool Source

Test whether the process with the specified identifier was interrupted.

passivateProcess :: MonadComp m => Process m () Source

Passivate the process.

processPassive :: MonadComp m => ProcessId m -> Event m Bool Source

Test whether the process with the specified identifier is passivated.

reactivateProcess :: MonadComp m => ProcessId m -> Event m () Source

Reactivate a process with the specified identifier.

cancelProcessWithId :: MonadComp m => ProcessId m -> Event m () Source

Cancel a process with the specified identifier, interrupting it if needed.

cancelProcess :: (MonadComp m, MonadIO m) => Process m a Source

The process cancels itself.

processCancelled :: MonadComp m => ProcessId m -> Event m Bool Source

Test whether the process with the specified identifier was cancelled.

processCancelling :: ProcessId m -> Signal m () Source

Return a signal that notifies about cancelling the process with the specified identifier.

whenCancellingProcess :: MonadComp m => Event m () -> Process m () Source

Register a handler that will be invoked in case of cancelling the current process.

Awaiting Signal

processAwait :: MonadComp m => Signal m a -> Process m a Source

Await the signal.

Yield of Process

processYield :: MonadComp m => Process m () Source

Yield to allow other Process and Event computations to run at the current simulation time point.

Process Timeout

timeoutProcess :: (MonadComp m, MonadIO m) => Double -> Process m a -> Process m (Maybe a) Source

Try to run the child process within the specified timeout. If the process will finish successfully within this time interval then the result wrapped in Just will be returned; otherwise, the child process will be cancelled and Nothing will be returned.

If an exception is raised in the child process then it is propagated to the parent computation as well.

A cancellation of the child process doesn't lead to cancelling the parent process. Then Nothing is returned within the computation.

timeoutProcessUsingId :: (MonadComp m, MonadIO m) => Double -> ProcessId m -> Process m a -> Process m (Maybe a) Source

Try to run the child process with the given identifier within the specified timeout. If the process will finish successfully within this time interval then the result wrapped in Just will be returned; otherwise, the child process will be cancelled and Nothing will be returned.

If an exception is raised in the child process then it is propagated to the parent computation as well.

A cancellation of the child process doesn't lead to cancelling the parent process. Then Nothing is returned within the computation.

Parallelizing Processes

processParallel :: MonadComp m => [Process m a] -> Process m [a] Source

Execute the specified computations in parallel within the current computation and return their results. The cancellation of any of the nested computations affects the current computation. The exception raised in any of the nested computations is propagated to the current computation as well.

Here word parallel literally means that the computations are actually executed on a single operating system thread but they are processed simultaneously by the event queue.

New ProcessId identifiers will be assigned to the started processes.

processParallelUsingIds :: MonadComp m => [(ProcessId m, Process m a)] -> Process m [a] Source

Like processParallel but allows specifying the process identifiers. It will be more efficient than as you would specify the process identifiers with help of the processUsingId combinator and then would call processParallel.

processParallel_ :: MonadComp m => [Process m a] -> Process m () Source

Like processParallel but ignores the result.

processParallelUsingIds_ :: MonadComp m => [(ProcessId m, Process m a)] -> Process m () Source

Like processParallelUsingIds but ignores the result.

Exception Handling

catchProcess :: (MonadComp m, Exception e) => Process m a -> (e -> Process m a) -> Process m a Source

Exception handling within Process computations.

finallyProcess :: MonadComp m => Process m a -> Process m b -> Process m a Source

A computation with finalization part.

throwProcess :: (MonadComp m, Exception e) => e -> Process m a Source

Throw the exception with the further exception handling.

By some reason, an exception raised with help of the standard throw function is not handled properly within Process computation, altough it will be still handled if it will be wrapped in the IO monad. Therefore, you should use specialised functions like the stated one that use the throw function but within the IO computation, which allows already handling the exception.

Utilities

zipProcessParallel :: MonadComp m => Process m a -> Process m b -> Process m (a, b) Source

Zip two parallel processes waiting for the both.

zip3ProcessParallel :: MonadComp m => Process m a -> Process m b -> Process m c -> Process m (a, b, c) Source

Zip three parallel processes waiting for their results.

unzipProcess :: (MonadComp m, MonadIO m) => Process m (a, b) -> Simulation m (Process m a, Process m b) Source

Unzip the process using memoization so that the both returned processes could be applied independently, although they will refer to the same pair of values.

Memoizing Process

memoProcess :: MonadComp m => Process m a -> Simulation m (Process m a) Source

Memoize the process so that it would always return the same value within the simulation run.

Never Ending Process

neverProcess :: MonadComp m => Process m a Source

A computation that never computes the result. It behaves like a black hole for the discontinuous process, although such a process can still be canceled outside (see cancelProcessWithId), but then only its finalization parts (see finallyProcess) will be called, usually, to release the resources acquired before.