def get_node(self, req: Node) -> Optional[List[Node]]: builder = QueryBuilder() if req.get_entity() is not None: builder.add(f"MATCH (n:{req.get_entity()}") if req.get_meta() is not None and len(req.get_meta()) > 0: builder.add_meta(req.get_meta()) else: builder.add(f"MATCH (n") if req.get_properties() is not None: properties = dict( (x, y) for x, y in req.get_properties().items() if y != "") if len(properties) > 0: builder.add_parameters(properties) builder.add(")") if req.get_relationships() is not None: counter: int = 0 for key, value in req.get_relationships().items(): if len(value) > 0: builder.add( f"MATCH (n)-[r{counter}:{key}]-(m{counter}) WHERE ID(m{counter}) IN {str(value)}" ) counter += 1 builder.add("RETURN COLLECT(ID(n)) AS ids") records = self._exec_query(builder.get())[0] result_nodes: List[Node] = [] # nodes from result if len(records) > 0: for record in records: result_node = self.get_node_by_id(int(record)) result_nodes.append(result_node[0]) return result_nodes
def update_node(self, req: Node) -> Optional[Node]: if req.get_properties() is not None: properties = dict( (x, y) for x, y in req.get_properties().items() if y != "") if len(properties) > 0: builder = QueryBuilder() builder.match_by_id(req.get_id(), "n").set_values(req.get_properties(), "n").add("RETURN n") _ = self._exec_query(builder.get()) if req.get_relationships() is not None: relationships = dict((x, y) for x, y in req.get_relationships().items() if len(y) > 0) if len(relationships) > 0: builder = QueryBuilder() builder.match_by_id(req.get_id(), "n") counter: int = 0 for _, value in relationships.items(): builder.add( f"MATCH (m{counter}) WHERE ID(m{counter}) IN [{','.join((str(rel) for rel in value))}] " ) counter += 1 counter = 0 for key, _ in relationships.items(): builder.add(f"MERGE (n)-[r{counter}:{key}]-(m{counter})") counter += 1 builder.add("RETURN n") _ = self._exec_query(builder.get()) result_node = self.get_node_by_id(req.get_id())[0] # node from result return result_node
def delete_properties_relationships(self, req: Node) -> bool: response = False if req.get_properties() is not None: properties = dict( (x, y) for x, y in req.get_properties().items() if y != "") if len(properties) > 0: builder = QueryBuilder() builder.match_by_id(req.get_id(), "n") for key, _ in properties.items(): if key != "name": builder.add(f"REMOVE n.{key}") builder.add("RETURN n") result = self._exec_query(builder.get()) if result: response = True if req.get_relationships() is not None: relationships = dict((x, y) for x, y in req.get_relationships().items() if len(y) > 0) if len(relationships) > 0: builder = QueryBuilder() builder.match_by_id(req.get_id(), "n") counter: int = 0 for key, value in relationships.items(): builder.add( f"MATCH (n)-[r{counter}:{key}]-(m{counter}) WHERE ID(m{counter}) IN {value}" ) counter += 1 counter -= 1 builder.add(f"DELETE r{counter}") while counter > 0: counter -= 1 builder.add(f",r{counter}") builder.add("RETURN n") result = self._exec_query(builder.get()) if result: response = True return response
def create_node(self, req: Node) -> Optional[Node]: builder = QueryBuilder() builder.add("CREATE (a:" + req.get_entity()) if req.get_meta() is not None and len(req.get_meta()) > 0: builder.add_meta(req.get_meta()) builder.add_parameters(req.get_properties()).add(") RETURN a") record: Neo4jNode = self._exec_query(builder.get())[0] req.set_id(int(record.id)) return req
def roboyqa(ctx): """ answers question regarding roboy by retrieving the information out of the neo4j roboy memory graph state gets triggered when nlp extracts a new triple: subject, predicate, object by analysing the triple the content of the question can be ascertained the necessary information is gathered using the neo4j memory session if the triple combination is known and the information could be retrieved an answer will be given list of questions that can be answered: - who are you? - what is your name? - how old are you? - what is your age? - what is your hobby? - what are your hobbies? - what do you like? - where are you from? - where do you live? - who is your father/dad? - who is your brother/sibling? - who is your friend? - what do you want to become? - what are you a member of? - what can you do? - what are your skills? - what have you learned? - what are your abilities? """ sess: Session = ravestate_ontology.get_session() onto: Ontology = ravestate_ontology.get_ontology() roboy = Node(metatype=onto.get_type("Robot")) roboy.set_properties(ctx.conf(key=ROBOY_NODE_PROP_CONF_KEY)) node_list = sess.retrieve(request=roboy) if node_list: roboy = node_list[0] else: create_default_nodes() node_list = sess.retrieve(request=roboy) if node_list: roboy = node_list[0] else: logger.error( f"Seems like you do not have my memory running, or no node with properties" f"{ctx.conf(key=ROBOY_NODE_PROP_CONF_KEY)} exists!") return rs.Resign() triple = ctx[nlp.prop_triples][0] category = None memory_info = None # question word: What? if triple.is_question(nlp.QuestionWord.OBJECT): if triple.match_either_lemma(pred={"like"}, subj={"hobby"}): category = "HAS_HOBBY" elif triple.match_either_lemma(pred={"learn"}, subj={"skill"}): category = "skills" elif triple.match_either_lemma(pred={"can"}, subj={"ability"}): category = "abilities" elif triple.match_either_lemma(subj={"age"}): category = "age" memory_info = roboy_age(roboy.get_properties(key="birthdate")) elif triple.match_either_lemma(subj={"name"}): category = "full_name" memory_info = roboy.get_properties(key=category) elif triple.match_either_lemma(pred={"become"}): category = "future" # question word: Where? elif triple.is_question(nlp.QuestionWord.PLACE): if triple.match_either_lemma(pred={"be"}): category = "FROM" elif triple.match_either_lemma(pred={"live"}): category = "LIVE_IN" # question word: Who? elif triple.is_question(nlp.QuestionWord.PERSON): if triple.match_either_lemma(obj={"father", "dad"}): category = "CHILD_OF" elif triple.match_either_lemma(obj={"brother", "sibling"}): category = "SIBLING_OF" elif triple.match_either_lemma(obj={"friend", "girlfriend"}): category = "FRIEND_OF" else: category = "full_name" memory_info = roboy.get_properties(key=category) elif triple.match_either_lemma(obj={"part", "member"}): category = "MEMBER_OF" # question word: How? elif triple.is_question(nlp.QuestionWord.FORM): if triple.match_either_lemma(pred={"old"}): category = "age" memory_info = roboy_age(roboy.get_properties(key="birthdate")) elif triple.match_either_lemma(pred={"be"}): category = "well_being" elif triple.match_either_lemma(obj={"skill"}): category = "skills" elif triple.match_either_lemma(obj={"ability"}): category = "abilities" if category and category.isupper() and not isinstance( roboy.get_relationships(key=category), dict): node_id = random.sample(roboy.get_relationships(key=category), 1)[0] memory_info = sess.retrieve(node_id=int(node_id))[0].get_name() elif category and category.islower() and not isinstance( roboy.get_properties(key=category), dict): property_list = [ x.strip() for x in roboy.get_properties(key=category).split(',') ] memory_info = random.sample(property_list, 1)[0] if memory_info: ctx[rawio.prop_out] = verbaliser.get_random_successful_answer( "roboy_" + category) % memory_info elif category == "well_being": ctx[rawio.prop_out] = verbaliser.get_random_successful_answer( "roboy_" + category) else: return rs.Resign()