AnyLogic
Expand
Font size

Schedule API

You can work with a schedule from code using the corresponding API of the Schedule class.

Demo model: Schedule Created Programmatically Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

Obtaining schedule values

Getting the value: getValue()
Function Description
<ValueType> getValue() Returns the value corresponding to the current model date.
<ValueType> getValue(double time) Returns the value corresponding to the given model time (time).
<ValueType> getValue(double time, TimeUnits units) Returns the schedule value corresponding to the given model time. You define the time moment as the number of specified time units (minutes, hours, etc.). For example, getValue(9, TIME_UNIT_HOUR)returns the schedule value for model time: 9 hours.

time — the number of time units.
units — time unit constant (TIME_UNIT_MINUTE, TIME_UNIT_HOUR, etc.).
<ValueType> getValue(Date date) Returns the value corresponding to the given model date.
Getting a timeout to the next change moment: getTimeoutToNextValue()
Function Description
double getTimeoutToNextValue() Returns timeout to the next change moment in the schedule, measured in the model time units from the current model time.
double getTimeoutToNextValue(double time) Returns timeout to the schedule change moment next to the given time, measured in the model time units from that time.
double getTimeoutToNextValue(Date date) Returns timeout to the schedule change moment next to the given date, measured in the model time units from that date.
Getting a time of the next change moment: getTimeOfNextValue()
Function Description
double getTimeOfNextValue() Returns time of the schedule change moment next to the current model time.
double getTimeOfNextValue(double time) Returns time of the schedule change moment next to the given time.
double getTimeOfNextValue(Date date) Returns time of the schedule change moment next to the given date.
Getting a date of the next change moment: getDateOfNextValue()
Function Description
Date getDateOfNextValue() Returns date of the schedule change moment next to the current model time.
Date getDateOfNextValue(double time) Returns date of the schedule change moment next to the given time.
Date getDateOfNextValue(Date date) Returns date of the schedule change moment next to the given date.
Getting next value: getNextValue()
Function Description
<ValueType> getNextValue() Returns value corresponding to the schedule change moment next to the current model time.
<ValueType> getNextValue(double time) Returns value corresponding to the schedule change moment next to the given time.
<ValueType> getNextValue(double time, TimeUnits units) Returns value corresponding to the schedule change moment next to the given model time (defined as the number of specified time units). For example, getValue(9, TIME_UNIT_HOUR) returns the schedule value for the next schedule change moment after 9 hours.

time — the number of time units.
units — time unit constant (TIME_UNIT_MINUTE, TIME_UNIT_HOUR, etc.).
<ValueType> getNextValue(Date date) Returns value corresponding to the schedule change moment next to the given date.
Getting a date of value: getDateOfValue()
Function Description
Date getDateOfValue() Returns the model date the current value of the schedule has been held since.
Date getDateOfValue(Date date) Returns the model date of the schedule change moment of the value corresponding to the given model date.
Date getDateOfValue(double time) Returns the model date of the schedule change moment of the value corresponding to the given model time.
Getting a time of value: getTimeOfValue()
Function Description
double getTimeOfValue() Returns the model time the current value of the schedule has been held since.
double getTimeOfValue(Date date) Returns the model time of the schedule change moment of the value corresponding to the given model date.
double getTimeOfValue(double time) Returns the model time of the schedule change moment of the value corresponding to the given model time.
Getting time units of the schedule: getTimeUnits()
Function Description
long getTimeUnits() Returns time units of the schedule, one of the time unit constants (TIME_UNIT_MINUTE, TIME_UNIT_HOUR, etc.)

Creating and initializing schedule from code on model startup

Creating empty schedule with no data using the simple constructor
Function Description
new Schedule() Creates a new schedule with no data. You can specify the type of data this schedule will use like this:
new Schedule<Boolean>() — “on/off”.
new Schedule<Integer>() — integer.
new Schedule<Double>() — real.
new ScheduleWithUnits(U units) Creates a new rate schedule with the specified rate measure units.

units — rate measure unit.
Adding intervals: addInterval()
Depending on the type of schedule you are creating, in the following functions use the value argument to pass the value of the corresponding type. For example, for an “on/off” schedule pass the value of boolean type to specify the interval you are adding to the schedule. If the period you are adding is “on”, pass true. If the period you are adding is “off”, pass false.
Function Description
void addInterval(int startWeek, int startDayOfWeek, int startHour, int startMinute, int startSecond, int endWeek, int endDayOfWeek, int endHour, int endMinute, int endSecond, V value) Adds new particular time interval to this schedule.

startWeek — start week of the interval.
startDayOfWeek — start day of week of the interval.
startHour — start hour of the interval.
startMinute — start minute of the interval.
startSecond — start second of the interval.
endWeek — end week of the interval.
endDayOfWeek — end day of week of the interval.
endHour — end hour of the interval.
endMinute — end minute of the interval.
endSecond — end second of the interval.
value — value of the interval.
void addInterval(int startDay, int startHour, int startMinute, int startSecond, int endDay, int endHour, int endMinute, int endSecond, V value) Adds new particular time interval to this schedule.

