'Examines our rules and starts up flows based on the client.
Args:
client_id: Client id of the client for tasks to be assigned.
Returns:
Number of assigned tasks.'

def AssignTasksToClient(self, client_id):

    client_id = rdf_client.ClientURN(client_id)
    rules = self.Get(self.Schema.RULES)
    if (not rules):
        return 0
    client = aff4.FACTORY.Open(client_id, mode='rw', token=self.token)
    try:
        last_foreman_run = (client.Get(client.Schema.LAST_FOREMAN_TIME) or 0)
    except AttributeError:
        last_foreman_run = 0
    latest_rule = max((rule.created for rule in rules))
    if (latest_rule <= int(last_foreman_run)):
        return 0
    client.Set(client.Schema.LAST_FOREMAN_TIME(latest_rule))
    client.Close()
    object_urns = {}
    relevant_rules = []
    expired_rules = False
    now = (time.time() * 1000000.0)
    for rule in rules:
        if (rule.expires < now):
            expired_rules = True
            continue
        if (rule.created <= int(last_foreman_run)):
            continue
        relevant_rules.append(rule)
        for path in rule.client_rule_set.GetPathsToCheck():
            aff4_object = client_id.Add(path)
            object_urns[str(aff4_object)] = aff4_object
    objects = {}
    for fd in aff4.FACTORY.MultiOpen(object_urns, token=self.token):
        objects[fd.urn] = fd
    actions_count = 0
    for rule in relevant_rules:
        if self._EvaluateRules(objects, rule, client_id):
            actions_count += self._RunActions(rule, client_id)
    if expired_rules:
        self.ExpireRules()
    return actions_count
