// Independent worked listings. Load this chapter with vocabulary.sysml, not with other chapters. package Lch09_01 { private import CoffeeVocabulary::*; private import ScalarValues::Real; private import ScalarValues::Boolean; calc def Evaluate { in waterTemp : Real; return : Boolean = // ANCHOR: listing-1 waterTemp >= 90.0 and waterTemp <= 96.0 // ANCHOR_END: listing-1 ; } } package Lch09_02 { private import CoffeeVocabulary::*; private import ScalarValues::Real; calc def Evaluate { in grindSeconds : Real; in heatSeconds : Real; in extractSeconds : Real; return : Real = // ANCHOR: listing-2 grindSeconds + heatSeconds + extractSeconds // ANCHOR_END: listing-2 ; } } package Lch09_03 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-3 constraint def SafeToBrew { in waterTank : WaterTank[0..1]; in minimumLevel : Real; doc /* The second test is only reached when a tank is present. */ waterTank != null and waterTank.level > minimumLevel } // ANCHOR_END: listing-3 } package Lch09_04 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; calc def Evaluate { in brewCommand : Boolean; in cupDetected : Boolean; in lidClosed : Boolean; return : Boolean = // ANCHOR: listing-4 not brewCommand or cupDetected and lidClosed // ANCHOR_END: listing-4 ; } } package Lch09_05 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; calc def Evaluate { in brewCommand : Boolean; in cupDetected : Boolean; in lidClosed : Boolean; return : Boolean = // ANCHOR: listing-5 (not brewCommand) or (cupDetected and lidClosed) // ANCHOR_END: listing-5 ; } } package Lch09_06 { private import CoffeeVocabulary::*; private import ScalarValues::Integer; private import ScalarValues::Real; part def Station { attribute tempDelta : Real; attribute cycleNumber : Integer; // ANCHOR: listing-6 attribute squaredTemperatureError = tempDelta ** 2; attribute selfTestSlot = cycleNumber % 10; // every tenth cycle is a self-test // ANCHOR_END: listing-6 } } package Lch09_07 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; part def Station { attribute brewRequested : Boolean; attribute cupPresent : Boolean; attribute manualOverride : Boolean; attribute scheduledRun : Boolean; // ANCHOR: listing-7 attribute brewOk = brewRequested implies cupPresent; // identical to: not brewRequested or cupPresent attribute exactlyOne = manualOverride xor scheduledRun; // exactly one is active at a time // ANCHOR_END: listing-7 } } package Lch09_08 { private import CoffeeVocabulary::*; private import ScalarValues::Real; calc def Evaluate { in configuredTargetTemp : Real[0..1]; return : Real = // ANCHOR: listing-8 configuredTargetTemp ?? 93.0 // ANCHOR_END: listing-8 ; } } package Lch09_09 { private import CoffeeVocabulary::*; part def Station { part sensor : Sensor; // ANCHOR: listing-9 attribute isExactly = sensor istype Thermistor; // is `sensor` exactly Thermistor? attribute isAKind = sensor hastype Sensor; // is `sensor` Sensor or any subtype? ref part coerced = sensor as Thermistor; // treat sensor as a Thermistor // ANCHOR_END: listing-9 } } package Lch09_10 { private import CoffeeVocabulary::*; private import ControlFunctions::collect; private import ControlFunctions::exists; private import ControlFunctions::forAll; private import ControlFunctions::reject; private import ControlFunctions::select; part def Station { part brewers : Brewer[1..*]; // ANCHOR: listing-10 ref part highPower = brewers->select {in b : Brewer; b.power > 1000.0}; // filter attribute allTargets = brewers->collect {in b : Brewer; b.targetTemp}; // map ref part healthy = brewers->reject {in b : Brewer; b.fault}; // filter (negated) attribute allBelow = brewers->forAll {in b : Brewer; b.targetTemp <= 100.0}; // every element satisfies attribute anyFault = brewers->exists {in b : Brewer; b.fault}; // some element satisfies // ANCHOR_END: listing-10 } } package Lch09_11 { private import CoffeeVocabulary::*; private import ControlFunctions::forAll; private import ScalarValues::Boolean; calc def Evaluate { in brewers : Brewer[1..*]; return : Boolean = // ANCHOR: listing-11 brewers->forAll {in b : Brewer; b.waterTemp >= 90.0 and b.waterTemp <= 96.0} // ANCHOR_END: listing-11 ; } } package Lch09_12 { private import CoffeeVocabulary::*; part def Station { part brewers : Brewer[1..*]; // ANCHOR: listing-12 ref part firstBrewer = brewers#(1); // first brewer attribute cycleNumbers = 1..5; // sequence 1, 2, 3, 4, 5 // ANCHOR_END: listing-12 } } package Lch09_13 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; calc def Evaluate { in mode : BrewMode; return : Boolean = // ANCHOR: listing-13 mode == BrewMode::Espresso // not just `Espresso` // ANCHOR_END: listing-13 ; } } package Lch09_14 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; private import ScalarValues::String; part def Station { attribute waterLow : Boolean; // ANCHOR: listing-14 attribute label : String := "Brewing"; attribute statusText : String := if waterLow ? "Low Water" else "System OK"; // ANCHOR_END: listing-14 } } package Lch09_15 { private import CoffeeVocabulary::*; private import ScalarValues::Real; private import ScalarValues::String; // ANCHOR: listing-15 attribute def SensorReading { attribute name : String; attribute value : Real; attribute unit : String; } attribute reading : SensorReading; // ANCHOR_END: listing-15 } package Lch09_16 { private import CoffeeVocabulary::*; private import ScalarValues::Real; calc def Evaluate { in ambient : Real; return : Real = // ANCHOR: listing-16 if ambient < 18.0 ? 94.0 else 93.0 // ANCHOR_END: listing-16 ; } } package Lch09_17 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-17 calc def TargetBrewTemp { in ambient : Real; return result : Real = if ambient < 18.0 ? 94.0 else 93.0; } // ANCHOR_END: listing-17 } package Lch09_18 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-18 part def Brewer { attribute waterTemp : Real; attribute ambientTemp : Real; calc targetTemp : TargetBrewTemp { in :>> ambient = ambientTemp; return :>> result; } } // ANCHOR_END: listing-18 } package Lch09_19 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-19 part def Brewer { attribute ambientTemp : Real; attribute targetTemp : Real = if ambientTemp < 18.0 ? 94.0 else 93.0; } // ANCHOR_END: listing-19 } package Lch09_20 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; private import ScalarValues::Real; // ANCHOR: listing-20 part def Brewer { attribute ambientTemp : Real; attribute targetTemp : Real = if ambientTemp < 18.0 ? 94.0 else 93.0; attribute boilerLimit : Real = 100.0; attribute headroom : Real = boilerLimit - targetTemp; attribute safe : Boolean = headroom > 0.0; } // ANCHOR_END: listing-20 } package Lch09_21 { private import CoffeeVocabulary::*; // ANCHOR: listing-21 part highAltitudeBrewer : Brewer { calc temperatureAtAltitude : TargetBrewTemp { in :>> ambient = highAltitudeBrewer.ambientTemp - 5.0; // override: simulate cooler effective ambient return :>> result; } } // ANCHOR_END: listing-21 } package Lch09_22 { private import CoffeeVocabulary::*; private import ControlFunctions::collect; private import ScalarValues::Real; private import ControlFunctions::reduce; // ANCHOR: listing-22 calc def WorstHeadroom { in stations : Brewer[*]; return : Real = stations->collect {in s : Brewer; s.boilerLimit - s.targetTemp}->reduce NumericalFunctions::min; } // ANCHOR_END: listing-22 } package Lch09_23 { private import CoffeeVocabulary::*; private import ControlFunctions::forAll; // ANCHOR: listing-23 constraint def AllStationsSafe { in stations : Brewer[*]; stations->forAll {in s : Brewer; s.waterTemp >= 90.0 and s.waterTemp <= 96.0} } // ANCHOR_END: listing-23 } package Lch09_24 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-24 private import StateSpaceRepresentation::*; calc def HeaterRhs :> GetDerivative { in t : Real; in temp : Real; in heaterPower : Real; in lossCoeff : Real; in thermalMass : Real; return dT_dt : Real = (heaterPower - lossCoeff * temp) / thermalMass; } // ANCHOR_END: listing-24 } package Lch09_25 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-25 constraint def BrewTempInRange { in temp : Real; temp >= 90.0 and temp <= 96.0 } // ANCHOR_END: listing-25 } package Lch09_26 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-26 part def Brewer { attribute waterTemp : Real; attribute ambientTemp : Real; calc targetTemp : TargetBrewTemp { in :>> ambient = ambientTemp; return :>> result; } assert constraint tempCheck : BrewTempInRange { in :>> temp = waterTemp; } } // ANCHOR_END: listing-26 } package Lch09_27 { private import CoffeeVocabulary::*; private import ScalarValues::Real; part def Station { attribute waterTemp : Real; // ANCHOR: listing-27 assert constraint { waterTemp >= 90.0 and waterTemp <= 96.0 } // ANCHOR_END: listing-27 } } package Lch09_28 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; // ANCHOR: listing-28 part def Brewer { attribute dispenseRequested : Boolean; attribute cupPresent : Boolean; assert not constraint { dispenseRequested and not cupPresent } } // ANCHOR_END: listing-28 } package Lch09_29 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-29 package CoffeeMachineAnalysis { private import ScalarValues::*; private import CoffeeMachineDomain::*; calc def TargetBrewTemp { in ambient : Real; return target : Real = if ambient < 18.0 ? 94.0 else 93.0; } } // ANCHOR_END: listing-29 } package Lch09_30 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; private import ScalarValues::Real; // ANCHOR: listing-30 package CoffeeMachineAnalysis { private import ScalarValues::*; private import CoffeeMachineDomain::*; calc def TargetBrewTemp { in ambient : Real; return target : Real = if ambient < 18.0 ? 94.0 else 93.0; } constraint def SafetyInterlock { in cupDetected : Boolean; in brewCommand : Boolean; not brewCommand or cupDetected } } // ANCHOR_END: listing-30 } package Lch09_31 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; // ANCHOR: listing-31 constraint def SafetyInterlock { in cupDetected : Boolean; in brewCommand : Boolean; brewCommand implies cupDetected } // ANCHOR_END: listing-31 } package Lch09_32 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; private import ScalarValues::Real; // ANCHOR: listing-32 package CoffeeMachineSystem { private import ScalarValues::*; private import CoffeeMachineDomain::*; private import CoffeeMachineAnalysis::*; part coffeeMachine : CoffeeMachine { attribute ambientTemp : Real; attribute cupPresent : Boolean; attribute brewRequested : Boolean; part :>> brewer : Brewer { calc targetTemp : TargetBrewTemp { in :>> ambient = coffeeMachine.ambientTemp; return :>> result; } assert constraint tempSafe : BrewTempInRange { in :>> temp = waterTemp; } } assert constraint interlock : SafetyInterlock { in :>> cupDetected = cupPresent; in :>> brewCommand = brewRequested; } } } // ANCHOR_END: listing-32 } package Lch09_33 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-33 calc def BrewCycleTime { in grindSeconds : Real; in heatSeconds : Real; in extractSeconds : Real; return total : Real = grindSeconds + heatSeconds + extractSeconds; } // ANCHOR_END: listing-33 } package Lch09_34 { private import CoffeeVocabulary::*; private import ScalarValues::Real; // ANCHOR: listing-34 calc def AdjustedExtractTime { in baseSeconds : Real; in grindFactor : Real; return adjusted : Real = baseSeconds * grindFactor; } // ANCHOR_END: listing-34 } package Lch09_35 { private import CoffeeVocabulary::*; private import ScalarValues::Boolean; private import ScalarValues::Real; // ANCHOR: listing-35 constraint def ReadyToBrew { in waterLevel : Real; in minWaterLevel : Real; in temp : Real; in cupPresent : Boolean; cupPresent and waterLevel >= minWaterLevel and temp >= 90.0 and temp <= 96.0 } // ANCHOR_END: listing-35 }