def __init__(self, pattern_to_tree_plan_map: Dict[Pattern, TreePlan], storage_params: TreeStorageParameters, statistics_collector: StatisticsCollector = None, optimizer: Optimizer = None, statistics_update_time_window: timedelta = None): self.__is_multi_pattern_mode = len(pattern_to_tree_plan_map) > 1 if self.__is_multi_pattern_mode: # TODO: support statistic collection in the multi-pattern mode self._tree = MultiPatternTree(pattern_to_tree_plan_map, storage_params) else: pattern = list(pattern_to_tree_plan_map)[0] pattern.condition.set_statistics_collector(statistics_collector) self._tree = Tree(list(pattern_to_tree_plan_map.values())[0], list(pattern_to_tree_plan_map)[0], storage_params) self.__storage_params = storage_params self.__statistics_collector = statistics_collector self.__optimizer = optimizer self._event_types_listeners = {} self.__statistics_update_time_window = statistics_update_time_window # The remainder of the initialization process is only relevant for the freeze map feature. This feature can # only be enabled in single-pattern mode. self._pattern = list(pattern_to_tree_plan_map)[0] if not self.__is_multi_pattern_mode else None self.__freeze_map = {} self.__active_freezers = [] if not self.__is_multi_pattern_mode and self._pattern.consumption_policy is not None and \ self._pattern.consumption_policy.freeze_names is not None: self.__init_freeze_map()
def test_create_tree_2(self): # A tree with root, three children. t = Tree('r') t._add(t.root(), 'a') t._add(t.root(), 'b') t._add(t.root(), 'c') self.assertEqual(t.height(), 1) self.assertEqual(len(t), 4)
def test_create_tree_3(self): t = Tree() a = t._add(t.root(), 'a') t._add(t.root(), 'b') t._add(t.root(), 'c') t._add(a, 'aa') self.assertEqual(t.height(), 2) # Single child node of node a contains value 'aa' as element. self.assertEqual(t.children(a)[0].element(), 'aa')
def test_bfs_1(self): t = Tree('a') b = t._add(t.root(), 'b') t._add(b, 'd') c = t._add(t.root(), 'c') t._add(c, 'e') f = t._add(c, 'f') t._add(f, 'g') self.assertEqual([x.element() for x in t.bfs()], ['a', 'b', 'c', 'd', 'e', 'f', 'g'])
def test_imbalanced_tree(self): root = Tree(6) root.set_left(5) root.left.set_left(2) root.left.set_right(3) answer = [['|', '|', '|', '6', '|', '|', '|']] answer.append(['|', '5', '|', '|', '|', '|', '|']) answer.append(['2', '|', '3', '|', '|', '|', '|']) assert root.print_tree() == answer
def __construct_trees_for_patterns(self, pattern_to_tree_plan_map: Dict[Pattern, TreePlan], storage_params: TreeStorageParameters): """ Creates a list of tree objects corresponding to the specified tree plans. """ i = 1 # pattern IDs starts from 1 trees = [] for pattern, plan in pattern_to_tree_plan_map.items(): trees.append(Tree(plan, pattern, storage_params, i)) i += 1 return trees
def test_balanced_tree(self): root = Tree(1) root.set_left(4) root.set_right(5) root.left.set_left(3) root.left.set_right(5) root.right.set_left(7) root.right.set_right(9) answer = [['|', '|', '|', '1', '|', '|', '|']] answer.append(['|', '4', '|', '|', '|', '5', '|']) answer.append(['3', '|', '5', '|', '7', '|', '9']) assert root.print_tree() == answer
def test_level_order(self): t = Tree('r') r = t.root() a = t.add(r, 'a') b = t.add(a, 'b') c = t.add(a, 'c') d = t.add(a, 'd') e = t.add_between(r, a, 'e') f = t.add(e, 'f') self.assertEqual([[x.element() for x in level] for level in t.level_traversal()], [['r'], ['e'], ['a', 'f'], ['b', 'c', 'd']])
def test_add_between(self): t = Tree('r') r = t.root() a = t.add(r, 'a') b = t.add(a, 'b') c = t.add(a, 'c') d = t.add(a, 'd') e = t.add_between(r, a, 'e') f = t.add(e, 'f') self.assertEqual([x.element() for x in t.bfs()], ['r', 'e', 'a', 'f', 'b', 'c', 'd']) self.assertEqual(len(t), 7) self.assertEqual(t.height(), 3)
def __perform_reoptimization(self, last_statistics_refresh_time: timedelta, last_event: Event): """ If needed, reoptimizes the evaluation mechanism to reflect the current statistical properties of the input event stream. """ self.__statistics_collector.handle_event(last_event) if not self._should_try_reoptimize(last_statistics_refresh_time, last_event): # it is not yet time to recalculate the statistics return last_statistics_refresh_time new_statistics = self.__statistics_collector.get_statistics() if self.__optimizer.should_optimize(new_statistics, self._pattern): new_tree_plan = self.__optimizer.build_new_plan(new_statistics, self._pattern) new_tree = Tree(new_tree_plan, self._pattern, self.__storage_params) self._tree_update(new_tree, last_event.timestamp) # this is the new last statistic refresh time return last_event.timestamp
def __construct_multi_pattern_tree( self, pattern_to_tree_plan_map: Dict[Pattern, TreePlan], storage_params: TreeStorageParameters): """ Constructs a multi-pattern evaluation tree. It is assumed that each pattern appears only once in patterns (which is a legitimate assumption). """ i = 1 # pattern IDs starts from 1 plan_nodes_to_nodes_map = {} # a cache for already created subtrees for pattern, plan in pattern_to_tree_plan_map.items(): pattern.id = i new_tree_root = Tree(plan, pattern, storage_params, plan_nodes_to_nodes_map).get_root() self.__id_to_output_node_map[pattern.id] = new_tree_root self.__id_to_pattern_map[pattern.id] = pattern self.__output_nodes.append(new_tree_root) i += 1
def __init__(self, pattern_to_tree_plan_map: Dict[Pattern, TreePlan], storage_params: TreeStorageParameters, multi_pattern_eval_params: MultiPatternEvaluationParameters = MultiPatternEvaluationParameters()): is_multi_pattern_mode = len(pattern_to_tree_plan_map) > 1 if is_multi_pattern_mode: self.__tree = MultiPatternTree(pattern_to_tree_plan_map, storage_params, multi_pattern_eval_params) else: self.__tree = Tree(list(pattern_to_tree_plan_map.values())[0], list(pattern_to_tree_plan_map)[0], storage_params) self.__event_types_listeners = {} # The remainder of the initialization process is only relevant for the freeze map feature. This feature can # only be enabled in single-pattern mode. self.__pattern = list(pattern_to_tree_plan_map)[0] if not is_multi_pattern_mode else None self.__freeze_map = {} self.__active_freezers = [] if not is_multi_pattern_mode and self.__pattern.consumption_policy is not None and \ self.__pattern.consumption_policy.freeze_names is not None: self.__init_freeze_map()
def genericSearch(self, root): treeNode = TreeArrayListNode(root) tree = Tree(treeNode) vertexSet = set() #nodi da esaminare markedNodes = set() #nodi gia' marcati per essere esaminati markedNodes.add(root.index) vertexSet.add(treeNode) while len(vertexSet) > 0: #finche' ci sono nodi da esaminare treeNode = vertexSet.pop() #un generico nodo non esaminato nodes = self.foundNodesBySource(treeNode.info.index) for nodeIndex in nodes: if nodeIndex not in markedNodes: #crea il nodo per l'albero e #collega padre e figlio newTreeNode = TreeArrayListNode(self.nodes[nodeIndex]) markedNodes.add(nodeIndex) newTreeNode.father = treeNode treeNode.sons.append(newTreeNode) vertexSet.add(newTreeNode) else: currNode = tree.foundNodeByIndex(nodeIndex) #TODO: aggiorna il padre return tree
def test_one_node_tree(self): assert Tree(2).print_tree() == [['2']]
def test_get_height_1(self): root = Tree(2) assert root.get_height() == 1
from tree.Tree import Tree from In_out.Peripheric_manager import Peripheric_manager from data_manager.utils.Getter import Getter from data_manager.read_tree.configure_peripherics import config_peripherics from data_manager.read_tree.configure_tree import config_tree from data_manager.read_tree.reload_tree import reload_tree from In_out.network.Server import Server """ Create the tree and Peripheric_manager """ tree = Tree() manager = Peripheric_manager() getter = Getter(tree, manager) config_peripherics(getter) config_tree(getter) Server(getter).start()
def test_create_tree_1(self): # A tree with only a root. t = Tree() self.assertEqual(len(t), 1)
def test_get_height_2(self): root = Tree(2) root.set_left(3) assert root.get_height() == 2
def test_create_tree_0(self): t = Tree(None) self.assertEqual(t.root().element(), None)