/** * Update the best path reference * * @param PathNode $pathTail * @throws Exception */ public function setPath(PathNode $pathTail) { // Check that the node identifier match if ($pathTail->getStringIdentifier() != $this->getStringIdentifier()) { throw new Exception('Node identifiers do not match'); } // Check that we have the node we are coming from $comingFrom = $pathTail->getParentNode(); if (is_null($comingFrom)) { return; } $comingFromStringIdentifier = $comingFrom->getStringIdentifier(); // Check if the path is longer than anything we have seen for that node before $pathLength = $pathTail->getLength(); if (array_key_exists($comingFromStringIdentifier, $this->bestPathLengths)) { if ($this->bestPathLengths[$comingFromStringIdentifier] < $pathLength) { // Set the new best $this->bestPathLengths[$comingFromStringIdentifier] = $pathLength; $this->bestPaths[$comingFromStringIdentifier] = $pathTail; } } else { // Set the new best $this->bestPathLengths[$comingFromStringIdentifier] = $pathLength; $this->bestPaths[$comingFromStringIdentifier] = $pathTail; } // Keep a statistic of the best path overall if ($pathLength > $this->bestPathLength) { $this->bestPathLength = $pathLength; } }
/** * Add a statistic about a path (not necessarily the best) * * @param PathNode $pathNode * @param bool $initial * @throws Exception */ public function addPathLengthStatistic(PathNode $pathNode, $initial = false) { $graphNode = $this->findOrCreate($pathNode->getStringIdentifier(), $initial); $graphNode->setPath($pathNode); $bestPathTail = $graphNode->getBestPathTail(); // Update statistic about the best path (if any) if (!is_null($bestPathTail)) { $this->updateBestPath($bestPathTail); } }