<taskModel about="urn:dosuir.starfleet.MiracleWorker" xmlns="http://ce.org/cea-2018">
  <!-- v1.0, Miracle Worker: Starfleet Intelligent Training Utility -->
  
  <!-- Section 1:
  		Task Decomposition for Beaming Down an Away Team
  	-->
 
  <task id="BeamDown" >
  	<subtasks id="beaming" ordered="false" >
  		<step name="prepare" task="PreparePads" />
  		<step name="select" task="TransportDest" />
  		<step name="commit" task="Energize" requires="prepare select" />
  		<binding slot="$commit.coords" value="$select.coords" />
  	</subtasks>
  </task>
  
  <task id="Energize" >
  	<input name="coords" type="Location" />
  	<output name="dest" type="Location" />
  	<postcondition> ( TheShip.isCoreOn() == true &#38;&#38; ($this.dest != undefined) ) </postcondition>
  	<script> $this.dest = $this.coords; </script>
  </task>
  
  <task id="PreparePads" >
  	<subtasks id="preparing" >
  		<step name="clear" task="ClearPads" />
  		<step name="board" task="BoardPads" />
  	</subtasks>
  </task>
  
  <task id="TransportDest" >
  	<output name="coords" type="Location" />
  	<subtasks id="selecting" >
  		<step name="scan" task="ScanPlanet" />
  		<step name="coords" task="EnterCoords" />
  		<binding slot="$this.coords" value="$coords.loc" />
  	</subtasks>
  </task>
  
  <!-- Clear Transporter Pads -->
  <task id="ClearPads" />
  
  <!-- Board Transporter Pads -->
  <task id="BoardPads" />
  
  <!-- Scan Destination Planet -->
  <task id="ScanPlanet" />
  
  <!-- Enter Destination Coords -->
  <task id="EnterCoords" >
  	<input name="loc" type="Location" />
  </task>
  
  <!-- Section 2:
  		Task Decomposition for Exchanging Dilithium Crystal
  	-->
  	
  <task id="ExchangeCrystal" >
  	<subtasks id="exchanging" >
  		<step name="dropwarp" task="DropFromWarp" />
  		<step name="deactivate" task="TurnOffCore" />
  		<step name="replace" task="ReplaceCrystal" />
  		<step name="activate" task="TurnOnCore" />
  		<binding slot="$activate.crystal" value="$replace.crystal" />
  	</subtasks>
  </task>
  
  <task id="DropFromWarp" >
  	<postcondition> !TheShip.atWarp() </postcondition>
  	<script> TheShip.atwarp = false; </script>
  </task>
  
  <task id="TurnOffCore" >
  	<postcondition> !TheShip.isCoreOn() </postcondition>
  	<subtasks id="deactivating" >
  		<step name="vent" task="VentWarpPlasma" />
  		<step name="ma_off" task="MAInjectorsOff" />
  		<step name="turn_off" task="CorePowerOff" />
  	</subtasks>
  </task>
  
  <task id="ReplaceCrystal" >
  	<output name="crystal" type="Crystal" />
  	<subtasks id="replacing" ordered="false" >
  		<step name="open" task="OpenChamber" />
  		<step name="remove" task="RemoveCrystal" requires="open" />
  		<step name="select" task="SelectCrystal" />
  		<step name="insert" task="InsertCrystal" requires="remove select" />
  		<step name="close" task="CloseChamber" requires="insert" />
  		<binding slot="$this.crystal" value="$select.crystal" />
  	</subtasks>
  </task>
  
  <task id="TurnOnCore" >
  	<input name="crystal" type="Crystal" />
	<output name="doneyet" type="boolean" />
  	<postcondition sufficient="true"> 
  			TheShip.isCoreOn() &amp;&amp; ($this.doneyet == true) 
  	</postcondition>
  	<subtasks id="activating" >
  		<step name="ma_on" task="MAInjectorsOn" />
  		<step name="plasma" task="NewWarpPlasma" />
  		<step name="turn_on" task="CorePowerOn" />
  		<binding slot="$this.doneyet" value="$turn_on.result" />
  		<binding slot="$turn_on.crystal" value="$this.crystal" />
  	</subtasks>
  </task>
  
  <task id="VentWarpPlasma" />

  <task id="MAInjectorsOff" />
  
  <task id="CorePowerOff" >
  	<script> TheShip.warpcoreon = false; </script>
  </task>
  
  <task id="MAInjectorsOn" />
  
  <task id="NewWarpPlasma" />
  
  <task id="CorePowerOn" >
  	<input name="crystal" type="Crystal" />
  	<output name="result" type="boolean" />
  	<script> 
  		TheShip.warpcoreon = $this.crystal.isGood(); 
  		$this.result = $this.crystal.isGood();
  	</script>
  </task>
  
  <task id="OpenChamber" />
  
  <task id="RemoveCrystal" />
  
  <task id="SelectCrystal" >
  	<input name="crystal" type="Crystal" />
  </task>
  
  <task id="InsertCrystal" />
  
  <task id="CloseChamber" />
  	
  <!-- Section 3:
  		Task Decomposition for Refueling Impulse Engine
  	-->
  	
  <task id="RefuelImpulse" >
  	<subtasks id="refuelling" >
  		<step name="dropimpulse" task="DropImpulse" />
  		<step name="disengage" task="TurnOffFusion" />
  		<step name="replace" task="SwitchDeuterium" />
  		<step name="engage" task="TurnOnFusion" />
  		<binding slot="$engage.fuel" value="$replace.fuel" />
  	</subtasks>
  </task>
  
  <task id="DropImpulse" >
  	<postcondition> (TheShip.atImpulse() == false) </postcondition>
  	<script> TheShip.atimpulse = false; </script>
  </task>
  
  <task id="TurnOffFusion" >
  	<postcondition> !TheShip.isFusionOn() </postcondition>
  	<subtasks id="disengaging" >
  		<step name="plasma_off" task="PlasmaFlowOff" />
  		<step name="eps_off" task="DisconnectEPS" />
  		<step name="turn_off" task="EngineOff" />
  	</subtasks>
  </task>
  
  <task id="SwitchDeuterium" >
  	<output name="fuel" type="Deuterium" />
  	<subtasks id="switching" ordered="false" >
  		<step name="remove" task="RemoveDeuterium" />
  		<step name="select" task="SelectDeuterium" />
  		<step name="insert" task="InsertDeuterium" requires="remove select" />
  		<binding slot="$this.fuel" value="$select.fuel" />
  	</subtasks>
  </task>
  
  <task id="TurnOnFusion" >
  	<input name="fuel" type="Deuterium" />
  	<output name="doneyet" type="boolean" />
  	<postcondition sufficient="true"> 
  			TheShip.isFusionOn() &#38;&#38; ($this.doneyet == true) 
  	</postcondition>
  	<subtasks id="engaging" >
  		<step name="eps_on" task="ConnectEPS" />
  		<step name="plasma_on" task="PlasmaFlowOn" />
  		<step name="turn_on" task="EngineOn" />
  		<binding slot="$this.doneyet" value="$turn_on.result" />
  		<binding slot="$turn_on.fuel" value="$this.fuel" />
  	</subtasks>
  </task>
  
  <task id="PlasmaFlowOff" />
  
  <task id="DisconnectEPS" />
  
  <task id="EngineOff" >
  	<script> TheShip.fusionon = false; </script>
  </task>
  
  <task id="ConnectEPS" />
  
  <task id="PlasmaFlowOn" />
  
  <task id="EngineOn" >
  	<input name="fuel" type="Deuterium" />
  	<output name="result" type="boolean" />
  	<script> 
  		TheShip.fusionon = $this.fuel.isGood(); 
  		$this.result = $this.fuel.isGood();
  	</script>
  </task>
  
  <task id="RemoveDeuterium" />
  
  <task id="SelectDeuterium" >
  	<input name="fuel" type="Deuterium" />
  </task>
  
  <task id="InsertDeuterium" />
  	
  <!-- Section 4:
  		Domain Simulation Script
  	-->
  <script init="true" >
  
  // *****************************************
  // ************* Beaming Down **************
  // *****************************************
  
  // define class Location
  function Location( latt, longi ) {
  	this.latt	= latt;
  	this.longi	= longi;
  }
  Location.prototype.Latitude	= function () { return this.latt; }
  Location.prototype.Longitude	= function () { return this.longi; }
  Location.prototype.toString	= function () { return "Lattitude " + this.latt + ", Longitude " + this.longi; }
  
  // Available Locations
  var	loc1 = new Location( 0, 0 ),
  		loc2 = new Location( 59, 73 ),
  		loc3 = new Location( -27, 103 );
  		
  // *****************************************		
  // *********** Replace Dilithium ***********
  // *****************************************
  
  // define class Crystal
  function Crystal( fractured ) {
  	this.fractured = fractured;
  }
  Crystal.prototype.isGood = function () { return (this.fractured == false); }
  
  // Available Dilithium Crystals
  var	crys0 = new Crystal( true ),
  		crys1 = new Crystal( true ),
  		crys2 = new Crystal( false );
  		
  // *****************************************
  // ************* Refuel Impulse ************
  // *****************************************
  
  // define class Deuterium
  function Deuterium( empty ) {
  	this.empty = empty;
  }
  Deuterium.prototype.isGood = function () { return (this.empty == false); }
  
  // Available Deuterium Pods
  var	pod0 = new Deuterium( true ),
  		pod1 = new Deuterium( true ),
  		pod2 = new Deuterium( false );
  
  // *****************************************
  // **************** The Ship ***************
  // *****************************************
  
  // define class Ship
  function Ship( ) {
  	this.warpcoreon		= true;
  	this.fusionon		= true;
  	this.atimpulse		= false;
  	this.atwarp			= false;
  	this.crystal		= crys0;
  	this.dchamberopen	= false;
  	this.fuel			= pod0;
  }
  Ship.prototype.isCoreOn		= function () { return this.warpcoreon; }
  Ship.prototype.isFusionOn		= function () { return this.fusionon; }
  Ship.prototype.atWarp			= function () { return this.atwarp; }
  Ship.prototype.atImpulse		= function () { return this.atimpulse; }
  Ship.prototype.dChamberOpen	= function () { return this.dchamberopen; }
  Ship.prototype.dChamberEmpty	= function () { return (this.crystal == null); }
  
  // Create the Ship
  var TheShip = new Ship(); 
  
  </script>
       
</taskModel>