class _GdlTermRecordWrapper(): def __init__(self, gdl): """Initialize by parsing a GDL string. Args: gdl (str) : GDL string to parse. """ with Frame(): self._term_record = TermRecord( prefix_gdl_statement_to_prolog(gdl)) @classmethod def _from_term_record(cls, term_record): """Initialize from a term record.""" obj = cls.__new__(cls) obj._term_record = term_record return obj def __str__(self): """Representation as a GDL string.""" with Frame(): return prolog_term_to_prefix_gdl(self._term_record.get()) def __eq__(self, other): with Frame(): try: return self._term_record.get() == other._term_record.get() except AttributeError as e: if '_term_record' not in str(e): raise return NotImplemented
def __init__(self, gdl): """Initialize by parsing a GDL string. Args: gdl (str) : GDL string to parse. """ with Frame(): self._term_record = TermRecord( prefix_gdl_statement_to_prolog(gdl))
def __init__(self, game, move_history_term=None, truth_history_term=None, truth_state_term=None): self.game = game with Frame(): if move_history_term is None: move_history_term = Term.from_nil() self.move_history = TermRecord(move_history_term) if truth_history_term is None: truth_history_term = Term() self._truth_history_3_predicate( self._game_id_term(), move_history_term, truth_history_term, check=True) self.truth_history = TermRecord(truth_history_term) if truth_state_term is None: truth_state_term = Term() self._final_truth_state_predicate( truth_history_term, truth_state_term, check=True) self.truth_state = TermRecord(truth_state_term)
class GeneralGameState(object): """A general game state.""" _terminal_atom = Atom('terminal') _base_functor = GeneralGame._base_functor _does_functor = Functor('does', 2) _eq_functor = Functor('=', 2) _goal_functor = Functor('goal', 2) _legal_functor = Functor('legal', 2) _true_functor = Functor('true', 1) _truth_history_3_functor = Functor('truth_history', 3) _final_truth_state_predicate = Predicate( functor=Functor('final_truth_state', 2), module=GeneralGameManager._ggp_state) _length_predicate = Predicate(functor=Functor('length', 2)) _prepare_moves_predicate = Predicate(functor=Functor('prepare_moves', 3), module=GeneralGameManager._ggp_state) _truth_history_3_predicate = Predicate( functor=_truth_history_3_functor, module=GeneralGameManager._ggp_state) _truth_history_4_predicate = Predicate( functor=Functor('truth_history', 4), module=GeneralGameManager._ggp_state) def __init__(self, game, move_history_term=None, truth_history_term=None, truth_state_term=None): self.game = game with Frame(): if move_history_term is None: move_history_term = Term.from_nil() self.move_history = TermRecord(move_history_term) if truth_history_term is None: truth_history_term = Term() self._truth_history_3_predicate( self._game_id_term(), move_history_term, truth_history_term, check=True) self.truth_history = TermRecord(truth_history_term) if truth_state_term is None: truth_state_term = Term() self._final_truth_state_predicate( truth_history_term, truth_state_term, check=True) self.truth_state = TermRecord(truth_state_term) def __eq__(self, other): return (self.game == other.game and (self.truth_state == other.truth_state or self.truth_state.get() == other.truth_state.get())) def turn_number(self): """The current turn number.""" with Frame(): num_moves = Term() self._length_predicate(self.move_history.get(), num_moves, check=True) return int(num_moves) def utility(self, role): """The utility of the current state for the given role.""" with Frame(): utility = Term() utility_query = self._goal_functor( Term.from_atom(role._atom), utility) self._query_term(utility_query)(check=True) if utility.is_atom(): return int(utility.get_atom_name()) else: return int(utility) def legal_actions(self, role): """An iterator of legal actions for role in the current state.""" with Frame() as f: action = f.term() action_query = self._legal_functor( Term.from_atom(role._atom), action) query = self._query(action_query) for action_assignment in query.term_assignments(action, persistent=True): yield Action._from_term_record(action_assignment) def state_propositions(self): """Iterator of the base propositions that are true for this state.""" with Frame() as f: state_term = f.term() base_term_query = self._base_functor(state_term) true_term_query = self._true_functor(state_term) query = self._query(base_term_query, true_term_query) for state_term_assignment in query.term_assignments( state_term, persistent=True): yield GameProposition._from_term_record( state_term_assignment) def is_terminal(self): """True if the current game state is terminal.""" with Frame(): return self._query_term(Term.from_atom(self._terminal_atom))() def apply_moves(self, moves): """A new game state representing the game after a move is applied. Returns a new state, this state is unchanged. Args: moves (dict) : Dict of `Role` => `Action`, one entry per role. Returns: GeneralGameState: The new game state. """ with Frame() as f: game_id_term = self._game_id_term() moves_term = Term.from_list_terms([ self._does_functor(Term.from_atom(role._atom), action._term_record.get()) for (role, action) in moves.items()]) # prepare_moves(game_id, moves_term, PreparedMoves) prepared_moves = f.term() try: self._prepare_moves_predicate( game_id_term, moves_term, prepared_moves, check=True) except PrologCallFailed: raise ValueError( 'Invalid move set. Possibly not 1 move per role.') # NewMoveHistory = [PreparedMoves | old_move_history] new_move_history = Term.from_cons_list(prepared_moves, self.move_history.get()) # truth_history(game_id, NewMoveHistory, old_truth_history, # NewTruthHistory) new_truth_history = f.term() try: self._truth_history_4_predicate( game_id_term, new_move_history, self.truth_history.get(), new_truth_history, check=True) except PrologCallFailed: raise ValueError('Invalid moves: {}'.format( {str(role): str(action) for role, action in moves.items()})) # final_truth_state(NewTruthHistory, NewTruthState) new_truth_state = f.term() self._final_truth_state_predicate( new_truth_history, new_truth_state, check=True) return GeneralGameState( game=self.game, move_history_term=new_move_history, truth_history_term=new_truth_history, truth_state_term=new_truth_state, ) def game_id(self): return self.game.game_id def _game_id_term(self): return Term.from_atom_name(self.game_id()) def _query_term(self, *queries): return self.game._stateful_query_term(self.truth_state.get(), *queries) def _query(self, *queries): return self.game._stateful_query(self.truth_state.get(), *queries)