<commit_before>import state
import action
import mission # TODO: CIRCULAR


class Branch(object):
    def __init__(self, name, schema, estimators):
        """
        Constructs a new outcome branch.

        :param  name:           the name of this branch.
        :param  schema:         the action schema to which this outcome \
                                branch belongs, given as an ActionSchema \
                                instance.
        :param  estimators:     a list of state estimators for this branch.
        """
        assert (isinstance(estimators, list) and estimators is not None)
        assert (all(isinstance(e, state.Estimator) for e in estimators))

        self.__effects = {e.getVariableName(): e for e in estimators}
        assert (isinstance(self.__effects, dict) and self.__effects is not None)

        assert (isinstance(name, str))
        assert (name is not None)
        assert (name is not "")
        self.__name = name

        assert (isinstance(schema, action.ActionSchema) and schema is not None)
        self.__schema = schema


    def getSchema(self):
        """
        Returns the action schema to which this outcome branch belongs.
        """
        return self.__schema


    def getSchemaName(self):
        """
        Returns the name of the schema to which this outcome branch belongs.
        """
        return self.getSchema().getName()


    def getName(self):
        """
        Returns the name of this branch.
        """
        return self.__name


    def getID(self):
        """
        Returns the identifier for this branch.
        """
        return BranchID(self.getSchemaName(), self.__name)


    def generate(self, initialState, env):
        """
        Generates an action that would cause the system to take this branch.

        :param  initialState:   the state of the system immediately before \
                                executing the generated action.
        :param  env:            the environment in which the mission will be \
                                conducted.
        """
        raise NotImplementedError


    def isSatisfiable(self, initialState, env):
        """
        Determines whether there exists a set of parameter values that would
        satisify this precondition given a fixed initial state and
        environment.
        """
        raise NotImplementedError


    def isApplicable(self, act, initialState, env):
        """
        Determines whether the guard for this outcome branch is satisfied by
        the parameters for the action, the state of the system immediately
        prior to the execution of the action, and the state of the environment.

        :param  action:         a description of the action that is about to \
                                be performed
        :param  initialState:   the state of the system immediately prior to \
                                the execution of the action
        :param  env:            the environment in which the action is being \
                                performed

        :returns    True if the guard is satisfied by the given context, \
                    otherwise False.
        """
        raise NotImplementedError


    def computeTimeout(self, act, state, environment):
        """
        Computes the timeout for the current branch.
        """
        raise NotImplementedError


    def computeExpectedState(self, act, initialState, env):
        """
        Produces an estimate of the system state following the execution of
        this branch within a given context.

        :param  act:            a description of the action being performed
        :param  initialState:   the state of the system immediately prior to \
                                the execution of this action
        :param  env:            the environment in which the action is being \
                                performed

        :returns    An ExpectedState object, describing the state that the \
                    system is expected to be in immediately after the \
                    execution of this branch
        """
        assert (isinstance(act, action.Action) and act is not None)
        assert (isinstance(initialState, state.State) and state is not None)
        assert (isinstance(env, state.Environment) and env is not None)


        # store the expected state values in a dictionary, indexed by the name
        # of the associated state variable
        values = {}
        for (varName, initialValue) in initialState.getValues().items():
            if varName in self.__effects:
                expected = self.__effects[varName].computeExpectedValue(act, initialState, env)
                values[varName] = expected
            else:
                values[varName] = state.ExpectedStateValue(initialValue)

        return state.ExpectedState(values)


class IdleBranch(Branch):
    def __init__(self, schema):
        super(IdleBranch, self).__init__("idle", schema, [])


    def computeTimeout(self, act, state, environment):
        return 1.0


    def isApplicable(self, act, state, environment):
        return True


    def isSatisfiable(self, state, environment):
        return True


    def generate(self, state, environment):
        return self.getSchema.generate()


class BranchID(object):
    @staticmethod
    def fromJSON(jsn):
        assert (isinstance(jsn, str) or isinstance(jsn, unicode))
        assert (jsn is not None)
        assert (jsn != '')
<commit_msg>fixed bug in IdleBranch::generate<commit_after>import state
import action
import mission # TODO: CIRCULAR


