def get_partial_graph_for_statement(uid: int, db_issue: Issue, path: str): """ Returns the partial graph where the statement is embedded :param uid: Statement.uid :param db_issue: Issue :param path: Users history :return: dict() """ logger('PartialGraph', 'main with uid {} and path {}'.format(uid, path.split('?')[0])) path = path.split('?')[0] if db_issue and len(path) > 1: path = path.split(db_issue.slug)[1] # if we have a attitude, we are asking for supporting/attacking a conclusion if 'attitude' in path: db_statement = DBDiscussionSession.query(Statement).get(uid) if not db_statement: return get_d3_data(db_issue) uid = db_statement.uid # special case - dont know branche if 'justify' in path and '/dontknow' in path: db_argument = DBDiscussionSession.query(Argument).get(uid) db_premise = DBDiscussionSession.query(Premise).filter_by( premisegroup_uid=db_argument.premisegroup_uid).first() uid = db_premise.statement_uid # if there is no justify, we have an argument if 'justify' not in path and len(path) > 1: db_argument = DBDiscussionSession.query(Argument).get(uid) db_premise = DBDiscussionSession.query(Premise).filter_by( premisegroup_uid=db_argument.premisegroup_uid).first() uid = db_premise.statement_uid db_arguments = get_all_arguments_by_statement(uid) if db_arguments is None or len(db_arguments) == 0: return get_d3_data(db_issue, [DBDiscussionSession.query(Statement).get(uid)], []) current_arg = db_arguments[0] del db_arguments[0] db_positions = __find_position_for_conclusion_of_argument( current_arg, db_arguments, [], []) logger('PartialGraph', 'positions are: ' + str([pos.uid for pos in db_positions])) graph_arg_lists = __climb_graph_down(db_positions) return __return_d3_data(graph_arg_lists, db_issue)
def __return_d3_data(graph_arg_lists, db_issue: Issue): """ :param graph_arg_lists: :param db_issue: :param nickname: :return: """ logger('PartialGraph', 'main') graph_arg_list = [] for key in graph_arg_lists: graph_arg_list += graph_arg_lists[key] graph_arg_list = [ DBDiscussionSession.query(Argument).get(uid) for uid in list(set(graph_arg_list)) ] graph_stat_list = __get_all_statements_for_args(graph_arg_list) graph_stat_list = [ DBDiscussionSession.query(Statement).get(uid) for uid in graph_stat_list ] logger('PartialGraph', 'stat_list: {}'.format([stat.uid for stat in graph_stat_list])) logger('PartialGraph', 'arg_list: {}'.format([arg.uid for arg in graph_arg_list])) return get_d3_data(db_issue, graph_stat_list, graph_arg_list)
def test_get_d3_data(self): ret_dict, error = lib.get_d3_data(self.issue_elektroautos) self.assertFalse(error) self.assertIn('nodes', ret_dict) self.assertIn('edges', ret_dict) self.assertIn('extras', ret_dict) self.assertLess(0, len(ret_dict['nodes'])) self.assertLess(0, len(ret_dict['edges'])) self.assertLess(0, len(ret_dict['extras']))
def test_get_d3_data(self): db_issue = DBDiscussionSession.query(Issue).get(4) ret_dict, error = lib.get_d3_data(db_issue) self.assertFalse(error) self.assertIn('nodes', ret_dict) self.assertIn('edges', ret_dict) self.assertIn('extras', ret_dict) self.assertLess(0, len(ret_dict['nodes'])) self.assertLess(0, len(ret_dict['edges'])) self.assertLess(0, len(ret_dict['extras']))
def get_d3_complete_dump(request): LOG.debug("Creating a complete d3 dump. %s", request.json_body) path = request.json_body.get('path', '') db_issue = request.validated['issue'] graph, error = get_d3_data(db_issue) return_dict = graph return_dict.update({'type': 'complete'}) if not error: return_dict.update({'path': get_path_of_user(request.application_url, path, db_issue)}) return_dict.update({'error': ''}) else: # gets called if the data itself is malicious ui_locales = get_language_from_cookie(request) _t = Translator(ui_locales) error = _t.get(_.internalKeyError) return_dict = {'error': error} return return_dict
def resolve_complete_graph_cypher(self, info, **kwargs) -> str: def dict2cypher(d: dict) -> str: data = [ "{key}: \"{data}\"".format(key=key, data=d[key]) for key in d.keys() ] return "{" + ",".join(data) + "}" def node_to_cypher(node: dict) -> str: data = { 'text': node['label'], 'time': node['timestamp'], 'author': node['author'].get('name', 'unknown') } t = node['type'] if node['type'] != "" else "argument" return f"CREATE ({node['id']}:{t} {dict2cypher(data)})" def edge_to_cypher(edge: dict) -> str: if edge['source'].startswith( 'statement') and edge['target'].startswith('statement'): rtype = "support" if edge["color"] == "greene" else "rebut" return f"CREATE ({edge['source']})-[:{rtype}]->(:argument)-[:conclusion]->({edge['target']})" else: if edge['target'].startswith('argument'): if edge['color'] == "red": if edge['edge_type'] == "arrow": rtype = "undercut" else: rtype = "undermine" else: rtype = "support" else: rtype = "conclusion" return f"CREATE ({edge['source']})-[:{rtype}]->({edge['target']})" graph, _ = get_d3_data(DBDiscussionSession.query(Issue).get(self.uid)) cypher_nodes = [node_to_cypher(node) for node in graph['nodes']] cypher_edges = [edge_to_cypher(edge) for edge in graph['edges']] return " ".join(cypher_nodes + cypher_edges)
def __return_d3_data(graph_arg_lists, db_issue: Issue): """ :param graph_arg_lists: :param db_issue: :return: """ graph_arg_list = [] for key in graph_arg_lists: graph_arg_list += graph_arg_lists[key] graph_arg_list = [ DBDiscussionSession.query(Argument).get(uid) for uid in list(set(graph_arg_list)) ] graph_stat_list = get_all_statements_for_args(graph_arg_list) graph_stat_list = [ DBDiscussionSession.query(Statement).get(uid) for uid in graph_stat_list ] LOG.debug("Stat_list: %s", [stat.uid for stat in graph_stat_list]) LOG.debug("Arg_list: %s", [arg.uid for arg in graph_arg_list]) return get_d3_data(db_issue, graph_stat_list, graph_arg_list)
def resolve_complete_graph(self, info, **kwargs): graph, _ = get_d3_data(DBDiscussionSession.query(Issue).get(self.uid)) return graph