Ejemplo n.º 1
0
    def setUp(self):
        self.DB = PymongoDB.get_db()
        load_data.load_data()
        self.member = self.DB.find_one(db_constants.MEMBERS,
            {"full_name" : "vote_test"})

        self.MEMBER = "vote_test"
        self.BILL1 = "VOTE_BILL1"
        self.BILL2 = "VOTE_BILL2"
        self.BILL3 = "VOTE_BILL3"

        self.GROUP1 = "VOTE_GROUP1"
        self.GROUP2 = "VOTE_GROUP2"

        self.strategy1_name = "AlwaysSucceed"
        self.strategy2_name = "AlwaysFail"
        self.strategy3_name = "AlwaysFail2"
        self.strategy4_name = "Inactive"

        self.bill1 = self.DB.find_one(db_constants.BILLS,
            queries.bill_query(self.BILL1))
        self.bill2 = self.DB.find_one(db_constants.BILLS,
            queries.bill_query(self.BILL2))
        self.bill3 = self.DB.find_one(db_constants.BILLS,
            queries.bill_query(self.BILL3))

        self.group1 = self.DB.find_one(db_constants.GROUPS,
            queries.bill_query(self.GROUP1))
        self.group2 = self.DB.find_one(db_constants.GROUPS,
            queries.bill_query(self.GROUP2))

        AlwaysFailStrategy.call_count = 0
        self.DB.DB.drop_collection(db_constants.DECISIONS)
Ejemplo n.º 2
0
def vote(member_identifier, bill_identifier):
    """Predicts how the specified member will vote on the given bill.

    Arguments:
        member_identifier: A value that identifies the member such as full_name
            or database id. Make sure the value uniquely identifies the member
            in the database.
        bill_identifier: A value that identifies the bill such as bill_number or
            database id. Make sure the value uniquely identifies the bill in the
            database.

    Returns:
        A decision object containing the results of the decision on the vote
    """

    member = PymongoDB.get_db().find_one(db_constants.MEMBERS,
        queries.member_query(member_identifier))
    bill = PymongoDB.get_db().find_one(db_constants.BILLS,
        queries.bill_query(bill_identifier))

    if not member:
        logger.LOGGER.error("Member not found in DB: %s" % member_identifier)
        return None
    if not bill:
        logger.LOGGER.error("Bill not found in DB: %s" % bill_identifier)
        return None

    return _vote_helper(member, bill)
Ejemplo n.º 3
0
def _extract_single_voting_stance(vote):
    """Helper to extract_voting_stances. Extracts a stance the member holds
    based on voting for a particular bill. It checks the bill and returns
    the stances that can be inferred from voting for or against it.
       
    Arguments:
        vote: a vote from member.voting_record
        
    Return
        A list containing all stances to be inferred from the vote.
    """
    logger.LOGGER.info("Extracting stances from vote: %s" % vote)

    # Check if the name used in vote is the name of the bill, bill number, or
    # a synonym
    bill_identifier = vote.data
    query = queries.bill_query(bill_identifier)
    bill = PymongoDB.get_db().find_one(db_constants.BILLS, query)
    if bill is None:
        logger.LOGGER.error("Bill not found: %s" % bill_identifier)
        return []

    if vote.outcome == outcomes.FOR:
        return bill.stances_for
    elif vote.outcome == outcomes.AGN:
        return bill.stances_agn
    else:
        msg = "Bad Bill outcome. Expected FOR or AGN. Received %s" % vote.outcome
        logger.LOGGER.error(msg)
        return []
Ejemplo n.º 4
0
def _update_regular_stances(decision):
    """ Updates the various stances within decision. In particular, this
    function calculates norms, determines the stances groups have on the issue,
    and determines split groups, credos, voting records, etc.
    
    Arguments:
        decision: The decision object to update
    """
    logger.LOGGER.info("Updating regular stances...")
    fors = decision.for_stances
    agns = decision.agn_stances
    bill = PymongoDB.get_db().find_one(db_constants.BILLS,
        queries.bill_query(decision.bill))

    logger.LOGGER.info("Updating norms...")
    decision.for_norms = stance_analyze.collect_normative_stances(fors)
    decision.agn_norms = stance_analyze.collect_normative_stances(agns)

    if bill is None:
        logger.LOGGER.error("Decision bill not found.")
    else:
        logger.LOGGER.info("Updating bill norms...")
        decision.for_bill_norms = stance_analyze.collect_normative_stances(bill.stances_for)
        decision.agn_bill_norms = stance_analyze.collect_normative_stances(bill.stances_agn)

    logger.LOGGER.info("Updating group stances...")
    decision.groups_for = util.collect_group_stances(fors)
    decision.groups_agn = util.collect_group_stances(agns)

    # Equality is defined by seeing if a stance has the same source as another.
    # This is needed to detect split groups. However, the stances are not
    # equal to each other in the normal sense. Hence, the intersection is done
    # twice to get all unique stances.
    eq_fun = lambda stance1, stance2: stance1.source == stance2.source
    for_intersect = util.intersection(decision.groups_for,
        decision.groups_agn, eq_fun)
    agn_intersect = util.intersection(decision.groups_agn,
        decision.groups_for, eq_fun)
    decision.split_group = for_intersect + agn_intersect
    if decision.split_group:
        logger.LOGGER.info("Split group detected.")


    for_bills = util.collect_bill_stances(fors)
    agn_bills = util.collect_bill_stances(agns)
    if for_bills and agn_bills:
        logger.LOGGER.info("Split record detected.")
        decision.split_record = for_bills + agn_bills

    for_credo = util.collect_credo_stances(fors)
    agn_credo = util.collect_credo_stances(agns)
    if for_credo and agn_credo:
        logger.LOGGER.info("Split credo detected.")
        decision.split_credo = for_credo + agn_credo

    logger.LOGGER.info("Regular stances update complete.")
Ejemplo n.º 5
0
    def setUp(self):
        self.DB = PymongoDB.get_db()
        load_data.load_data()
        self.member = self.DB.find_one(db_constants.MEMBERS,
            {"full_name" : "member_analyze_test"})

        self.BILL1 = "BILL1"
        self.BILL2 = "BILL2"
        self.BILL3 = "BILL3"

        self.GROUP1 = "GROUP1"
        self.GROUP2 = "GROUP2"

        self.bill1 = self.DB.find_one(db_constants.BILLS,
            queries.bill_query(self.BILL1))
        self.bill2 = self.DB.find_one(db_constants.BILLS,
            queries.bill_query(self.BILL2))
        self.bill3 = self.DB.find_one(db_constants.BILLS,
            queries.bill_query(self.BILL3))

        self.group1 = self.DB.find_one(db_constants.GROUPS,
            queries.bill_query(self.GROUP1))
        self.group2 = self.DB.find_one(db_constants.GROUPS,
            queries.bill_query(self.GROUP2))
Ejemplo n.º 6
0
def _get_bills(bill_identifier=None):
    """Retrieves the bills for vote_all. If the bill_identifier is specified, it
    returns a list of the specified bill. If not, it returns an iterator over
    all the bills in the database.

    Arguments:
        bill_identifier: an identifier for the bill to retrieve.

    Returns:
        Either an array with the bill specified by bill_identifier or an
        iterator for all bills that will be voted upon.
    """
    bill = None
    if bill_identifier:
        bill = PymongoDB.get_db().find_one(db_constants.BILLS,
            queries.bill_query(bill_identifier))

    if bill:
        return [bill]
    else:
        if bill_identifier:
            logger.LOGGER.error("Bill not found: %s" % bill_identifier)
        return PymongoDB.get_db().find(db_constants.BILLS)