def tcpDumpsRunning(self): result = self.doc.createElement('tcp-dumps-running') self.doc.documentElement.appendChild(result) allCaptures = [] # Look for all the running RiOS captures. riosCaptures = Nodes.getMgmtSetEntries(self.mgmt, '/rbt/tcpdump/state/capture') for name in riosCaptures: allCaptures.append((name, 'RiOS', riosCaptures[name]['start'])) # Look for all the running hypervisor captures (for BOB). if RVBDUtils.isBOB(): hostCaptures = Nodes.getMgmtSetEntries(self.mgmt, '/host/tcpdump/state/capture') for name in hostCaptures: allCaptures.append((name, 'Hypervisor', hostCaptures[name]['start'])) # Sort by the capture name (which is basically a timestamp). allCaptures.sort(key=lambda x: x[0]) for capture in allCaptures: dumpEl = self.doc.createElement('tcp-dump') result.appendChild(dumpEl) dumpEl.setAttribute('name', capture[0]) dumpEl.setAttribute('runningOn', capture[1]) dumpEl.setAttribute('start', capture[2]) dumpEl.setAttribute('internalName', '%s/%s' % (capture[0], capture[1])) self.writeXmlDoc()
def solve(squares): open_nodes = [] closed_nodes = [] grid = [[0 for x in range(CANTIDAD)] for y in range(CANTIDAD)] for x in range(CANTIDAD): for y in range(CANTIDAD): s = squares[x][y] if s.color == GREEN: #open starting node grid[x][y] = 'A' start = N.node(x,y) open_nodes.append(start) #open start elif s.color == BLUE: #set end node grid[x][y] = 'B' finish = N.node(x,y) elif s.color == BLACK: grid[x][y] = 'X' path = finder(grid,open_nodes,closed_nodes,start,finish) #change nodes to represent the path for p in path: (x,y) = p squares[x][y].color = PURPLE
def connectTheDots(): """ Called by the Anatomy module (if you are using it) after all of the anatomy has been read into memory. This connects the different object types together in many different ways. """ global _partOfParentsForChild, _partOfChildrenForParent _partOfParentsForChild = {} _partOfChildrenForParent = {} for timedNode in Iterator(): node = Nodes.getByOid(timedNode.getNodeOid()) for childNode in Nodes.getPartOfChildrenForNode(node): childTimedNode = getByNodeStageOids(childNode.getOid(), timedNode.getStageOid()) if childTimedNode: if childTimedNode in _partOfParentsForChild: _partOfParentsForChild[childTimedNode].add(timedNode) else: _partOfParentsForChild[childTimedNode] = sets.Set([timedNode]) for parentNode in Nodes.getPartOfParentsForNode(node): parentTimedNode = getByNodeStageOids(parentNode.getOid(), timedNode.getStageOid()) if parentTimedNode: if parentTimedNode in _partOfChildrenForParent: _partOfChildrenForParent[parentTimedNode].add(timedNode) else: _partOfChildrenForParent[parentTimedNode] = sets.Set([timedNode]) return
def create_new_gen(self): for child in self.population: func_point = math.sqrt(child[1]) if random.uniform(0, 1) >= func_point: self.population.remove(child) temp = [] for _ in range(self.population_size): if max(list(map(lambda child: child[1], self.population))) == 0: self.create_random_population() return father_index = random.randint(0, len(self.population) - 1) mother_index = random.randint(0, len(self.population) - 1) while father_index == mother_index: father_index = random.randint(0, len(self.population) - 1) mother_index = random.randint(0, len(self.population) - 1) father = self.population[father_index][0] mother = self.population[mother_index][0] opt = [father, mother] child = createChild(father, mother) while not checkTreeHeight(child.data, self.max_dna_height): child = createChild(father, mother) while not Nodes.checkTreeHeight(child.data, self.max_dna_height): child = createChild(father, mother) temp.append(child) self.population = list( map(lambda child: [child, -1, Nodes.getTreeHeight(child.data)], temp))
def hotReload(): reload(Pins) reload(FunctionLibraries) reload(Nodes) reload(SubGraphs) Nodes._getClasses() FunctionLibraries._getFunctions() SubGraphs._getClasses()
def __init__(self): A = Nodes.Node("A") B = Nodes.Node("B") C = Nodes.Node("C") D = Nodes.Node("D") E = Nodes.Node("E") self.nodes_list = [A, B, C, D, E] self.adj_list = [[C, 1, A, C, 2, D], [A, 3, B], [D, 5, B, D, 7, E], [B, 3, A, B, 5, D, B, 1, E], [E, 1, B, E, 7, D]]
def regenerateTable(): """ Called to completely regenerate the contents of this table. First, it (generates the sql to) empties the table, and then it repopulates it. """ # Anatomy part-of relationships form a directed acyclic graph (DAG) # # Walk the graph top to bottom, breadth first, generating # transitive closure relationships as we go. # Delete everything in existing table. deleteAll() _initialiseGlobals() # Start with the root of everything. nodeQueue = [Nodes.getRoot()] beenQueued = sets.Set(nodeQueue) # what has been put on the Q beenProcessed = sets.Set() # what has been pulled from the Q while len(nodeQueue) > 0: current = nodeQueue.pop(0) currentOid = current.getOid() _createImmediateRelsWhereChild(current) # This node's children can be added to the queue if all that # child's parents have been processed. beenProcessed.add(current) relsWithChildren = Relationships.getByParentOidRelType( currentOid, Relationships.PART_OF) for childRel in relsWithChildren: childOid = childRel.getChildOid() child = Nodes.getByOid(childOid) if child not in beenQueued: # Get all the child's parents childsRelsWithParents = Relationships.getByChildOidRelType( childOid, Relationships.PART_OF) allParentsProcessed = True for childsRelWithParent in childsRelsWithParents: childsParent = Nodes.getByOid( childsRelWithParent.getParentOid()) if childsParent not in beenProcessed: allParentsProcessed = False if allParentsProcessed: nodeQueue.append(child) beenQueued.add(child) # Add self-referential relationships for node in beenProcessed: createRelationshipTransitive(Relationships.PART_OF, descendentOid = node.getOid(), ancestorOid = node.getOid()) return None
def _add_subtree(self, parent_id=None, tree=None): """Adds leaf or tree (in newick format) to a parent_id. (self,parent_id,tree).""" if parent_id is None: raise TreeError('Need node_id to connect to.') for st in tree: if type(st[0]) == list: # it's a subtree nd = self.dataclass() if isinstance(st[1][-1], str) and st[1][-1].startswith( NODECOMMENT_START ): # last element of values is a text and starts with [& nd.comment = st[1].pop(-1) if len( st[1] ) >= 2: # if there's two values, support comes first. Is that always so? nd.support = st[1][0] if st[1][1] is not None: nd.branchlength = st[1][1] elif len( st[1] ) == 1: # otherwise it could be real branchlengths or support as branchlengths if not self.__values_are_support: # default if st[1][0] is not None: nd.branchlength = st[1][0] else: nd.support = st[1][0] sn = Nodes.Node(nd) self.add(sn, parent_id) self._add_subtree(sn.id, st[0]) else: # it's a leaf nd = self.dataclass() if isinstance(st[1][-1], str) and st[1][-1].startswith(NODECOMMENT_START): nd.comment = st[1].pop(-1) nd.taxon = st[0] if len(st) > 1: if len( st[1] ) >= 2: # if there's two values, support comes first. Is that always so? nd.support = st[1][0] if st[1][1] is not None: nd.branchlength = st[1][1] elif len( st[1] ) == 1: # otherwise it could be real branchlengths or support as branchlengths if not self.__values_are_support: # default if st[1][0] is not None: nd.branchlength = st[1][0] else: nd.support = st[1][0] leaf = Nodes.Node(nd) self.add(leaf, parent_id)
def Evaluate(tree, track): startingPoint = Nodes.coord(5, 5) moves = 0 result = 0 for x in xrange(0, 59): res = Nodes.EvaluateTree(track, startingPoint, tree) #print "Result of tree", str(tree), ":", result if res == -1: return result result += res return result
def _add_subtree(self, parent_id=None, tree=None): """Adds leaf or tree (in newick format) to a parent_id. (self,parent_id,tree).""" if parent_id is None: raise TreeError('Need node_id to connect to.') for st in tree: nd = self.dataclass() nd = self._add_nodedata(nd, st) if type(st[0]) == list: # it's a subtree sn = Nodes.Node(nd) self.add(sn, parent_id) self._add_subtree(sn.id, st[0]) else: # it's a leaf nd.taxon = st[0] leaf = Nodes.Node(nd) self.add(leaf, parent_id)
def smtpAction(self): if 'apply' in self.fields: ## eventRecipients and failureRecipients are word lists eventRecipients = self.fields.get('eventRecipients', '') Nodes.setWordList(self.mgmt, self.cmcPolicyRetarget('/email/notify/events/recipients'), eventRecipients) failureRecipients = self.fields.get('failureRecipients', '') Nodes.setWordList(self.mgmt, self.cmcPolicyRetarget('/email/notify/failures/recipients'), failureRecipients) params = [] # remove the 'from_address' if the checkbox is unchecked if 'enableFromAddress' not in self.fields: params.append((self.cmcPolicyRetarget('/email/client/from_address'), 'string', '')) FormUtils.setNodesFromConfigForm(self.mgmt, self.fields, *params)
def __init__(self, tree=None, weight=1.0, rooted=False, name='', data=NodeData, values_are_support=False, max_support=1.0): """Ntree(self,tree).""" Nodes.Chain.__init__(self) self.dataclass = data self.__values_are_support = values_are_support self.max_support = max_support self.weight = weight self.rooted = rooted self.name = name root = Nodes.Node(data()) self.root = self.add(root) if tree: # use the tree we have # if Tree is called from outside Nexus parser, we need to get rid of linebreaks, etc tree = tree.strip().replace('\n', '').replace('\r', '') # there's discrepancy whether newick allows semicolons et the end tree = tree.rstrip(';') subtree_info, base_info = self._parse(tree) root.data = self._add_nodedata(root.data, [[], base_info]) self._add_subtree(parent_id=root.id, tree=subtree_info)
def __init__(self, sensorCount, outputCount, species, activationFunction='ReLU'): #First SC genes are the senor nodes, e.g. 0-6 are Sensors #Next node is the bias, e.g. 7 is Bias #Final OC genes are the output nodes, e.g. 8-10 are Outputs self.nodeGenes = [ Nodes.node(str(x), 'simp', float(x > sensorCount)) for x in range(sensorCount + outputCount + 1) ] #Sets up the sensor, bias, and output nodes, which output only the values given to them self.nodeGenes[sensorCount].inputs = [ 1 ] #This just takes care of the bias node, so that it outputs something #This just assigns the nodes a location in a picture, if you want to go draw them on the screen for i in range(sensorCount + 1): self.nodeGenes[i].loc = [ Settings.IOBufferSpace, (1 / (sensorCount + 1)) * (i + 0.5) ] for o in range(outputCount): self.nodeGenes[sensorCount + 1 + o].loc = [ 1 - Settings.IOBufferSpace, (1 / (outputCount)) * (o + 0.5) ] self.connectionGenes = [] #The genome's connection genes self.SC = sensorCount #Number of sensors it should have self.OC = outputCount #Number of outputs it should have self.SP = species #What species the genome belongs to self.FI = 0 #The fitness of the genome self.AF = activationFunction #What activation function the intermediary nodes will be using self.nodeDict = { } #This is just a thing for when it comes time to evaluate the genome self.champion = False #Keeping track of which genomes are champions
def create_rec_tree(depth): # si `depth` es mayor a 0, nos toca crear un sub-arbol if depth > 0: # elegimos una funcion aleatoriamente node_cls = random.choice(self.functions) # aqui iremos dejando los argumentos que necesita la funcion arguments = [] # para cada argumento que la funcion necesite... for _ in range(node_cls.num_args): # existe un `prob` probabilidad de que no sigamos creando # sub-arboles y lleguemos y cortemos aqui para hacer # un nodo terminal if random.random() < self.prob: arguments.append(create_rec_tree(0)) else: # la otra es seguir creando sub-arboles recursivamente arguments.append(create_rec_tree(depth - 1)) # `arguments` es una lista y los nodos necesitan argumentos # asi que hacemos "unpacking" de la lista return node_cls(*arguments) else: # si `depth` es 0 entonces creamos un nodo terminal con # alguno de los terminales permitidos que definimos inicialmente return N.TerminalNode(random.choice(self.terminals))
def setupRESTAccessCodes(self): user = self.session().value('localId') fields = self.request().fields() policyName, pathPrefix, policyType = Nodes.cmcDecodeNodeMode(fields) if 'addAccessCode' in fields: desc = fields['accessCodeAdd_desc'] if 'true' == fields.get('addAccessCode_generate'): self.sendAction(pathPrefix + '/papi/action/generate_access_code', ('user', 'string', user), ('desc', 'string', desc)) else: data = fields['addAccessCode_data'] self.sendAction(pathPrefix + '/papi/action/import_access_code', ('data', 'string', data), ('desc', 'string', desc)) elif 'editAccessCode' in fields: desc = fields['accessCodeEdit_desc'] jti = fields['accessCodeEdit_jti'] self.setNodes(('%s/papi/config/code/%s/desc' % (pathPrefix, jti), 'string', desc)) elif 'removeAccessCode' in fields: FormUtils.deleteNodesFromConfigForm(self.mgmt, self.cmcPolicyRetarget('/papi/config/code'), 'accesscode_', fields)
def snmpTrapReceivers(self): table = self.getXmlTable(None, tagNames=('snmp-trap-receivers', 'receiver')) table.open(self.request()) base = self.cmcPolicyRetarget('/snmp/trapsink/sink') receivers = Nodes.getMgmtSetEntries(self.mgmt, base) hosts = receivers.keys() hosts.sort(FormUtils.alphanumericCompare) for host in hosts: receiver = receivers[host] version = receiver.get('type')[len('trap-'):] port = receiver.get('port') if 'v3' == version: user = receiver.get('username', '') protocol = receiver.get('hash_function', '') auth = receiver.get('sec_level', '') authValue = (('auth' == auth) and 'Auth' or ('noauth' == auth) and 'No Auth' or ('authpriv' == auth) and 'AuthPriv') # update the community/user to include privacy protocol and security level as authpriv if authValue == 'AuthPriv': privacyProtocol = receiver.get('privacy_protocol', '') desc = 'user: %s, %s, %s, %s' % (user, protocol, authValue, privacyProtocol) else: desc = 'user: %s, %s, %s' % (user, protocol, authValue) else: desc = 'community: %s' % (receiver.get('community') or 'public') table.addEntry(host=host, version=version, port=port, desc=desc, enabled=('true' == receiver['enable']) and 'enabled' or 'disabled') table.close()
def _respond(self, transaction): mgmt = self.session().value('mgmt') assert 'monitor' != mgmt.remoteUser.lower(), \ 'Monitor may not view or download logs.' # check permissions assert Nodes.permission(mgmt, '/logging/syslog/action/file/\/var\/log\/messages') != 'deny', \ ('%s may not view or download logs.' % mgmt.remoteUser) request = transaction.request() response = transaction.response() fields = request.fields() logtype = fields.get('logtype', 'user') mime = fields.get('mime', '') or self.ContentType logfilename = '/var/log/%smessages' % self.LOGTYPE_PREFIX[logtype] try: bytesize = os.stat(logfilename)[stat.ST_SIZE] seekto = max(0, bytesize - 500) response.setHeader('Content-type', mime) response.commit() if mime.endswith('html'): response.write('<html>\n') response.write('<head>\n') response.write('<title>Continuous Log</title>\n') response.write('</head>\n') response.write('<body>\n') response.write('<pre>\n') response.flush(True) logfile = file(logfilename) logfile.seek(seekto) # Seek to almost EOF # Print out the last few lines of the log, # stripping out any partial line artifact of our seek() txt = cgi.escape(logfile.read() or '') txt = txt[txt.find('\n') + 1:] writingDots = 0 # Write to log after seek so we should read it in the first # iteration of reading logfile. while hasattr(request, '_transaction') and \ 2 < AppServerModule.globalAppServer._running: if txt: if writingDots: response.write('\n') writingDots = 0 response.write(txt) else: writingDots += 1 if 0 == writingDots % 80: response.write('\n') response.write('. ') time.sleep(3) response.flush() # I don't trust auto flush txt = cgi.escape(logfile.read() or '') except ConnectionAbortedError: pass except: OSUtils.logException()
def local_planner(x_near, x_samp, factor=0.5): '''returns a new node along the line from''' '''x_near to x_samp at distance d''' if factor < 0 or factor > 1: factor = 0.5 x = x_near.x + (x_samp.x - x_near.x) * factor y = x_near.y + (x_samp.y - x_near.y) * factor x_new = Nodes(x, y) return x_new
def __construct__(self): #extract, contain, and properly indent python content funcname = self._elem.getAttribute('name') args = self._elem.getAttribute('args') script = Nodes.normalize_python(node=self, definition="def %s(%s):"%(funcname, args)) #run the code (which will define a function where we need it) try: exec script in self.parent.__dict__ except Exception, msg: Events.Error(self, msg, "bad syntax in python block")
def newUser(uId, name): ''' Creates a new user. It's added to the user's hashtable. Worst-Case: O(U) Average-Case: θ(1) ''' user = Nodes.User(uId, name) usersHash.put(user) return user
def create_node(id,parent,cost,open,close): new_node = Nodes.node() new_node.map_key = create_key(id) new_node.cost = copy.deepcopy(cost) new_node.is_open = copy.deepcopy( open ) new_node.is_closed = close new_node.id = copy.deepcopy ([id[0], id[1]]) if(parent!=None): new_node.parent_id = copy.deepcopy([parent[0],parent[1]]) return new_node
def __construct__(self): #extract, contain, and properly indent python content funcname = self._elem.getAttribute('name') args = self._elem.getAttribute('args') prefix = "def %s(%s): import threading; t = threading.Thread(target=_thread_%s, args=[%s]); t.setDaemon(True); t.start();"%(funcname, args, funcname, args) definitionLine = "def _thread_%s(%s):"%(funcname, args) script = Nodes.normalize_python(node=self, prefix=prefix, definition=definitionLine) #run the code (which will define a function where we need it) try: exec script in self.parent.__dict__ except Exception, msg: Events.Error(self, msg, "bad syntax in python block\s%s"%script)
def read_xml(file_name): doc = minidom.parse(file_name + ".xml").documentElement directed = True if doc.getAttribute("directed") == 'False': directed = False radius = int(doc.getAttribute("radius")) minimal_path_set_search = doc.getElementsByTagName( "minimal_path_set_search")[0].firstChild.data if minimal_path_set_search == 'None': minimal_path_set_search = None D_MP_search = doc.getElementsByTagName("D_MP_search")[0].firstChild.data if D_MP_search == 'None': D_MP_search = None reliability_evaluation = doc.getElementsByTagName( "reliability_evaluation")[0].firstChild.data if reliability_evaluation == 'None': reliability_evaluation = None ns = Nodes() nodes = doc.getElementsByTagName("node") for node in nodes: name = node.getAttribute("name") p = node.getAttribute("position") position = tuple(map(int, p[1:-1].split(', '))) ns.add_node(Node(name, position)) es = Edges() edges = doc.getElementsByTagName("edge") for edge in edges: name = edge.getAttribute("name") start_node = edge.getAttribute("start_node") end_node = edge.getAttribute("end_node") capacity = int(edge.getAttribute("capacity")) probability_distribution = edge.getAttribute( "probability_distribution") if probability_distribution == '[]': probability_distribution = None es.add_edge( EdgeInfo(name, ns.nodes[start_node], ns.nodes[end_node], capacity, probability_distribution)) return ns, es, directed, radius, minimal_path_set_search, D_MP_search, reliability_evaluation
def monitoredPorts(self): base = RVBDUtils.monitoredPortsPath(self.fields) portMap = Nodes.getMgmtTabularSubtree(self.mgmt, base, Nodes.parentKeyStringIntCmp) result = self.doc.createElement('monitoredPorts') for eachPort in portMap: portEl = self.doc.createElement('port') portEl.setAttribute('number', eachPort['parentKey']) portEl.setAttribute('desc', eachPort['desc']) result.appendChild(portEl) self.doc.documentElement.appendChild(result) self.writeXmlDoc()
def _relCmp(rel1, rel2): """ Sort comparison routine to compare 2 relationships. First sorts by rel sequence (if set), and then by child component names if sequence is a tie. """ relType1 = rel1.getRelationshipType() relType2 = rel2.getRelationshipType() if relType1 != relType2: Util.fatalError([ "Attempting to sort relationships of two different types:", "Type 1: " + relType1 + " Type 2: " + relType2 ]) if rel1.getParentOid() != rel2.getParentOid(): Util.fatalError([ "Attempting to sort relationships that have different parents."]) seq1 = rel1.getSequence() seq2 = rel2.getSequence() if seq1 != seq2: # sort based purely on sequence if seq1 != None and seq2 != None: if seq1 < seq2: return -1 elif seq1 > seq2: return +1 elif seq1 != None: return -1 elif seq2 != None: return +1 else: # sequence same, sort based on name. if relType1 == PART_OF: return cmp(Nodes.getByOid(rel1.getChildOid()).getComponentName(), Nodes.getByOid(rel2.getChildOid()).getComponentName()) else: # assume relationship is between timed nodes. nodeOid1 = TimedNodes.getByOid(rel1.getChildOid()).getNodeOid() nodeOid2 = TimedNodes.getByOid(rel2.getChildOid()).getNodeOid() return cmp(Nodes.getByOid(nodeOid1).getComponentName(), Nodes.getByOid(nodeOid2).getComponentName())
def createRelationship(relationshipType, childOid, parentOid, sequence = None): """ Create a new relationship record. """ # Create the record and generate the insert rel = AnaRelationshipDb.AnaRelationshipDbRecord() oid = Oids.createNextOid() rel.setOid(oid) rel.setRelationshipType(relationshipType) rel.setChildOid(childOid) rel.setParentOid(parentOid) rel.insert() # add relationship to this module's knowledge. _addRelToKnowledge(rel) # Add relationship to other module's knowledge Nodes.connectTheDotsForRelationship(rel) return rel
def Evaluate(tree, track): startingPoint=Nodes.coord(5,5) moves = 0 result = 0 for x in xrange(0,59): res = Nodes.EvaluateTree(track, startingPoint, tree) #print "Result of tree", str(tree), ":", result if res == -1: return result result += res return result
def newAccount(aId, userId, aType, balance): ''' Creates a new account. It's added to the account's hashtable. First it searches the user object to assign this new account. Then the account is added to the user's maxheap. Worst-Case: O(U + A + log(A)) Average-Case: θ(1) ''' user = getUser(userId) account = Nodes.Account(aId, userId, aType, balance) user.addAccount(account) accountsHash.put(account) return account
def BuildSPN(layers, numNodesPerLayer, nodeTypePerLayer, inputNodeValues,flag,pcn=1): i = 1 layer = [[]]*layers layer[0] = MakeNodes(0,numNodesPerLayer[0], nodeTypePerLayer, inputNodeValues) while i < layers: if i < layers - 1: layer[i] = MakeNodes(i, numNodesPerLayer[i], nodeTypePerLayer) else: layer[i] = [Nodes.SumNode([],[],i)] #SigmoidNode layer[i][0].setGradient(1,1) layer[i], layer[i-1] = ConnectNodes(layer[i],layer[i-1],flag,pcn) if nodeTypePerLayer[i] == 2: count = 0 while count < numNodesPerLayer[i]: layer[i][count].childWeights = Nodes.normalize(layer[i][count].childWeights) count = count+1 i = i+1 j=0 while j < len(layer[layers-2]): layer[layers-2][j].addChildren([Nodes.Node(layer[layers-2][j],[],layers-2,1)]) j = j+1 return layer
def _openPathname(self, pathName): # check permissions mgmt = self.session().value('mgmt') assert 'monitor' != mgmt.remoteUser.lower(), \ 'Download prohibited for the monitor user.' assert Nodes.permission( mgmt, self.__getTarget()['rbaNode']) != 'deny', \ ('Download prohibited for user %s.' % mgmt.remoteUser) if 'openFileFn' in self.__getTarget(): return self.__getTarget()['openFileFn'](pathName) else: return file(pathName, "r")
def connectTheDots(): """ Called after all the anatomy has been read into memory to connect it all together. NOTE: This implementation is a hack. It creates additional structures in the high level modules, rather than directly modifies the individual objects. For each node, create: list of all its parent nodes list of all its child nodes list of all its timed nodes, by stage For each timed node, create: list of all its parent timed nodes list of all its child timed nodes """ Nodes.connectTheDots() TimedNodes.connectTheDots() return
def minerNode(): global me global balance global actN global pkhs if me == None: me = nd.MinerNode(blockchain) balance = me.getWallet().getBalance() actN = 'Miner' pkhs = me.getWallet().getPubKeyHs() elif me.getType() != 'Miner': flash('You must have a Miner Node for this page to work!') return redirect('/home') connectedNodes = me.bchain.getNodes() form1 = RegisterNodeForm() form2 = SendToForm() form3 = MiningForm() form4 = RefreshBalance() if form1.submit1.data and form1.validate_on_submit(): addNodes([form1.nodeAddress.data]) flash('You have added a the node {} to your network!'.format( form.nodeAddress.data)) return redirect('/minerNode') if form2.submit2.data and form2.validate_on_submit(): sendTransaction(form2.amount.data, form2.toAddress.data, form2.txFee.data) flash('You have sent {} coins to the address {}!'.format( form2.amount.data, form2.toAddress.data)) return redirect('/minerNode') if form3.submit3.data and form3.validate_on_submit(): test1 = me.submitBlock() balance = me.getWallet().getBalance() flash('You have begun mining! {}'.format(test1)) return redirect('/minerNode') if form4.submit4.data and form4.validate_on_submit(): balance = me.getWallet().getBalance() flash('Your balance has been refreshed!') return redirect('/minerNode') return render_template('miner.html', title='Mine', nodes=connectedNodes, keys=pkhs, bal=balance, activeN=actN, reg_form=form1, send_form=form2, mine_form=form3, refresh_form=form4)
def initialise(sortProject=None, dbHost=None, dbName=None, dbUser=None, dbPass=None, outputDir=None, outputFilePath=None, charset=None): """ Initialise DB connection and read the whole darn anatomy database into memory. """ if dbHost != None: # Only create connection if connection params provided. # If not provided then we assume we already have an open # connection. DbAccess.initialise( dbHost = dbHost, dbName = dbName, dbUser = dbUser, dbPass = dbPass, outputDir = outputDir, outputFilePath = outputFilePath, charset = charset) # Read in every table we care about. # Base tables Oids.initialise() Versions.initialise() Stages.initialise() Nodes.initialise() TimedNodes.initialise() Relationships.initialise(sortProject) Synonyms.initialise() Perspectives.initialise() PerspectiveAmbits.initialise() # Derived tables RelationshipsTransitive.initialise() PartOfs.initialise() PartOfPerspectives.initialise() connectTheDots() return None
def cert(self): certType = self.fields.get('type') certName = self.fields.get('name') # adjust the path for these cert types if 'ca' == certType: path = '/rbt/sport/ssl/state/ca/%s' % (certName) elif 'peering' == certType: path = '/rbt/sport/ssl/state/tunnel/ca/%s' % (certName) elif 'mobile' == certType: path = '/rbt/sport/ssl/state/tunnel/shm_ca/%s' % (certName) elif certType in ('black', 'gray', 'white'): path = '/rbt/sport/ssl/state/tunnel/%s_list/%s/cert' % (certType, certName) elif 'chain' == certType: sid = self.request().fields().get('sid') path = '/rbt/sport/ssl/state/server_certs/names/%s/chain_cert/%s' % (sid, certName) elif 'appliance' == certType: path = '/rbt/sport/ssl/state/tunnel/cert' else: raise 'unknown cert type ' + certType policyName = self.fields.get('policy') if policyName: # get the cert data for the CMC cert = Nodes.action(self.mgmt, '/rbt/sport/ssl/action/iterate_node', ('profile', 'string', policyName), ('node', 'string', path)) prefixLen = len(path) + 1 # remove the prefixes cert = dict([(k[prefixLen:], v) for k,v in cert.iteritems()]) else: # get the cert data for the SH or other cert = self.mgmt.getChildren(path) # tweakage for colors if certType in ('black', 'gray', 'white'): self.transaction().color_info = True cert['IP'] = certName # display cert self.transaction().cert = cert # Bug 33438 specified that we needed a way to move certs between "Peering" and "Mobile" # bins. Allow peering/mobile base64 PEM certs to be viewable in the popup. User can move # certs now by cutting and pasting. # TODO May want to extend this functionality to all popup certs in the future. if certType in ('peering', 'mobile', 'appliance'): self.application().includeURL(self.transaction(), '/Templates/presentCertWithPEM') else: self.application().includeURL(self.transaction(), '/Templates/presentCert')
def snmpAcls(self): base = self.cmcPolicyRetarget('/snmp/vacm/acls') acls = Nodes.getMgmtSetEntries(self.mgmt, base) ids = acls.keys() ids.sort(lambda a, b: cmp(int(a), int(b))) table = self.getXmlTable(None, tagNames=('policies', 'policy')) table.open(self.request()) for id in ids: acl = acls[id] table.addEntry(id=id, group_name=acl.get('group_name'), sec_level=acl.get('sec_level'), read_view=acl.get('read_view')) table.close()
def snmpUsers(self): base = self.cmcPolicyRetarget('/snmp/usm/users') users = Nodes.getMgmtSetEntries(self.mgmt, base) names = users.keys() names.sort(FormUtils.alphanumericCompare) table = self.getXmlTable(None, tagNames=('users', 'user')) table.open(self.request()) for name in names: user = users[name] table.addEntry(name=name, protocol=user.get('hash_function'), key=user.get('auth_key'), privacy=user.get('privacy_protocol'), privacyKey=user.get('privacy_key')) table.close()
def monitoredPorts(self): if 'editPolicy' in self.fields: base = self.cmcPolicyRetarget('/rbt/sport/reports/config/bandwidth/port') else: base = RVBDUtils.monitoredPortsPath() portMap = Nodes.getMgmtTabularSubtree(self.mgmt, base, Nodes.parentKeyStringIntCmp) result = self.doc.createElement('monitoredPorts') for eachPort in portMap: portEl = self.doc.createElement('port') portEl.setAttribute('number', eachPort['parentKey']) portEl.setAttribute('desc', eachPort['desc']) result.appendChild(portEl) self.doc.documentElement.appendChild(result) self.writeXmlDoc()
def MakeNodes(k, numNodesPerLayer, nodeTypePerLayer, inputNodeValues=[]): i = 0 nodes = [] if nodeTypePerLayer[k] == 1: while (i < numNodesPerLayer): nodes.append(Nodes.ProdNode([],[],k)) i = i+1 elif nodeTypePerLayer[k] == 2: while (i < numNodesPerLayer): nodes.append(Nodes.SumNode([],[],k,)) i = i+1 elif nodeTypePerLayer[k] == 3: while (i < numNodesPerLayer): nodes.append(Nodes.MaxNode([],[],k)) i = i+1 elif nodeTypePerLayer[k] == 4: while (i < numNodesPerLayer): nodes.append(Nodes.MaxPoolNode([],[],k, inputNodeValues[i])) i = i+1 else: while (i < numNodesPerLayer): nodes.append(Nodes.Node([],[],k,inputNodeValues[i])) i = i+1 return nodes
def __construct__(self): #extract, wrap, and properly indent python content self.funcname = self['name'] or "_%s_%s"%(self.on, self.canvas._uidcount) self.canvas.__dict__['_uidcount'] += 1 args = self['args'] or "value" script = Nodes.normalize_python(node=self, definition="def %s(%s):"%(self.funcname, args)) #run the code (which will define a function where we need it) try: exec script in self.parent.__dict__ except Exception, msg: Events.Error(self, msg, "bad syntax in python block") #if a reference is given adjust the target to that, otherwise use the parent per normal if self['reference']: self.parent.constrain(attr='early', func=self._reference) else: self.parent.constrain(attr=self.on, func=self.parent.__dict__[self.funcname])
def monitoredPorts(self): base = RVBDUtils.monitoredPortsPath(self.fields) if 'addPort' in self.fields.keys(): number = self.fields.get('addPort_number') if Nodes.present(self.mgmt, '%s/%s' % (base, number)): self.setFormError("Port %s is already being monitored." % number) return else: desc = self.fields.get('addPort_desc') self.setNodes(('%s/%s/desc' % (base, number), 'string', desc)) elif 'removePorts' in self.fields.keys(): FormUtils.deleteNodesFromConfigForm(self.mgmt, base, 'ck_', self.fields) elif 'editPort' in self.fields.keys(): number = self.fields.get('editPort_number', None) desc = self.fields.get('editPort_desc', None) self.setNodes(('%s/%s/desc' % (base, number), 'string', desc))
def restAccessCodes(self): mgmt = self.mgmt result = self.doc.createElement('restAccessCodes') path = self.cmcPolicyRetarget('/papi/config/code') jtis = Nodes.getMgmtSetEntries(self.mgmt, path) for jti, attribs in jtis.iteritems(): entryEl = self.doc.createElement('accessCode') entryEl.setAttribute('jti', jti) entryEl.setAttribute('owner', attribs['user']) entryEl.setAttribute('desc', attribs['desc']) entryEl.setAttribute('code', attribs['data']) result.appendChild(entryEl) self.doc.documentElement.appendChild(result) self.writeXmlDoc()
def logserverXmldata(self): remotePrefix = self.cmcPolicyRetarget('/logging/syslog/action/host') priority = 'selector/0/priority' lvl=['emerg','alert','crit','err','warning','notice','info','debug','none'] levelNameToInt = dict(zip(lvl, range(len(lvl)))) remoteMap = Nodes.getMgmtTabularDescendents( self.mgmt, remotePrefix, lambda x, y: cmp(levelNameToInt[x[priority]], levelNameToInt[y[priority]]), priority) result = self.doc.createElement('logservers') for eachRemote in remoteMap: logserver = self.doc.createElement('logserver') logserver.setAttribute('serverIp', eachRemote['parentKey']) logserver.setAttribute('priority', eachRemote[priority]) result.appendChild(logserver) self.doc.documentElement.appendChild(result) self.writeXmlDoc()
def fullNode(): global me global balance global actN global pkhs if me == None: me = nd.FullNode(blockchain) balance = me.getWallet().getBalance() actN = 'Full' pkhs = me.getWallet().getPubKeyHs() elif me.getType() != 'Full': flash('You must have a Full Node for this page to work!') return redirect('/home') connectedNodes = me.bchain.getNodes() form1 = RegisterNodeForm() form2 = SendToForm() form3 = RefreshBalance() if form1.submit1.data and form1.validate_on_submit(): blockchain.registerNode(form1.nodeAddress.data) flash('You have added a the node {} to your network!'.format( form.nodeAddress.data)) return redirect('/fullNode') if form2.submit2.data and form2.validate_on_submit(): sendTransaction(form2.amount.data, form2.toAddress.data, form2.txFee.data) flash('You have sent {} coins to the address {}!'.format( form2.amount.data, form2.toAddress.data)) return redirect('/fullNode') if form3.submit4.data and form3.validate_on_submit(): balance = me.getWallet().getBalance() flash('Your balance has been refreshed!') return redirect('/fullNode') return render_template('full.html', title='Full', nodes=connectedNodes, keys=pkhs, activeN=actN, bal=balance, reg_form=form1, send_form=form2, refresh_form=form3)
def split(self,parent_id=None,n=2,branchlength=1.0): """Speciation: generates n (default two) descendants of a node. [new ids] = split(self,parent_id=None,n=2,branchlength=1.0): """ if parent_id is None: raise TreeError('Missing node_id.') ids=[] parent_data=self.chain[parent_id].data for i in range(n): node=Nodes.Node() if parent_data: node.data=self.dataclass() # each node has taxon and branchlength attribute if parent_data.taxon: node.data.taxon=parent_data.taxon+str(i) node.data.branchlength=branchlength ids.append(self.add(node,parent_id)) return ids
def newRandomNode(self, innovation): if len(self.connectionGenes) > 0: c = random.choice(range(len(self.connectionGenes))) priority = ((self.nodeGenes[int(self.connectionGenes[c].A)].p + self.nodeGenes[int(self.connectionGenes[c].Z)].p) / 2) + (random.random() / 10000) name = str(len(self.nodeGenes)) location = list( ((numpy.array(self.nodeGenes[int(self.connectionGenes[c].A)]. loc) + numpy.array(self.nodeGenes[int( self.connectionGenes[c].Z)].loc)) / 2) + (numpy.array([random.random() - 0.5, random.random() - 0.5]) / 5)) newNode = Nodes.node(name, self.AF, priority, location) self.nodeGenes.append(newNode) self.connectionGenes = self.connectionGenes + self.connectionGenes[ c].split(newNode, innovation) else: print("Womp womp")
def finalize(self, fs_digest, fs_level, fs_stats, num_files): if self.readonly != False: raise Exception("Increment already finalized") self.attributes["fs_digest"] = fs_digest self.attributes["fs_level"] = str(fs_level) self.attributes["fs_stats"] = Nodes.serialize_stats(fs_stats) self.attributes["ftime"] = str(time.time()) self.attributes["num_files"] = str(num_files) self.readonly = True #print "Finalizing increment", self.fs_digest PREFIX = "Increment.%s.%s." % (self.attributes["storage_index"], self.attributes["index"]) for key, val in self.attributes.iteritems(): self.db[PREFIX + key] = val message = self.compute_message() digest = Digest.dataDigest(message) self.block_database.add_block( digest, Container.CODE_INCREMENT_DESCRIPTOR, message) return digest
def finder(grid,open_nodes,closed_nodes,start,finish): while(open_nodes != []): sorted(open_nodes) #sort nodes so first is the closest one current = open_nodes.pop(0) closed_nodes.append(current) (x,y) = current.position if(equals(current,finish)): path = [] while current != start: path.append(current.position) current = current.father path.append(start.position) return path[::-1] #path reversed else: #current != finish (x,y) = current.position neighbors = [(x-1, y),(x+1, y),(x, y-1),(x, y+1)] for i in neighbors: (a,b) = i #neighbors coordinates if grid[a][b] == 'X': continue son = N.node(a,b) son.father = current door = True for c in closed_nodes: if equals(son,c): door = False if door: son.hcost = distance(son,finish) son.gcost = current.gcost + 1 son.fcost = son.hcost + son.gcost if(addToOpen(open_nodes,son)): for f in open_nodes: if equals(f,son): open_nodes.remove(f) open_nodes.append(son) return []
def perProcessLogging(self): # level -> pretty name map levelOptions = { 'emerg': 'Emergency', 'alert': 'Alert', 'crit': 'Critical', 'err': 'Error', 'warning': 'Warning', 'notice': 'Notice', 'info': 'Info', } # filters <- {process: {..., 'level': level, ...} base = self.cmcPolicyRetarget('/logging/syslog/config/filter/process') filters = Nodes.getMgmtSetEntries(self.mgmt, base) filterDescription = \ self.cmcPolicyRetarget('/logging/syslog/state/filter/process/%s/description') # fetch pretty names filterNameMap = self.mgmt.getMultiple(*[ filterDescription % k for k in filters.iterkeys()]) # filters <- [(prettyName, process, level), ...] filters = [(filterNameMap.get(filterDescription % k) or k, k, v.get('level')) for k, v in filters.items()] # Sort on prettyName, then process, then level. filters.sort() result = self.doc.createElement('perProcessLogging') for prettyName, process, level in filters: processFilter = self.doc.createElement('processFilter') processFilter.setAttribute('prettyName', prettyName) processFilter.setAttribute('process', process) processFilter.setAttribute( 'level', levelOptions.get(level, level)) result.appendChild(processFilter) self.doc.documentElement.appendChild(result) self.writeXmlDoc()
def main(): treeList = [] for i in xrange(0,5000): tree = Nodes.generateNodes() treeList.append(tree) #for i in xrange(0, len(treeList)): # print str(treeList[i]) totalGenWinner = None totalGenWinnerPoints = 0 genWinnerTrack = None for i in xrange(0, len(treeList)): a = treeList[random.randint(0, len(treeList)) - 1] b = treeList[random.randint(0, len(treeList)) - 1] c = treeList[random.randint(0, len(treeList)) - 1] d = treeList[random.randint(0, len(treeList)) - 1] ta = Track.Track() tb = Track.Track() pointA = Evaluate(a, ta.track) pointB = Evaluate(b, tb.track) if a > b: winner1 = a looser1 = b if pointA > totalGenWinnerPoints: totalGenWinnerPoints = pointA totalGenWinner = a genWinnerTrack = ta else: winner1 = b looser1 = a if pointB > totalGenWinnerPoints: totalGenWinnerPoints = pointB totalGenWinner = b genWinnerTrack = tb tc = Track.Track() td = Track.Track() pointC = Evaluate(c, tc.track) pointD = Evaluate(d, td.track) if c > d: winner2 = c looser2 = d if pointC > totalGenWinnerPoints: totalGenWinnerPoints = pointC totalGenWinner = c genWinnerTrack = tc else: winner2 = d looser2 = c if pointD > totalGenWinnerPoints: totalGenWinnerPoints = pointD totalGenWinner = d genWinnerTrack = td (looser1, looser2) = Nodes.merge(winner1, winner2) print "Total generation winner, with %d points is: %s" % (totalGenWinnerPoints, totalGenWinner) print genWinnerTrack
def consensus(trees, threshold=0.5,outgroup=None): """Compute a majority rule consensus tree of all clades with relative frequency>=threshold from a list of trees.""" total=len(trees) if total==0: return None # shouldn't we make sure that it's NodeData or subclass?? dataclass=trees[0].dataclass max_support=trees[0].max_support clades={} #countclades={} alltaxa=set(trees[0].get_taxa()) # calculate calde frequencies c=0 for t in trees: c+=1 #if c%50==0: # print c if alltaxa!=set(t.get_taxa()): raise TreeError, 'Trees for consensus must contain the same taxa' t.root_with_outgroup(outgroup=outgroup) for st_node in t._walk(t.root): subclade_taxa=t.get_taxa(st_node) subclade_taxa.sort() subclade_taxa=str(subclade_taxa) # lists are not hashable if subclade_taxa in clades: clades[subclade_taxa]+=float(t.weight)/total else: clades[subclade_taxa]=float(t.weight)/total #if subclade_taxa in countclades: # countclades[subclade_taxa]+=t.weight #else: # countclades[subclade_taxa]=t.weight # weed out clades below threshold for (c,p) in clades.items(): if p<threshold: del clades[c] # create a tree with a root node consensus=Tree(name='consensus_%2.1f' % float(threshold),data=dataclass) # each clade needs a node in the new tree, add them as isolated nodes for (c,s) in clades.items(): node=Nodes.Node(data=dataclass()) node.data.support=s node.data.taxon=set(eval(c)) consensus.add(node) # set root node data consensus.node(consensus.root).data.support=None consensus.node(consensus.root).data.taxon=alltaxa # we sort the nodes by no. of taxa in the clade, so root will be the last consensus_ids=consensus.all_ids() consensus_ids.sort(lambda x,y:len(consensus.node(x).data.taxon)-len(consensus.node(y).data.taxon)) # now we just have to hook each node to the next smallest node that includes all taxa of the current for i,current in enumerate(consensus_ids[:-1]): # skip the last one which is the root #print '----' #print 'current: ',consensus.node(current).data.taxon # search remaining nodes for parent in consensus_ids[i+1:]: #print 'parent: ',consensus.node(parent).data.taxon if consensus.node(parent).data.taxon.issuperset(consensus.node(current).data.taxon): break else: sys.exit('corrupt tree structure?') # internal nodes don't have taxa if len(consensus.node(current).data.taxon)==1: consensus.node(current).data.taxon=consensus.node(current).data.taxon.pop() # reset the support for terminal nodes to maximum #consensus.node(current).data.support=max_support else: consensus.node(current).data.taxon=None consensus.link(parent,current) # eliminate root taxon name consensus.node(consensus_ids[-1]).data.taxon=None return consensus
def root_with_outgroup(self,outgroup=None): def _connect_subtree(parent,child): """Hook subtree starting with node child to parent.""" for i,branch in enumerate(self.unrooted): if parent in branch[:2] and child in branch[:2]: branch=self.unrooted.pop(i) break else: raise TreeError, 'Unable to connect nodes for rooting: nodes %d and %d are not connected' % (parent,child) self.link(parent,child) self.node(child).data.branchlength=branch[2] self.node(child).data.support=branch[3] #now check if there are more branches connected to the child, and if so, connect them child_branches=[b for b in self.unrooted if child in b[:2]] for b in child_branches: if child==b[0]: succ=b[1] else: succ=b[0] _connect_subtree(child,succ) # check the outgroup we're supposed to root with if outgroup is None: return self.root outgroup_node=self.is_monophyletic(outgroup) if outgroup_node==-1: return -1 # if tree is already rooted with outgroup on a bifurcating root, # or the outgroup includes all taxa on the tree, then we're fine if (len(self.node(self.root).succ)==2 and outgroup_node in self.node(self.root).succ) or outgroup_node==self.root: return self.root self.unroot() # now we find the branch that connects outgroup and ingroup #print self.node(outgroup_node).prev for i,b in enumerate(self.unrooted): if outgroup_node in b[:2] and self.node(outgroup_node).prev in b[:2]: root_branch=self.unrooted.pop(i) break else: raise TreeError, 'Unrooted and rooted Tree do not match' if outgroup_node==root_branch[1]: ingroup_node=root_branch[0] else: ingroup_node=root_branch[1] # now we destroy the old tree structure, but keep node data. Nodes will be reconnected according to new outgroup for n in self.all_ids(): self.node(n).prev=None self.node(n).succ=[] # now we just add both subtrees (outgroup and ingroup) branch for branch root=Nodes.Node(data=NodeData()) # new root self.add(root) # add to tree description self.root=root.id # set as root self.unrooted.append([root.id,ingroup_node,root_branch[2],root_branch[3]]) # add branch to ingroup to unrooted tree self.unrooted.append([root.id,outgroup_node,0.0,0.0]) # add branch to outgroup to unrooted tree _connect_subtree(root.id,ingroup_node) # add ingroup _connect_subtree(root.id,outgroup_node) # add outgroup # if theres still a lonely node in self.chain, then it's the old root, and we delete it oldroot=[i for i in self.all_ids() if self.node(i).prev is None and i!=self.root] if len(oldroot)>1: raise TreeError, 'Isolated nodes in tree description: %s' % ','.join(oldroot) elif len(oldroot)==1: self.kill(oldroot[0]) return self.root
def alarmdAlarms(self): # First check if the alarmd is terminated alarmdStatus = Nodes.present(self.mgmt, '/pm/monitor/process/alarmd/state') if alarmdStatus != 'running': self.xmlError('', 'the alarm service is not running.') return # slashes are escaped in this list alarmNames = Nodes.getMgmtLocalChildrenNames(self.mgmt, '/alarm/state/alarm') # avoid the appliance and group healths alarmNames = [name for name in alarmNames if not (name.startswith('app:') or name.startswith('group:'))] alarmsRaw = self.mgmt.getPattern(*['/alarm/state/alarm/%s/*' % name for name in alarmNames]) # build the alarm table (with unescaped slashes) alarms = {} for k, v in alarmsRaw.iteritems(): name, entry = re_alarmSplit.match(k).groups() name = name.replace('\\/', '/') if name not in alarms: alarms[name] = {} alarms[name][entry] = v # Insert an item into a CSV string. # The CSV string is returned alphabetically sorted. def csvInsert(csv, item): if csv: csvList = csv.split(',') csvList.append(item) csvList.sort(FormUtils.alphanumericCompare) csv = ','.join(csvList) else: csv = item return csv # Add in any synthetic alarms. root = alarms['health'] synths = self.getSyntheticAlarms() for k, v in synths.iteritems(): root['aggregates'] = csvInsert(root['aggregates'], k) alarms.update(synths) # Add in any custom trigger notes. notesMap = self._alarmNotesMap.copy() notesMap.update(self.getCustomTriggerMessages()) # get children for this alarm id def getChildren(alarmId): aggs = alarms[alarmId]['aggregates'] if aggs: return aggs.split(',') else: return [] # does this alarm have any non-hidden children? def hasVisibleChildren(alarmId): for ch in getChildren(alarmId): if 'true' != alarms[ch].get('hidden'): return True return False # sort this alarm id's chilren, plop them in def xmlizeChildren(parentEl, alarmId): children = getChildren(alarmId) children.sort(FormUtils.alphanumericCompare, key=lambda a: alarms[a]['display_name']) for ch in children: xmlizeAlarm(parentEl, ch) # xmlize an alarm, and place it in the parent el def xmlizeAlarm(parentEl, alarmId): alarm = alarms[alarmId] # skip it if "hidden" if 'true' == alarm.get('hidden'): return status = 'OK' statusStyle = 'statusSuccess' collapse = alarm['aggregates'] note = False # 'suppressed' == true overrides 'enabled' != true if 'true' == alarm.get('suppressed'): # suppressed: status = 'Suppressed' statusStyle = 'statusDisabled' elif 'true' != alarm.get('enabled'): # not enabled: status = 'Disabled' statusStyle = 'statusDisabled' if 'true' == alarm.get('triggered'): # triggered: status = alarm['severity_str'] if status.lower() == 'status': status = alarm.get('health_note', 'Needs Attention') statusStyle = 'statusFailure' collapse = False note = alarm.get('trigger_reason', '') if alarmId in notesMap: special = notesMap[alarmId] if type(special) is MethodType: note = special(alarm) elif type(special) is FunctionType: note = special(self.mgmt, alarm) elif type(special) is StringType: note = note + special el = self.doc.createElement('alarm') el.setAttribute('id', alarmId) el.setAttribute('prettyName', alarm['display_name']) el.setAttribute('status', status) el.setAttribute('description', alarm['description']) el.setAttribute('statusStyle', statusStyle) el.setAttribute('collapse', collapse and 'true' or 'false') if note: noteEl = self.doc.createElement('triggerMessage') noteEl.setAttribute('hasChildAlarms', hasVisibleChildren(alarmId) and 'true' or 'false') noteEl.appendChild(self.doc.createTextNode(note)) el.appendChild(noteEl) parentEl.appendChild(el) xmlizeChildren(el, alarmId) # the 'health' alarm is a top-level container of all the others # (this may need to be tweaked in the future) alarmsEl = self.doc.createElement('alarms') xmlizeChildren(alarmsEl, 'health') self.doc.documentElement.appendChild(alarmsEl) self.writeXmlDoc()
def unregister_nodes(): for node in Nodes.listNodes(): unregister_node(node)
def register_nodes(): for node in Nodes.listNodes(): register_node(node)
def AI_entry(placed_pieces, moves_left, node_owner, recycle=False, trace_mode=False, i=1): start = time.time() # Dictionary simplifies conversion global alphabet2integer valid = False # We have to do something when trace_mode is on. if recycle: while not valid: # Prepare AI input MinMaxObj = Nodes.MinMaxNode(node_pieces=placed_pieces, node_command="root", moves_left=moves_left, node_owner=node_owner) text_input = MinMaxObj.minimax_depth() if trace_mode: MinMaxObj.trace(i) try: var_input = text_input.split() var_input[1], var_input[3], var_input[4], var_input[6] = int( var_input[1]), int(var_input[3]), int(var_input[4]), int( var_input[6]) var_input[0], var_input[2], var_input[5] = alphabet2integer[ var_input[0]], alphabet2integer[ var_input[2]], alphabet2integer[var_input[5]] # recycling move to be in order. i.e. if D 10 D 9 1 D 9 => D 9 D 10 1 D 9 if var_input[1] > var_input[3]: old = text_input.split() text_input = "{} {} {} {} {} {} {}".format( old[2], old[3], old[0], old[1], old[4], old[5], old[6]) elif var_input[1] == var_input[ 3] and var_input[0] > var_input[2]: old = text_input.split() text_input = "{} {} {} {} {} {} {}".format( old[2], old[3], old[0], old[1], old[4], old[5], old[6]) except: print("AI have entered invalid format of input. AI LOSE") exit() print( "Recycle Input format: 'PosX PosY PosX PosY Type PosX PosY':{}" .format(text_input)) # Simplifies check later on if (var_input[0], var_input[1]) is (var_input[2], var_input[3]): print("AI move error: Invalid recycling coordinates. AI LOSE") valid = False exit() elif var_input[4] < 1 or var_input[4] > 8: print("AI move error: No such rotation.AI LOSE") valid = False exit() elif (var_input[4] in [1, 3, 5, 7] and (var_input[5] < 1 or var_input[5] >= 8 or var_input[6] > 12) ) or (var_input[4] in [2, 4, 6, 8] and (var_input[6] < 1 or var_input[6] >= 12 or var_input[5] > 8)): print( "AI move error: You cannot have a block outside of the grid. AI LOSE" ) valid = False exit() next_piece = BoardPiece(pos_x=var_input[5], pos_y=var_input[6], rot_type=var_input[4]) # Look for placed pieces that much recycle coordinates recycle_found = False recycle_index = -1 for piece_index, piece in enumerate(placed_pieces): # Because we know explicitly (0, 1) != (2,3) from previous check, this check is sufficient if (var_input[0], var_input[1]) in {(piece.white_pos_x, piece.white_pos_y), (piece.red_pos_x, piece.red_pos_y)} and \ (var_input[2], var_input[3]) in {(piece.white_pos_x, piece.white_pos_y), (piece.red_pos_x, piece.red_pos_y)}: if piece is placed_pieces[-1]: print( "AI move error: Cannot recycle most recently used piece. AI LOSE" ) exit() else: recycle_found = True recycle_index = piece_index # If a matching piece is found, we make sure the board is legal after removal and after placement if recycle_found: potential_pieces = placed_pieces.copy() old_piece = potential_pieces.pop(recycle_index) old_pieces = potential_pieces.copy() intermediate_grid = list2grid(potential_pieces) potential_pieces.append(next_piece) final_grid = list2grid(potential_pieces) if (next_piece.red_pos_x, next_piece.red_pos_y) == (old_piece.red_pos_x, old_piece.red_pos_y) and \ (next_piece.white_pos_x, next_piece.white_pos_y) == ( old_piece.white_pos_x, old_piece.white_pos_y) and \ (next_piece.side is old_piece.side and next_piece.angle is old_piece.angle): print( "AI move error: Cannot recycle a piece into exact same position. AI LOSE" ) exit() elif isOverlap(next_piece, old_pieces): print( "AI move error: You cannot overlap the piece. AI LOSE") exit() # Move is accepted if both checks are passed elif isLegal(intermediate_grid) and isLegal(final_grid): end = time.time() print("AI computational time: {}".format(end - start)) return potential_pieces, final_grid else: print("AI move error: Resulting move is invalid. AI LOSE") exit() else: print( "AI move error: Could not find piece to recycle. AI LOSE") exit() valid = False else: while not valid: if moves_left > 21 and moves_left < 37: MinMaxObj = Nodes.MinMaxNode(node_pieces=placed_pieces, node_command="root", moves_left=moves_left, node_owner=node_owner, max_depth=4) else: MinMaxObj = Nodes.MinMaxNode(node_pieces=placed_pieces, node_command="root", moves_left=moves_left, node_owner=node_owner) text_input = MinMaxObj.minimax_depth() if trace_mode: MinMaxObj.trace(i) print("Input format: '0 Type PosX PosY':{}".format(text_input)) # Acquire entry, split and convert (where necessary) try: var_input = text_input.split() var_input[1] = int(var_input[1]) var_input[2] = alphabet2integer[var_input[2]] var_input[3] = int(var_input[3]) except: print( "AI move error: You have entered invalid format of input. AI LOSE" ) exit() if var_input[0] is not "0": print( "AI move error: You are still in the regular move, AI LOSE" ) elif var_input[1] < 1 or var_input[1] > 8: print("AI move error: No such rotation.AI LOSE") valid = False exit() elif (var_input[1] in [1, 3, 5, 7] and (var_input[2] < 1 or var_input[2] >= 8 or var_input[3] > 12) ) or (var_input[1] in [2, 4, 6, 8] and (var_input[3] < 1 or var_input[3] >= 12 or var_input[2] > 8)): print( "AI move error: You cannot have a block outside of the grid. AI LOSE" ) valid = False exit() # Note: Board pieces operate on a cartesian plane, (1, 1) is bottom left next_piece = BoardPiece(pos_x=var_input[2], pos_y=var_input[3], rot_type=var_input[1]) if isOverlap(next_piece, placed_pieces): print( "AI move error: Cannot overlap with another piece. AI LOSE" ) valid = False exit() else: placed_pieces.append(next_piece) new_grid = list2grid(placed_pieces) if isLegal(new_grid): valid = True else: valid = False del placed_pieces[-1] print("AI move error: Invalid input. AI LOSE") exit() end = time.time() print("AI computational time: {}".format(end - start)) return placed_pieces, new_grid
def ensure_statlist(node): if not isinstance(node, Nodes.StatListNode): node = Nodes.StatListNode(pos=node.pos, stats=[node]) return node
def sysDetails(self): basePath = self.cmcPolicyRetarget('/rbt/sport/sysdetail/state') fields = self.fields appliance = fields.get('appliance') if appliance: # Get the data from the appliance directly. data = self.mgmt.action( '/cmc/actions/appliance/query', ('appliance', 'string', appliance), ('operation', 'string', 'iterate'), ('flags', 'string', 'subtree'), ('timeout', 'uint32', '30'), ('node', 'string', basePath)) # Pop off the basePath from the keys so we can treeify correctly. for k in data.keys(): v = data.pop(k) if k.startswith(basePath): k = k[len(basePath):] if k.startswith("/"): k = k[1:] if k: data[k] = v data = Nodes.treeifySubtree(data); else: data = Nodes.getTreeifiedSubtree(self.mgmt, basePath); # if sport is not running there won't be a 'sport' subtree so # use an empty dict instead sportModules = data.get('sport') or {} systemModules = data.get('system') or {} result = self.doc.createElement('systemDetails') # Maps to a 'status_map' enum in the backend: # products/rbt_sh/src/bin/cli/modules/python/cli_rbt_sysdetail_cmds.py prettyStatuses = ['OK', 'Warning', 'Error', 'Disabled'] # Takes in a system details data dict and adds each entry as an # attribute to the XML element in a pretty way. # moduleEl - an XML element # moduleName - the name the system module. Due to the design # of the backend, this attribute is not included # in moduleData dict. # moduleData - a data dict with entries that become XML attributes. def parseDataAndAttributizeXml(moduleEl, moduleName, moduleData): moduleEl.setAttribute('name', moduleName) # Formatting module data by replacing newlines with semicolons. # Semicolons get converted to newlines in JS. info = moduleData.get('info', '').replace('\n', ';') # Strip the last newline from info output (this fixes the bug where # an extra <br /> would show up at the end of each module info div) if info[-1:] == ';': info = info[:-1] if not info: # If no details, insert "<None>" message. info = "No details." moduleEl.setAttribute('info', info) statusMsg = prettyStatuses[int(moduleData.get('status', '2'))] moduleEl.setAttribute('status', statusMsg) moduleEl.setAttribute('statusTdClass', statusMsg.lower()) return moduleEl # Sort module names to maintain display order. sysModuleNames = systemModules.keys() sysModuleNames.sort(FormUtils.alphanumericCompare) sportModuleNames = sportModules.keys() sportModuleNames.sort(FormUtils.alphanumericCompare) sortedModuleNames = sysModuleNames + sportModuleNames for moduleName in sortedModuleNames: if moduleName in sysModuleNames: moduleData = systemModules.get(moduleName) else: moduleData = sportModules.get(moduleName) moduleEl = self.doc.createElement('module') moduleEl = parseDataAndAttributizeXml(moduleEl, moduleName, moduleData) # Display additional submodules if 'items' in moduleData: submodules = moduleData.get('items') submoduleNames = submodules.keys() submoduleNames.sort(FormUtils.alphanumericCompare) # Loop through each submodule and add each entry as a child to # the module XML node for subName in submoduleNames: subData = submodules.get(subName) submoduleEl = self.doc.createElement('module') prettyName = subData.get('name', '') submoduleEl = parseDataAndAttributizeXml(submoduleEl, prettyName, subData) moduleEl.appendChild(submoduleEl) result.appendChild(moduleEl) self.doc.documentElement.appendChild(result) self.writeXmlDoc()
def _createImmediateRelsWhereChild(currentNode): """ Create transitive relationships for immeditate relationships where the given node is the child. """ currentOid = currentNode.getOid() # Add relationships from this node to its immediate parents relsWithParents = Relationships.getByChildOidRelType( currentOid, Relationships.PART_OF) for parentRel in relsWithParents: parentOid = parentRel.getParentOid() if not exists(Relationships.PART_OF, descendentOid = currentOid, ancestorOid = parentOid): start, stop = Nodes.getStageWindowForNodeOid(currentOid) DbAccess.writeSql("/* direct */") createRelationshipTransitive( Relationships.PART_OF, descendentOid = currentOid, ancestorOid = parentOid) # Add relationships based on its parents' already defined # transitive relationships. Transitive relationships # do not cross relationship types. ancestorRels = _byDescendentOidRelType.get((parentOid, Relationships.PART_OF)) if ancestorRels != None: for ancestorRel in ancestorRels.itervalues(): ancestorOid = ancestorRel.getAncestorOid() ancestor = Nodes.getByOid(ancestorOid) if not exists(relType = Relationships.PART_OF, descendentOid = currentOid, ancestorOid = ancestorOid): # if reltype is part-of then only generate a transitive # relationship if the ancestor and this node overlap in time. # Group ancestors sometimes do not overlap with their # descendents. ancestorStart, ancestorStop = ( Nodes.getStageWindowForNodeOid(ancestorOid)) if (start.getSequence() > ancestorStop.getSequence() or stop.getSequence() < ancestorStart.getSequence()): if not ancestor.isGroup(): # oops, supposed to be a group Util.fatalError([ "Ancestor stage window does not overlap " + "with descendent stage window and", "ancestor is not a group.", "Ancestor ID: " + ancestor.getPublicId() + " Name: " + ancestor.getName(), " Start-Stop: " + ancestorStart.getName() + "-" + ancestorStop.getName(), "Descendent ID: " + currentNode.getPublicId() + " Name: " + currentNode.getName(), " Start-Stop: " + start.getName() + "-" + stop.getName()]) else: DbAccess.writeSql("/* from ancestor */") createRelationshipTransitive(Relationships.PART_OF, descendentOid = currentOid, ancestorOid = ancestorOid) return
import requests import time import json app = Flask(__name__) from BlockChain import * from Transaction import * from Network import * from Node import * from Nodes import * n0 = Node("0.0.0.0", 1337, str(uuid4()).replace('-', '')) n1 = Node("0.0.0.0", 1338, str(uuid4()).replace('-', '')) nodes = Nodes([n0, n1]) blockchain = BlockChain() blockchain.chain.append(blockchain.genesis()) # add into db # share to other nodes walletAlice = Wallet(blockchain, "Alice") walletBob = Wallet(blockchain, "Bob") @app.route('/valid', methods=['GET']) def valid(): return jsonify(blockchain.is_valid())
def main(): # Global vars used across simulation global box, rect, ax, undividuals, init_state, df, dt, timeSpace, frames #------------------------------------------------------------ # USER DEFINED VALUES FOR SIMULATION. (CHANGE AS YOU LIKE) N = 20 # N amount of nodes for simulation space. # Can have min amount of 20 to work with Ns-3 networking software. length = 1500.0 # Length (float) of square simulation space (meters). size = 2.0 # Size of the vehicle (m) fps = 10 # Frames per second of the video frames = 20 * fps # Length of the video (seconds * fps) maxVel = 30 # Max velocity a node can travel m/s maxAccel = 3 # Max acceleration a node can undergo m/s^2 np.random.seed(0); # Consistent randomness (paradox? lol) cmap = mpl.cm.get_cmap('brg') # colormap used for scatter plot #------------------------------------------------------------ # INTERNAL VARIABLE DECLARATIONS. (DO NOT CHANGE) # Main setup of the node matrix, fit with all of our attributes print("Running...") # Set up Nx8 matrix of values [node, ax, ay, vx, vy, px, py, refX, refY] init_state = np.zeros((N, 8)) # Set random position somewhere in box for first timestep init_state[:, :4] = 0 init_state[:, 4:6] = (-0.50 + np.random.random((N, 2)))*length init_state[:, 6:] = 1 # Dimension of simulation space bounds = [-length/2, length/2, -length/2, length/2] # Timestep width, we need to adjust maxVel and maxAccel to reflect this dt = 1. / fps maxVel = maxVel * dt maxAccel = maxAccel * dt # Create dataframe to hold information we will throw into XML df = pd.DataFrame({"node":[], "time":[], "px":[], "py":[], "speed":[]}) # Create time space timeSpace = np.linspace(0, frames/fps, frames) # definition of the class box = nd.Nodes(init_state, bounds, size=size/100, fps = fps, frames = frames, N = N, maxVel = maxVel, maxAccel = maxAccel) #------------------------------------------------------------ # SETUP FIGURE FOR ANIMATION. (DO NOT CHANGE) # Set up figure for scatter plot fig = plt.figure(figsize=(4,4)) grid = plt.GridSpec(4, 4, hspace=0.05, wspace=0.05, top=0.90, bottom = 0.1, right = 0.98, left = 0.1) ax = fig.add_subplot(grid[:, :], aspect='equal', autoscale_on=False, xlim=(-length/2-0.2, length/2+0.2), ylim=(-length/2-0.4, length/2+0.4)) plt.title(f'Simulated Nodes = {N}') #ax.set_axis_off() # Invert the y axis so 0,0 is at upper left corner. ax = plt.gca() ax.set_ylim(ax.get_ylim()[::-1]) # Undividuals holds the locations for each timestep (change in pos) undividuals = ax.scatter([], [], s = size*3, cmap = cmap, vmin = 0.0, vmax = 1.0) # Rect is artist element for the box edge outline (we turned axis off) rect = plt.Rectangle(box.bounds[::2], box.bounds[1] - box.bounds[0], box.bounds[3] - box.bounds[2], ec='none', lw=2, fc='none') ax.add_patch(rect) # Creating the simulation from this animation function ani = animation.FuncAnimation(fig, animate, frames=frames, interval=10, blit=True)#, init_func=init) # Save the animation as an mp4. This requires ffmpeg or mencoder to be # installed. The extra_args ensure that the x264 codec is used, so that # the video can be embedded in html5.You may need to adjust this for # your system: for more information, see # http://matplotlib.sourceforge.net/api/animation_api.html FFwriter = animation.FFMpegWriter(fps=fps, extra_args=['-vcodec', 'libx264']) ani.save(f'Nodes_{N}.mp4', writer = FFwriter) # Show the plot, not really necesary because we only care about animation. plt.show() # Create a TCL file using the TCLWriter class. TCLWriter.writeToTCL(df, N, mps = 10, fps = fps) # Inform user of output files print("Simulation success! You can expect two output files saved to your working directory:") print("1.) Animation video titled '"+f'Nodes_{N}.mp4'+"'") print("2.) Trace file for use with Ns-3 software titled 'mobility.tcl'")