def populate_graph(self): global redis_graph # Construct a graph with the form: # (v1)-[e1]->(v2)-[e2]->(v3) node_props = ['v1', 'v2', 'v3'] nodes = [] for idx, v in enumerate(node_props): node = Node(label="L", properties={"val": v}) nodes.append(node) redis_graph.add_node(node) edge = Edge(nodes[0], "E", nodes[1], properties={"edge_val": ['v1', 'v2']}) redis_graph.add_edge(edge) edge = Edge(nodes[1], "E", nodes[2], properties={"edge_val": ['v2', 'v3']}) redis_graph.add_edge(edge) redis_graph.commit()
def populate_graph(self): global graph nodes = {} # Create entities for idx, p in enumerate(people): node = Node(label="person", properties={"name": p, "val": idx}) graph.add_node(node) nodes[p] = node # Fully connected graph for src in nodes: for dest in nodes: if src != dest: edge = Edge(nodes[src], "know", nodes[dest]) graph.add_edge(edge) for src in nodes: for dest in nodes: if src != dest: edge = Edge(nodes[src], "works_with", nodes[dest]) graph.add_edge(edge) graph.commit() query = """MATCH (a)-[:know]->(b) CREATE (a)-[:know]->(b)""" graph.query(query)
def test_bi_directional_path_functions(self): node0 = Node(node_id=0, label="L1") node1 = Node(node_id=1, label="L1") node2 = Node(node_id=2, label="L1") edge01 = Edge(node0, "R1", node1, edge_id=0, properties={'value': 1}) edge12 = Edge(node1, "R1", node2, edge_id=1, properties={'value': 2}) redis_graph.add_node(node0) redis_graph.add_node(node1) redis_graph.add_node(node2) redis_graph.add_edge(edge01) redis_graph.add_edge(edge12) redis_graph.flush() # Reverse direction edges which are not part of the graph. Read only values. edge10 = Edge(1, "R1", 0, edge_id=0, properties={'value': 1}) edge21 = Edge(2, "R1", 1, edge_id=1, properties={'value': 2}) expected_results = [[[node0, node1, node0], [edge01, edge10], 2], [[node0, node1, node2, node1], [edge01, edge12, edge21], 3], [[node0, node1, node2, node1, node0], [edge01, edge12, edge21, edge10], 4], [[node1, node2, node1], [edge12, edge21], 2], [[node1, node2, node1, node0], [edge12, edge21, edge10], 3]] query = "MATCH p=(:L1)-[:R1*]->(:L1)<-[:R1*]-() RETURN nodes(p), relationships(p), length(p)" query_info = QueryInfo(query = query, description="Tests path functions over bi directional variable length paths", \ expected_result = expected_results) self._assert_resultset_and_expected_mutually_included( redis_graph.query(query), query_info)
def worstcase(r: Redis): g = Graph('simple_2', r) v0 = Node(properties={'name': 'v0'}) v1 = Node(properties={'name': 'v1'}) v2 = Node(properties={'name': 'v2'}) v3 = Node(properties={'name': 'v3'}) v4 = Node(properties={'name': 'v4'}) g.add_node(v0) g.add_node(v1) g.add_node(v2) g.add_node(v3) g.add_node(v4) e1 = Edge(v0, 'A', v1, properties={'name': 'r1'}) e2 = Edge(v1, 'A', v2, properties={'name': 'r2'}) e3 = Edge(v2, 'A', v0, properties={'name': 'r3'}) e4 = Edge(v0, 'B', v3, properties={'name': 'r5'}) e5 = Edge(v3, 'B', v0, properties={'name': 'r6'}) g.add_edge(e1) g.add_edge(e2) g.add_edge(e3) g.add_edge(e4) g.add_edge(e5) g.commit() return g
def test_bi_directional_path(self): node0 = Node(node_id=0, label="L1") node1 = Node(node_id=1, label="L1") node2 = Node(node_id=2, label="L1") edge01 = Edge(node0, "R1", node1, edge_id=0, properties={'value': 1}) edge12 = Edge(node1, "R1", node2, edge_id=1, properties={'value': 2}) redis_graph.add_node(node0) redis_graph.add_node(node1) redis_graph.add_node(node2) redis_graph.add_edge(edge01) redis_graph.add_edge(edge12) redis_graph.flush() # Reverse direction edges which are not part of the graph. Read only values. edge10 = Edge(1, "R1", 0, edge_id = 0 , properties={'value':1}) edge21 = Edge(2, "R1", 1, edge_id = 1 , properties={'value':2}) path010 = Path.new_empty_path().add_node(node0).add_edge(edge01).add_node(node1).add_edge(edge10).add_node(node0) path0121 = Path.new_empty_path().add_node(node0).add_edge(edge01).add_node(node1).add_edge(edge12) \ .add_node(node2).add_edge(edge21).add_node(node1) path01210 = Path.new_empty_path().add_node(node0).add_edge(edge01).add_node(node1).add_edge(edge12) \ .add_node(node2).add_edge(edge21).add_node(node1).add_edge(edge10).add_node(node0) path121 = Path.new_empty_path().add_node(node1).add_edge(edge12).add_node(node2).add_edge(edge21).add_node(node1) path1210 = Path.new_empty_path().add_node(node1).add_edge(edge12).add_node(node2).add_edge(edge21) \ .add_node(node1).add_edge(edge10).add_node(node0) expected_results=[[path010], [path0121], [path01210], [path121], [path1210]] query = "MATCH p=(:L1)-[:R1*]->(:L1)<-[:R1*]-() RETURN p" query_info = QueryInfo(query = query, description="Tests bi directional variable length paths", \ expected_result = expected_results) self._assert_resultset_and_expected_mutually_included(redis_graph.query(query), query_info)
def test_simple_path(self): node0 = Node(node_id=0, label="L1") node1 = Node(node_id=1, label="L1") node2 = Node(node_id=2, label="L1") edge01 = Edge(node0, "R1", node1, edge_id=0, properties={'value': 1}) edge12 = Edge(node1, "R1", node2, edge_id=1, properties={'value': 2}) redis_graph.add_node(node0) redis_graph.add_node(node1) redis_graph.add_node(node2) redis_graph.add_edge(edge01) redis_graph.add_edge(edge12) redis_graph.flush() # Rewrite the edges with IDs instead of node values to match how they are returned. edge01 = Edge(0, "R1", 1, edge_id=0, properties={'value': 1}) edge12 = Edge(1, "R1", 2, edge_id=1, properties={'value': 2}) path01 = Path.new_empty_path().add_node(node0).add_edge( edge01).add_node(node1) path12 = Path.new_empty_path().add_node(node1).add_edge( edge12).add_node(node2) expected_results = [[path01], [path12]] query = "MATCH p=(:L1)-[:R1]->(:L1) RETURN p" query_info = QueryInfo(query=query, description="Tests simple paths", expected_result=expected_results) self._assert_resultset_and_expected_mutually_included( redis_graph.query(query), query_info)
def test_variable_length_path(self): node0 = Node(node_id=0, label="L1") node1 = Node(node_id=1, label="L1") node2 = Node(node_id=2, label="L1") edge01 = Edge(node0, "R1", node1, edge_id=0, properties={'value': 1}) edge12 = Edge(node1, "R1", node2, edge_id=1, properties={'value': 2}) redis_graph.add_node(node0) redis_graph.add_node(node1) redis_graph.add_node(node2) redis_graph.add_edge(edge01) redis_graph.add_edge(edge12) redis_graph.flush() path01 = Path.new_empty_path().add_node(node0).add_edge( edge01).add_node(node1) path12 = Path.new_empty_path().add_node(node1).add_edge( edge12).add_node(node2) path02 = Path.new_empty_path().add_node(node0).add_edge( edge01).add_node(node1).add_edge(edge12).add_node(node2) expected_results = [[path01], [path12], [path02]] query = "MATCH p=(:L1)-[:R1*]->(:L1) RETURN p" query_info = QueryInfo(query=query, description="Tests variable length paths", expected_result=expected_results) self._assert_resultset_and_expected_mutually_included( redis_graph.query(query), query_info)
def populate_graph(self): # Construct a graph with the form: # (v1)-[:E]->(v2)-[:E]->(v3)-[:E]->(v4), (v1)-[:E]->(v5)-[:E2]->(v4) global nodes for v in range(1, 6): node = Node(label="L", properties={"v": v}) nodes.append(node) redis_graph.add_node(node) edge = Edge(nodes[0], "E", nodes[1]) redis_graph.add_edge(edge) edge = Edge(nodes[1], "E", nodes[2]) redis_graph.add_edge(edge) edge = Edge(nodes[2], "E", nodes[3]) redis_graph.add_edge(edge) edge = Edge(nodes[0], "E", nodes[4]) redis_graph.add_edge(edge) edge = Edge(nodes[4], "E2", nodes[3]) redis_graph.add_edge(edge) redis_graph.commit()
def create_simple(r: Redis): g = Graph('simple', r) v0 = Node(label='v0', properties={'name': 'v0'}) v1 = Node(label='v1', properties={'name': 'v1'}) v2 = Node(label='v2', properties={'name': 'v2'}) v3 = Node(label='v3', properties={'name': 'v3'}) v4 = Node(label='v4', properties={'name': 'v4'}) g.add_node(v0) g.add_node(v1) g.add_node(v2) g.add_node(v3) g.add_node(v4) e1 = Edge(v0, 'r0', v1, properties={'name': 'r0'}) e2 = Edge(v1, 'r1', v2, properties={'name': 'r1'}) e3 = Edge(v2, 'r0', v3, properties={'name': 'r0'}) e4 = Edge(v3, 'r1', v4, properties={'name': 'r1'}) g.add_edge(e1) g.add_edge(e2) g.add_edge(e3) g.add_edge(e4) g.commit() return g
def test07_transposed_multi_hop(self): redis_con = self.env.getConnection() g = Graph("tran_multi_hop", redis_con) # (a)-[R]->(b)-[R]->(c)<-[R]-(d)<-[R]-(e) a = Node(properties={"val": 'a'}) b = Node(properties={"val": 'b'}) c = Node(properties={"val": 'c'}) d = Node(properties={"val": 'd'}) e = Node(properties={"val": 'e'}) g.add_node(a) g.add_node(b) g.add_node(c) g.add_node(d) g.add_node(e) ab = Edge(a, "R", b) bc = Edge(b, "R", c) ed = Edge(e, "R", d) dc = Edge(d, "R", c) g.add_edge(ab) g.add_edge(bc) g.add_edge(ed) g.add_edge(dc) g.flush() q = """MATCH (a)-[*2]->(b)<-[*2]-(c) RETURN a.val, b.val, c.val ORDER BY a.val, b.val, c.val""" actual_result = g.query(q) expected_result = [['a', 'c', 'a'], ['a', 'c', 'e'], ['e', 'c', 'a'], ['e', 'c', 'e']] self.env.assertEquals(actual_result.result_set, expected_result)
def test04_repeated_edges(self): graphname = "repeated_edges" g = Graph(graphname, redis_con) src = Node(label='p', properties={'name': 'src'}) dest = Node(label='p', properties={'name': 'dest'}) edge1 = Edge(src, 'e', dest, properties={'val': 1}) edge2 = Edge(src, 'e', dest, properties={'val': 2}) g.add_node(src) g.add_node(dest) g.add_edge(edge1) g.add_edge(edge2) g.commit() # Verify the new edge q = """MATCH (a)-[e]->(b) RETURN e.val, a.name, b.name ORDER BY e.val""" actual_result = g.query(q) expected_result = [[ edge1.properties['val'], src.properties['name'], dest.properties['name'] ], [ edge2.properties['val'], src.properties['name'], dest.properties['name'] ]] assert (actual_result.result_set == expected_result) # Save RDB & Load from RDB redis_con.execute_command("DEBUG", "RELOAD") # Verify that the latest edge was properly saved and loaded actual_result = g.query(q) assert (actual_result.result_set == expected_result)
def test07_test_edge_filters(self): node0 = Node(node_id=0, label="L", properties={'x': 'a'}) node1 = Node(node_id=1, label="L", properties={'x': 'b'}) node2 = Node(node_id=2, label="L", properties={'x': 'c'}) edge01 = Edge(src_node=node0, dest_node=node1, relation="R", properties={'x': 1}) edge12 = Edge(src_node=node1, dest_node=node2, relation="R") redis_graph.add_node(node0) redis_graph.add_node(node1) redis_graph.add_node(node2) redis_graph.add_edge(edge01) redis_graph.add_edge(edge12) redis_graph.flush() query = "MATCH (n:L) WHERE (n)-[:R {x:1}]->() RETURN n.x" result_set = redis_graph.query(query) expected_results = [['a']] query_info = QueryInfo( query=query, description="Tests pattern filter edge conditions", expected_result=expected_results) self._assert_resultset_and_expected_mutually_included( result_set, query_info)
def main(): r = redis.Redis(decode_responses=True) r.delete('social') redis_graph = Graph('social', r) john = Node(label='person', properties={'name':'John Doe', 'age': 33, 'gender':'male', 'status': 'single'}) redis_graph.add_node(john) japan = Node(label='country', properties={'name':'Japan'}) redis_graph.add_node(japan) edge_john = Edge(john, 'visited', japan, properties={'purpose':'pleasure'}) redis_graph.add_edge(edge_john) pearl = Node(label='person', properties={'name':'Pearl White', 'age':25, 'gender':'female', 'status':'married'}) redis_graph.add_node(pearl) australia = Node(label='country', properties={'name':'Australia'}) redis_graph.add_node(australia) edge_pearl = Edge(pearl, 'visited', australia, properties={'purpose':'business'}) redis_graph.add_edge(edge_pearl) mary = Node(label='person', properties={'name':'Mary Mueller', 'age':45, 'gender':'divers', 'status':'divers'}) redis_graph.add_node(mary) germany = Node(label='country', properties={'name':'Germany'}) redis_graph.add_node(germany) edge_mary = Edge(mary, 'visited', germany, properties={'purpose':'business'}) redis_graph.add_edge(edge_mary) redis_graph.commit() for i in ['pleasure', 'business']: print('==== Purpose: {} ===='.format(i)) query = '''MATCH (p:person)-[v:visited {{purpose:"{}"}}]->(c:country) RETURN p.name, p.age, p.status, c.name'''.format(i) result = redis_graph.query(query) print_res(result)
def test14_post_deletion_traversal_directions(self): self.env.flush() redis_con = self.env.getConnection() redis_graph = Graph("G", redis_con) nodes = {} # Create entities. labels = ["Dest", "Src", "Src2"] for idx, l in enumerate(labels): node = Node(label=l, properties={"val": idx}) redis_graph.add_node(node) nodes[l] = node edge = Edge(nodes["Src"], "R", nodes["Dest"]) redis_graph.add_edge(edge) edge = Edge(nodes["Src2"], "R", nodes["Dest"]) redis_graph.add_edge(edge) redis_graph.commit() # Delete a node. query = """MATCH (n:Src2) DELETE n""" actual_result = redis_graph.query(query) self.env.assertEquals(actual_result.nodes_deleted, 1) self.env.assertEquals(actual_result.relationships_deleted, 1) query = """MATCH (n1:Src)-[*]->(n2:Dest) RETURN COUNT(*)""" actual_result = redis_graph.query(query) expected_result = [[1]] self.env.assertEquals(actual_result.result_set, expected_result) # Perform the same traversal, this time traveling from destination to source. query = """MATCH (n1:Src)-[*]->(n2:Dest {val: 0}) RETURN COUNT(*)""" actual_result = redis_graph.query(query) expected_result = [[1]] self.env.assertEquals(actual_result.result_set, expected_result)
def test11_bidirectional_multiple_edge_type(self): # Construct a simple graph: # (a)-[E1]->(b), (c)-[E2]->(d) g = Graph("multi_edge_type", redis_con) a = Node(properties={'val': 'a'}) b = Node(properties={'val': 'b'}) c = Node(properties={'val': 'c'}) d = Node(properties={'val': 'd'}) g.add_node(a) g.add_node(b) g.add_node(c) g.add_node(d) ab = Edge(a, "E1", b) cd = Edge(c, "E2", d) g.add_edge(ab) g.add_edge(cd) g.flush() query = """MATCH (a)-[:E1|:E2]-(z) RETURN a.val, z.val ORDER BY a.val, z.val""" actual_result = g.query(query) expected_result = [['a', 'b'], ['b', 'a'], ['c', 'd'], ['d', 'c']] self.env.assertEquals(actual_result.result_set, expected_result)
def dump_data(n_records): r = redis.Redis(host='localhost', port=6379) data = data_gen(n_records) redis_graph = Graph('file_activity1', r) nodes = {} edges = {} pprint(data) for rec in data: _node = Node(label='file', properties={ 'fid': rec['_id'], 'name': rec['name'], 'date_added': rec['date_added'], 'platform': rec['platform'] }) r.set(rec['_id'], _node.alias) redis_graph.add_node(_node) nodes[rec['_id']] = _node for rec in data: for fileid, time_stamp in rec['downloaded']: edge = Edge(nodes[rec['_id']], 'DOWNLOADED', nodes[fileid], properties={ 'time': time_stamp, 'activity': 'downloaded' }) redis_graph.add_edge(edge) for fileid, time_stamp in rec['executed']: edge = Edge(nodes[rec['_id']], 'EXECUTED', nodes[fileid], properties={ 'time': time_stamp, 'activity': 'executed' }) redis_graph.add_edge(edge) for fileid, time_stamp in rec['removed']: edge = Edge(nodes[rec['_id']], 'REMOVED', nodes[fileid], properties={ 'time': time_stamp, 'activity': 'removed' }) redis_graph.add_edge(edge) redis_graph.commit() print("Graph created")
def populate_graph(self): global graph3 a = Node() b = Node() c = Node() graph3.add_node(a) graph3.add_node(b) graph3.add_node(c) graph3.add_edge(Edge(a, "know", b)) graph3.add_edge(Edge(a, "know", b)) graph3.add_edge(Edge(a, "know", c)) graph3.commit()
def generate_redisgraph_integration_data(client): """Create Redisgraph test DB from the standard integration data.""" vertex_values, edge_values, _ = get_integration_data() client.query("create (n)") client.query("match (n) delete n") uuid_to_node = {} for vertex_name, vertices in six.iteritems(vertex_values): for vertex_props in vertices: # NOTE(bojanserafimov): Dates and datetimes are not supported in redisgraph, # so we just omit them from the dataset. uuid_to_node[vertex_props["uuid"]] = Node( label=vertex_name, properties={ key: value for key, value in six.iteritems(vertex_props) if not isinstance(value, (datetime.date, datetime.datetime)) }, ) client.add_node(uuid_to_node[vertex_props["uuid"]]) for edge_name, edges in six.iteritems(edge_values): for edge_spec in edges: client.add_edge( Edge( uuid_to_node[edge_spec["from_uuid"]], edge_name, uuid_to_node[edge_spec["to_uuid"]], ) ) client.commit()
def new_edge(self, from_node, to_node): return Edge(from_node, types[edge_ctr % 2], to_node, properties={'unique': edge_ctr, 'group': random.choice(groups), 'doubleval': round(random.uniform(-1, 1), 2), 'intval': random.randint(1, 10000), 'stringval': ''.join(random.choice(string.ascii_lowercase) for x in range(6))})
def test_v6_decode(self): graph_name = "v6_rdb_restore" # dump created with the following query (v6 supported property value: integer, double, boolean, string, null, array) # graph.query g "CREATE (:L1 {val:1, strval: 'str', numval: 5.5, nullval: NULL, boolval: true, array: [1,2,3]})-[:E{val:2}]->(:L2{val:3})" # graph.query g "CREATE INDEX ON :L1(val)" # dump g v6_rdb = b"\a\x81\x82\xb6\xa9\x85\xd6\xadh\x06\x05\x02g\x00\x02\x06\x05\x04val\x00\x05\astrval\x00\x05\anumval\x00\x05\bnullval\x00\x05\bboolval\x00\x05\x06array\x00\x02\x02\x02\x00\x05\x03L1\x00\x02\x01\x02\x00\x05\x04val\x00\x02\x01\x05\x03L2\x00\x02\x00\x02\x01\x02\x00\x05\x02E\x00\x02\x00\x02\x02\x02\x01\x02\x00\x02\x06\x05\x04val\x00\x02`\x00\x02\x01\x05\astrval\x00\x02H\x00\x05\x04str\x00\x05\anumval\x00\x02\x80\x00\x00@\x00\x04\x00\x00\x00\x00\x00\x00\x16@\x05\bnullval\x00\x02\x80\x00\x00\x80\x00\x05\bboolval\x00\x02P\x00\x02\x01\x05\x06array\x00\x02\b\x02\x03\x02`\x00\x02\x01\x02`\x00\x02\x02\x02`\x00\x02\x03\x02\x01\x02\x01\x02\x01\x05\x04val\x00\x02`\x00\x02\x03\x02\x01\x02\x00\x02\x01\x02\x00\x02\x01\x05\x04val\x00\x02`\x00\x02\x02\x00\t\x00\xd9\r\xb4c\xf2Z\xd9\xb3" redis_con.restore(graph_name, 0, v6_rdb, True) redis_graph = Graph(graph_name, redis_con) node0 = Node(node_id=0, label='L1', properties={ 'val': 1, 'strval': 'str', 'numval': 5.5, 'boolval': True, 'array': [1, 2, 3] }) node1 = Node(node_id=1, label='L2', properties={'val': 3}) edge01 = Edge(src_node=0, relation='E', dest_node=1, edge_id=0, properties={'val': 2}) results = redis_graph.query("MATCH (n)-[e]->(m) RETURN n, e, m") self.env.assertEqual(results.result_set, [[node0, edge01, node1]]) plan = redis_graph.execution_plan("MATCH (n:L1 {val:1}) RETURN n") self.env.assertIn("Index Scan", plan) results = redis_graph.query("MATCH (n:L1 {val:1}) RETURN n") self.env.assertEqual(results.result_set, [[node0]])
def test15_update_deleted_entities(self): self.env.flush() redis_con = self.env.getConnection() redis_graph = Graph("delete_test", redis_con) src = Node() dest = Node() edge = Edge(src, "R", dest) redis_graph.add_node(src) redis_graph.add_node(dest) redis_graph.add_edge(edge) redis_graph.flush() # Attempt to update entities after deleting them. query = """MATCH (a)-[e]->(b) DELETE a, b SET a.v = 1, e.v = 2, b.v = 3""" actual_result = redis_graph.query(query) self.env.assertEquals(actual_result.nodes_deleted, 2) self.env.assertEquals(actual_result.relationships_deleted, 1) # No properties should be set. # (Note that this behavior is left unspecified by Cypher.) self.env.assertEquals(actual_result.properties_set, 0) # Validate that the graph is empty. query = """MATCH (a) RETURN a""" actual_result = redis_graph.query(query) expected_result = [] self.env.assertEquals(actual_result.result_set, expected_result)
def create_friends(r: Redis): g = Graph('friends', r) adam = Node(label='User', properties={'name': 'Adam', 'mail': 'f****r'}) pernilla = Node(label='User', properties={'name': 'Pernilla'}) david = Node(label='User', properties={'name': 'David'}) g.add_node(adam) g.add_node(pernilla) g.add_node(david) g.add_edge(Edge(adam, 'FRIEND', pernilla)) g.add_edge(Edge(pernilla, 'FRIEND', david)) g.commit() return g
def populate_graph(self, graph_name): # quick return if graph already exists if redis_con.exists(graph_name): return redis_graph people = ["Roi", "Alon", "Ailon", "Boaz", "Tal", "Omri", "Ori"] visits = [("Roi", "USA"), ("Alon", "Israel"), ("Ailon", "Japan"), ("Boaz", "United Kingdom")] countries = ["Israel", "USA", "Japan", "United Kingdom"] redis_graph = Graph(graph_name, redis_con) personNodes = {} countryNodes = {} # create nodes for p in people: person = Node(label="person", properties={ "name": p, "height": random.randint(160, 200) }) redis_graph.add_node(person) personNodes[p] = person for p in countries: country = Node(label="country", properties={ "name": p, "population": random.randint(100, 400) }) redis_graph.add_node(country) countryNodes[p] = country # create edges for v in visits: person = v[0] country = v[1] edge = Edge(personNodes[person], 'visit', countryNodes[country], properties={'purpose': 'pleasure'}) redis_graph.add_edge(edge) redis_graph.commit() # delete nodes, to introduce deleted item within our datablock query = """MATCH (n:person) WHERE n.name = 'Roi' or n.name = 'Ailon' DELETE n""" redis_graph.query(query) query = """MATCH (n:country) WHERE n.name = 'USA' DELETE n""" redis_graph.query(query) # create indices actual_result = redis_con.execute_command( "GRAPH.QUERY", graph_name, "CREATE INDEX ON :person(name, height)") actual_result = redis_con.execute_command( "GRAPH.QUERY", graph_name, "CREATE INDEX ON :country(name, population)") return redis_graph
def addEdge(self, source_node, predicate, dest_node): """Add edge between nodes in redis graph :param source_node: source node of the edge. :param predicate: relationship between the source and destination node :param dest_node: destination node of the edge. """ edge = Edge(source_node, predicate, dest_node) self.redis_graph.add_edge(edge)
def populate_graph(self): global nodes # Construct a graph with the form: # (v1)-[:E1]->(v2)-[:E2]->(v3), (v4) node_props = ['v1', 'v2', 'v3', 'v4'] for idx, v in enumerate(node_props): node = Node(label="L", properties={"v": v}) nodes[v] = node redis_graph.add_node(node) edge = Edge(nodes['v1'], "E1", nodes['v2']) redis_graph.add_edge(edge) edge = Edge(nodes['v2'], "E2", nodes['v3']) redis_graph.add_edge(edge) redis_graph.flush()
def test06_test_level_2_nesting_logical_operators_over_path_filters(self): node0 = Node(node_id=0, label="L") node1 = Node(node_id=1, label="L", properties={'x':1}) node2 = Node(node_id=2, label="L2") edge01 = Edge(src_node=node0, dest_node=node1, relation="R") edge12 = Edge(src_node=node1, dest_node=node2, relation="R2") redis_graph.add_node(node0) redis_graph.add_node(node1) redis_graph.add_node(node2) redis_graph.add_edge(edge01) redis_graph.add_edge(edge12) redis_graph.flush() query = "MATCH (n:L) WHERE (n)-[:R]->(:L) OR (n.x=1 AND ((n)-[:R2]->(:L2) OR (n)-[:R]->(:L))) RETURN n" result_set = redis_graph.query(query) expected_results = [[node0],[node1]] query_info = QueryInfo(query = query, description="Tests AND condition with simple filter and nested OR", expected_result = expected_results) self._assert_resultset_and_expected_mutually_included(result_set, query_info)
def post(self): """Add or Update File Meta Data and Changes Relations Accordingly""" print(api.payload) _node = Node(label='file', properties={ 'fid': api.payload['_id'], 'name': api.payload['name'], 'date_added': api.payload['date_added'], 'platform': api.payload['platform'] }) redis_graph.add_node(_node) redis_graph.commit() for fileid, time_stamp in api.payload['downloaded']: edge = Edge(api.payload['_id'], 'DOWNLOADED', fileid, properties={ 'time': time_stamp, 'activity': 'downloaded' }) redis_graph.add_edge(edge) for fileid, time_stamp in api.payload['executed']: edge = Edge(api.payload['_id'], 'EXECUTED', fileid, properties={ 'time': time_stamp, 'activity': 'executed' }) redis_graph.add_edge(edge) for fileid, time_stamp in api.payload['removed']: edge = Edge(api.payload['_id'], 'REMOVED', fileid, properties={ 'time': time_stamp, 'activity': 'removed' }) redis_graph.add_edge(edge) return {'status': 'OK'}
def model_to_edge(self, instance: EdgeModel, auto_add=False): in_node = self.model_to_node_dict[instance.get_in_node()] out_node = self.model_to_node_dict[instance.get_out_node()] edge = Edge(in_node, instance.get_relationship(), out_node, properties=instance.get_properties()) if auto_add and self.graph: self.graph.add_edge(edge) return edge
def test_path_comparison(self): node0 = Node(node_id=0, label="L1") node1 = Node(node_id=1, label="L1") node2 = Node(node_id=2, label="L1") edge01 = Edge(node0, "R1", node1, edge_id=0, properties={'value': 1}) edge12 = Edge(node1, "R1", node2, edge_id=1, properties={'value': 2}) redis_graph.add_node(node0) redis_graph.add_node(node1) redis_graph.add_node(node2) redis_graph.add_edge(edge01) redis_graph.add_edge(edge12) redis_graph.flush() # Rewrite the edges with IDs instead of node values to match how they are returned. edge01 = Edge(0, "R1", 1, edge_id=0, properties={'value': 1}) edge12 = Edge(1, "R1", 2, edge_id=1, properties={'value': 2}) path01 = Path.new_empty_path().add_node(node0).add_edge( edge01).add_node(node1) path12 = Path.new_empty_path().add_node(node1).add_edge( edge12).add_node(node2) # Test a path equality filter query = "MATCH p1 = (:L1)-[:R1]->(:L1) MATCH p2 = (:L1)-[:R1]->(:L1) WHERE p1 = p2 RETURN p1" expected_results = [[path01], [path12]] query_info = QueryInfo(query=query, description="Test path equality", expected_result=expected_results) self._assert_resultset_and_expected_mutually_included( redis_graph.query(query), query_info) # Test a path inequality filter query = "MATCH p1 = (:L1)-[:R1]->(:L1) MATCH p2 = (:L1)-[:R1]->(:L1) WHERE p1 <> p2 RETURN DISTINCT p1, p2" expected_results = [[path01, path12], [path12, path01]] query_info = QueryInfo(query=query, description="Test path inequality", expected_result=expected_results) self._assert_resultset_and_expected_mutually_included( redis_graph.query(query), query_info)
def populate_graph(self): # Construct a graph with the form: # (a:A)-[:E]->(b:B), (c:C)-[:E]->(b) a = Node(label="A", properties={"v": 1}) graph.add_node(a) b = Node(label="B", properties={"v": 2}) graph.add_node(b) c = Node(label="C", properties={"v": 3}) graph.add_node(c) edge = Edge(a, "E", b) graph.add_edge(edge) edge = Edge(c, "E", b) graph.add_edge(edge) graph.commit()