Skip to content

Commit

Permalink
Merge pull request #2 from fmizzell/fixes
Browse files Browse the repository at this point in the history
The machine of machines' logic was getting too complex, refactoring.
  • Loading branch information
fmizzell authored May 30, 2019
2 parents c6ede30 + 3f4accc commit 9c355a0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/StateMachine/Execution.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ trait Execution
private function recordStateExecution($next_states)
{
if (json_encode($this->currentStates) != json_encode($next_states)) {
$this->execution->push($next_states);
$this->execution->push("");
array_push($this->execution, $next_states);
array_push($this->execution, "");
}
}

private function recordInputExecution($input)
{
$inputs = $this->execution->pop();
$inputs = array_pop($this->execution);
$inputs .= $input;
$this->execution->push($inputs);
array_push($this->execution, $inputs);
}
}
14 changes: 8 additions & 6 deletions src/StateMachine/Machine.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ class Machine implements IStateMachine

private $endStates = [];

private $halted = false;
protected $halted = false;

/**
* Constructor.
*/
public function __construct(array $initial_states)
{
$this->execution = new \SplStack();
$this->execution = [];
$this->initialStates = $initial_states;

$this->recordStateExecution($this->initialStates);
Expand Down Expand Up @@ -73,7 +73,7 @@ public function processInput(string $input)

$this->currentStates = $next_states;

$this->didWeHalt();
$this->halted = $this->didWeHalt();
} else {
throw new \Exception("Invalid Input {$input}");
}
Expand All @@ -83,21 +83,23 @@ public function reset()
{
$this->recordStateExecution($this->initialStates);
$this->currentStates = $this->initialStates;
$this->halted = false;
}

public function getCurrentStates(): array
{
return $this->currentStates;
}

private function didWeHalt()
protected function didWeHalt()
{
$this->halted = false;
$halted = false;
foreach ($this->currentStates as $current_state) {
if (in_array($current_state, $this->endStates)) {
$this->halted = true;
$halted = true;
}
}
return $halted;
}

private function transitionIsValid($input)
Expand Down
104 changes: 101 additions & 3 deletions src/StateMachine/MachineOfMachines.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,28 @@ public function addMachine($state, IStateMachine $machine)

public function processInput(string $input)
{
$got_machine = false;
$machines = $this->getCurrentMachines();
$errors = [];
if (!empty($machines)) {
foreach ($machines as $state => $machine) {
if ($this->feedMachine($state, $machine, $input)) {
$errors[] = true;
}
}
}

$all_machines_errored_out = ((!empty($machines)) && (count($errors) == count($machines)));
if ($all_machines_errored_out || empty($machines)) {
if ($all_machines_errored_out && !$this->machineHalted()) {
throw new \Exception("We had machines for state(s) " .
implode(", ", $this->currentStates) . " but no machine finished");
}

$this->resetCurrentMachines();
parent::processInput($input);
}

/*$got_machine = false;
$machine_finished = false;
$machines = [];
Expand All @@ -26,6 +47,7 @@ public function processInput(string $input)
$got_machine = true;
try {
$machine->processInput($input);
$this->halted = $this->didWeHalt();
} catch (\Exception $e) {
$errors[] = true;
Expand All @@ -36,6 +58,7 @@ public function processInput(string $input)
unset($this->currentStates[$key]);
}
}
$this->halted = $this->didWeHalt();
$machine->reset();
}
}
Expand All @@ -44,18 +67,71 @@ public function processInput(string $input)
if ($got_machine) {
if (count($machines) === count($errors)) {
if ($machine_finished) {
$real_halted = $this->halted;
parent::processInput($input);
$this->halted = $real_halted;
} else {
throw new \Exception("We had machines for state(s) " .
implode(", ", $this->currentStates) . " but no machine finished");
}
}
} else {
$real_halted = $this->halted;
parent::processInput($input);
$this->halted = $real_halted;
}*/
}

private function machineHalted()
{
$halted = false;
foreach ($this->getCurrentMachines() as $machine) {
if ($machine->isCurrentlyAtAnEndState()) {
$halted = true;
}
}
return $halted;
}

private function getCurrentMachines()
{
$machines = [];
foreach ($this->currentStates as $key => $current_state) {
$machine = $this->getStateMachine($current_state);
if ($machine) {
$machines[$current_state] = $machine;
}
}
return $machines;
}

public function isCurrentlyAtAnEndState(): bool
private function feedMachine($state, $machine, $input)
{
$error = false;
try {
$machine->processInput($input);
$this->halted = $this->didWeHalt();
} catch (\Exception $e) {
if (count($this->currentStates) > 1) {
$key = array_search($state, $this->currentStates);
if ($key !== false) {
$machine->reset();
unset($this->currentStates[$key]);
}
}
$error = true;
}
return $error;
}

public function resetCurrentMachines()
{
foreach ($this->getCurrentMachines() as $machine) {
$machine->reset();
}
}

/*public function isCurrentlyAtAnEndState(): bool
{
$is = parent::isCurrentlyAtAnEndState();
if ($is === false) {
Expand Down Expand Up @@ -87,7 +163,7 @@ public function isCurrentlyAtAnEndState(): bool
return true;
}
}
}
}*/

public function getStateMachine($state): ?IStateMachine
{
Expand All @@ -105,4 +181,26 @@ public function gsm($state): ?IStateMachine
{
return $this->getStateMachine($state);
}

protected function didWeHalt()
{
$parent_halt = parent::didWeHalt();
$machines = $this->getCurrentMachines();

if (!$parent_halt) {
return false;
}

if ($parent_halt && empty($machines)) {
return true;
} else {
$halted = false;
foreach ($machines as $machine) {
if ($machine->isCurrentlyAtAnEndState()) {
$halted = true;
}
}
return $halted;
}
}
}

0 comments on commit 9c355a0

Please sign in to comment.