class Branch(object):
    def __init__(self, name, schema, estimators):
        """
        Constructs a new outcome branch.

        :param  name:           the name of this branch.
        :param  schema:         the action schema to which this outcome \
                                branch belongs, given as an ActionSchema \
                                instance.
        :param  estimators:     a list of state estimators for this branch.
        """
        assert (isinstance(estimators, list) and estimators is not None)
        assert (all(isinstance(e, state.Estimator) for e in estimators))

        self.__effects = {e.getVariableName(): e for e in estimators}
        assert (isinstance(self.__effects, dict) and self.__effects is not None)

        assert (isinstance(name, str))
        assert (name is not None)
        assert (name is not "")
        self.__name = name

        assert (isinstance(schema, action.ActionSchema) and schema is not None)
        self.__schema = schema


    def getSchema(self):
        """
        Returns the action schema to which this outcome branch belongs.
        """
        return self.__schema


    def getSchemaName(self):
        """
        Returns the name of the schema to which this outcome branch belongs.
        """
        return self.getSchema().getName()


    def getName(self):
        """
        Returns the name of this branch.
        """
        return self.__name


    def getID(self):
        """
        Returns the identifier for this branch.
        """
        return BranchID(self.getSchemaName(), self.__name)


    def generate(self, initialState, env):
        """
        Generates an action that would cause the system to take this branch.

        :param  initialState:   the state of the system immediately before \
                                executing the generated action.
        :param  env:            the environment in which the mission will be \
                                conducted.
        """
        raise NotImplementedError


    def isSatisfiable(self, initialState, env):
        """
        Determines whether there exists a set of parameter values that would
        satisify this precondition given a fixed initial state and
        environment.
        """
        raise NotImplementedError


    def isApplicable(self, act, initialState, env):
        """
        Determines whether the guard for this outcome branch is satisfied by
        the parameters for the action, the state of the system immediately
        prior to the execution of the action, and the state of the environment.

        :param  action:         a description of the action that is about to \
                                be performed
        :param  initialState:   the state of the system immediately prior to \
                                the execution of the action
        :param  env:            the environment in which the action is being \
                                performed

        :returns    True if the guard is satisfied by the given context, \
                    otherwise False.
        """
        raise NotImplementedError


    def computeTimeout(self, act, state, environment):
        """
        Computes the timeout for the current branch.
        """
        raise NotImplementedError


    def computeExpectedState(self, act, initialState, env):
        """
        Produces an estimate of the system state following the execution of
        this branch within a given context.

        :param  act:            a description of the action being performed
        :param  initialState:   the state of the system immediately prior to \
                                the execution of this action
        :param  env:            the environment in which the action is being \
                                performed

        :returns    An ExpectedState object, describing the state that the \
                    system is expected to be in immediately after the \
                    execution of this branch
        """
        assert (isinstance(act, action.Action) and act is not None)
        assert (isinstance(initialState, state.State) and state is not None)
        assert (isinstance(env, state.Environment) and env is not None)


        # store the expected state values in a dictionary, indexed by the name
        # of the associated state variable
        values = {}
        for (varName, initialValue) in initialState.getValues().items():
            if varName in self.__effects:
                expected = self.__effects[varName].computeExpectedValue(act, initialState, env)
                values[varName] = expected
            else:
                values[varName] = state.ExpectedStateValue(initialValue)

        return state.ExpectedState(values)


class IdleBranch(Branch):
    def __init__(self, schema):
        super(IdleBranch, self).__init__("idle", schema, [])


    def computeTimeout(self, act, state, environment):
        return 1.0


    def isApplicable(self, act, state, environment):
        return True


    def isSatisfiable(self, state, environment):
        return True


    def generate(self, state, environment):
        return self.getSchema().generate()


class BranchID(object):
    @staticmethod
    def fromJSON(jsn):
        assert (isinstance(jsn, str) or isinstance(jsn, unicode))
        assert (jsn is not None)
        assert (jsn != '')

{'fraction_non_alphanumeric': 0.06570397111913358, 'fraction_numerical': 0.00036101083032490973, 'mean_word_length': 1.8886861313868613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4366135', 'n_tokens_mistral': 2672, 'n_tokens_neox': 2544, 'n_words': 1059}