startDay — start day of the interval.
startHour — start hour of the interval.
startMinute — start minute of the interval.
startSecond — start second of the interval.
endDay — end day of the interval.
endHour — end hour of the interval.
endMinute — end minute of the interval.
endSecond — end second of the interval.
value — value of the interval.
void addInterval(int startHour, int startMinute, int startSecond, int endHour, int endMinute, int endSecond, V value) Adds new particular time interval to this schedule.

startHour — start hour of the interval.
startMinute — start minute of the interval.
startSecond — start second of the interval.
endHour — end hour of the interval.
endMinute — end minute of the interval.
endSecond — end second of the interval.
value — value of the interval.
void addInterval(int startHour, int startMinute, int startSecond, int endHour, int endMinute, int endSecond, V value, int[] weekDays) Adds new particular time intervals to this schedule.

startHour — start hour of the interval.
startMinute — start minute of the interval.
startSecond — start second of the interval.
endHour — end hour of the interval.
endMinute — end minute of the interval.
endSecond — end second of the interval.
value — value of the interval.
weekDays — an array of week days to create intervals.
void addInterval(long start, long end, V value) Adds new particular time interval to this schedule.

start — start time of the interval, measured in milliseconds.
end — end time of the interval, measured in milliseconds.
value — value of the interval
Adding exact time moments: addMoment()
Depending on the type of schedule you are creating, in the following functions use the value argument to pass the value of the corresponding type.
Function Description
void addMoment(int week, int dayOfWeek, int hour, int minute, int second, V value) Adds new particular time moment to this schedule.

week — week of the moment.
dayOfWeek — day of the week of the moment.
hour — hour of the moment.
minute — minute of the moment.
second — second of the moment.
value — value of the moment.
void addMoment(int day, int hour, int minute, int second, V value) Adds new particular time moment to this schedule.

day — day of the moment.
hour — hour of the moment.
minute — minute of the moment.
second — second of the moment.
value — value of the moment.
void addMoment(int hour, int minute, int second, V value) Adds new particular time moment to this schedule.

hour — hour of the moment.
minute — minute of the moment.
second — second of the moment.
value — value of the moment.
void addMoment(int hour, int minute, int second, V value, int[] weekDays) Adds new particular time moment to this schedule.

hour — hour of the moment.
minute — minute of the moment.
second — second of the moment.
value — value of the moment.
weekDays — an array of week days to create moments.
void addMoment(long time, V value) Adds new particular time moment to this schedule.

time — time of the moment, measured in milliseconds.
value — value of the moment.
Adding exceptions: addException()
Depending on the type of schedule you are creating, in the following function use the value argument to pass the value of the corresponding type.
Function Description
void addException(int startYear, int startMonth, int startDay, int startHour, int startMinute, int startSecond, int endYear, int endMonth, int endDay, int endHour, int endMinute, int endSecond, V value, boolean annually) Adds new particular time intervals when the value defined by this schedule should have other values.

startYear — start year of the interval.
startMonth — start month of the interval.
startDay — start day of the interval.
startHour — start hour of the interval.
startMinute — start minute of the interval.
startSecond — start second of the interval.
endYear — end year of the interval.
endMonth — end month of the interval.
endDay — end day of the interval.
endHour — end hour of the interval.
endMinute — end minute of the interval.
endSecond — end second of the interval.
value — value of the interval.
annually — if you want the defined exception to occur annually in the specified day, sets true. Otherwise, if you want it to occur only in the specified year, sets false.
Setting the start time of the schedule: setSnapTo()
Function Description
void setSnapTo(int year, int month, int day, int hour, int minute, int second) Sets start time of the schedule.

year — start year of the interval.
month — start month of the interval.
day — start day of the interval.
hour — start hour of the interval.
minute — start minute of the interval.
second — start second of the interval
void setSnapTo(long snapTo) Sets start time of the schedule.

snapTo — the absolute date (if calendarType == true) or the number of milliseconds from the zero model time.
Setting a calendar type: setCalendarType()
Function Description
void setCalendarType(boolean calendarType) Sets the calendar type.

calendarType — if true then schedule works with calendar dates.
For example, if you create a schedule for 8:00-17:00 each day — these times will be preserved even during the days of Daylight Saving Time switching.
If this parameter is false, “1 day” will be treated as 24*60*60 seconds — this is good for modeling the behavior of some automatic machines which don't watch at the wall-clock during e.g. self-maintenance procedures.
Setting the default value: setDefaultValue()
Function Description
void setDefaultValue(V defaultValue) Sets the default value used when there is no interval defined in the schedule.
defaultValue — the value used when there is no interval defined in the schedule.
Setting the first day of week: setFirstDayOfWeek()
Function Description
void setFirstDayOfWeek(int firstDayOfWeek) Sets the first day of the week.

