/** * Add a node to the tree. * * @param DecisionTree_Node $node */ function addNode(DecisionTree_Node $node) { $id = $node->getId(); if (isset($this->nodes[$id])) { throw new Exception("Node with id " . $node->id() . " is already part of the tree."); } $node->_bind($this); $this->nodes[$id] = $node; }
/** * Set the transition to the next node. * Note: To decide the next node, the transitions will be checked in the reverse order they were set. * * @param DecisionTree_Node|mixed $node Next node or id of next node * @param array $conditions Set with conditiona which needs to be true to do this transition */ function setTransition($node, $conditions = null) { if (!isset($this->_index)) { $this->_index = DecisionTree_Index(); } if (!is_array($conditions)) { $conditions = empty($conditions) ? array() : array($conditions); } foreach ($conditions as $key => $condition) { if ($condition instanceof DecisionTree_Condition) { continue; } unset($conditions[$key]); $condition = $this->_index->loadCondition($condition); if (is_array($condition)) { $conditions = array_merge($conditions, $condition); } else { $conditions[$key] = $condition; } } $transition = (object) array('node' => $node, 'conditions' => $conditions); if (empty($conditions)) { if (isset($this->_transitions[0])) { throw new Exception("Unable to create transition from node '" . $this->id() . "' to node '" . ($node instanceof self ? $node->id() : $node) . ". A default transition already exists."); } array_unshift($this->_transitions, $transition); } else { $transkey = reset($conditions)->id(); if (isset($this->_transitions[$transkey])) { throw new Exception("Unable to create transition from node '" . $this->id() . "' to node '" . ($node instanceof self ? $node->id() : $node) . ". A transition for condition '{$transkey}' already exists."); } $this->_transitions[$transkey] = $transition; } if (is_object($node) && $node instanceof self) { if ($node->index() === null) { $this->_index->addNode($node); } elseif ($this->_index != $node->index()) { $this->_index->merge($node->index()); } } }