2010-09-18 11:11:44 -04:00
|
|
|
<?php
|
|
|
|
namespace Moserware\Skills\FactorGraphs;
|
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
require_once(dirname(__FILE__) . "/Factor.php");
|
|
|
|
|
2010-09-18 11:11:44 -04:00
|
|
|
abstract class Schedule
|
|
|
|
{
|
|
|
|
private $_name;
|
|
|
|
|
|
|
|
protected function __construct($name)
|
|
|
|
{
|
|
|
|
$this->_name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract function visit($depth = -1, $maxDepth = 0);
|
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return $this->_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ScheduleStep extends Schedule
|
|
|
|
{
|
|
|
|
private $_factor;
|
|
|
|
private $_index;
|
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
public function __construct($name, Factor &$factor, $index)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
parent::__construct($name);
|
|
|
|
$this->_factor = $factor;
|
|
|
|
$this->_index = $index;
|
|
|
|
}
|
|
|
|
|
2010-09-23 22:14:56 -04:00
|
|
|
public function visit($depth = -1, $maxDepth = 0)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
$delta = $this->_factor->updateMessageIndex($this->_index);
|
|
|
|
return $delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ScheduleSequence extends Schedule
|
|
|
|
{
|
|
|
|
private $_schedules;
|
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
public function __construct($name, array $schedules)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
parent::__construct($name);
|
|
|
|
$this->_schedules = $schedules;
|
|
|
|
}
|
|
|
|
|
2010-09-23 22:14:56 -04:00
|
|
|
public function visit($depth = -1, $maxDepth = 0)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
$maxDelta = 0;
|
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
$schedules = &$this->_schedules;
|
|
|
|
foreach ($schedules as &$currentSchedule)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
$maxDelta = max($currentSchedule->visit($depth + 1, $maxDepth), $maxDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $maxDelta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ScheduleLoop extends Schedule
|
|
|
|
{
|
|
|
|
private $_maxDelta;
|
|
|
|
private $_scheduleToLoop;
|
|
|
|
|
2010-09-30 08:25:31 -04:00
|
|
|
public function __construct($name, Schedule &$scheduleToLoop, $maxDelta)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
parent::__construct($name);
|
|
|
|
$this->_scheduleToLoop = $scheduleToLoop;
|
|
|
|
$this->_maxDelta = $maxDelta;
|
|
|
|
}
|
|
|
|
|
2010-09-23 22:14:56 -04:00
|
|
|
public function visit($depth = -1, $maxDepth = 0)
|
2010-09-18 11:11:44 -04:00
|
|
|
{
|
|
|
|
$totalIterations = 1;
|
|
|
|
$delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
|
|
|
|
while ($delta > $this->_maxDelta)
|
|
|
|
{
|
|
|
|
$delta = $this->_scheduleToLoop->visit($depth + 1, $maxDepth);
|
|
|
|
$totalIterations++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|