def set_statements_as_new_premisegroup(statements: List[Statement],
                                       db_user: User, db_issue: Issue):
    """
    Set the given statements together as new premise group

    :param statements: [Statement]
    :param db_user: User
    :param db_issue: Issue
    :return: PremiseGroup.uid
    """
    LOG.debug("User: %s, statement: %s, issue: %s", db_user.uid,
              [s.uid for s in statements], db_issue.uid)
    # check for duplicate
    all_groups = []
    for statement in statements:
        # get the premise
        db_premise = DBDiscussionSession.query(Premise).filter_by(
            statement_uid=statement.uid).first()
        if db_premise:
            # getting all groups, where the premise is member
            db_premisegroup = DBDiscussionSession.query(Premise).filter_by(
                premisegroup_uid=db_premise.premisegroup_uid).all()
            groups = set()
            for group in db_premisegroup:
                groups.add(group.premisegroup_uid)
            all_groups.append(groups)
    # if every set in this array has one common member, they are all in the same group
    if len(all_groups) > 0:
        intersec = set.intersection(*all_groups)
        for group in intersec:
            db_premise = DBDiscussionSession.query(Premise).filter_by(
                premisegroup_uid=group).all()
            if len(db_premise) == len(statements):
                return DBDiscussionSession.query(PremiseGroup).get(group)

    premise_group = PremiseGroup(author=db_user.uid)
    DBDiscussionSession.add(premise_group)
    DBDiscussionSession.flush()

    premise_list = []
    for statement in statements:
        premise = Premise(premisesgroup=premise_group.uid,
                          statement=statement.uid,
                          is_negated=False,
                          author=db_user.uid,
                          issue=db_issue.uid)
        premise_list.append(premise)

    DBDiscussionSession.add_all(premise_list)
    DBDiscussionSession.flush()

    db_premisegroup = DBDiscussionSession.query(PremiseGroup).filter_by(
        author_uid=db_user.uid).order_by(PremiseGroup.uid.desc()).first()

    return db_premisegroup
Exemple #2
0
def set_statements_as_new_premisegroup(statements: List[Statement],
                                       db_user: User,
                                       db_issue: Issue) -> PremiseGroup:
    """
    Set the given statements together as new premise group

    Re-uses existing premise groups containing the same statements.

    :param statements: [Statement]
    :param db_user: User
    :param db_issue: Issue
    :return: PremiseGroup
    """
    LOG.debug("User: %s, statement: %s, issue: %s", db_user.uid,
              [s.uid for s in statements], db_issue.uid)
    # check for duplicate
    existing_premisegroup = find_existing_premisegroup(statements)
    if existing_premisegroup is not None:
        return existing_premisegroup

    premise_group = PremiseGroup(author=db_user.uid)
    DBDiscussionSession.add(premise_group)
    DBDiscussionSession.flush()

    premise_list = []
    for statement in statements:
        premise = Premise(premisesgroup=premise_group.uid,
                          statement=statement.uid,
                          is_negated=False,
                          author=db_user.uid,
                          issue=db_issue.uid)
        premise_list.append(premise)

    DBDiscussionSession.add_all(premise_list)
    DBDiscussionSession.flush()

    db_premisegroup = DBDiscussionSession.query(PremiseGroup).filter_by(
        author_uid=db_user.uid).order_by(PremiseGroup.uid.desc()).first()

    return db_premisegroup
Exemple #3
0
    def __merge_premisegroup(db_review: ReviewMerge):
        """
        Merges a premisegroup into the items, which are mapped with the given review

        :param db_review: ReviewSplit.uid
        :return: None
        """
        db_values = DBDiscussionSession.query(ReviewMergeValues).filter_by(
            review_uid=db_review.uid).all()
        db_old_premises = DBDiscussionSession.query(Premise).filter_by(
            premisegroup_uid=db_review.premisegroup_uid).all()
        db_issue = DBDiscussionSession.query(Issue).get(
            db_old_premises[0].issue_uid)
        db_first_old_statement = DBDiscussionSession.query(Statement).get(
            db_old_premises[0].uid)
        discussion_lang = db_first_old_statement.lang
        db_user = DBDiscussionSession.query(User).get(db_review.detector_uid)

        if db_values:
            LOG.debug(
                "Merge was given premisegroup with the mapped, new statements")
            texts = [values.content for values in db_values]
            translator_discussion = Translator(discussion_lang)
            new_text = ' {} '.format(translator_discussion.get(
                _.aand)).join(texts)
        else:
            LOG.debug("Just merge the premisegroup")
            new_text = DBDiscussionSession.query(PremiseGroup).get(
                db_review.premisegroup_uid).get_text()

        # now we have new text as a variable, let's set the statement
        new_statement, tmp = set_statement(new_text, db_user,
                                           db_first_old_statement.is_position,
                                           db_issue)

        # new premisegroup for the statement
        db_new_premisegroup = PremiseGroup(author=db_user.uid)
        DBDiscussionSession.add(db_new_premisegroup)
        DBDiscussionSession.flush()

        # new premise
        db_new_premise = Premise(db_new_premisegroup.uid, new_statement.uid,
                                 False, db_user.uid, db_issue.uid)
        DBDiscussionSession.add(db_new_premise)
        DBDiscussionSession.flush()
        LOG.debug("Added new premise %s with pgroup %s", db_new_premise.uid,
                  db_new_premisegroup.uid)

        # swap the premisegroup occurence in every argument
        db_arguments = DBDiscussionSession.query(Argument).filter_by(
            premisegroup_uid=db_review.premisegroup_uid).all()
        for argument in db_arguments:
            LOG.debug("Reset argument %s from pgroup %s to new pgroup %s",
                      argument.uid, argument.premisegroup_uid,
                      db_new_premisegroup.uid)
            argument.set_premisegroup(db_new_premisegroup.uid)
            DBDiscussionSession.add(argument)
            DBDiscussionSession.flush()

        # add swap to database
        DBDiscussionSession.add(
            PremiseGroupMerged(db_review.uid, db_review.premisegroup_uid,
                               db_new_premisegroup.uid))

        # swap the conclusion in every argument
        old_statement_ids = [p.statement_uid for p in db_old_premises]
        for old_statement_id in old_statement_ids:
            db_arguments = DBDiscussionSession.query(Argument).filter_by(
                conclusion_uid=old_statement_id).all()
            for argument in db_arguments:
                LOG.debug(
                    "Reset arguments %s from conclusions %s to new merges statement %s",
                    argument.uid, argument.conclusion_uid, new_statement.uid)
                argument.set_conclusion(new_statement.uid)
                DBDiscussionSession.add(argument)
                DBDiscussionSession.add(
                    StatementReplacementsByPremiseGroupMerge(
                        db_review.uid, old_statement_id, new_statement.uid))
                DBDiscussionSession.flush()

        # finish
        DBDiscussionSession.flush()
        transaction.commit()
