Context Modeling
Context Constraints
Context constraints define relationships between contexts, such as mutual exclusion, requirements, and alternatives. Use these functions to model rules for context activation. Note that, e.g., an exclusion between two contexts C1 and C2 does not mean that C1 will not be activated if C2 is already active. Instead, after activating C1, a Petri net will check if this activation complies with the defined constraints. If constraints are not fulfilled, the Petri net will heal the state by deactivating it again.
The following constraints are defined:
Contexts.exclusion
— Methodexclusion(c1::T1, c2::T2) where {T1, T2 <: Context}
Creates a strict mutual exclusion rule between two contexts. When one context is active, the other context will be forced to deactivate and cannot be activated.
Arguments:
c1
,c2
: Two contexts of any type that inherits from Context
Returns true
after creating and adding the Petri net rule to the control network.
Contexts.weakExclusion
— MethodweakExclusion(c1::T1, c2::T2) where {T1, T2 <: Context}
Creates a weak mutual exclusion rule between two contexts. When one context becomes active, it will deactivate the other context, but the other context can be reactivated.
Arguments:
c1
,c2
: Two contexts of any type that inherits from Context
Returns true
after creating and adding the Petri net rule to the control network.
weakExclusion(c1::T1, c2::T2, args...) where {T1, T2 <: Context}
Creates weak mutual exclusion rules between multiple contexts. Applies weakExclusion to every pair of contexts in the provided list.
Arguments:
c1
,c2
: First two contextsargs...
: Additional contexts
Returns true
after creating all Petri net rules.
Contexts.weakExclusion
— MethodweakExclusion(c1::T1, c2::T2, args...) where {T1, T2 <: Context}
Creates weak mutual exclusion rules between multiple contexts. Applies weakExclusion to every pair of contexts in the provided list.
Arguments:
c1
,c2
: First two contextsargs...
: Additional contexts
Returns true
after creating all Petri net rules.
Contexts.directedExclusion
— MethoddirectedExclusion(p::Pair{T1, T2}) where {T1, T2 <: Context}
Creates a one-way exclusion rule. When the first context becomes active, it forces the second context to deactivate.
Arguments:
p
: A Pair where first element is the controlling context and second is the controlled context
Returns true
after creating and adding the Petri net rule.
Contexts.requirement
— Functionrequirement(p::Pair{T1, T2}) where {T1 <: Context, T2 <: Union{AbstractContextRule, Context}}
Creates a requirement rule. The first context can only be active if the second context/rule is active. If second becomes inactive, first is forced to deactivate.
Arguments:
p
: A Pair where first element must be a Context, second can be Context or ContextRule
Returns true
after creating and adding the Petri net rule.
Contexts.weakExclusion
— MethodweakExclusion(c1::T1, c2::T2) where {T1, T2 <: Context}
Creates a weak mutual exclusion rule between two contexts. When one context becomes active, it will deactivate the other context, but the other context can be reactivated.
Arguments:
c1
,c2
: Two contexts of any type that inherits from Context
Returns true
after creating and adding the Petri net rule to the control network.
weakExclusion(c1::T1, c2::T2, args...) where {T1, T2 <: Context}
Creates weak mutual exclusion rules between multiple contexts. Applies weakExclusion to every pair of contexts in the provided list.
Arguments:
c1
,c2
: First two contextsargs...
: Additional contexts
Returns true
after creating all Petri net rules.
Contexts.weakExclusion
— MethodweakExclusion(c1::T1, c2::T2) where {T1, T2 <: Context}
Creates a weak mutual exclusion rule between two contexts. When one context becomes active, it will deactivate the other context, but the other context can be reactivated.
Arguments:
c1
,c2
: Two contexts of any type that inherits from Context
Returns true
after creating and adding the Petri net rule to the control network.
weakExclusion(c1::T1, c2::T2, args...) where {T1, T2 <: Context}
Creates weak mutual exclusion rules between multiple contexts. Applies weakExclusion to every pair of contexts in the provided list.
Arguments:
c1
,c2
: First two contextsargs...
: Additional contexts
Returns true
after creating all Petri net rules.
Contexts.strongInclusion
— MethodstrongInclusion(p::Pair{T1, T2}) where {T1 <: Union{OrContextRule, Context}, T2 <: Context}
Creates a strong inclusion rule. Similar to weak inclusion, but also deactivates the first context if the second context becomes inactive while first is active.
Arguments:
p
: A Pair where first element must be either a Context or OrContextRule, second must be a Context
Returns true
after creating and adding the Petri net rule. Throws an error if first element contains non-OR operations.
Contexts.alternative
— Methodalternative(contexts::Context...)
Creates a rule ensuring exactly one context is active at all times from the given set of contexts. When one context is activated, any previously active context is deactivated. If the active context is deactivated, the deactivation is prevented.
Arguments:
contexts...
: Variable number of Context arguments
Returns true
after creating and adding the Petri net rule to the control network. Throws an error if less than 2 contexts are provided.
Context Groups
Contexts.ContextGroup
— TypeContextGroup
Groups multiple contexts for alternative activation.
Fields:
subContexts
: Vector of Contexts in the group.
Example: ContextGroup([ctx1, ctx2])
Contexts.ContextGroup
— MethodContextGroup(subContexts::Context...)
Creates a group of contexts, allowing alternative activation, meaning exactly one context of the grouped contexts must be active. Returns a ContextGroup
object containing the provided contexts.
Calling the `ContextGroup` object returns the currently active context in the group.
Context groups allow you to bundle contexts for alternative activation. Only one context in a group can be active at a time.
Calling a context group returns the currently active context of this group. This can be helpful in combination with the @context
macro:
Context State Machines
Contexts.@ContextStateMachine
— Macro@ContextStateMachine(name, body)
Macro to define a context state machine. Accepts a name and a body containing:
@variable
declarations@contexts
declarations@initialState
declaration@transition
rules
Generates a ContextStateMachine
instance with specified configuration.
Conditions of transitions are evaluated whenever a variable is set. Only Boolean expressions containing variables specified with @variable are allowed as conditions.
Example: @ContextStateMachine MyStateMachine begin @variable x::Int = 0 @variable y::Float64 = 1.0 @contexts StateA, StateB, StateC @initialState StateA @transition StateA => StateB : x > 10 @transition StateB => StateC : y < 0.5 @transition StateC => StateA : x <= 10 && y >= 0.5 end
Contexts.ContextStateMachine
— Typestruct ContextStateMachine
Represents a state machine over contexts.
variables
: Dict of variable names to value/type pairs.transitions
: Dict mapping contexts to transition expressions.subContexts
: Dict of context names to context instances.
Context state machines allow you to model transitions between contexts based on variable values and transition rules. Use the @ContextStateMachine
macro to define a state machine, and checkStateMachineCondition
to enforce transitions.