Activities Module
Activity framework and state machine components for modeling complex entity lifecycles.
Activity
Time-based operations with capacity constraints.
- class simcraft.activities.activity.Activity[source]
Bases:
Generic[T]Time-based activity with capacity and resource synchronization.
Activities model operations that take time, with support for: - Capacity constraints - External resource requirements - State-based lifecycle management - Statistics collection
The activity lifecycle is: 1. PENDING: Entity is waiting to start 2. READY_TO_START: Resources available, can start 3. PROCESSING: Currently being processed 4. COMPLETED: Processing done 5. READY_TO_FINISH: Can depart (external signal) 6. READY_TO_DEPART: Ready to leave
- Parameters:
sim (Simulation) – Parent simulation
capacity (int) – Maximum concurrent entities
processing_time (Union[float, Callable[[T], float]]) – Fixed time or function returning processing time
need_external_start (bool) – Whether external signal needed to start
need_external_finish (bool) – Whether external signal needed to finish
name (str) – Activity name
Examples
>>> def processing_time(job): ... return job.size * 0.1 ... >>> activity = Activity( ... sim, ... capacity=2, ... processing_time=processing_time, ... name="Assembly" ... ) >>> >>> activity.on_departure(lambda job: output_queue.enqueue(job)) >>> activity.request_to_start(job)
- __init__(sim, capacity=1, processing_time=1.0, need_external_start=False, need_external_finish=False, name='')[source]
Initialize activity.
- Parameters:
sim (Simulation)
capacity (int)
processing_time (Any)
need_external_start (bool)
need_external_finish (bool)
name (str)
- Return type:
None
- property stats: ActivityStats
Get activity statistics.
- request_to_start(entity)[source]
Request an entity to start the activity.
- Parameters:
entity (T) – Entity requesting to start
- Return type:
None
- try_start(entity)[source]
External signal that entity can start.
- Parameters:
entity (T) – Entity to start
- Returns:
True if entity was found and can start
- Return type:
- try_finish(entity)[source]
External signal that entity can finish.
- Parameters:
entity (T) – Entity to finish
- Returns:
True if entity was found and can finish
- Return type:
- on_start(callback)[source]
Set callback for processing start.
- Parameters:
callback (Callable[[T], None])
- Return type:
None
- on_complete(callback)[source]
Set callback for processing complete.
- Parameters:
callback (Callable[[T], None])
- Return type:
None
- class simcraft.activities.activity.ActivityState[source]
Bases:
EnumStates for activity lifecycle.
- PENDING = 1
- READY_TO_START = 2
- PROCESSING = 3
- COMPLETED = 4
- READY_TO_FINISH = 5
- READY_TO_DEPART = 6
- class simcraft.activities.activity.ActivityStats[source]
Bases:
objectStatistics for activity performance.
- record_entry(time)[source]
Record entity entering waiting state.
- Parameters:
time (float)
- Return type:
None
- __init__(entries=0, completions=0, departures=0, total_processing_time=0.0, total_wait_time=0.0, _area_processing=0.0, _area_waiting=0.0, _last_change_time=0.0, _current_processing=0, _current_waiting=0)
- class simcraft.activities.activity.ParallelActivity[source]
Bases:
Generic[T]Parallel activities with shared waiting queue.
Useful for modeling parallel processing stations that share a common input queue.
- Parameters:
sim (Simulation) – Parent simulation
num_parallel (int) – Number of parallel activities
processing_time (Any) – Processing time per activity
Examples
>>> parallel = ParallelActivity(sim, num_parallel=3, processing_time=5.0) >>> parallel.request_to_start(job) # Goes to first available
- __init__(sim, num_parallel, processing_time=1.0, name='')[source]
Initialize parallel activities.
- Parameters:
sim (Simulation)
num_parallel (int)
processing_time (Any)
name (str)
- Return type:
None
- request_to_start(entity)[source]
Request entity to start on any available activity.
- Parameters:
entity (T) – Entity to process
- Return type:
None
State Machine
State machine framework for complex entity behavior.
- class simcraft.activities.state_machine.State[source]
Bases:
objectA state in a state machine.
- on_enter
Called when entering the state
- Type:
Optional[Callable]
- on_exit
Called when exiting the state
- Type:
Optional[Callable]
- on_stay
Called while staying in the state
- Type:
Optional[Callable]
- metadata
Additional state metadata
- Type:
Dict
- __init__(name, on_enter=None, on_exit=None, on_stay=None, is_initial=False, is_final=False, metadata=<factory>)
- class simcraft.activities.state_machine.Transition[source]
Bases:
objectA transition between states.
- guard
Condition that must be true for transition
- Type:
Optional[Callable]
- action
Action to perform during transition
- Type:
Optional[Callable]
- class simcraft.activities.state_machine.StateMachine[source]
Bases:
Generic[T]Flexible state machine for entity lifecycle management.
Supports hierarchical states, guards, actions, and timed transitions.
- Parameters:
sim (Simulation) – Parent simulation
name (str) – State machine name
Examples
>>> sm = StateMachine(sim, name="OrderProcess") >>> >>> # Define states >>> sm.add_state("created", is_initial=True) >>> sm.add_state("processing") >>> sm.add_state("shipped") >>> sm.add_state("delivered", is_final=True) >>> >>> # Define transitions >>> sm.add_transition("created", "processing", trigger="start") >>> sm.add_transition("processing", "shipped", ... trigger="complete", ... guard=lambda ctx: ctx.payment_verified) >>> sm.add_transition("shipped", "delivered", trigger="arrive") >>> >>> # Create instance and process >>> order = Order(id=1) >>> instance = sm.create_instance(order) >>> instance.trigger("start") # -> processing >>> instance.trigger("complete") # -> shipped (if payment verified)
- __init__(sim, name='')[source]
Initialize state machine.
- Parameters:
sim (Simulation)
name (str)
- Return type:
None
- add_state(name, on_enter=None, on_exit=None, is_initial=False, is_final=False, **metadata)[source]
Add a state to the machine.
- Parameters:
- Returns:
The created state
- Return type:
- add_transition(source, target, trigger='', guard=None, action=None, priority=0)[source]
Add a transition between states.
- Parameters:
- Returns:
The created transition
- Return type:
- add_timed_transition(source, target, duration, action=None)[source]
Add a timed transition that fires after a duration.
- Parameters:
- Returns:
The created transition
- Return type:
- get_transitions_from(state)[source]
Get all transitions from a state.
- Parameters:
state (str) – Source state name
- Returns:
Transitions from the state
- Return type:
List[Transition]
- create_instance(context)[source]
Create a new state machine instance.
- Parameters:
context (T) – Context object (entity) for this instance
- Returns:
New instance in initial state
- Return type:
- class simcraft.activities.state_machine.StateMachineInstance[source]
Bases:
Generic[T]Instance of a state machine for a specific entity.
- Parameters:
machine (StateMachine) – Parent state machine definition
context (T) – Context object (entity)
- __init__(machine, context)[source]
Initialize instance.
- Parameters:
machine (StateMachine[T])
context (T)
- Return type:
None
- property context: T
Get context object.
- property history: List[Tuple[float, str, str]]
Get transition history (time, from_state, to_state).