コード例 #1
0
 def get_all_outgoing_as_two_tuple(self, agent = None):
     """
     Get all outgoing relations as a tuple with two elements, namely the source and destination state.
     :return: set of tuples with states
     :rtype: set
     """
     if not agent:
         return set([
             (source, destination)
             for (source, destination, _)
             in [
                 relation.to_tuple()
                 for _, relations in self.outgoing.iteritems()
                 for relation in relations
             ]
         ])
     else:
         if agent in self.model.agents:
             return set([
                 (source, destination)
                 for (source, destination, _)
                 in [
                     relation.to_tuple()
                     for relation in self.outgoing.get(agent, [])
                 ]
             ])
         else:
             raise errors.ModelError(
                 'The state {agent} does not exist in the model.'.format(
                     agent=agent
                 )
             )
コード例 #2
0
 def add_state(self, state):
     """
     Add a state to the list of states
     :param state: the state to be added to the models
     :return:
     """
     if state.name in self.states.keys():
         raise errors.ModelError(
             "The state '{state.name}' has two different definitions.".
             format(state=state))
     self.states[state.name] = state
コード例 #3
0
 def get_state_by_name(self, state_name):
     """
     Get a state in the  models by its name.
     :param name: the name of the state
     :return: the state with the name name
     :raises: modelError if name does not represent an existing state.
     """
     state = self.states.get(state_name, None)
     if state:
         return state
     raise errors.ModelError(
         'Could not find the state {}'.format(state_name))
コード例 #4
0
 def _create_valuation(propositions, valuations):
     """
     Create a valution dictionary.
     :param propositions: the list of propositions
     :param valuations: the list of valuations
     :raises ModelError: if the list of propositions does not have the same length as the list of propositions.
     :return: dictionary with valutions.
     """
     if not len(propositions) == len(valuations):
         raise errors.ModelError(
             "The length of the list of valuations and the list of propositions differs."
         )
     return OrderedDict(zip(propositions, valuations))
コード例 #5
0
    def add_states_from_json(self, json_data):
        """
        Add the states defined in json_data to the object.
        :param json_data: python representation of a json_object, containing at least:
            {
              'states': [
                  {   'vals': [True, ...],
                      'id': 'a'
                  }, ...
              ],
              'propositions': ['p', ...],
            }
        :return: void
        :raises:
            modelError if multiple states in the list but have the same name.
        """
        def _create_valuation(propositions, valuations):
            """
            Create a valution dictionary.
            :param propositions: the list of propositions
            :param valuations: the list of valuations
            :raises ModelError: if the list of propositions does not have the same length as the list of propositions.
            :return: dictionary with valutions.
            """
            if not len(propositions) == len(valuations):
                raise errors.ModelError(
                    "The length of the list of valuations and the list of propositions differs."
                )
            return OrderedDict(zip(propositions, valuations))

        propositions = json_data['propositions']
        for json_state in json_data['states']:
            try:
                self.add_state(
                    state.State(
                        str(json_state['id']),
                        _create_valuation(propositions, json_state['vals']),
                        self))
            except errors.ModelError as e:
                raise e

        if len(self.states.keys()) == 0:
            raise errors.ModelError('A model should have at least one state.')
コード例 #6
0
 def add_relations_from_json(self, json_data):
     """
     Add the states defined in json_data to the object.
     :param json_data: python representation of a json_object, containing at least:
         {
             'relations': [['a', 1, 'b'], ['c', 2, 'd']],
         }
     :return: void
     """
     for [source_name, agent, destination_name] in json_data['relations']:
         try:
             self.add_relation(
                 relation.Relation(
                     str(agent), self.get_state_by_name(source_name),
                     self.get_state_by_name(destination_name)))
         except errors.ModelError:
             raise errors.ModelError(
                 "The relationship {source}R_{agent}{destination} is not between existing states"
                 .format(source=source_name,
                         destination=destination_name,
                         agent=agent))