<?php
	namespace Mocha\Oop\MethodImplementations;

	use Mocha\Core\InstanceReference;
	use Mocha\Core\KnownAttributeGuids;
	use Mocha\Core\KnownInstanceGuids;
	use Mocha\Core\KnownRelationshipGuids;
	use Mocha\Core\OmsContext;
	use Mocha\Oms\Oms;
	use Mocha\Oop\MethodImplementation;

	class ConditionalSelectInstanceSetMethodImplementation extends MethodImplementation
	{
		protected function executeInternal(Oms $oms, OmsContext $context, InstanceReference $method, ?array $parms)
		{
			echo ("<br /> Conditional Select Instance Set <br />");
			echo ("<br />The work data is: <br />");
			$oms->printWorkData($context->workData);
			echo ("<br /><br />");
			$retval = null;
			$returnsWorkSet = $oms->getRelatedInstance($method, KnownRelationshipGuids::Conditional_Select_from_Instance_Set_Method__returns__Work_Set);
			$cases = $oms->getRelatedInstances($method, KnownRelationshipGuids::Conditional_Select_from_Instance_Set_Method__has__Conditional_Select_from_Instance_Set_Case);
			foreach ($cases as $case)
			{
				$trueConditions = $oms->getRelatedInstances($case, KnownRelationshipGuids::Condition_Group__has_true_condition__Executable_returning_Attribute);
				$falseConditions = $oms->getRelatedInstances($case, KnownRelationshipGuids::Condition_Group__has_false_condition__Executable_returning_Attribute);
				if (!$oms->evaluateConditions($context, $trueConditions, $falseConditions))
				{
					continue;
				}

				$invokesExecutableReturningInstanceSet = $oms->getRelatedInstance($case, KnownRelationshipGuids::Conditional_Select_from_Instance_Set_Case__invokes__Executable_returning_Instance_Set);
				if ($invokesExecutableReturningInstanceSet !== null)
				{
					$ws = $oms->execute($context, $invokesExecutableReturningInstanceSet);
					echo ("<br /><br />setting work data " . $returnsWorkSet . " = " . $ws . " [ from " . $invokesExecutableReturningInstanceSet . " ]<br /><br />");
					$context->setWorkData($returnsWorkSet, $ws);
					$retval = $returnsWorkSet;
				}
				else
				{
					$context->setWorkData($returnsWorkSet, "SSC case with NULL instance set");
					$retval = $returnsWorkSet;
				}
			}
			if ($retval === null)
			{
				$context->setWorkData($returnsWorkSet, "SSC returned no case");
				$retval = $returnsWorkSet;
			}
			return $retval;
		}
	}

?>