﻿<taskModel about="urn:cetask.wpi.edu:project:Mails" xmlns="http://ce.org/cea-2018">
  <task id="SendPackage">
    <input name="origin" type="string"/>
    <subtasks id="primary" ordered="false">
      <step name="weight" task="SpecifyWeight" minOccurs="0" maxOccurs="3"/>
      <step name="dest" task="SpecifyDestination" maxOccurs="3"/>
      <step name="services" task="SelectServices" requires="weight dest"/>
      <step name="done" task="CompleteTransaction" requires="services"/>

      <binding slot="$dest.origin" value="$this.origin"/>
      <binding slot="$services.weight" value="$weight.output"/>
      <binding slot="$services.dest" value="$dest.output"/>
      <binding slot="$done.weight" value="$weight.output"/>
      <binding slot="$done.origin" value="$this.origin"/>
      <binding slot="$done.dest" value="$dest.output"/>
      <binding slot="$done.primary" value="$services.primary"/>
      <binding slot="$done.addons" value="$services.addons"/>

    </subtasks>
  </task>

  <task id="SpecifyWeight">
    <input name="input" type="number"/>
    <output name="output" type="number"/>
	<script> 
	if ( $this.input &gt; 0 ) {
		$this.output = $this.input;
		varWeight = $this.input;
	} else {
		$this.success = false;
	}
	</script>
  </task>

  <task id="SpecifyDestination">
    <input name="origin" type="string"/>
    <output name="output" type="string"/>
    <subtasks id="top">
      <step name="list" task="LookupTopDests"/>
      <step name="select" task="SelectDest"/>
      <binding slot="$this.output" value="$select.output"/>
    </subtasks>
	<subtasks id="search">
      <step name="search" task="SearchForDest"/>
      <step name="select" task="SelectDest"/>
      <binding slot="$this.output" value="$select.output"/>
	</subtasks>
	
  </task>

  <task id="LookupTopDests">
  	<output name="topDests" type="ArrayList"/>
	<postcondition> $this.topDests != undefined; </postcondition>
	<script>$this.topDests = broker.getTopTenDestinations();</script>
  </task>
  
  <task id="SelectDest">
  	<input name="input" type="string"/>
    <output name="output" type="string"/>
    <output name="valid" type="boolean"/>
	<script> 
		$this.success = broker.validateDestination($this.input);
		if ($this.success) {
			$this.output = $this.input;
			varDest = $this.input;
		}
	</script>
  </task>

  <task id="SearchForDest">
  	<input name="searchString" type="string"/>
    <output name="matches" type="ArrayList"/>
	<postcondition> $this.matches != undefined; </postcondition>
	<script> $this.matches = broker.findDestinationMatches($this.searchString); </script>
  </task>

  <task id="SelectServices">
    <input name="weight" type="number"/>
    <input name="dest" type="string"/>
    <output name="primary" type="string"/>
    <output name="addons" type="ArrayList"/>
    <subtasks id="only">
      <step name="getPrimaries" task="GetPrimarySvcs"/>
      <step name="selectPrimary" task="SelectPrimary"/>
      <step name="getAddOns" task="GetAddSvcs"/>
      <step name="selectAddOn" task="SelectAddOn" minOccurs="0" maxOccurs="3"/>
      <binding slot="$getPrimaries.weight" value="$this.weight"/>
      <binding slot="$getPrimaries.dest" value="$this.dest"/>
      <binding slot="$selectPrimary.weight" value="$this.weight"/>
      <binding slot="$selectPrimary.dest" value="$this.dest"/>
      <binding slot="$this.primary" value="$selectPrimary.service_out"/>
      <binding slot="$getAddOns.weight" value="$this.weight"/>
      <binding slot="$getAddOns.dest" value="$this.dest"/>
      <binding slot="$getAddOns.primary" value="$selectPrimary.service_out"/>
      <binding slot="$selectAddOn.weight" value="$this.weight"/>
      <binding slot="$selectAddOn.dest" value="$this.dest"/>
      <binding slot="$selectAddOn.primary" value="$selectPrimary.service_out"/>
    </subtasks>
  </task>

  <task id="GetPrimarySvcs">
  	<input name="weight" type="number"/>
  	<input name="dest" type="string"/>
  	<output name="services" type="ArrayList"/>
	<postcondition> $this.services != undefined; </postcondition>
	<script> $this.services = broker.getPrimaryServices($this.weight, $this.dest); </script>
  </task>
  
  <task id="SelectPrimary">
    <input name="weight" type="number"/>
    <input name="dest" type="string"/>
    <input name="service_in" type="string"/>
    <output name="service_out" type="string"/>
    <output name="valid" type="boolean"/>
	<postcondition> $this.valid </postcondition>
	<script> 
		$this.valid = broker.selectPrimaryService($this.dest, $this.weight, $this.service_in); 
		if ($this.valid) {
			$this.service_out = $this.service_in;
			varPrimary = $this.service_in;
			varCost = 1.0 + ($this.weight * .23);
		}		
	</script>  
  </task>

  <task id="GetAddSvcs">
  	<input name="weight" type="number"/>
  	<input name="dest" type="string"/>
  	<input name="primary" type="string"/>
  	<output name="services" type="ArrayList"/>
	<postcondition> $this.services != undefined; </postcondition>
	<script>$this.services = broker.getAddOnServices($this.weight, $this.dest, $this.primary);</script>
  </task>
  
  <task id="SelectAddOn">
    <input name="weight" type="number"/>
    <input name="dest" type="string"/>
    <input name="primary" type="string"/>
    <input name="service_in" type="string"/>
    <output name="service_out" type="string"/>
    <output name="valid" type="boolean"/>
	<postcondition> $this.valid </postcondition>
	<script> 
		$this.valid = broker.selectAddOnService($this.dest, $this.weight, $this.primary, $this.service_in); 
		if ($this.valid) {
			$this.service_out = $this.service_in;
			varAddOns.add($this.service_in);
			varCost = varCost + 1.0;
		}
	</script>  
  </task>

  <task id="CompleteTransaction">
    <input name="weight" type="number"/>
    <input name="dest" type="string"/>
    <input name="origin" type="string"/>
    <input name="primary" type="string"/>
    <input name="addons" type="ArrayList"/>
    <script>
      broker.saveTransaction($this.origin, $this.dest, $this.weight, $this.primary, $this.addons);
    </script>

  </task>

  <script init="true">

	importPackage(java.util);
	importPackage(Packages.edu.wpi.mails);

	broker = new MailsBroker();
	
	varOrigin = "";
	varDest = "";
	varWeight = 0.0;
	varPrimary = "";
	varAddOns = new ArrayList();
	varCost = 0.0;
	
  </script>
  
</taskModel>