///////////////////////////////////////////////////////////////////////////////// // Copyright © 2008 W. Kowarschick // // Permission is granted to copy, distribute and/or modify this document // under the terms of the WK License, Version 1.0 (WKL 1.0) or any later version // published by Wolfgang Kowarschick. ///////////////////////////////////////////////////////////////////////////////// package mvc.model { import automaton.AutomatonEvent; import automaton.IAutomaton; [Bindable] public class ModelCar { ///////////////////////////////////////////////////////////////////////////// // Attributes ///////////////////////////////////////////////////////////////////////////// public var reaction: String = ""; private var v_automaton: IAutomaton; ///////////////////////////////////////////////////////////////////////////// // Constructor ///////////////////////////////////////////////////////////////////////////// public function ModelCar(p_automaton: IAutomaton) { v_automaton = p_automaton; // Mealy + Mealy Simplified + Moore this.addListener("motorAn"); this.addListener("motorAus"); this.addListener("getankt"); this.addListener("gefahren"); this.addListener("benzinAlle"); this.addListener("errorBenzinAlle"); // Mealy + Moore this.addListener(AutomatonEvent.ERROR); // Mealy Simplified + Moore Simplified this.addListener("errorMotorAus"); this.addListener("errorMotorAn"); } ///////////////////////////////////////////////////////////////////////////// // Protected Methods ///////////////////////////////////////////////////////////////////////////// protected function addListener(p_reaction: String):void { if (p_reaction == AutomatonEvent.ERROR) v_automaton.addEventListener(p_reaction, eh_error); else v_automaton.addEventListener(p_reaction, eh_reaction); } ///////////////////////////////////////////////////////////////////////////// // Public Methods ///////////////////////////////////////////////////////////////////////////// public function startEngine(): void { v_automaton.doAction("starteMotor"); } public function stopEngine(): void { v_automaton.doAction("stoppeMotor"); } public function drive(): void { v_automaton.doAction("fahre"); } public function driveUntilFuelConsumed(): void { v_automaton.doAction("fahreBisBenzinAlle"); } public function refuel(): void { v_automaton.doAction("tanke"); } ///////////////////////////////////////////////////////////////////////////// // Event Handlers ///////////////////////////////////////////////////////////////////////////// private function eh_error(p_event: AutomatonEvent): void { this.reaction = "Error: Aktion: " + p_event.action + ", State: " + p_event.state; } private function eh_reaction(p_event: AutomatonEvent): void { this.reaction = "Aktion: " + p_event.action + ", Reaktion: " + p_event.type + ", State: " + p_event.state; } ///////////////////////////////////////////////////////////////////////////// // End of Class ///////////////////////////////////////////////////////////////////////////// } }