Example #1
0
def process_causalities(action, deconflict=True):
    causalities = KB.exists_causality(action)

    initiates = OrderedSet()
    terminates = OrderedSet()

    if not causalities:
        return OrderedSet(), OrderedSet()

    for causality in causalities:
        action_subs = unify_args(causality.action.args, action.args)
        '''
        TODO: This check should be shifted into generating fluents
        Because the fluent might not be grounded yet
        '''

        constraint_subs = _check_reqs(causality.reqs, action_subs)

        if not constraint_subs:
            continue

        # Handle the case where there is no constraint
        if isinstance(constraint_subs, bool):
            constraint_subs = [action_subs]

        for c_sub in constraint_subs:
            for causality_outcome in causality.outcomes:
                outcome = causality_outcome.outcome
                fluent = copy.deepcopy(causality_outcome.fluent)

                fluent.args = reify_args(fluent.args, c_sub)

                # TODO: Constraint check on solution
                check_outcome_constraint(fluent)

                fluents = generate_outcome_fluents(fluent)

                if outcome is A_INITIATE:
                    for f in fluents:
                        initiates.add((f, action.end_time))
                elif outcome is A_TERMINATE:
                    for f in fluents:
                        terminates.add((f, action.end_time))
                else:
                    raise UnknownOutcomeError(outcome)

    if deconflict:
        terminates = terminates - initiates

    return initiates, terminates
Example #2
0
def add_to_cycle_proposed(cycle_proposed, state):
    actions = reify_actions(state, reify=True)
    cycle_proposed.add_actions(actions)

    for action in actions:
        causalities = KB.exists_causality(action)

        if not causalities:
            continue

        for causality in causalities:
            action_subs = unify_args(causality.action.args, action.args)

            for causality_outcome in causality.outcomes:
                reify_outcome = copy.deepcopy(causality_outcome)
                reify_outcome.fluent.args = reify_args_constraint_causality(
                    reify_outcome.fluent.args, action_subs)

                cycle_proposed.add_fluent(reify_outcome)
Example #3
0
def constraints_satisfied(o_goal, state, cycle_proposed: Proposed,
                          is_observation=False):

    constraints = KB.get_constraints(o_goal)
    causalities = KB.exists_causality(o_goal)

    if not constraints:
        return (True, True) if is_observation else True

    # Handle goal
    goal = reify_obj_args(o_goal, state.subs)
    # print(goal)

    all_proposed = copy.deepcopy(cycle_proposed)

    # The new action
    all_proposed._actions.add(goal)

    all_proposed._actions = OrderedSet(
        [reify_action(c_action, state.subs)
         for c_action in all_proposed._actions]
    )

    for obs in KB.cycle_obs:
        end_time = o_goal.end_time
        if not isinstance(end_time, int):
            end_time = reify(var(end_time.name), state.subs)

        # print(obs.action.end_time, end_time)
        if obs.end_time == end_time:
            all_proposed._actions.add(obs.action)

    if causalities:
        for causality in causalities:
            action_subs = unify_args(causality.action.args, goal.args)

            for causality_outcome in causality.outcomes:
                reify_outcome = copy.deepcopy(causality_outcome)
                reify_outcome.fluent.args = reify_args_constraint_causality(
                    reify_outcome.fluent.args, action_subs)

                if reify_outcome in all_proposed.fluents:
                    continue

                all_proposed.add_fluent(reify_outcome)  # REMOVED_DEEPCOPY

                # TODO: Check this addition for duplicates
                co_cons = KB.get_constraints(causality_outcome.fluent)
                if co_cons:
                    constraints.extend(co_cons)

    for constraint in constraints:

        try:
            res = next(check_constraint(constraint, all_proposed))

            if is_observation:
                return (False, res)

            return False
        except StopIteration:
            continue

    if is_observation:
        return (True, True)

    return True