def solve_conjunction_tree(self, conjunction_node, fw_propositions): root = AnyNode(goal=conjunction_node.goal, exhausted=True, suppositions=conjunction_node.suppositions) current_node = root # the arguments of root.goal are either atomic or negation new_subgoals, matched_props, _, _ = self.apply_backward_rules( fw_propositions, current_node) if any(p is not None for p in matched_props): # direct match of the conjunction return True if len(new_subgoals) != 2: return False # ? left_conjunct = AnyNode(goal=new_subgoals[0], parent=root, exhausted=False, suppositions=root.suppositions) right_conjunct = AnyNode(goal=new_subgoals[1], parent=root, exhausted=True, suppositions=root.suppositions) left_branch_sat, conjunct2_alternatives = self.solve_disjunctive_tree( left_conjunct, fw_propositions, right_conjunct) if not left_branch_sat: return False for c in conjunct2_alternatives: alternative_node = AnyNode(goal=c, suppositions=root.suppositions) right_branch_sat, _ = self.solve_disjunctive_tree( alternative_node, fw_propositions) if right_branch_sat: return True return False
def get_dependency_tree(package_json, package_lock_tree): root = AnyNode( name=package_json.get("name", "package.json"), dependencies=package_lock_tree, version=package_json.get("version"), ) # load root dependencies from package.json for dep_name in package_json.get("dependencies", {}).keys(): if not dep_name in package_lock_tree: print( f"{dep_name} package not found in package-lock.json", file=sys.stderr, ) exit(1) dependency = package_lock_tree[dep_name] version = dependency.get("version") parent = AnyNode( name=dep_name, version=version, parent=root, dependencies=dependency.get("dependencies"), ) add_nested_dependencies(dependency, package_lock_tree, parent=parent) return root
def WavRec(y,shape,decLevel=5,family='haar',randperm=None): if randperm is not None: y=y[inv(randperm)] y=y.reshape(shape) root=AnyNode(id="root",shape=shape) lastParent=root lls=shape#lastLevelShape for i in range(decLevel): lls=tuple([int(x/2) for x in lls]) childA=AnyNode(id="child"+str(i+1)+"A",value=np.zeros(lls),shape=lls,level=i,parent=lastParent) childH=AnyNode(id="child"+str(i+1)+"H",value=np.zeros(lls),shape=lls,level=i,parent=lastParent) childV=AnyNode(id="child"+str(i+1)+"V",value=np.zeros(lls),shape=lls,level=i,parent=lastParent) childD=AnyNode(id="child"+str(i+1)+"D",value=np.zeros(lls),shape=lls,level=i,parent=lastParent) lastParent=childA # dec=np.zeros(y.shape) layers=[children for children in LevelOrderGroupIter(root)] for layer in layers[::-1]: try: lls=tuple([int(x*2) for x in lls]) cA=y[0:int(lls[0]/2),0:int(lls[1]/2)] cV=y[int(lls[0]/2):lls[0],0:int(lls[1]/2)] cH=y[0:int(lls[0]/2),int(lls[1]/2):lls[1]] cD=y[int(lls[0]/2):lls[0],int(lls[1]/2):lls[1]] y[0:lls[0],0:lls[1]]=pywt.idwt2((cA,(cH,cV,cD)),'haar') except: break return y.reshape(-1,1)
def create(tweetsFile): # A node should have an nodeNr (starting on 0), idStr(tweet id), parent. propTree = PropTree() # an instance of a tree nodeNr = 0 # to be ordered by time unknownNodeNr = 0 requestCounter = 1 posts = [] print(tweetsFile) for line in open(tweetsFile, 'r'): posts.append(json.loads(line)) # make a list of json arrays print("This file contains " + str(len(posts)) + " posts.") for post in posts: post[ 'tweet_nr'] = nodeNr # adds a new key, which is the id for a post when in the tree (does this do anything really? Should we include post as a JSON in the AnyNode object?) idStr = post['id_str'] idUser = post['user']['id_str'] if 'retweeted_status' in post: parentIdStr = post['retweeted_status']['id_str'] parentIdUser = post['retweeted_status']['user']['id_str'] elif 'quoted_status' in post: parentIdStr = post['quoted_status']['id_str'] parentIdUser = post['quoted_status']['user']['id_str'] if 'retweeted_status' in post or 'quoted_status' in post: # make retweet or quote node parentNode = getFriendInTree(propTree, idUser, parentIdStr, requestCounter, len(posts)) requestCounter += 1 if parentNode is None: # if this node has no parent we want to artificially create one parentNodeNr = "ex" + str( unknownNodeNr ) # artificial parents can be distinguished by an ex in their id parentNode = AnyNode(nodeNr=parentNodeNr, idStr=parentIdStr, idUser=parentIdUser) propTree.addRoot(parentNode) unknownNodeNr += 1 AnyNode(nodeNr=nodeNr, idStr=idStr, idUser=idUser, parent=parentNode) else: # this is original content reference = AnyNode(nodeNr=nodeNr, idStr=idStr, idUser=idUser) propTree.addRoot(reference) nodeNr += 1 propTree.updatePosts(posts) exporter = JsonExporter(indent=2, sort_keys=True) open('./data/tree/trees/' + tweetsFile[30:-4] + '.txt', 'w').close savedFile = open('./data/tree/trees/' + tweetsFile[30:-4] + '.txt', 'r+') for root in propTree.roots: exporter.write(root, savedFile) savedFile.write("&\n") savedFile.close() propTree.makeNodeTree() writeToFile(propTree) return propTree
def show(self, verbose): from anytree import Node as AnyNode, RenderTree verbose = False # TODO def visit(node, parent, verbose): any_node = AnyNode(repr(node), parent=parent) for child in node: if is_leaf(child): if verbose: AnyNode(repr(child), parent=any_node) else: visit(child, any_node, verbose) root = self root_any_node = AnyNode(repr(root)) for child in root: if is_leaf(child): if verbose: AnyNode(repr(child), parent=root_any_node) else: visit(child, root_any_node, verbose) s = '' for pre, fill, node in RenderTree(root_any_node): s += '%s%s\n' % (pre, node.name) return s
def WavDec(x,shape,decLevel=5,family='haar',randperm=None): x=x.reshape(shape) root=AnyNode(id="root",value=x,shape=x.shape) lastParent=root for i in range(decLevel): coeffs = pywt.dwt2(lastParent.value, family) cA, (cH, cV, cD) = coeffs childA=AnyNode(id="child"+str(i+1)+"A",value=cA,shape=cA.shape,level=i,parent=lastParent) childH=AnyNode(id="child"+str(i+1)+"H",value=cH,shape=cH.shape,level=i,parent=lastParent) childV=AnyNode(id="child"+str(i+1)+"V",value=cV,shape=cV.shape,level=i,parent=lastParent) childD=AnyNode(id="child"+str(i+1)+"D",value=cD,shape=cD.shape,level=i,parent=lastParent) lastParent=childA dec=np.zeros(x.shape) layers=[children for children in LevelOrderGroupIter(root)] for layer in layers[::-1]: try: layerImg=np.vstack((np.hstack((layer[0].value,layer[1].value)),np.hstack((layer[2].value,layer[3].value)))) layer[0].parent.value=layerImg except: break #plt.imshow(np.log(1+np.abs(root.value))) y=root.value.reshape((-1,1)) if randperm is not None: y=y[randperm] return y
def NavigateState(self, graph_root: AnyNode, node: AnyNode): """ This function sets the state of a node depending on its (position in the) corresponding tree (-> DAG review as Tree) :param graph_root:AnyNode: tree root :param node:AnyNode: node from tree you want to update """ try: if isNotNone(node.label) and isNone(node.content): label = node.label regex = str('\\b' + label + '\\b') desired = [] tmp_desired = findall(graph_root, lambda node: node.label in label) for i in tmp_desired: match = re.findall(regex, i.label) if len(match) > 0: desired.append(i) if (len(desired) < 1): print(node.state) elif (len(desired) == 1): self.NormalState(node) else: node.followerNodes = desired[0].followerNodes node.hasFollowerNodes = desired[0].hasFollowerNodes node.hasInputNode = desired[0].hasInputNode node.state = 'navigator' node.name = 'navigator_' + str(node.id) else: self.NormalState(node) except Exception as ex: template = "An exception of type {0} occurred in [TParser.NavigateState]. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) print(message)
def get_node_tree(): """ reads nodes from topology and builds tree """ topology = read() tree_nodes = [] for link in topology['links']: parent_ip = link['source'] child_ip = link['target'] link_capacity = link['properties']['capacity'][0] parent = next((x for x in tree_nodes if x.id == parent_ip), None) if not parent: parent_node = next((x for x in topology['nodes'] if x['id'] == parent_ip), None) parent = AnyNode(id=parent_node['id'], label=parent_node['label'], properties=parent_node['properties'], capacity=float("inf")) tree_nodes.append(parent) child = next((x for x in tree_nodes if x.id == child_ip), None) if not child: child_node = next((x for x in topology['nodes'] if x['id'] == child_ip), None) child = AnyNode(id=child_node['id'], parent=parent, label=child_node['label'], properties=child_node['properties'], capacity=min(parent.capacity, link_capacity)) tree_nodes.append(child) child.parent = parent root = next((x for x in tree_nodes if x.parent is None), None) # with open(config.TOPOLOGY_PATH + '.txt', 'w') as topology_file: # topology_file.write(u' '.join(RenderTree(root).by_attr('id')).encode('utf-8')) return root
def SplitTree(X, y,Level=1,Node=AnyNode(id="root",vPredictedClass=-1),ThresholdCount=1): ri,ci=GetBestSplit(X,y,ThresholdCount) if( ri!=-1 and ci!=-1): SplitFeature=ci SplitValue=X[ri,ci] #PlotTreeSplit(X,SplitFeature,SplitValue,Level) #Plot While Training X0=X[np.where(X[:,SplitFeature]<=SplitValue)] Y0=y[np.where(X[:,SplitFeature]<=SplitValue)] X1=X[np.where(X[:,SplitFeature]>SplitValue)] Y1=y[np.where(X[:,SplitFeature]>SplitValue)] s0 = AnyNode(id="Level_"+str(Level)+"_Left("+"X"+str(SplitFeature)+"<"+str(round(SplitValue,1))+")", parent=Node,vLevel=Level,vSplitFeature=SplitFeature,vOp="<",vSplitValue=SplitValue,vSplitSign=-1,vPredictedClass=-1) s1 = AnyNode(id="Level_"+str(Level)+"_Right("+"X"+str(SplitFeature)+">"+str(round(SplitValue,1))+")", parent=Node,vLevel=Level,vSplitFeature=SplitFeature,vOp=">",vSplitValue=SplitValue,vSplitSign=1,vPredictedClass=-1) s0=SplitTree(X0,Y0,Level+1,s0,ThresholdCount=ThresholdCount) s1=SplitTree(X1,Y1,Level+1,s1,ThresholdCount=ThresholdCount) else: PredictedClass=0 PredictedClassLen=0 for i in range(int(y.max()+1)): if (len(y[np.where(y==i)])>PredictedClassLen): PredictedClass=i PredictedClassLen=len(y[np.where(y==i)]) Node.vPredictedClass=PredictedClass return Node
def createKeywordFrequancyTree(self, root, parent, stoperIndex, deep, keywords, iterationUrl, urlAmount, resultDic, chosenUrl): # bu fonkisyon verilen url in alt urllerine bakarak skor hesaplar if stoperIndex == deep: return root urlList = self.getLinksFromAWebSite(iterationUrl, chosenUrl)[0:urlAmount] chosenUrl += urlList for url in urlList: keywords2 = self.FindKeywords(url, 10) similartyDic = self.FindSimilarity(keywords, keywords2) kwf = similartyDic['wordCounts'] resultDic['score'] += (similartyDic['score'] / (deep + 2)) if self.isSemantic: newParent = AnyNode(urlName=url, kwf=kwf, parent=parent, synonymWords=similartyDic['synonymWords']) else: newParent = AnyNode(urlName=url, kwf=kwf, parent=parent) newDeep = deep + 1 self.createKeywordFrequancyTree(root, newParent, stoperIndex, newDeep, keywords, url, urlAmount, resultDic, chosenUrl) return root
def add_nested_dependencies(dependency, package_lock_tree, parent): for name, version_request in dependency.get("requires", {}).items(): node = AnyNode( name=name, version_request=version_request, parent=parent, dependencies=dependency.get("dependencies"), ) dep = None p = node names = [] while p.parent: if dep is None and p.dependencies and p.dependencies.get(name): dep = p.dependencies.get(name) p = p.parent names.append(p.name) # check the root if dep is None and p.dependencies and p.dependencies.get(name): dep = p.dependencies.get(name) node.version = dep.get("version") names = names[:-1] # let's forget about top level # if I'm not already in a tree if name not in names: if dep: add_nested_dependencies(dep, package_lock_tree, node)
def pre_order(node, parent=None): if node: if type(node).__name__ == 'BinaryOperationNode': root = AnyNode(name=node.op_token.value, parent=parent) trace.append(root) visualize_ast(trace[0]) pre_order(node=node.left_node, parent=root) pre_order(node=node.right_node, parent=root) else: if type(node).__name__ == 'UnaryOperationNode': root = AnyNode(name=node, parent=parent) trace.append(root) visualize_ast(trace[0]) pre_order(node.right_node, parent=root) elif type(node).__name__ in [ 'NumberNode', 'Token', 'VariableAccessNode' ]: trace.append(AnyNode(name=node, parent=parent)) visualize_ast(trace[0]) elif type(node).__name__ == 'VariableAssignNode': root = AnyNode(name=node.op_token, parent=parent) trace.append(root) pre_order(node=node.var_name_token, parent=root) pre_order(node=node.var_value_node, parent=root)
def __init__(self, type, children=None, leaf=None): AnyNode.__init__(self) self.type = type if children: self.children = children else: self.children = [] self.leaf = leaf
def make_tree_sbar(tree, t, sent_list): #this fn. converts nltk tree to anytree if tree not in sent_list: ttt = AnyNode(id=str(tree.label()), parent=t) for tt in tree: make_tree_sbar(tt, ttt, sent_list) else: AnyNode(id=str(tree), parent=t)
def visit(node, parent, verbose): any_node = AnyNode(repr(node), parent=parent) for child in node: if is_leaf(child): if verbose: AnyNode(repr(child), parent=any_node) else: visit(child, any_node, verbose)
def test_anynode_children_init(): """Anynode With Children Attribute.""" root = AnyNode(foo="root", children=[ AnyNode(foo="a", children=[AnyNode(foo="aa")]), AnyNode(foo="b") ]) eq_(repr(root.descendants), "(AnyNode(foo='a'), AnyNode(foo='aa'), AnyNode(foo='b'))")
def setup_test(w,l): # The weights file is expected to be a csv file containing 2 columns. The first row are columns numbers. # Each row after that contained an example number followed by its weights. Rows are ordered by example index. weights = np.genfromtxt(w, delimiter=',') # The links/tree file is expected to be in the format produced by the skicy linkage method. # Specificly, each row contain 5 entries: row number, first cluster to be marged, second cluster to be marged, distance between clusters, number of examples in new cluster. # Cluster with index under the size of dataset represent leafs containing the example of the same index. Otherwise, the cluster is described in the row numbered: index - number of examples in dataset +1 links = np.genfromtxt(l, delimiter=',') # normally the weights/links csv comes with line and columns numbering, that should be removed weights = weights[1:, 1:] links = links[1:, 1:] size = np.size(weights, 0) # number of examples in input data nodes = np.empty((size + np.size(links, 0)), dtype=AnyNode) # creating the leafs + updated weight for a in range(size): nodes[a] = AnyNode(id="x" + str(a), weight=weights[a,0], num_examples=1, approx_discrepancy=0, Tdiscrepancy=0, delta=0, num_sampled=0) # parameters to calculate the split quality and average of the hierarchical tree worst_q = 0 sum_q = 0 counter_q = 0 # creating the internal tree nodes for j in range(np.size(links, 0)): sum_of_weights = nodes[int(links[j, 0])].weight + nodes[int(links[j, 1])].weight sum_of_examples = nodes[int(links[j, 0])].num_examples + nodes[int(links[j, 1])].num_examples nodes[size + j] = AnyNode(id="v" + str(j), num_examples=sum_of_examples, num_sampled=0, messeured = 0 , messeuredBx = 0 ,messeuredBy = 0 , weight=sum_of_weights, approx_discrepancy=0, delta=np.inf, Tdiscrepancy=0) nodes[int(links[j, 0])].parent = nodes[size + j] nodes[int(links[j, 1])].parent = nodes[size + j] # calculating true discrepancy leafs = get_leaves(nodes[size + j]) sum_dis = 0 for k in range(np.size(leafs, 0)): sum_dis = sum_dis + abs(leafs[k].weight - (nodes[size + j].weight / nodes[size + j].num_examples)) nodes[size + j].Tdiscrepancy = sum_dis # calculating q if sum_dev > 0: current_q = max(nodes[int(links[j, 0])].Tdeviation, nodes[int(links[j, 1])].Tdeviation) / nodes[ size + j].Tdeviation sum_q += current_q counter_q += 1 if current_q > worst_q: worst_q = current_q print("Deviation of root is: " + str(nodes[-1].Tdeviation)) print("Split quality q of this tree is: " + str(worst_q)) print("Average split quality q of this tree is: " + str(sum_q / counter_q)) # returns the root of the tree return nodes[-1]
def test_is_problematic_node(self): node = AnyNode(subtree_problem=False, license_problem=False) self.assertFalse(is_problematic_node(node)) node = AnyNode(subtree_problem=True, license_problem=False) self.assertFalse(is_problematic_node(node)) self.assertTrue(is_problematic_node(node, check_subtree=True)) node = AnyNode(subtree_problem=False, license_problem=True) self.assertTrue(is_problematic_node(node))
def test_filter_dep_tree_multiple(self): tree = AnyNode( children=[ AnyNode(id="good", subtree_problem=False, license_problem=False), AnyNode(id="node", subtree_problem=False, license_problem=True), ] ) filtered_tree = filter_dep_tree(tree) self.assertEqual(len(filtered_tree.children), 1) self.assertEqual(filtered_tree.children[0].id, "node")
def test_dict_exporter(): """Dict Exporter.""" root = AnyNode(id="root") s0 = AnyNode(id="sub0", parent=root) s0b = AnyNode(id="sub0B", parent=s0) s0a = AnyNode(id="sub0A", parent=s0) s1 = AnyNode(id="sub1", parent=root, foo="bar") s1a = AnyNode(id="sub1A", parent=s1) s1b = AnyNode(id="sub1B", parent=s1) s1c = AnyNode(id="sub1C", parent=s1) s1ca = AnyNode(id="sub1Ca", parent=s1c) exporter = DictExporter() eq_(exporter.export(root), {'id': 'root', 'children': [ {'id': 'sub0', 'children': [ {'id': 'sub0B'}, {'id': 'sub0A'} ]}, {'id': 'sub1', 'foo':'bar', 'children': [ {'id': 'sub1A'}, {'id': 'sub1B'}, {'id': 'sub1C', 'children': [ {'id': 'sub1Ca'} ]} ]} ]} )
def test_json_exporter(): """Json Exporter.""" root = AnyNode(id="root") s0 = AnyNode(id="sub0", parent=root) AnyNode(id="sub0B", parent=s0) AnyNode(id="sub0A", parent=s0) s1 = AnyNode(id="sub1", parent=root) AnyNode(id="sub1A", parent=s1) AnyNode(id="sub1B", parent=s1) s1c = AnyNode(id="sub1C", parent=s1) AnyNode(id="sub1Ca", parent=s1c) lines = [ '{', ' "children": [', ' {', ' "children": [', ' {', ' "id": "sub0B"', ' },', ' {', ' "id": "sub0A"', ' }', ' ],', ' "id": "sub0"', ' },', ' {', ' "children": [', ' {', ' "id": "sub1A"', ' },', ' {', ' "id": "sub1B"', ' },', ' {', ' "children": [', ' {', ' "id": "sub1Ca"', ' }', ' ],', ' "id": "sub1C"', ' }', ' ],', ' "id": "sub1"', ' }', ' ],', ' "id": "root"', '}' ] exporter = JsonExporter(indent=2, sort_keys=True) exported = exporter.export(root).split("\n") exported = [e.rstrip() for e in exported] # just a fix for a strange py2x behavior. eq_(exported, lines) with NamedTemporaryFile(mode="w+") as ref: with NamedTemporaryFile(mode="w+") as gen: ref.write("\n".join(lines)) exporter.write(root, gen) assert filecmp.cmp(ref.name, gen.name)
def populate_node(root_node): process_list = [root_node] while process_list: # Remove one node at a time from the list process_node = process_list.pop(0) # Find a reply to the root node children_query = collection.find({'in_reply_to_status_id': process_node.id}) for child in children_query: child_node = AnyNode(id=child['id'], name=child['user']['name'], text=child['text'], parent=process_node) child_node.id = child['id'] process_list.insert(0, child_node) # put on top the stack
def get_dependency_tree( flat_tree: Dict, package_json: Dict, package_map: Dict ) -> AnyNode: """Get dependency tree. Arguments: flat_tree {Dict} -- Yarn flat tree package_json {Dict} -- package.json of yarn project package_map {Dict} -- package_map with locked versions Returns: AnyNode -- Dependency tree """ # Create root node with project info root = AnyNode( name=package_json.get("name", "package.json"), dependencies=flat_tree, version=package_json.get("version"), ) # Load root dependencies from package.json for dep_name, dep_version in package_json.get("dependencies", {}).items(): resolved_version = package_map.get( f"{dep_name}@{dep_version}" ) # Resolve locked version of the package full_name = f"{dep_name}@{resolved_version}" if full_name not in flat_tree.keys(): print( f"{dep_name} package not found in yarn.lock", file=sys.stderr, ) exit(1) dependency = flat_tree.get( full_name ) # Use package full name to get it dependencies # Create first level of nodes based on package.json dependencies parent = AnyNode( name=dep_name, version=resolved_version, parent=root, dependencies=dependency.get("dependencies") if dependency else {}, ) if dependency: add_nested_dependencies( dependency, parent ) # Add nested dependencies of first level nodes # Delete helper dependencies field for node in PreOrderIter(root): delattr(node, "dependencies") return root
def loadData(self): for root, dirs, files in os.walk(self.search_dir): for file in files: place = (file.split("2018_passData_")[-1]).split("_")[0] with open(os.path.join(self.search_dir, file), "r") as f: reader = csv.DictReader(f) for row in reader: try: date = row["Date"] time_key = ["pm25"] time = ["00:00:00"] except: date = row["YY-MM-DD"] date = date[:4] + "-" + date[4:6] + "-" + date[6:8] time_key = [ " {:.1f}".format(t) for t in np.arange(0.5, 24.5, 1.0) ] time = [ "{:02d}:30:00".format(i) for i in range(24) ] date_node = search.find_by_attr(self.time_tree, name="date", value=date, maxlevel=2) if date_node is None: date_node = AnyNode(parent=self.time_tree, date=date) for t in zip(time, time_key): time_node = search.find_by_attr(date_node, name="time", value=t[0], maxlevel=2) if time_node is None: try: pm25 = float(row[t[1]]) except: pm25 = None time_node = AnyNode(parent=date_node, time=t[0]) place_node = search.find_by_attr(time_node, name="place", value=place, maxlevel=2) if place_node is None: place_node = AnyNode( parent=time_node, place=place, pm25=pm25, position=self.place_dict[place]) LOG.debug(RenderTree(self.time_tree)) with open("time_tree.txt", "w") as f: print(RenderTree(self.time_tree), file=f)
def handle_message(self, message): #m = 0 if m == 0: self.value = message["value"] self.decide() #m > 0 else: path = message["path"] #Commander message, empty tree if len(message["path"]) == 1: self.root = AnyNode(id=path[0], value=message["value"], decide_value=message["value"], name=path[0]) #Message from a lieutenant else: #Get last known node on path and add new node as its child with open("./debug/messages_handled_lieutenant.txt", "a") as f: print(self.id, message["path"], path[-1], file=f) r = Resolver('id') last_node = r.get(self.root, '/'.join(path[1:-1])) AnyNode(id=path[-1], value=message["value"], decide_value=message["value"], parent=last_node, name=path[-1]) with open("./debug/all_tree_operations.txt", "a") as f: print(self.id, message["path"], last_node.id, path[-1], file=f) if self.id not in path and len(path) <= m: #If possible, apend its own id to the path and broadcast the message path.append(self.id) message = self.construct_message(path, message["value"]) self.multicast(self.process_list[1:], message) leafs = [ node for node in LevelOrderIter(self.root) if node.is_leaf ] if len(leafs) == n_leafs: #If already received all the messages, start phase 2 reverse_nodes = [node for node in LevelOrderIter(self.root)][::-1] for node in reverse_nodes: if not node.is_leaf: node.decide_value = majority( [n.decide_value for n in node.children]) self.value = self.root.decide_value print("Process", self.id, "has decided", self.value) #print("Process", self.id, "has decided", self.value) with open('./results/' + self.id + ".txt", "w") as f: print(self.id, RenderTree(self.root), file=f)
def dir_structure_to_json_generator(self, start_dir, ffilter, dfilter, hasdirs): # Non recursive function class Data: def __init__(self, path, childD): self.path = path self.childD = childD if ffilter: filter_file_list= str(ffilter).split(',') else: filter_file_list = '*.*' if hasdirs is None: hasdirs= False else: if hasdirs == 'True': hasdirs = True else: hasdirs = False flag = True initial_root = None if start_dir and os.path.exists(start_dir): start = Data(start_dir, None) directories = [start] while len(directories) > 0: directory = directories.pop() if flag: root = AnyNode(type="Directory", path=directory.path) initial_root = root flag = False else: root = directory.childD for name in os.listdir(directory.path): fullpath = os.path.join(directory.path, name) if os.path.isfile(fullpath): if self.directory_regex_pattern_matching(fullpath, dfilter) and self.is_file_machting_filter(fullpath, filter_file_list): AnyNode(type="File", path=fullpath, lastmodified=str(self.modification_date(fullpath)), parent=root) elif os.path.isdir(fullpath): if hasdirs and\ len(os.listdir(fullpath)) > 0 and self.directory_regex_pattern_matching(fullpath, dfilter) and\ self.has_files_by_filter(fullpath, filter_file_list): # Check if the directory is not empty and if the directory contains any of the files from filter child_directory = AnyNode(type="Directory", path=fullpath, parent=root) directories.append(Data(fullpath, child_directory)) # It's a directory, store it. exporter = DictExporter() data = exporter.export(initial_root) else: data = {'Directory': 'Does not exist or was not given'} return data
def __init__(self, name, tree, expanded, sign, parent=None, children=None, **kwargs): AnyNode.__init__(self, parent, children, **kwargs) self.name = name self.sign = sign self.tree = tree self.expanded = expanded
def load_data(data): root = AnyNode(validators=[None], name=None) for ligne in data: active_node = root for letter in ligne.split("."): need_new_node = True if active_node.children is not tuple(): # if node have a child for child in active_node.children: if child.name == letter: # if this child already exist don't create node need_new_node = False active_node = child break if need_new_node: # create node if needed new_node = AnyNode(name=str(letter), validators=None, parent=active_node) active_node = new_node # set validator node_json_type = data[ligne].split('|') if len(node_json_type) == 2 and node_json_type[ 0] == "object": # if key:value value is "object" for key in node_json_type[1].split(':')[1].split( ","): # for each key add node AnyNode(name=str(key), validators=None, parent=active_node) active_node.validators = ["object"] else: active_node.validators = node_json_type # All node_json parsed to tree for node in PreOrderIter(root): for n in node.children: if not n.is_leaf and ( n.validators is None or "array" in n.validators ): # change validator for array and not set validator if n.children[0].name == "*": n.validators = ["array"] n.children[0].name = "items" n.children[0].validators = ["object"] else: n.validators = ["object"] # set type if node.is_leaf: node.type = 'leaf' else: node.type = node.validators[0] return root
def __init__(self, eventLog): root = AnyNode(id='Root', name="Root", cases=set(), sequence="", annotation=dict()) current = root currentCase = "" traceToSequenceDict = dict() sequence = None self.caseIDColName = "Case ID" self.activityColName = "Activity" self.annotationColName = "Duration" self.constantEventNr = "Event_Nr" self.annotationDataOverAll = dict() self.normaltest_alpha = 0.05 for index, row in eventLog.iterrows(): activity = row[self.activityColName] annotation = row[self.annotationColName] if row[self.caseIDColName] != currentCase: if not sequence is None: traceToSequenceDict[currentCase] = sequence currentCase = row[self.caseIDColName] current = root current.cases.add(currentCase) sequence = "" childAlreadyExists = False sequence = sequence + "@" + activity for child in current.children: if child.name == activity: childAlreadyExists = True current = child if not childAlreadyExists: node = AnyNode(id=index, name=activity, parent=current, cases=set(), sequence=sequence, annotations=dict()) current = node current.cases.add(currentCase) current.annotations[currentCase] = annotation self.__addAnnotation(annotation, activity) #Handle last case traceToSequenceDict[currentCase] = sequence self.tree = root self.traceToSequenceDict = traceToSequenceDict self.numberOfTracesOriginal = len(self.tree.cases) self.sequentialPrunning = True self.__setMaxDifferences()
def SetSubnodes(self, node: AnyNode): """ This function places sub nodes to the current node if some exist. :param node:AnyNode: given node """ try: followers = [node.label for node in node.children] if (len(followers) > 0): node.hasFollowerNodes = True node.followerNodes = followers except Exception as ex: template = "An exception of type {0} occurred in [TParser.SetSubnodes]. Arguments:\n{1!r}" message = template.format(type(ex).__name__, ex.args) print(message)