function testDependcy() { // test depends $items = array('C', 'B', 'F', 'A' => array('B', 'C'), 'E' => array('B'), 'D' => array('A', 'E')); $source = new OA_Algorithm_Dependency_Source_HoA($items); $dep = new OA_Algorithm_Dependency($source); $ret = $dep->depends(array('B')); $this->assertEqual($ret, array()); $ret = $dep->depends(array('A')); $this->assertEqual($ret, array('B', 'C')); $ret = $dep->depends(array('D')); $this->assertEqual($ret, array('A', 'B', 'C', 'E')); // test schedule $ret = $dep->schedule(array('G')); $this->assertFalse($ret); $ret = $dep->schedule(array('B')); $this->assertEqual($ret, array('B')); $ret = $dep->schedule(array('A')); $this->assertEqual($ret, array('A', 'B', 'C')); $ret = $dep->schedule(array('D')); $this->assertEqual($ret, array('A', 'B', 'C', 'D', 'E')); // test schedule all $ret = $dep->scheduleAll(); $this->assertEqual($ret, array('A', 'B', 'C', 'D', 'E', 'F')); }
/** * Returns the dependencies sorted in correct order. * * @param array $items * @return array */ function schedule($items = array()) { if (!$items) { return false; } $rv = parent::schedule($items); if (!$rv) { return false; } $errorMarker = ''; $selected = $this->selected; $itemsIds = $this->source->getItemsIds(); while ($id = array_shift($rv)) { // have we checked every item in the stack if ($id == $errorMarker) { return false; } // are there any un-met dependencies $item = $this->source->getItem($id); if (!$item) { if ($this->ignoreOrphans) { continue; } return false; } $missing = array_diff($item->getDependencies(), $selected); if ($this->ignoreOrphans) { $missing = array_intersect($itemsIds, $missing); } if ($missing) { if (!$errorMarker) { $errorMarker = $id; $rv[] = $id; continue; } } // All dependencies have been met. Add the item to the schedule // and to the selected index $schedule[$id] = $id; $selected[$id] = $id; $errorMarker = ''; } return $schedule; }