Class Simulation

    • Constructor Detail

      • Simulation

        public Simulation()
    • Method Detail

      • numPrintListener

        public int numPrintListener()
      • init

        public void init()
        Performs all initializations required for a successful simulation run().
      • initComponentTree

        protected void initComponentTree​(SimComponent parent,
                                         SimComponent child)
        Recursively initialize components before run by setting simulation and parent node.
        Parameters:
        parent -
        child -
      • run

        public void run()
        Runs the main simulation loop. This means:
        1. taking an event from the event queue,
        2. advancing simulation time, and
        3. triggering event processing.
        A simulation is terminated if either the maximum simulation length is reached, there are no more application events in the queue, or the method end() was called.
        See Also:
        SimEvent.isAppEvent()
      • runEventHandler

        protected void runEventHandler​(SimEvent evt)
        Runs the handle-method of evt (see SimEvent.handle(). This method exists to allow executing custom code before and after each event by sub-classing Simulation.
        Parameters:
        evt - The event to execute
      • defaultErrorHandler

        protected boolean defaultErrorHandler​(Exception e)
      • beforeRun

        public void beforeRun()
        Override this method to perform initializations after init(), but before running the simulation. This method is usually used to schedule initial events. It is executed automatically at the beginning of the run() method.
      • resetStats

        protected void resetStats()
        This method is called once after beforeRun() and immediately before the main simulation loop starts. It is called a second time if the simulation has a a value for statsResetTime() set.

        It should contain code to initialize statistics variables.

      • afterRun

        public void afterRun()
        Override this method to perform some action after running the simulation, but before done() is called. It is executed automatically at the end of the run() method.
      • done

        public void done()
        Performs clean-up etc., after a simulation's run() method finished.
      • unschedule

        public boolean unschedule​(SimEvent event)
        Removes the given event object from the event queue.
        Parameters:
        event - the event to remove
        Returns:
        true if the operation was present in the event queue and could be successfully removed, false otherwise
      • scheduleProcess

        public void scheduleProcess​(int prio,
                                    DoubleSupplier method)
        Calls a certain method at the times returned by the method itself. The first invocation is performed at the current time (asynchronously, i.e., scheduleProcess() returns before method is called for the first time). Subsequent calls are scheduled at the absolute times returned by the previous method invocation. No more invocations are scheduled if method returned NaN or a negative value.
        Specified by:
        scheduleProcess in interface SimOperations
        See Also:
        scheduleProcess(int, DoubleSupplier)
      • scheduleProcess

        public void scheduleProcess​(double firstInvocation,
                                    int prio,
                                    DoubleSupplier method)
        Calls a certain method at the times returned by the method itself. The first invocation is performed at firstInvocation. Subsequent calls are scheduled at the absolute times returned by the previous method invocation. No more invocations are scheduled if method returned NaN or a negative value.
        Specified by:
        scheduleProcess in interface SimOperations
        See Also:
        scheduleProcess(double, int, DoubleSupplier)
      • end

        public void end()
        After calling end() the simulation is terminated (after handling the current event). This method might also be called from an external thread.
        Specified by:
        end in interface SimOperations
      • pause

        public void pause()
        After calling pause() the simulation is paused. This means, the run() method returns after handling the current event.

        This method might also be called from an external thread.

      • unpause

        public void unpause()
        After calling unpause() a paused simulation is continued. Internally each pause request increases a counter that has to be followed by an unpause request. Simulation only resumes if the pause counter reaches zero.

        This method might also be called from an external thread.

      • clock

        public Clock clock()
        Returns a Java Clock object using the simulation time and its time zone.
      • simTimeToDuration

        public Duration simTimeToDuration​(double simTime)
        Converts the given simulation time span to a Java Duration.
      • toSimTime

        public double toSimTime​(Instant instant)
        Converts a given Java Instant (absolute UTC time stamp) to the simulation time it corresponds to.
        Specified by:
        toSimTime in interface SimOperations
        Parameters:
        instant - The instant to be converted to simulation time.
        Returns:
        The instant converted to simulation time.
        See Also:
        toSimTime(Instant)
      • toSimTime

        public double toSimTime​(Duration d)
        Converts a given Java Duration (i.e., a time span) to the corresponding (relative) simulation time.
        Specified by:
        toSimTime in interface SimOperations
        Parameters:
        d - The duration to be converted to simulation time.
        Returns:
        The amount of simulation time.
        See Also:
        toSimTime(Duration)
      • currentEvent

        public SimEvent currentEvent()
        Returns the SimEvent object that is currently processed.
      • numEventsProcessed

        public long numEventsProcessed()
        Returns the number of events processed by the main simulation loop.
      • numAppEvents

        public long numAppEvents()
        Returns the number of normal, i.e., application events currently contained in the event queue.
      • numEvents

        public long numEvents()
        Returns the total number of events (both application and utility events) currently contained in the event queue.
      • scheduledEvents

        public List<SimEvent> scheduledEvents()
        Returns an ordered list of all events currently in the event queue. Use with care, this is an expensive operation. The list does not include the current
      • produceResults

        public void produceResults​(Map<String,​Object> res)
        Populates the given HashMap with results produced in the simulation run.
      • activateEntity

        public <T extends SimEntity> T activateEntity​(T e)
        Activates the given entity but does not add it to the components tree. Therefore they will not be notified of any future simulation lifecycle events such as produceResults. It is therefore intended for temporary SimEntitys only.
        Specified by:
        activateEntity in interface SimOperations
        See Also:
        activateEntity(SimEntity)
      • getComponentByHierarchicalName

        public SimComponent getComponentByHierarchicalName​(String hierarchicalName)
        Convenience method to get a component by its name given a fully qualified name such as "container1.sub1.myMachine".
      • print

        public void print​(String message)
        Triggers a print event for the given message of category "INFO".
        Parameters:
        message - The message to print.
        See Also:
        print(MsgCategory, String)
      • print

        public void print​(MsgCategory category,
                          String message)
        Triggers a print event of the given category. If an appropriate listener is installed, this should produce an output of message.
        Parameters:
        message - The message to print.
      • print

        public void print​(MsgCategory category,
                          Object... params)
        Triggers a print event of the given category. If an appropriate listener is installed, this should produce an output of message.
      • trace

        public void trace​(Object... params)
        Produces a trace message (if there are any print listeners such as TraceFileProducer are registered that do something with such messages). A trace message consists of the simulation time and all parameters converted to Strings (separated by tabs).
        Specified by:
        trace in interface SimOperations
        Parameters:
        params - The components of the trace message.
        See Also:
        trace(Object...)
      • isTraceEnabled

        public boolean isTraceEnabled()
        Description copied from interface: SimOperations
        Returns true is trace messages should be produced.
        Specified by:
        isTraceEnabled in interface SimOperations
        Returns:
        Whether or not trace messages are to be produced.
        See Also:
        isTraceEnabled()
      • getPrintLevel

        public MsgCategory getPrintLevel()
        Returns:
        The current maximum print message category.
      • setPrintLevel

        public void setPrintLevel​(MsgCategory printLevel)
        Sets the maximum print message category to be forwared to the print listeners. If this is set to e.g. INFO, then only messages of the categories ERROR, WARN and INFO are forwared to
        Parameters:
        printLevel -
      • printFmt

        public void printFmt​(MsgCategory category,
                             String messageFormatString,
                             Object... params)
        Triggers a print event of the given category with the message produced by a Java format String. If an appropriate listener is installed, this produces a message defined by the format string messageFormatString (used with the arguments given in params).
      • createEventQueue

        protected EventQueue createEventQueue()
        Factory method to create a new event queue.
        Returns:
        The event queue to use in this simulation.
      • setSimulationLength

        public void setSimulationLength​(double simulationLength)
        Sets the maximum simulation time. A value of 0.0 means no such limit.
      • getSimulationLength

        public double getSimulationLength()
        Returns:
        The maximum simulation time; a value of 0.0 means no such limit.
      • getRndStreamFactory

        public RandomFactory getRndStreamFactory()
        Returns:
        The RandomFactory used to create random number streams.
      • setRndStreamFactory

        public void setRndStreamFactory​(RandomFactory rndStreamFactory)
        Sets the random factory to use to create random number streams for stochastic simulations.
      • getName

        public String getName()
        Returns:
        The name of this simulation.
      • setName

        public void setName​(String name)
        Sets a name for this simulation.
      • getInitialSimTime

        public double getInitialSimTime()
      • setInitialSimTime

        public void setInitialSimTime​(double initialSimTime)
        Sets the initial value of the simulation clock.
      • getInitialEventPriority

        public int getInitialEventPriority()
      • setInitialEventPriority

        public void setInitialEventPriority​(int initialEventPriority)
        Sets the initial priority value (default=0, i.e., SimEvent.EVENT_PRIO_NORMAL).
      • setSimTimeStartInstant

        public void setSimTimeStartInstant​(Instant simTimeStartInstant)
        Sets the Instant corresponding to the a simulation time of 0. The default setting is to use the beginning of the current year.

        The Instant will be truncated to milliseconds, so any nano-second part will be cleared.

        See Also:
        simTimeToInstant(double)
      • getStatsResetTime

        public double getStatsResetTime()
        Returns the statistics reset time.
      • setStatsResetTime

        public void setStatsResetTime​(double statsResetTime)
        Sets the time when to perform a statistics reset. The value set here will only have an effect before the simulation is started.
        Parameters:
        statsResetTime - The new statistics reset time.
      • getLocale

        public Locale getLocale()
        Returns the currently set Locale, i.e., language and region.
      • setLocale

        public void setLocale​(Locale locale)
        Sets the current Locale.
      • getZoneId

        public ZoneId getZoneId()
      • message

        public String message​(Enum<?> key)
      • formattedMessage

        public String formattedMessage​(Enum<?> key,
                                       Object... params)
      • setSimTimeToMillisFactor

        public void setSimTimeToMillisFactor​(long simTimeToMillisFactor)
        Sets the factor used to convert the (double-valued) simulation time to milli-seconds since getSimTimeStartInstant(). The default value is 60*1000=60000, assuming simulation time to be in minutes.
        See Also:
        simTimeToInstant(double)
      • continueSim

        public boolean continueSim()
      • runInSimThread

        public void runInSimThread​(Runnable r)
      • valueStoreImpl

        public ValueStore valueStoreImpl()
        Description copied from interface: ValueStore
        Returns the implementation to use for adding ValueStore functionality.
        Specified by:
        valueStoreImpl in interface ValueStore
      • numRunnableProcesses

        public int numRunnableProcesses()