Skip to content

Commit

Permalink
Refactor new Event class. Makes it work with DependencyTree. Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcCote committed May 12, 2021
1 parent 8d35689 commit c79c96a
Show file tree
Hide file tree
Showing 25 changed files with 1,055 additions and 1,918 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ tmp/*
*.ipynb_checkpoints
/dist
/wheelhouse
docs/build
docs/src
*.orig
2 changes: 1 addition & 1 deletion textworld/challenges/tests/test_coin_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def test_making_coin_collector():

settings = {"level": level}
game = coin_collector.make(settings, options)
assert len(game.quests[0].commands) == expected[level]["quest_length"]
assert len(game.walkthrough) == expected[level]["quest_length"]
assert len(game.world.rooms) == expected[level]["nb_rooms"]
2 changes: 1 addition & 1 deletion textworld/challenges/tests/test_treasure_hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def test_making_treasure_hunter_games():

settings = {"level": level}
game = treasure_hunter.make(settings, options)
assert len(game.quests[0].commands) == game.metadata["quest_length"], "Level {}".format(level)
assert len(game.walkthrough) == game.metadata["quest_length"], "Level {}".format(level)
assert len(game.world.rooms) == game.metadata["world_size"], "Level {}".format(level)
21 changes: 13 additions & 8 deletions textworld/challenges/tw_coin_collector/coin_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,30 @@
other than the coin to collect.
"""

import os
import argparse
from os.path import join as pjoin
from typing import Mapping, Optional, Any

import textworld
from textworld.generator.graph_networks import reverse_direction

from textworld.utils import encode_seeds
from textworld.generator.game import GameOptions, Quest, EventCondition
from textworld.generator.data import KnowledgeBase
from textworld.generator.game import GameOptions, Quest, Event
from textworld.challenges import register


KB_PATH = pjoin(os.path.dirname(__file__), "textworld_data")


def build_argparser(parser=None):
parser = parser or argparse.ArgumentParser()

group = parser.add_argument_group('Coin Collector game settings')
group.add_argument("--level", required=True, type=int,
help="The difficulty level. Must be between 1 and 300 (included).")

group.add_argument("--force-entity-numbering", required=True, action="store_true",
help="This will set `--entity-numbering` to be True which is required for this challenge.")

return parser


Expand All @@ -49,7 +52,7 @@ def make(settings: Mapping[str, Any], options: Optional[GameOptions] = None) ->
:py:class:`textworld.GameOptions <textworld.generator.game.GameOptions>`
for the list of available options).
.. warning:: This challenge requires `options.grammar.allowed_variables_numbering` to be `True`.
.. warning:: This challenge enforces `options.grammar.allowed_variables_numbering` to be `True`.
Returns:
Generated game.
Expand All @@ -68,9 +71,11 @@ def make(settings: Mapping[str, Any], options: Optional[GameOptions] = None) ->
"""
options = options or GameOptions()

# Load knowledge base specific to this challenge.
options.kb = KnowledgeBase.load(KB_PATH)

# Needed for games with a lot of rooms.
options.grammar.allowed_variables_numbering = settings["force_entity_numbering"]
assert options.grammar.allowed_variables_numbering
options.grammar.allowed_variables_numbering = True

level = settings["level"]
if level < 1 or level > 300:
Expand Down Expand Up @@ -167,7 +172,7 @@ def make_game(mode: str, options: GameOptions) -> textworld.Game:

# Generate the quest thats by collecting the coin.
quest = Quest(win_events=[
EventCondition(conditions={M.new_fact("in", coin, M.inventory)})
Event(conditions={M.new_fact("in", coin, M.inventory)})
])

M.quests = [quest]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ type P {
look :: at(P, r) -> at(P, r); # Nothing changes.
}

reverse_rules {
look :: look;
}

inform7 {
commands {
look :: "look" :: "looking";
Expand Down
2 changes: 1 addition & 1 deletion textworld/challenges/tw_cooking/cooking.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ def make(settings: Mapping[str, str], options: Optional[GameOptions] = None) ->
start_room = rng_map.choice(M.rooms)
M.set_player(start_room)

M.grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar)
M.grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar, kb=options.kb)

# Remove every food preparation with grilled, if there is no BBQ.
if M.find_by_name("BBQ") is None:
Expand Down
44 changes: 22 additions & 22 deletions textworld/challenges/tw_simple/textworld_data/logic/key.twl
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# # key
# type k : o {
# predicates {
# match(k, c);
# match(k, d);
# }
# key
type k : o {
predicates {
match(k, c);
match(k, d);
}

# constraints {
# k1 :: match(k, c) & match(k', c) -> fail();
# k2 :: match(k, c) & match(k, c') -> fail();
# k3 :: match(k, d) & match(k', d) -> fail();
# k4 :: match(k, d) & match(k, d') -> fail();
# }
constraints {
k1 :: match(k, c) & match(k', c) -> fail();
k2 :: match(k, c) & match(k, c') -> fail();
k3 :: match(k, d) & match(k', d) -> fail();
k4 :: match(k, d) & match(k, d') -> fail();
}

# inform7 {
# type {
# kind :: "key";
# }
inform7 {
type {
kind :: "key";
}

# predicates {
# match(k, c) :: "The matching key of the {c} is the {k}";
# match(k, d) :: "The matching key of the {d} is the {k}";
# }
# }
# }
predicates {
match(k, c) :: "The matching key of the {c} is the {k}";
match(k, d) :: "The matching key of the {d} is the {k}";
}
}
}
17 changes: 9 additions & 8 deletions textworld/challenges/tw_simple/textworld_data/logic/room.twl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ type r {
north_of(r, r);
west_of(r, r);

north_of/d(r, d, r);
west_of/d(r, d, r);

free(r, r);

south_of(r, r') = north_of(r', r);
east_of(r, r') = west_of(r', r);

# north_of/d(r, d, r);
# west_of/d(r, d, r);
# south_of/d(r, d, r') = north_of/d(r', d, r);
# east_of/d(r, d, r') = west_of/d(r', d, r);
south_of/d(r, d, r') = north_of/d(r', d, r);
east_of/d(r, d, r') = west_of/d(r', d, r);
}

rules {
Expand Down Expand Up @@ -65,10 +66,10 @@ type r {
east_of(r, r') :: "The {r} is mapped east of {r'}";
west_of(r, r') :: "The {r} is mapped west of {r'}";

# north_of/d(r, d, r') :: "South of {r} and north of {r'} is a door called {d}";
# south_of/d(r, d, r') :: "North of {r} and south of {r'} is a door called {d}";
# east_of/d(r, d, r') :: "West of {r} and east of {r'} is a door called {d}";
# west_of/d(r, d, r') :: "East of {r} and west of {r'} is a door called {d}";
north_of/d(r, d, r') :: "South of {r} and north of {r'} is a door called {d}";
south_of/d(r, d, r') :: "North of {r} and south of {r'} is a door called {d}";
east_of/d(r, d, r') :: "West of {r} and east of {r'} is a door called {d}";
west_of/d(r, d, r') :: "East of {r} and west of {r'} is a door called {d}";
}

commands {
Expand Down
2 changes: 1 addition & 1 deletion textworld/challenges/tw_treasure_hunter/treasure_hunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def make_game(mode: str, options: GameOptions) -> textworld.Game:
quest = Quest(win_events=[event],
fail_events=[Event(conditions={Proposition("in", [wrong_obj, world.inventory])})])

grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar)
grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar, kb=options.kb)
game = textworld.generator.make_game_with(world, [quest], grammar)
game.metadata.update(metadata)
mode_choice = modes.index(mode)
Expand Down
15 changes: 8 additions & 7 deletions textworld/generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from textworld.generator.chaining import ChainingOptions, QuestGenerationError
from textworld.generator.chaining import sample_quest
from textworld.generator.world import World
from textworld.generator.game import Game, Quest, Event, GameOptions
from textworld.generator.game import Game, Quest, GameOptions
from textworld.generator.game import EventCondition, EventAction, EventAnd, EventOr
from textworld.generator.game import Event # For backward compatibility
from textworld.generator.graph_networks import create_map, create_small_map
from textworld.generator.text_generation import generate_text_from_grammar

Expand Down Expand Up @@ -142,19 +144,18 @@ def make_quest(world: Union[World, State], options: Optional[GameOptions] = None
for i in range(1, len(chain.nodes)):
actions.append(chain.actions[i - 1])
if chain.nodes[i].breadth != chain.nodes[i - 1].breadth:
event = Event(actions)
quests.append(Quest(win_events=[event]))
quests.append(Quest(win_event=EventCondition(actions=actions)))

actions.append(chain.actions[-1])
event = Event(actions)
quests.append(Quest(win_events=[event]))
quests.append(Quest(win_event=EventCondition(actions=actions)))

return quests


def make_grammar(options: Mapping = {}, rng: Optional[RandomState] = None) -> Grammar:
def make_grammar(options: Mapping = {}, rng: Optional[RandomState] = None,
kb: Optional[KnowledgeBase] = None) -> Grammar:
rng = g_rng.next() if rng is None else rng
grammar = Grammar(options, rng)
grammar = Grammar(options, rng, kb)
grammar.check()
return grammar

Expand Down
56 changes: 0 additions & 56 deletions textworld/generator/data/text_grammars/house_quests.twg

This file was deleted.

Loading

0 comments on commit c79c96a

Please sign in to comment.