def create_relationship(graph, rel_tuple): """ create the new rel in the graph if the rel doesn't exist, create new nodes if necessary :param graph: :param rel_tuple: :return: """ label_1 = str(rel_tuple[0][0]) name_1 = str(rel_tuple[0][1]) label_2 = str(rel_tuple[2][0]) name_2 = str(rel_tuple[2][1]) rel_type = str(rel_tuple[1][0]) rel_name = str(rel_tuple[1][1]) # check dup matcher = NodeMatcher(graph) node_1 = matcher.match(label_1, name=name_1).first() node_2 = matcher.match(label_2, name=name_2).first() if node_1 and node_2: r_matcher = RelationshipMatcher(graph) first_rel = r_matcher.match([node_1, node_2], rel_type, name=rel_name).first() if first_rel: return elif node_1 and not node_2: node_2 = Node(label_2, name=name_2) elif node_2 and not node_1: node_1 = Node(label_1, name=name_1) else: node_1 = Node(label_1, name=name_1) node_2 = Node(label_2, name=name_2) # create rel rel = Relationship(node_1, rel_type, node_2, name=rel_name) graph.create(rel)
def get_rels_from_db(graph): graph = connect_graph() matcher = RelationshipMatcher(graph) relationships = list(matcher.match()) rel_data = [] for rel in relationships: rel_json = { 'direction': rel['direction'], 'length': rel['length'], 'is_tram': rel['is_tram'], 'is_bus': rel['is_bus'], 'is_trolleybus': rel['is_trolleybus'], 'is_connector': rel['is_connector'], 'link_type_id': rel['link_type_id'], 'pedestrian_mode_id': rel['pedestrian_mode_id'], 'move_time': rel['move_time'], 'walking_time': rel['walking_time'], 'null_time': rel['null_time'], 'time_bus': rel['time_bus'], 'time_tram': rel['time_tram'], 'speed': rel['speed'], 'node_id_from': rel.start_node.identity, 'node_id_to': rel.end_node.identity } rel_data.append(rel_json) return rel_data
def main(): MDG = Graph() tx = MDG.begin() node_matcher = NodeMatcher(MDG) rel_matcher = RelationshipMatcher(MDG) all_nodes = node_matcher.match("Artifact") for node in all_nodes: nexts = rel_matcher.match(nodes=(node, None), r_type="NEXT") if len(nexts) <= 1: continue if len(nexts) > 2: exceptions.write(node[coordinates] + ": node has 3 or more NEXT!\n") continue seq_next = [] seq_rel = [] for r in nexts: seq_rel.append(r) i = 0 for n in walk(r): if i == 1: seq_next.append(n) r1, r2 = seq_rel[0], seq_rel[1] n1, n2 = seq_next[0], seq_next[1] if n1["version"] < n2["version"]: tx.separate(r2) else: tx.separate(r1) tx.commit()
def __init__(self): config_str = ''.join(open(CFG_FILE).readlines()).replace('\n', ' ') config = json.loads(config_str) self.graph = Graph(config['url'], name=config['name'], password=config['password']) self.nodematcher = NodeMatcher(self.graph) self.relmatcher = RelationshipMatcher(self.graph)
def getRelations(): start_type = request.args['start_type'] start_node = request.args['start_node'] end_node = request.args['end_node'] end_type = request.args['end_type'] rel_type = request.args['rel_type'] results = [] r_matcher = RelationshipMatcher(DB) # 条件0: 没有给出任何信息 if not rel_type and not start_type and not start_node and not end_node and not end_type: return jsonify({'code': '1001', 'msg': 'no messages'}) # 条件1:没给出开始节点和结束节点的任何信息,但给出关系类型 elif rel_type and not start_type and not start_node and not end_node and not end_type: for re in r_matcher.match(r_type=rel_type): results.append({ 'start_node': re.start_node['name'], 'end_node': re.end_node['name'], 'attr': dict(re) }) return jsonify(results) # 条件2:给出了开始节点或者结束节点 elif start_node or end_node: results = [] # 条件2.1: 并且给出关系类型 if rel_type: matcher = RelationshipMatch(DB, r_type=rel_type) # 条件2.2:没有给出关系类型 else: matcher = RelationshipMatch(DB) # 条件2.3:同时给出了开始节点和结束节点 if start_node and end_node: for re in matcher.where('a.name="%s" AND b.name= "%s"' % (start_node, end_node)): results.append({ 'start_node': re.start_node['name'], 'end_node': re.end_node['name'], 'attr': dict(re) }) # 条件2.4:只存在开始节点,没有结束节点 elif start_node and not end_node: for re in matcher.where('a.name="%s"' % start_node): results.append({ 'start_node': re.start_node['name'], 'end_node': re.end_node['name'], 'attr': dict(re) }) # 条件2.5:只存在结束节点不存在开始节点 elif end_node and not start_node: for re in matcher.where('b.name= "%s"' % end_node): results.append({ 'start_node': re.start_node['name'], 'end_node': re.end_node['name'], 'attr': dict(re) }) return jsonify(results) else: return jsonify({'code': '1002', 'msg': 'error messages'})
def __init__(self): cfg = ConfigParser() cfg.read('%s/../config/neo4j.ini' % currdir) uri = cfg.get('db', 'uri') username = cfg.get('db', 'username') password = cfg.get('db', 'password') self.graph = Graph(uri=uri, auth=(username, password)) self.matcher = NodeMatcher(self.graph) self.relation_matcher = RelationshipMatcher(self.graph)
def returnPodcasts(): # Connect to database graph = Graph("http://*****:*****@localhost:7474") nodematcher = NodeMatcher(graph) relamatcher = RelationshipMatcher(graph) # Find the nodes for the cast members and the podcast. castNodes = nodematcher.match("CAST") podcastNodes = nodematcher.match("PODCAST") nodes = [] # Convert the nodes to GraphJSON format for Alchemy.js for node in castNodes: nodes.append({ "id": node.identity, "name": node["name"], "rta": node["rta"], "tp": node["tp"], "ao": node["ao"] }) for node in podcastNodes: nodes.append({ "id": node.identity, "name": node["name"], "rta": node["rta"], "tp": node["tp"], "ao": node["ao"] }) jsonNodes = dumps(nodes) # Find the Relationships within the database relationships = relamatcher.match(None, "APPEARED_ON") edges = [] # Convert the relationships to GraphJSON for rel in relationships: edges.append({ "source": rel.start_node.identity, "target": rel.end_node.identity, "role": "appeared_on" }) jsonEdges = dumps(edges) # Create a json string with all GraphJSON finalJson = '{"nodes": ' + jsonNodes + ', "edges": ' + jsonEdges + '}' # Save file to server cache with open(webserverstatic + 'main.json', "w+") as jsonFile: jsonFile.write(finalJson) jsonFile.close() # Send file to template to display generated graph return render_template('showdata.html', message="main")
def processItem(item): neo4jGraph = item node_labels = neo4jGraph.schema.node_labels relationship_types = neo4jGraph.schema.relationship_types node_matcher = NodeMatcher(neo4jGraph) relationship_matcher = RelationshipMatcher(neo4jGraph) vertices = [] edges = [] nodes = [] for node_label in node_labels: nodes = nodes + (list(node_matcher.match(node_label))) print(nodes) for node in nodes: #Check if they have X, Y, Z coordinates if ('x' in node.keys()) and ('y' in node.keys()) and ( 'z' in node.keys()) or ('X' in node.keys()) and ( 'Y' in node.keys()) and ('Z' in node.keys()): x = node['x'] y = node['y'] z = node['z'] vertex = topologic.Vertex.ByCoordinates(x, y, z) else: vertex = randomVertex(vertices, 1) keys = list(node.keys()) values = [] for key in keys: values.append(node[key]) d = processKeysValues(keys, values) _ = vertex.SetDictionary(d) vertices.append(vertex) for node in nodes: for relationship_type in relationship_types: relationships = list( relationship_matcher.match([node], r_type=relationship_type)) for relationship in relationships: print(" ", relationship.start_node['name'], relationship_type, relationship.end_node['name']) print("Nodes Index:", nodes.index(relationship.start_node)) sv = vertices[nodes.index(relationship.start_node)] ev = vertices[nodes.index(relationship.end_node)] edge = topologic.Edge.ByStartVertexEndVertex(sv, ev) if relationship.start_node['name']: sv_name = relationship.start_node['name'] else: sv_name = 'None' if relationship.end_node['name']: ev_name = relationship.end_node['name'] else: ev_name = 'None' d = processKeysValues(["relationship_type", "from", "to"], [relationship_type, sv_name, ev_name]) if d: _ = edge.SetDictionary(d) edges.append(edge) return topologic.Graph.ByVerticesEdges(vertices, edges)
def node_to_rela(self, node_object): #节点转关系 relmatch = RelationshipMatcher(self.graph) rela = [] for i in range(len(node_object) - 1): temp = (list( relmatch.match(nodes=(node_object[i]['node'], node_object[i + 1]['node'])))) for j in temp: rela.append(j) return rela
def get_triple(cls, id_): """ 返回三元组节点 :rtype: list[Node] :param id_: 节点uuid :return: s节点, p节点, o节点 """ node = NodeMatcher(graph_).match(cls.__name__, id=id_).first() r_matcher = RelationshipMatcher(graph_) r = r_matcher.match((node, None), "HAS_SUBJECT").first() # r.__repr__() print(r) subject_node = r.end_node r_matcher = RelationshipMatcher(graph_) r = r_matcher.match((node, None), "HAS_PREDICATE").first() print(r) predictate_node = r.end_node r_matcher = RelationshipMatcher(graph_) r = r_matcher.match((node, None), "HAS_OBJECT").first() print(r) object_node = r.end_node return subject_node, predictate_node, object_node
def set_predicate(cls, id_, predicate_id): _, _, node = cls.find_by_id(id_) _, _, predicate_node = Predicate.find_by_id(predicate_id) relationship = RelationshipMatcher(graph_).match( (node, ), "HAS_PREDICATE").first() if relationship is not None: old_node = relationship.end_node() graph_.separate(relationship) graph_.delete(old_node) relationship = Relationship(node, "HAS_PREDICATE", predicate_node) graph_.create(relationship)
def get_or_create_relationship(start_node, relationship_type, end_node, **properties): created = False matcher = RelationshipMatcher(graph) relationship = matcher.match(nodes=[start_node, end_node], r_type=relationship_type) if not relationship.exists(): created = True relationship = graph.create( Relationship(start_node, relationship_type, end_node, **properties)) return created, relationship
def __init__(self, auth=('neo4j', 'dbuserdbuser'), host='localhost', port=7687, secure=False, encrypted=False): self._graph = Graph(secure=secure, bolt=True, auth=auth, host=host, port=port) self._node_matcher = NodeMatcher(self._graph) self._relationship_matcher = RelationshipMatcher(self._graph)
def test_rel_index_cache(self): folder = os.path.dirname(os.path.abspath(__file__)) test_driller = CacheDriller(os.path.join(folder, 'cnfg_simple.yml')) test_driller.drill_batch_cache_sequential() # test that all relationships were indexed rel_matcher = RelationshipMatcher(test_driller.graph) all_branch = list(rel_matcher.match(None, "BranchCommit")) assert len(all_branch) == 8 all_authorship = list(rel_matcher.match(None, "Author")) assert len(all_authorship) == 8 all_parent = list(rel_matcher.match(None, "Parent")) assert len(all_parent) == 8 all_updadedfile = list(rel_matcher.match(None, "UpdateFile")) assert len(all_updadedfile) == 9 all_hasmethod = list(rel_matcher.match(None, "Method")) assert len(all_hasmethod) == 5 all_updatemethod = list(rel_matcher.match(None, "UpdateMethod")) assert len(all_updatemethod) == 9 test_driller.clean()
def rate_post(post_id, relation, username): # get current time for time of rating time = get_time() try: # get user node matcher = NodeMatcher(GRAPH) user_node = matcher.match("User", username=username).first() # get post node post_node = matcher.match("Post", post_id=post_id).first() if user_node is not None and post_node is not None: # get relationship matcher rel_matcher = RelationshipMatcher(GRAPH) # check if the user has already liked/disliked the post already_liked = rel_matcher.match([user_node, post_node], 'LIKED').first() already_disliked = rel_matcher.match([user_node, post_node], 'DISLIKED').first() # if the user is trying to rate a post the same way twice, their rating is removed if already_liked is not None and relation == "LIKED": GRAPH.separate(already_liked) return str(True) elif already_disliked is not None and relation == "DISLIKED": GRAPH.separate(already_disliked) return str(True) # if the user is switching their rating, we delete to old one, and create a new one elif already_liked is not None and relation == "DISLIKED": GRAPH.separate(already_liked) elif already_disliked is not None and relation == "LIKED": GRAPH.separate(already_disliked) # create relationship between user and post GRAPH.merge( Relationship(user_node, relation, post_node, rate_time=time), relation) return str(True) else: print("Either the user or the post couldn't be found") return str(False) except Exception as e: print(e) return str(False)
def connect(self): """Instantiates the connection to Neo4j and stores the graph internally. Throws exception if the connection can not pe realized """ try: self.graph = Graph(host=self.config.ct.db_url, user=self.config.ct.db_user, password=self.config.ct.db_pwd, http_port=self.config.ct.port) self.node_matcher = NodeMatcher(self.graph) self.rel_matcher = RelationshipMatcher(self.graph) self.init_miners() except Exception as exc: LG.log_and_raise(exc)
def test_get_all(self): folder = os.path.dirname(os.path.abspath(__file__)) test_driller = Driller(os.path.join(folder, 'cnfg_simple.yml')) test_driller.drill_batch() n_matcher = NodeMatcher(test_driller.graph) r_matcher = RelationshipMatcher(test_driller.graph) f_miner = FileMiner(test_driller.graph, n_matcher, r_matcher) all_files = f_miner.get_all() assert len(all_files) == 6 # get readme file readme = f_miner.query(name='README.MD') assert readme['name'] == 'README.MD' # get file history f_hash = 'f85f4af5b20ddd617f93da13c7789a65fb972e68a8d634d5f253abab' update_history = f_miner.get_change_history(f_hash) assert len(update_history) == 3 # test file get methods current_m = f_miner.get_current_methods(f_hash) assert len(current_m) == 2 test_driller.clean()
def batch_relations(self, rela, relations): tx = self.graph.begin() new_relationships = [] old_relationships = [] for data in relations: entityname1 = data["entityname1"] entityname2 = data["entityname2"] matcher = NodeMatcher(self.graph) node1 = matcher.match(name=entityname1).first() node2 = matcher.match(name=entityname2).first() # print("node-----------", node1, node2) matcher = RelationshipMatcher(self.graph) old_relationship = matcher.match([node1, node2], r_type=rela).first() print("-------old_relationship", old_relationship) if old_relationship is None: relationship = Relationship(node1, rela, node2, score=100) print("-------relationship", relationship) new_relationships.append(relationship) if len(new_relationships) > 0: print("new_relationships--------", new_relationships) sub = Subgraph(relationships=new_relationships) tx.create(sub) tx.commit()
def get_field(cls, entity_name): """ 图中根据实体名称查找字段信息 :param entity_name: 实体名称 :return: {"name": field_node["name"], "field": field_node["field"]},如果找不到返回None """ scores = [] for node in NodeMatcher(graph_).match(cls.__name__): # 遍历计算匹配度 scores.append( [node, cls._get_matching_score(entity_name, node["name"])]) scores.sort(key=lambda x: x[1], reverse=True) top_node, top_score = scores[0] if top_score <= 0.5: # 找不的匹配节点 return None else: if "Alia" in top_node.labels: # TODO:这里可以对关系进行调用,这个别名的连接用了多少次 relationship = RelationshipMatcher(graph_).match( nodes=[None, top_node], r_type="HAS_ALIA").first() field_node = relationship.start_node else: field_node = top_node return {"name": field_node["name"], "field": field_node["field"]}
def __init__(self): self.graph = Graph("bolt://localhost:7687", username="******", password='******') self.node_matcher = NodeMatcher(self.graph) self.rel_matcher = RelationshipMatcher(self.graph) self.type = "" self.name = "" self.disease = disease.Disease()
def dfs(self, *label, **properties): ''' Version 1:dfs 直到没有边为止 doing 有边相连就遍历,除非遍历到函数-ROOT节点,此时不再往上遍历 Version 2:dfs 遍历时候添加条件判断 ''' matcher = NodeMatcher(self.graph) rel_matcher = RelationshipMatcher(self.graph) node = matcher.match(*label, **properties) node_passed = before = list(node) after = list() while before: # 每次进入循环,向外再遍历一层(层次遍历) for now_node in before: rels = rel_matcher.match({now_node}) for rel in rels: start, end = rel.nodes # 此处后面添加,对节点的判断(如到函数根节点时停,节点属性进行判断操作) if start not in node_passed and start.get( "attribute")[-9:] != "java-ROOT": after.append(start) node_passed.append(start) if end not in node_passed: after.append(end) node_passed.append(end) before = after after = list() # 使用字典保存所有出现的标签 all_labels = list() for _ in node_passed: if all_labels: temp = sorted(_.labels) if temp not in all_labels: all_labels.append(temp) else: all_labels.append(sorted(_.labels)) # test all_id = set() for _ in node_passed: all_id.add(_.get("id")) return node_passed, sorted(all_id), all_labels
def getEntityRels(): type = request.args['type'] entity_name = request.args['name'] # 获取特定的节点 node = NodeMatcher(DB).match(type, name=entity_name).first() # 获取该节点的所有关系 rel_matcher = RelationshipMatcher(DB) rels = rel_matcher.match(nodes=set([node, None])) result = [] for re in rels: result.append({ 'start_node': re.start_node, 'end_node': re.end_node, 'path': str(re), 'attr': dict(re) }) return jsonify(result)
def get_intent_relations(self): ''' 获取所有意图关系数据: 34 --> 35 意图之间的层级关系 :return: [(34,35),(56,57)] ''' re_matcher = RelationshipMatcher(self.graph) re_list = list(re_matcher.match(r_type='ex_context')) for _r in re_list: name_to_id = [] _re_unit = tuple(re.findall('\((.*?)\)', str(_r))) # 匹配数字 for _name in _re_unit: _id = self.id_to_name[_name] name_to_id.append(str(_id)) _tuple = tuple(name_to_id) self.re_cols.append(_tuple) print("self.re_cols:",self.re_cols) return self.re_cols
def _createAuthor(pub, names): graph = connectGraph() print(' Creating Authors') for auth in names: matcher = NodeMatcher(graph) aNode = matcher.match("Author", name=auth['name']).first() if aNode is None: aNode = Node('Author', name=auth['name']) path_1 = Path(aNode, 'AUTHORED', pub) graph.create(path_1) else: #Check the relationship rmatcher = RelationshipMatcher(graph) auNode = rmatcher.match((aNode,pub), "AUTHORED").first() if auNode is None: path_1 = Path(aNode, 'AUTHORED', pub) graph.create(path_1)
def list(): qa_pairs = [] matcher = NodeMatcher(graph) rel_match = RelationshipMatcher(graph) r = graph.relationships.match((None, None), "Answer is") if r is not None: for rel in r: qa_pairs.append({rel.start_node['name']: rel.end_node['name']}) return jsonify({"key": qa_pairs})
def __init__(self): self.graph = Graph("bolt://localhost:7687", username="******", password='******') self.node_matcher = NodeMatcher(self.graph) self.rel_matcher = RelationshipMatcher(self.graph) self.search_type = "" self.name = "" self.alpha_order = False
def _createKeywords(pub, keywords): graph = connectGraph() print(' Creating Keywords') for kw in keywords: #Find keywords TODO : Check for duplicate on re-run matcher = NodeMatcher(graph) #pbNode = matcher.match("KEYWORDS", kw.upper(), name=kw.lower()).first() pbNode = matcher.match("Keywords", kw.lower(), name=kw.lower()).first() if pbNode == None: pbNode = Node('Keywords', kw.lower(), name=kw.lower()) path_1 = Path(pub, 'CONTAINS', pbNode) graph.create(path_1) else: #Check for existing relationship rmatcher = RelationshipMatcher(graph) auNode = rmatcher.match((pbNode,pub), "CONTAINS").first() if auNode is None: path_1 = Path(pub, 'CONTAINS', pbNode) graph.create(path_1)
def count_Selected_User_MENTIONS(id_str): if (Neo4jConnection.Neo4jEntry.graph_neo4j == None): #print("Conect neo4j before with Conect_Neo4j() function.") return None else: try: selector = NodeMatcher(Neo4jConnection.Neo4jEntry.graph_neo4j) user = selector.match("Selected_User", id_str=id_str).first() count = 0 if(user!=None): relationship = RelationshipMatcher(Neo4jConnection.Neo4jEntry.graph_neo4j) rels = relationship.match(r_type="MENTIONS",nodes=(None,user)) for rel in rels: if(rel != None): count = count + 1 return count except Exception as e: print(e) return None
def _createReferences(pub, references): graph = connectGraph() print(' Creating References') for ref in references: #Find the Paper details matcher = NodeMatcher(graph) pbNode = matcher.match("Paper", id=ref).first() if pbNode == None: #path_1 = Node('PAPER', 'AMINER', name=ref) pbNode = Node('Paper', id=ref) path_1 = Path(pub, 'REFERENCE', pbNode) graph.create(path_1) else: #Check for existing relationship rmatcher = RelationshipMatcher(graph) auNode = rmatcher.match((pbNode, pub), "REFERENCE").first() if auNode is None: path_1 = Path(pub, 'REFERENCE', pbNode) graph.create(path_1)
def set_object(cls, id_, object_id): _, _, node = cls.find_by_id(id_) _, _, object_node = BaseInterface.find_by_id_in_graph(object_id) if "Subject" not in object_node.labels: object_node.add_label("Subject") relationship = RelationshipMatcher(graph_).match((node, ), "HAS_OBJECT").first() if relationship is not None: graph_.separate(relationship) relationship = Relationship(node, "HAS_OBJECT", object_node) graph_.create(relationship)