firstDayOfWeek — the first day of the week, this parameter is actually used when calendarType is true and timeUnit is week.
Setting intervals merging: setGlueIntervals()
Function Description
void setGlueIntervals(boolean glueIntervals) Sets the intervals with the same value to merge.

glueIntervals — if true then intersecting intervals with the same value will be merged, e.g. two intervals Day1 18:00-24:00 value=5 and Day2 00:00-02:00 value=5 will be treated as one interval Day1 18:00 - Day2 02:00 value=5.
Setting a calendar period: setPeriod()
Function Description
void setPeriod(int period) Sets the recurrence period of the schedule.

period — recurrence period of the schedule, measured in the time units specified in setTimeUnit() function. Time units may be actual — if calendarType is false, and hypothetical — if true.
Setting the thread mode: setThreadMode()
Function Description
void setSingleThreadMode(boolean singleThreadMode) Sets the thread mode.

singleThreadMode — this parameter indicates whether schedule will work in single thread environment or will be used by multiple concurrent threads. This parameter is usually set to true for schedules inside agents and experiment which don’t allow parallel execution (e.g. simulation experiment). But for schedules, located in optimization and parameter variation experiment which allow parallel execution this parameter should be set to false.
Setting time units of the schedule: setTimeUnits()
Function Description
void setTimeUnits(long timeUnits) Sets the time units of the schedule.

timeUnits — time units of the schedule, one of the time unit constants (TIME_UNIT_MINUTE, TIME_UNIT_HOUR, etc.). In the calendar-mode the only supported time units are day and week.
Creating and initializing schedules using one complex constructor (not recommended)
Function Description
new Schedule<ValueType>(Utilities owner, boolean calendarType, int firstDayOfWeek, long period, long timeUnits, Long snapTo, <ValueType> defaultValue, long[] starts, long[] ends, Object[] values, boolean glueIntervals, boolean[] exceptionsAnnually, boolean singleThreadMode) Example:
Schedule<Integer> NursePlan = new Schedule<Integer>(this, true, MONDAY, 1L * TIME_UNIT_DAY, TIME_UNIT_DAY, null, 0, varNursePlanTime, null, varNursePlanNumber, false, null, true );

owner — owner agent or experiment owning the schedule.
calendarType — if true then schedule works with calendar dates. For example, if you create a schedule for 8:00-17:00 each day — these times will be preserved even during the days of Daylight Saving Time switching. If this parameter is false, “1 day” will be treated as 24*60*60 seconds — this is good for modeling the behavior of some automatic machines which don’t watch at the wall-clock during e.g. self-maintenance procedures.
firstDayOfWeek — the first day of the week, this parameter is actually used when calendarType is true and timeUnit is TIME_UNIT_WEEK.
period — recurrence period of the schedule, measured in milliseconds. Milliseconds may be actual - if calendarType is false, and hypothetical — if true, which means that 1000*60*60*24 milliseconds denotes one day independently on Daylight Saving Time switching.
timeUnits — time units of the schedule, one of the time unit constants (TIME_UNIT_MINUTE, TIME_UNIT_HOUR, etc.).
snapTo — the absolute date (if calendarType == true) or the number of milliseconds from the zero model time (if calendarType == false) which points to the time moment to which the schedule should be snapped (usually it is defined in the Snap to property of the schedule). Moments are cloned started from the given time moment in both directions of the time-axis, with the schedule’s recurrence period. May be null — in this case schedule will be snapped to the date corresponding to the zero model time.
defaultValue — the value used when there is no interval defined in the schedule.
starts — start times of intervals, measured in milliseconds from the beginning of the period (milliseconds may be actual — if calendarType is false, and hypothetical - if true, which means that 1000*60*60*24 milliseconds denotes one day independently on Daylight Saving Time switching). This array may also contain starts exceptions (see exceptionsAnnually parameter).
ends — end times of intervals, the same description as for starts.
values — values for intervals.
glueIntervals — if true then intersecting intervals with the same value will be merged, e.g. two intervals Day1 18:00-24:00 value=5 and Day2 00:00-02:00 value=5 will be treated as one interval Day1 18:00 - Day2 02:00 value=5.
exceptionsAnnually — the array of exception flags (true means annual exception, false — single occurrence), may be empty. The number of exceptions equals the length of exceptionsAnnually array. The exceptions are read from starts, ends, values arrays. Exceptions data is located in the tails of these arrays. Note that for exceptions, starts and ends arrays contain absolute dates encoded as long numbers (see Date.getTime()).
singleThreadMode — this parameter indicates whether schedule will work in single thread environment or will be used by multiple concurrent threads. This parameter is usually set to true for schedules inside agents and experiment which don’t allow parallel execution (e.g. simulation experiment). But for schedules, located in optimization and parameter variation experiment which allow parallel execution this parameter should be set to false.
How can we improve this article?