def servAddrUpdate(params): connection = neo4j.connect("http://localhost:7474") cursor = connection.cursor() cursor.execute("MATCH (n:Node{id:" + str(params["id"]) + '}) SET n.server_ip = "' + params["ip"] + '" ') connection.commit() cursor.execute("MATCH (n:Node{id:" + str(params["id"]) + "}) SET n.server_port = " + str(params["port"]) + " ") connection.commit()
def gpsUpdate(params) : id = params['id'] ip = params['ip'] port = params['port'] connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() cursor.execute('MATCH (n:Node{id:'+str(id)+'}) SET n.server_ip = "' + ip + '" ') connection.commit() cursor.execute('MATCH (n:Node{id:'+str(id)+'}) SET n.server_port = ' + str(port) + ' ') connection.commit() try : cursor.execute('MATCH (n:Node{id:'+str(id)+'})-[rel:IS_CONNECTED]-(r:Node) DELETE rel') connection.commit() except : pass cursor.execute('MATCH (n:Node{id:'+str(id)+'}) SET n.x = n.x + ' + str(random.randrange(10,30)) + ' ') connection.commit() for r in cursor.execute('MATCH (p:Node{id:'+str(id)+'}) RETURN p') : outer = r[0] for p in cursor.execute('MATCH (n:Node) RETURN collect(n)') : for inner in p[0] : dist = math.sqrt( ( inner['x'] - outer['x']) **2 + (inner['y'] - outer['y']) **2 ) if dist < radio_radius : angle,reliability,delay,hc = link_parameters(inner,outer,dist) if inner['x'] > outer['x'] : cursor.execute('MATCH (n:Node {id:'+str(inner['id'])+'}), (r:Node {id:'+str(outer['id'])+'}) CREATE UNIQUE (r)-[:IS_CONNECTED { type:"edge", reliability:' + str(reliability)+ ', delay:"' + str(delay) + '" , to:' + str(inner['id']) +' ,hc:1 , angle:' + str(angle) + ' , from:'+ str(outer['id']) +'}]->(n)') connection.commit() else : cursor.execute('MATCH (n:Node {id:'+str(inner['id'])+'}), (r:Node {id:'+str(outer['id'])+'}) CREATE UNIQUE (n)-[:IS_CONNECTED { reliability:' + str(reliability)+ ', delay:"' + str(delay) + '" , from:' + str(inner['id']) +' ,hc:1 , angle:' + str(angle) + ' , to:'+ str(outer['id']) +'}]->(r)') connection.commit()
def __init__(id) : self.id = id connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() print 'haha' for r in cursor.execute(' MATCH (node:Node{id:' + str(id) + '}) RETURN node ') : print r[0]
def create(self): if self.connection is not None: self.connection.close() self.connection = neo4j.connect("http://" + self.host + ":" + str(self.port)); self.cursor = self.connection.cursor() return self
def getRoute(params): if conf['biDirectional']: query = "MATCH p = (:Node{id:" + str( params['start']) + "})-[r:IS_CONNECTED*1..10]-(:Node{id:" + str( params['end']) + "}) RETURN DISTINCT p" else: if params['end'] > params['start']: query = "MATCH p = (:Node{id:" + str( params['start'] ) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str( params['end']) + "}) RETURN DISTINCT p" else: query = "MATCH p = (:Node{id:" + str( params['end']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str( params['start']) + "}) RETURN DISTINCT p" qosMatrix = [] maxQoS = 0 max_index = 0 minQoS = 100000000000000 min_index = 0 count = 0 Route = [] connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() nodeDetails = {} for r in cursor.execute(query): delay = 0 reliability = 1 hc = 0 temp = [] for link in r[0]: if link['type'] == 'node': temp.append(link['id']) nodeDetails.update({ str(link['id']): { 'server_ip': link['server_ip'], 'server_port': link['server_port'] } }) elif link['type'] == 'edge': delay += float(link['delay']) reliability *= link['reliability'] hc += link['hc'] data = str(temp) Route.append(temp) obj_val = obj_func(delay, hc, reliability) qosMatrix.append([delay, hc, reliability, obj_val]) if obj_val > maxQoS: maxQoS = obj_val max_index = count if obj_val < minQoS: minQoS = obj_val min_index = count count += 1 print 'Route to be followed : ', Route[max_index][1:] reply = nodeDetails[str(Route[max_index][1])] reply.update({'next': Route[max_index][1]}) return json.dumps(reply)
def servAddrUpdate(params): connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() cursor.execute('MATCH (n:Node{id:' + str(params['id']) + '}) SET n.server_ip = "' + params['ip'] + '" ') connection.commit() cursor.execute('MATCH (n:Node{id:' + str(params['id']) + '}) SET n.server_port = ' + str(params['port']) + ' ') connection.commit()
def main(): conn = neo4j.connect("http://localhost:7474") start = time.time() iterations = 1000 for it in xrange(iterations): cursor = conn.cursor() for i in xrange(50): cursor.execute("CREATE (n:User)") conn.commit() delta = time.time() - start print('Tx/s: ' + str(iterations / delta))
def main(): conn = neo4j.connect("http://localhost:7474") conn.authorization('neo4j', 'testing') start = time.time() iterations = 10000 for it in xrange(iterations): cursor = conn.cursor() for i in xrange(50): cursor.execute("CREATE (n:User)") conn.commit() delta = time.time() - start print('Tx/s: ' + str(iterations / delta))
def gpsUpdate(params): connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() try: cursor.execute('MATCH (n:Node{id:' + str(params['id']) + '})-[rel:IS_CONNECTED]-(r:Node) DELETE rel') connection.commit() except: pass cursor.execute('MATCH (n:Node{id:' + str(params['id']) + '}) SET n.x = n.x + ' + str(float(random.randrange(-200, 200)) / 100) + ' ') connection.commit() for r in cursor.execute('MATCH (p:Node{id:' + str(params['id']) + '}) RETURN p'): outer = r[0] try: for n in cursor.execute('MATCH (n:Node) RETURN n '): for inner in n: if inner == outer: pass else: dist = math.sqrt((inner['x'] - outer['x'])**2 + (inner['y'] - outer['y'])**2) if dist < radio_radius: angle, reliability, delay, hc = link_parameters( inner, outer, dist) if inner['x'] > outer['x']: cursor.execute( 'MATCH (n:Node {id:' + str(inner['id']) + '}), (r:Node {id:' + str(outer['id']) + '}) CREATE UNIQUE (r)-[:IS_CONNECTED { type:"edge", reliability:' + str(reliability) + ', delay:"' + str(delay) + '" , to:' + str(inner['id']) + ' ,hc:1 , type:"edge", angle:' + str(angle) + ' , from:' + str(outer['id']) + '}]->(n)') connection.commit() else: cursor.execute( 'MATCH (n:Node {id:' + str(inner['id']) + '}), (r:Node {id:' + str(outer['id']) + '}) CREATE UNIQUE (n)-[:IS_CONNECTED { reliability:' + str(reliability) + ', delay:"' + str(delay) + '" , from:' + str(inner['id']) + ' ,hc:1 , angle:' + str(angle) + ' , type:"edge", to:' + str(outer['id']) + '}]->(r)') connection.commit() connection.close() except: connection.close() pass
def _transaction(self): connection = connect(self.dsn) cursor = connection.cursor() try: yield cursor except self.connection.Error as e: connection.rollback() raise e else: connection.commit() finally: cursor.close() connection.close()
def gpsUpdate(id,addr) : connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() cursor.execute('MATCH (n:Node{id:'+str(id)+'}) SET n.server_ip = "' + addr[0] + '" ') connection.commit() cursor.execute('MATCH (n:Node{id:'+str(id)+'}) SET n.server_port = ' + str(addr[1]) + ' ') connection.commit() while True : cursor.execute('MATCH (n:Node{id:'+str(id)+'}) SET n.x = n.x + ' + str(random.randrange(50,100)) + ' ') connection.commit() time.sleep(1) while True : time.sleep(1)
def test_rollback(self): # Given cursor = self.conn.cursor() cursor.execute("CREATE (n:TestRollback {name:1337})") # When self.conn.rollback() # Then the same cursor should not see it cursor.execute("MATCH (n:TestRollback) RETURN n.name") self.assertEqual(cursor.rowcount, 0) # And other connections should see it cursor = neo4j.connect("http://localhost:7474").cursor() cursor.execute("MATCH (n:TestRollback) RETURN n.name") self.assertEqual(cursor.rowcount, 0)
def gpsUpdate(id, addr): connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() cursor.execute('MATCH (n:Node{id:' + str(id) + '}) SET n.server_ip = "' + addr[0] + '" ') connection.commit() cursor.execute('MATCH (n:Node{id:' + str(id) + '}) SET n.server_port = ' + str(addr[1]) + ' ') connection.commit() while True: cursor.execute('MATCH (n:Node{id:' + str(id) + '}) SET n.x = n.x + ' + str(random.randrange(50, 100)) + ' ') connection.commit() time.sleep(1) while True: time.sleep(1)
def test_commit(self): # Given cursor = self.conn.cursor() cursor.execute("CREATE (n:TestCommit {name:1337})") # When self.conn.commit() # Then other cursors should see it cursor = self.conn.cursor() cursor.execute("MATCH (n:TestCommit) RETURN n.name") self.assertEqual(cursor.fetchone(), (1337, )) # And other connections should see it cursor = neo4j.connect("http://localhost:7474").cursor() cursor.execute("MATCH (n:TestCommit) RETURN n.name") self.assertEqual(cursor.fetchone(), (1337, ))
def test_commit(self): # Given cursor = self.conn.cursor() cursor.execute("CREATE (n:TestCommit {name:1337})") # When self.conn.commit() # Then other cursors should see it cursor = self.conn.cursor() cursor.execute("MATCH (n:TestCommit) RETURN n.name") self.assertEqual(cursor.fetchone(), (1337,)) # And other connections should see it cursor = neo4j.connect("http://localhost:7474").cursor() cursor.execute("MATCH (n:TestCommit) RETURN n.name") self.assertEqual(cursor.fetchone(), (1337,))
def getRoute(params) : if conf['biDirectional'] : query = "MATCH p = (:Node{id:" + str(params['start']) + "})-[r:IS_CONNECTED*1..10]-(:Node{id:" + str(params['end']) + "}) RETURN DISTINCT p" else : if params['end'] > params['start'] : query = "MATCH p = (:Node{id:" + str(params['start']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params['end']) + "}) RETURN DISTINCT p" else : query = "MATCH p = (:Node{id:" + str(params['end']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params['start']) + "}) RETURN DISTINCT p" qosMatrix = [] maxQoS = 0 max_index = 0 minQoS = 100000000000000 min_index = 0 count = 0 Route = [] connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() nodeDetails = {} print 'starting' for r in cursor.execute(query) : delay = 0 reliability = 1 hc = 0 temp = [] for link in r[0] : if link['type'] == 'node' : temp.append(link['id']) #nodeDetails.update({str(link['id']) : {'server_ip':link['server_ip'],'server_port':link['server_port']}}) elif link['type'] == 'edge' : delay += float(link['delay']) reliability *= link['reliability'] hc += link['hc'] print temp data = str(temp) Route.append(temp) obj_val = obj_func( delay , hc , reliability ) qosMatrix.append([ delay , hc , reliability , obj_val ]) if obj_val > maxQoS : maxQoS = obj_val max_index = count if obj_val < minQoS : minQoS = obj_val min_index = count count += 1 print '\nRoute to be followed : ' , Route[max_index][1:]
def getRoute(params) : query = "MATCH p = (:Node{id:" + str(params['start']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params['end']) + "}) RETURN DISTINCT p" qosMatrix = [] maxQoS = 0 max_index = 0 minQoS = 100000000000000 min_index = 0 count = 0 Route = [] connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() nodeDetails = {} for r in cursor.execute(query) : delay = 0 reliability = 1 hc = 0 temp = [] for link in r[0]: if link['type'] == 'node' : temp.append(link['id']) nodeDetails.update({str(link['id']) : {'server_ip':link['server_ip'],'server_port':link['server_port']}}) elif link['type'] == 'edge' : temp.append(link['to']) delay += float(link['delay']) reliability *= link['reliability'] hc += link['hc'] temp = sorted(list(set(temp))) data = str(temp) Route.append(temp) obj_val = obj_func( delay , hc , reliability ) qosMatrix.append([ delay , hc , reliability , obj_val ]) if obj_val > maxQoS : maxQoS = obj_val max_index = count if obj_val < minQoS : minQoS = obj_val min_index = count count += 1 print '\tRoute :\t' , Route[max_index] reply = nodeDetails[str(Route[max_index][1])] reply.update({'next':Route[max_index][1]}) return json.dumps(reply)
def getRoute(params): if conf["biDirectional"]: query = ( "MATCH p = (:Node{id:" + str(params["start"]) + "})-[r:IS_CONNECTED*1..10]-(:Node{id:" + str(params["end"]) + "}) RETURN DISTINCT p" ) else: if params["end"] > params["start"]: query = ( "MATCH p = (:Node{id:" + str(params["start"]) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params["end"]) + "}) RETURN DISTINCT p" ) else: query = ( "MATCH p = (:Node{id:" + str(params["end"]) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params["start"]) + "}) RETURN DISTINCT p" ) qosMatrix = [] maxQoS = 0 max_index = 0 minQoS = 100000000000000 min_index = 0 count = 0 Route = [] connection = neo4j.connect("http://localhost:7474") cursor = connection.cursor() nodeDetails = {} for r in cursor.execute(query): delay = 0 reliability = 1 hc = 0 temp = [] for link in r[0]: if link["type"] == "node": temp.append(link["id"]) nodeDetails.update( {str(link["id"]): {"server_ip": link["server_ip"], "server_port": link["server_port"]}} ) elif link["type"] == "edge": delay += float(link["delay"]) reliability *= link["reliability"] hc += link["hc"] data = str(temp) Route.append(temp) obj_val = obj_func(delay, hc, reliability) qosMatrix.append([delay, hc, reliability, obj_val]) if obj_val > maxQoS: maxQoS = obj_val max_index = count if obj_val < minQoS: minQoS = obj_val min_index = count count += 1 print "Route to be followed : ", Route[max_index][1:] reply = nodeDetails[str(Route[max_index][1])] reply.update({"next": Route[max_index][1]}) return json.dumps(reply)
conf = json.loads(f.read()) reAssign = conf['reAssign'] link_speed = conf['link_speed'] radio_radius = conf['radio_radius'] packet_size = conf['packet_size'] prop_speed_channel = conf['prop_speed_channel'] x_reliability = conf['x_reliability'] x_delay = conf['x_delay'] x_hc = conf['x_hc'] reliability_constant = conf['reliability_constant'] hc_constant = conf['hc_constant'] delay_constant = conf['delay_constant'] db_address = conf['db_address'] connection = neo4j.connect(conf['db_address']) cursor = connection.cursor() def clear(): cursor.execute('MATCH n-[rel:IS_CONNECTED]->r DELETE rel') connection.commit() cursor.execute("MATCH (n:Node) DELETE n") connection.commit() if reAssign: clear() def obj_func(delay, hc, reliability):
def __init__(self, dsn, username=None, password=None): self.dsn = dsn self.connection = connect(dsn, username, password)
def read_json_by_domains(q): with open('loads.json') as f: dumps = [ast.literal_eval(json.loads(i.strip())) for i in f] return [i for i in dumps if q in i['domains']] def bolden(s): return "\033[1m {} \033[0;0m".format(s) def under(s): return "\033[4m {} \033[0;0m".format(s) domainNames = [ "KNOWLEDGE" , "COMPREHENSION" , "APPLICATION" , "ANALYSIS" , "SYNTHESIS" , "EVALUATION"] connection = neo4j.connect("http://localhost:7474") cursor = connection.cursor() s = 'MATCH (d:`Domain`)-[]-(v:`Verb`) WHERE d.OldTerminology = "{}" RETURN v ' domain = { "KNOWLEDGE" : {0} , "COMPREHENSION" : {0} , "APPLICATION" : {0} , "ANALYSIS" : {0} , "SYNTHESIS" : {0} , "EVALUATION" : {0} } for d in domain.keys(): s = s.format(d) x = cursor.execute(s) for i in x: domain[d].add(i[0]['verb']) domain[d].remove(0) s = 'MATCH (d:`Domain`)-[]-(v:`Verb`) WHERE d.OldTerminology = "{}" RETURN v ' with open("NewDomainVector.txt",'w') as fp: fp.write(repr(domain)) '''
def setUp(self): self.conn = neo4j.connect("http://localhost:7474")
def setUp(self): self.conn = neo4j.connect("http://localhost:7474") self.conn.authorization('neo4j', 'testing')
def gpsUpdate(params): connection = neo4j.connect("http://localhost:7474") cursor = connection.cursor() try: cursor.execute("MATCH (n:Node{id:" + str(params["id"]) + "})-[rel:IS_CONNECTED]-(r:Node) DELETE rel") connection.commit() except: pass cursor.execute( "MATCH (n:Node{id:" + str(params["id"]) + "}) SET n.x = n.x + " + str(float(random.randrange(-200, 200)) / 100) + " " ) connection.commit() for r in cursor.execute("MATCH (p:Node{id:" + str(params["id"]) + "}) RETURN p"): outer = r[0] try: for n in cursor.execute("MATCH (n:Node) RETURN n "): for inner in n: if inner == outer: pass else: dist = math.sqrt((inner["x"] - outer["x"]) ** 2 + (inner["y"] - outer["y"]) ** 2) if dist < radio_radius: angle, reliability, delay, hc = link_parameters(inner, outer, dist) if inner["x"] > outer["x"]: cursor.execute( "MATCH (n:Node {id:" + str(inner["id"]) + "}), (r:Node {id:" + str(outer["id"]) + '}) CREATE UNIQUE (r)-[:IS_CONNECTED { type:"edge", reliability:' + str(reliability) + ', delay:"' + str(delay) + '" , to:' + str(inner["id"]) + ' ,hc:1 , type:"edge", angle:' + str(angle) + " , from:" + str(outer["id"]) + "}]->(n)" ) connection.commit() else: cursor.execute( "MATCH (n:Node {id:" + str(inner["id"]) + "}), (r:Node {id:" + str(outer["id"]) + "}) CREATE UNIQUE (n)-[:IS_CONNECTED { reliability:" + str(reliability) + ', delay:"' + str(delay) + '" , from:' + str(inner["id"]) + " ,hc:1 , angle:" + str(angle) + ' , type:"edge", to:' + str(outer["id"]) + "}]->(r)" ) connection.commit() connection.close() except: connection.close() pass
conf = json.loads(f.read()) reAssign = conf['reAssign'] link_speed = conf['link_speed'] radio_radius = conf['radio_radius'] packet_size = conf['packet_size'] prop_speed_channel = conf['prop_speed_channel'] x_reliability = conf['x_reliability'] x_delay = conf['x_delay'] x_hc = conf['x_hc'] reliability_constant = conf['reliability_constant'] hc_constant = conf['hc_constant'] delay_constant = conf['delay_constant'] db_address = conf['db_address'] connection = neo4j.connect(conf['db_address']) cursor = connection.cursor() def clear() : cursor.execute('MATCH n-[rel:IS_CONNECTED]->r DELETE rel') connection.commit() cursor.execute("MATCH (n:Node) DELETE n") connection.commit() if reAssign : clear() def obj_func( delay , hc , reliability ) : return x_reliability * ( reliability / reliability_constant) + x_delay * ( delay_constant / delay ) + x_hc * ( hc_constant / hc )
packet_size = conf['packet_size'] prop_speed_channel = conf['prop_speed_channel'] x_reliability = conf['x_reliability'] x_delay = conf['x_delay'] x_hc = conf['x_hc'] reliability_constant = conf['reliability_constant'] hc_constant = conf['hc_constant'] delay_constant = conf['delay_constant'] db_address = conf['db_address'] def obj_func( delay , hc , reliability ) : return x_reliability * ( reliability / reliability_constant) + x_delay * ( delay_constant / delay ) + x_hc * ( hc_constant / hc ) radio_radius = 250 connection = neo4j.connect('http://localhost:7474') cursor = connection.cursor() def getRoute(params) : if conf['biDirectional'] : query = "MATCH p = (:Node{id:" + str(params['start']) + "})-[r:IS_CONNECTED*1..10]-(:Node{id:" + str(params['end']) + "}) RETURN DISTINCT p" else : if params['end'] > params['start'] : query = "MATCH p = (:Node{id:" + str(params['start']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params['end']) + "}) RETURN DISTINCT p" else : query = "MATCH p = (:Node{id:" + str(params['end']) + "})-[r:IS_CONNECTED*1..10]->(:Node{id:" + str(params['start']) + "}) RETURN DISTINCT p" qosMatrix = [] maxQoS = 0 max_index = 0 minQoS = 100000000000000 min_index = 0