// Independent worked listings. Load this chapter with vocabulary.sysml, not with other chapters. package Lch08_01 { private import CoffeeVocabulary::*; // ANCHOR: listing-1 state def Idle; state def Brewing; // ANCHOR_END: listing-1 } package Lch08_02 { private import CoffeeVocabulary::*; // ANCHOR: listing-2 state def MachineStates { state idle : Idle; state brewing : Brewing; transition first idle then brewing; } // ANCHOR_END: listing-2 } package Lch08_03 { private import CoffeeVocabulary::*; // ANCHOR: listing-3 package CoffeeMachineStates { state def Idle; state def Brewing; state def Cleaning; state def Error; state def MachineStates { state idle : Idle; state brewing : Brewing; state cleaning : Cleaning; state error : Error; transition first idle then brewing; transition first brewing then idle; transition first idle then cleaning; transition first cleaning then idle; } } // ANCHOR_END: listing-3 } package Lch08_04 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; // ANCHOR: listing-4 package CoffeeMachineStates { private import ScalarValues::*; attribute def StartBrewSignal; attribute def StartCleanSignal; state def Idle; state def Brewing; state def Cleaning; state def Error; state def MachineStates { attribute waterAvailable : Boolean; attribute cupDetected : Boolean; attribute brewComplete : Boolean; attribute cleanComplete : Boolean; attribute faultDetected : Boolean; state idle : Idle; state brewing : Brewing; state cleaning : Cleaning; state error : Error; transition first idle if waterAvailable and cupDetected then brewing; transition first brewing if brewComplete then idle; transition first idle if cleanComplete == false then cleaning; transition first cleaning if cleanComplete then idle; transition first idle if faultDetected then error; transition first brewing if faultDetected then error; } } // ANCHOR_END: listing-4 } package Lch08_05 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; // ANCHOR: listing-5 attribute def StartBrewSignal; state def MachineStates { attribute waterAvailable : Boolean; attribute cupDetected : Boolean; state idle : Idle; state brewing : Brewing; transition first idle accept StartBrewSignal if waterAvailable and cupDetected then brewing; } // ANCHOR_END: listing-5 } package Lch08_06 { private import CoffeeVocabulary::*; private import SI::s; state def Controller { state heating; state ready; state cooling; state off; state warming; state idle; attribute t_dead : ISQ::DurationValue; // ANCHOR: listing-6 transition first heating accept after(30 [s]) then ready; transition first cooling accept after(0.5 [s]) then off; transition first warming accept after(t_dead) then idle; // a duration-valued attribute // ANCHOR_END: listing-6 } } package Lch08_07 { private import CoffeeVocabulary::*; private import ScalarValues::Real; state def Controller { state brewing; state ready; attribute waterTemp : Real; // ANCHOR: listing-7 transition first brewing accept when waterTemp >= 92.0 then ready; // ANCHOR_END: listing-7 } } package Lch08_08 { private import CoffeeVocabulary::*; state def Controller { state idle; state cleaning; // ANCHOR: listing-8 attribute cleanTime : Time::TimeInstantValue; transition first idle accept at cleanTime then cleaning; // ANCHOR_END: listing-8 } } package Lch08_09 { private import CoffeeVocabulary::*; // ANCHOR: listing-9 port def DispensePort { out item ready : CupReady; } part def Brewer { port dispensePort : DispensePort; } state def DispenseControl { part brewer : Brewer; state brewing; state dispensing; transition first brewing accept cup : CupReady via brewer.dispensePort then dispensing; } // ANCHOR_END: listing-9 } package Lch08_10 { private import CoffeeVocabulary::*; part def Station { part brewer : Brewer; // ANCHOR: listing-10 state Brewing { entry action setPower { assign brewer.heaterPower := 1500.0; } exit action clearPower { assign brewer.heaterPower := 0.0; } } // ANCHOR_END: listing-10 } } package Lch08_11 { private import CoffeeVocabulary::*; // ANCHOR: listing-11 action def LogMessage; action def StartHeating; action def StopHeating; state def Brewing { entry action : StartHeating; exit action : StopHeating; } // ANCHOR_END: listing-11 } package Lch08_12 { private import CoffeeVocabulary::*; // ANCHOR: listing-12 action def MonitorTemperature; state def Brewing { entry action : StartHeating; do action : MonitorTemperature; exit action : StopHeating; } // ANCHOR_END: listing-12 } package Lch08_13 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; // ANCHOR: listing-13 package CoffeeMachineStates { private import ScalarValues::*; // Signal definitions for triggers attribute def StartBrewSignal; attribute def StartCleanSignal; attribute def FaultSignal; attribute def ResetSignal; // Action definitions for entry/exit action def LogStatus; action def StartHeating; action def StopHeating; action def RunCleanCycle; action def StopCleanCycle; action def DisableOutputs; // State definitions with entry/exit behavior state def Idle { entry action : LogStatus; } state def Brewing { entry action : StartHeating; exit action : StopHeating; } state def Cleaning { entry action : RunCleanCycle; exit action : StopCleanCycle; } state def Error { entry action : DisableOutputs; } // The composite state machine state def MachineStates { attribute waterAvailable : Boolean; attribute cupDetected : Boolean; attribute brewComplete : Boolean; attribute cleanComplete : Boolean; state idle : Idle; state brewing : Brewing; state cleaning : Cleaning; state error : Error; // Start in idle entry; then idle; // Idle -> Brewing: user requests brew, preconditions met transition first idle accept StartBrewSignal if waterAvailable and cupDetected then brewing; // Brewing -> Idle: brew cycle finished transition first brewing if brewComplete then idle; // Idle -> Cleaning: user requests cleaning transition first idle accept StartCleanSignal then cleaning; // Cleaning -> Idle: clean cycle finished transition first cleaning if cleanComplete then idle; // Any operational state -> Error: fault detected transition first idle accept FaultSignal then error; transition first brewing accept FaultSignal then error; transition first cleaning accept FaultSignal then error; // Error -> Idle: operator resets transition first error accept ResetSignal then idle; } } // ANCHOR_END: listing-13 } package Lch08_14 { private import CoffeeVocabulary::*; // ANCHOR: listing-14 state def Brewing { entry action : StartHeating; then heating; state heating; state extracting; state dispensing; transition first heating then extracting; transition first extracting then dispensing; exit action : StopHeating; } // ANCHOR_END: listing-14 } package Lch08_15 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; // ANCHOR: listing-15 state def MachineStates parallel { attribute brewComplete : Boolean; attribute cleanComplete : Boolean; attribute waterAvailable : Boolean; attribute cupDetected : Boolean; // Region 1: the operational lifecycle state operational { state idle : Idle; state brewing : Brewing; state cleaning : Cleaning; entry; then idle; transition first idle accept StartBrewSignal if waterAvailable and cupDetected then brewing; transition first brewing if brewComplete then idle; transition first idle accept StartCleanSignal then cleaning; transition first cleaning if cleanComplete then idle; } // Region 2: fault supervision, running concurrently state supervision { state armed; state error : Error; entry; then armed; transition first armed accept FaultSignal then error; transition first error accept ResetSignal then armed; } } // ANCHOR_END: listing-15 } package Lch08_16 { private import CoffeeVocabulary::*; // ANCHOR: listing-16 state def OneShot { state warmup; state ready; entry; then warmup; transition first warmup accept ReadySignal then ready; transition first ready then done; } // ANCHOR_END: listing-16 } package Lch08_17 { private import CoffeeVocabulary::*; private import ScalarValues::String; // ANCHOR: listing-17 state def BrewPhases { attribute resumeAt : String; state dispatch; state grinding { entry action markGrinding { assign resumeAt := "grinding"; } } state extracting { entry action markExtracting { assign resumeAt := "extracting"; } } entry; then dispatch; // Re-entry branches on the recorded phase instead of always restarting transition first dispatch if resumeAt == "extracting" then extracting; transition first dispatch if resumeAt != "extracting" then grinding; transition first grinding accept PhaseDone then extracting; } // ANCHOR_END: listing-17 } package Lch08_18 { private import CoffeeVocabulary::*; // ANCHOR: listing-18 state def Brewing { entry action : StartHeating; do action : MonitorTemperature; exit action : StopHeating; } state def Dispensing; state def BrewSequence { state brewing : Brewing; state dispensing : Dispensing; transition first brewing then dispensing; // fires when brewing's `do` completes } // ANCHOR_END: listing-18 } package Lch08_19 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-19 part def Brewer { attribute waterTemp : Real; exhibit state brewingMode { state heating; state ready; entry; then heating; transition first heating accept when waterTemp >= 92.0 then ready; } } // ANCHOR_END: listing-19 } package Lch08_20 { private import CoffeeVocabulary::*; // ANCHOR: listing-20 action def BrewCycle { action grind : GrindBeans; action heat : HeatWater; action extract : ExtractCoffee; action dispense : DispenseCoffee; first grind then heat; first heat then extract; first extract then dispense; } state def Brewing { entry action : StartHeating; do action : BrewCycle; exit action : StopHeating; } // ANCHOR_END: listing-20 }