Exemple #4
0
def __split_premisegroup(review):
    """
    Splits a premisegroup into the items, which are mapped with the given review

    :param review: ReviewSplit.uid
    :return: None
    """
    db_values = DBDiscussionSession.query(ReviewSplitValues).filter_by(
        review_uid=review.uid).all()
    db_old_premises = DBDiscussionSession.query(Premise).filter_by(
        premisegroup_uid=review.premisegroup_uid).all()
    db_issue = DBDiscussionSession.query(Issue).get(
        db_old_premises[0].issue_uid)
    db_old_statement_ids = [p.statement_uid for p in db_old_premises]
    db_first_old_statement = DBDiscussionSession.query(Statement).get(
        db_old_premises[0].uid)
    db_user = DBDiscussionSession.query(User).get(review.detector_uid)

    if db_values:
        logger('review.opinions',
               'split given premisegroup into the mapped, new statements')
        db_statements = []
        for value in db_values:
            new_statement, tmp = set_statement(
                value.content, db_user, db_first_old_statement.is_position,
                db_issue)
            db_statements.append(new_statement)
    else:
        logger('review.opinions', 'just split the premisegroup')
        db_statements = DBDiscussionSession.query(Statement).filter(
            Statement.uid.in_(db_old_statement_ids)).all()

    # new premisegroups, for each statement a new one
    new_premisegroup_ids = []
    new_premise_ids = []
    for statement in db_statements:
        db_new_premisegroup = PremiseGroup(author=db_user.uid)
        DBDiscussionSession.add(db_new_premisegroup)
        DBDiscussionSession.flush()
        new_premisegroup_ids.append(db_new_premisegroup.uid)

        db_new_premise = Premise(db_new_premisegroup.uid, statement.uid, False,
                                 db_user.uid, statement.issue_uid)
        DBDiscussionSession.add(db_new_premise)
        DBDiscussionSession.flush()
        new_premise_ids.append(db_new_premise.uid)

        # note new added pgroup
        DBDiscussionSession.add(
            PremiseGroupSplitted(review.uid, review.premisegroup_uid,
                                 db_new_premisegroup.uid))

    # swap the premisegroup occurence in every argument and add new arguments for the new premises
    db_arguments = DBDiscussionSession.query(Argument).filter_by(
        premisegroup_uid=review.premisegroup_uid).all()
    for argument in db_arguments:
        argument.set_premisegroup(new_premisegroup_ids[0])
        DBDiscussionSession.add(argument)

        for uid in new_premisegroup_ids[1:]:
            argument = Argument(uid, argument.is_supportive,
                                argument.author_uid, argument.issue_uid,
                                argument.conclusion_uid, argument.argument_uid,
                                argument.is_disabled)
            DBDiscussionSession.add(argument)
            DBDiscussionSession.flush()
            DBDiscussionSession.add(
                ArgumentsAddedByPremiseGroupSplit(review.uid, argument.uid))

    # swap the conclusion in every argument
    new_statements_uids = [s.uid for s in db_statements]
    for old_statement_uid in db_old_statement_ids:
        db_arguments = DBDiscussionSession.query(Argument).filter_by(
            conclusion_uid=old_statement_uid).all()
        for argument in db_arguments:
            argument.set_conclusion(new_statements_uids[0])
            DBDiscussionSession.add(argument)
            DBDiscussionSession.add(
                StatementReplacementsByPremiseGroupSplit(
                    review.uid, old_statement_uid, new_statements_uids[0]))
            DBDiscussionSession.flush()

            for statement_uid in new_statements_uids[1:]:
                db_argument = Argument(argument.premisegroup_uid,
                                       argument.is_supportive,
                                       argument.author_uid, argument.issue_uid,
                                       statement_uid, argument.argument_uid,
                                       argument.is_disabled)
                DBDiscussionSession.add(db_argument)
                DBDiscussionSession.add(
                    StatementReplacementsByPremiseGroupSplit(
                        review.uid, old_statement_uid, statement_uid))
                DBDiscussionSession.flush()

    # finish
    DBDiscussionSession.flush()
    transaction.commit()