textworld.logic¶
- class textworld.logic.Action(name, preconditions, postconditions)[source]¶
Bases:
object
An action in the environment.
Create an Action.
- Parameters
name (
str
) – The name of this action.preconditions (
Iterable
[Proposition
]) – The preconditions that must hold before this action is applied.postconditions (
Iterable
[Proposition
]) – The conditions that replace the preconditions once applied.
- inverse(name=None)[source]¶
Invert the direction of this action.
- Parameters
name (optional) – The new name for the inverse action.
- Returns
- Return type
An action that does the exact opposite of this one.
- classmethod parse(expr)[source]¶
Parse an action expression.
- Parameters
expr (
str
) – The string to parse, in the formname :: [$]proposition [& [$]proposition]* -> proposition [& proposition]*
.- Return type
- property added: Collection[textworld.logic.Proposition]¶
All the new propositions being introduced by this action.
- Return type
Collection
[Proposition
]
- property all_propositions: Collection[textworld.logic.Proposition]¶
All the pre- and post-conditions.
- Return type
Collection
[Proposition
]
- property removed: Collection[textworld.logic.Proposition]¶
All the old propositions being removed by this action.
- Return type
Collection
[Proposition
]
- property variables¶
- class textworld.logic.Alias(pattern, replacement)[source]¶
Bases:
object
A shorthand predicate alias.
- class textworld.logic.Inform7Command(rule, command, event)[source]¶
Bases:
object
Information about an Inform 7 command.
- class textworld.logic.Inform7Predicate(predicate, source)[source]¶
Bases:
object
Information about an Inform 7 predicate.
- class textworld.logic.Inform7Type(name, kind, definition=None)[source]¶
Bases:
object
Information about an Inform 7 kind.
- class textworld.logic.Placeholder(name, type=None)[source]¶
Bases:
object
A symbolic placeholder for a variable in a Predicate.
Create a Placeholder.
- Parameters
name (
str
) – The name of this placeholder.type (optional) – The type of variable represented. Defaults to the name with any trailing apostrophes stripped.
- classmethod parse(expr)[source]¶
Parse a placeholder expression.
- Parameters
expr (
str
) – The string to parse, in the formname
orname: type
.- Return type
- name¶
- type¶
- class textworld.logic.Predicate(name, parameters)[source]¶
Bases:
object
A boolean-valued function over variables.
Create a Predicate.
- Parameters
name (
str
) – The name of this predicate.parameters (
Iterable
[Placeholder
]) – The symbolic arguments to this predicate.
- instantiate(mapping)[source]¶
Instantiate this predicate with the given mapping.
- Parameters
mapping (
Mapping
[Placeholder
,Variable
]) – A mapping from Placeholders to Variables.- Returns
- Return type
The instantiated Proposition with each Placeholder mapped to the corresponding Variable.
- match(proposition)[source]¶
Match this predicate against a concrete proposition.
- Parameters
proposition (
Proposition
) – The proposition to match against.- Return type
Optional
[Mapping
[Placeholder
,Variable
]]- Returns
The mapping from placeholders to variables such that
self.instantiate(mapping) == proposition
, orNone
if nosuch mapping exists.
- classmethod parse(expr)[source]¶
Parse a predicate expression.
- Parameters
expr (
str
) – The string to parse, in the formname(placeholder [, placeholder]*)
.- Return type
- substitute(mapping)[source]¶
Copy this predicate, substituting certain placeholders for others.
- Parameters
mapping (
Mapping
[Placeholder
,Placeholder
]) – A mapping from old to new placeholders.- Return type
- property names: Collection[str]¶
The names of the placeholders in this predicate.
- Return type
Collection
[str
]
- property types: Collection[str]¶
The types of the placeholders in this predicate.
- Return type
Collection
[str
]
- class textworld.logic.Proposition(name, arguments=[])[source]¶
Bases:
mementos.core.NewBase
An instantiated Predicate, with concrete variables for each placeholder.
Create a Proposition.
- Parameters
name (
str
) – The name of the proposition.arguments (
Iterable
[Variable
]) – The variables this proposition is applied to.
- classmethod parse(expr)[source]¶
Parse a proposition expression.
- Parameters
expr (
str
) – The string to parse, in the formname(variable [, variable]*)
.- Return type
- arguments¶
- name¶
- property names: Collection[str]¶
The names of the variables in this proposition.
- Return type
Collection
[str
]
- signature¶
- property types: Collection[str]¶
The types of the variables in this proposition.
- Return type
Collection
[str
]
- class textworld.logic.Rule(name, preconditions, postconditions)[source]¶
Bases:
object
A template for an action.
Create a Rule.
- Parameters
- instantiate(mapping)[source]¶
Instantiate this rule with the given mapping.
- Parameters
mapping (
Mapping
[Placeholder
,Variable
]) – A mapping from Placeholders to Variables.- Returns
- Return type
The instantiated Action with each Placeholder mapped to the corresponding Variable.
- inverse(name=None)[source]¶
Invert the direction of this rule.
- Parameters
name (optional) – The new name for the inverse rule.
- Returns
- Return type
A rule that does the exact opposite of this one.
- match(action)[source]¶
Match this rule against a concrete action.
- Parameters
action (
Action
) – The action to match against.- Return type
Optional
[Mapping
[Placeholder
,Variable
]]- Returns
The mapping from placeholders to variables such that
self.instantiate(mapping) == action
, orNone
if no suchmapping exists.
- classmethod parse(expr)[source]¶
Parse a rule expression.
- Parameters
expr (
str
) – The string to parse, in the formname :: [$]predicate [& [$]predicate]* -> predicate [& predicate]*
.- Return type
- substitute(mapping, name=None)[source]¶
Copy this rule, substituting certain placeholders for others.
- Parameters
mapping (
Mapping
[Placeholder
,Placeholder
]) – A mapping from old to new placeholders.- Return type
- property all_predicates: Iterable[textworld.logic.Predicate]¶
All the pre- and post-conditions.
- Return type
Iterable
[Predicate
]
- class textworld.logic.Signature(name, types)[source]¶
Bases:
mementos.core.NewBase
The type signature of a Predicate or Proposition.
Create a Signature.
- Parameters
name (
str
) – The name of the proposition/predicate this signature is for.types (
Iterable
[str
]) – The types of the parameters to the proposition/predicate.
- classmethod parse(expr)[source]¶
Parse a signature expression.
- Parameters
expr (
str
) – The string to parse, in the formname(type [, type]*)
.- Return type
- name¶
- types¶
- class textworld.logic.State(logic, facts=None)[source]¶
Bases:
object
The current state of a world.
Create a State.
- Parameters
logic (
GameLogic
) – The logic for this state’s game.facts (optional) – The facts that will be true in this state.
- all_applicable_actions(rules, mapping=None)[source]¶
Get all the rule instantiations that would be valid actions in this state.
- Parameters
rules (
Iterable
[Rule
]) – The possible rules to instantiate.mapping (optional) – An initial mapping to start from, constraining the possible instantiations.
- Returns
- Return type
The actions that can be instantiated from the given rules in this state.
- all_assignments(rule, mapping=None, partial=False, allow_partial=None)[source]¶
Find all possible placeholder assignments that would allow a rule to be instantiated in this state.
- Parameters
rule (
Rule
) – The rule to instantiate.mapping (optional) – An initial mapping to start from, constraining the possible instantiations.
partial (optional) – Whether incomplete mappings, that would require new variables or propositions, are allowed.
allow_partial (optional) – A callback function that returns whether a partial match may involve the given placeholder.
- Return type
Iterable
[Mapping
[Placeholder
,Optional
[Variable
]]]- Returns
The possible mappings for instantiating the rule. Partial mappings requiring new variables will have None in
place of existing Variables.
- all_instantiations(rule, mapping=None)[source]¶
Find all possible actions that can be instantiated from a rule in this state.
- Parameters
rule (
Rule
) – The rule to instantiate.mapping (optional) – An initial mapping to start from, constraining the possible instantiations.
- Returns
- Return type
The actions that can be instantiated from the rule in this state.
- apply(action)[source]¶
Apply an action to the state.
- Parameters
action (
Action
) – The action to apply.- Returns
- Return type
Whether the action could be applied (i.e. whether the preconditions were met)
- are_facts(props)[source]¶
Returns whether the propositions are all true in this state.
- Return type
bool
- facts_with_signature(sig)[source]¶
Returns all the known facts with the given signature.
- Return type
Set
[Proposition
]
- has_variable(var)[source]¶
Returns whether this state is aware of the given variable.
- Return type
bool
- is_applicable(action)[source]¶
Check if an action is applicable in this state (i.e. its preconditions are met).
- Return type
bool
- is_sequence_applicable(actions)[source]¶
Check if a sequence of actions are all applicable in this state.
- Return type
bool
- variables_of_type(type)[source]¶
Returns all the known variables of the given type.
- Return type
Set
[Variable
]
- property facts: Iterable[textworld.logic.Proposition]¶
All the facts in the current state.
- Return type
Iterable
[Proposition
]
- property variables: Iterable[textworld.logic.Variable]¶
All the variables tracked by the current state.
- Return type
Iterable
[Variable
]
- class textworld.logic.Type(name, parents)[source]¶
Bases:
object
A variable type.
- property ancestors: Iterable[textworld.logic.Type]¶
The ancestors of this type (not including itself).
- Return type
Iterable
[Type
]
- property child_types: Iterable[textworld.logic.Type]¶
The direct children of this type.
- Return type
Iterable
[Type
]
- property children: Iterable[str]¶
The names of the direct children of this type.
- Return type
Iterable
[str
]
- property descendants: Iterable[textworld.logic.Type]¶
The descendants of this type (not including itself).
- Return type
Iterable
[Type
]
- property parent_types: Iterable[textworld.logic.Type]¶
The parents of this type as Type objects.
- Return type
Iterable
[Type
]
- property subtypes: Iterable[textworld.logic.Type]¶
This type and its descendants.
- Return type
Iterable
[Type
]
- property supertypes: Iterable[textworld.logic.Type]¶
This type and its ancestors.
- Return type
Iterable
[Type
]
- class textworld.logic.TypeHierarchy[source]¶
Bases:
object
A hierarchy of types.
- closure(type, expand)[source]¶
Compute the transitive closure in a type lattice according to some type relationship (generally direct sub-/super-types).
Such a lattice may look something like this:
A / \ B C \ / D
so the closure of D would be something like [B, C, A].
- Return type
Iterable
[Type
]
- multi_ancestors(types)[source]¶
Compute the ancestral closure of a sequence of types. If these are the types of some variables, the result will be all the function parameter types that could also accept those variables.
- Return type
Iterable
[Collection
[Type
]]
- multi_closure(types, expand)[source]¶
Compute the transitive closure of a sequence of types in a type lattice induced by some per-type relationship (generally direct sub-/super-types).
For a single type, such a lattice may look something like this:
A / \ B C \ / D
so the closure of D would be something like [B, C, A]. For multiple types at once, the lattice is more complicated:
__ (A,A) __ / | | \ (A,B) (A,C) (B,A) (C,A) ******************************* (A,D) (B,B) (B,C) (C,B) (C,C) (D,A) ******************************* (B,D) (C,D) (D,B) (D,C) \ | | / \_ (D,D) _/
- Return type
Iterable
[Collection
[Type
]]
- multi_descendants(types)[source]¶
Compute the descendant closure of a sequence of types. If these are the types of some function parameters, the result will be all the variable types that could also be passed to this function.
- Return type
Iterable
[Collection
[Type
]]
- class textworld.logic.Variable(name, type=None)[source]¶
Bases:
object
A variable representing an object in a world.
Create a Variable.
- Parameters
name (
str
) – The (unique) name of the variable.type (optional) – The type of the variable. Defaults to the same as the name.
- name¶
- type¶
- class textworld.logic.model.ActionNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- name = None¶
- postconditions = None¶
- preconditions = None¶
- class textworld.logic.model.ActionPreconditionNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- condition = None¶
- preserve = None¶
- class textworld.logic.model.AliasNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- lhs = None¶
- rhs = None¶
- class textworld.logic.model.ConstraintsNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- constraints = None¶
- class textworld.logic.model.DocumentNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- types = None¶
- class textworld.logic.model.GameLogicModelBuilderSemantics(context=None, types=None)[source]¶
Bases:
tatsu.semantics.ModelBuilderSemantics
- class textworld.logic.model.Inform7CodeNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- code = None¶
- class textworld.logic.model.Inform7CommandNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- command = None¶
- event = None¶
- rule = None¶
- class textworld.logic.model.Inform7CommandsNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- commands = None¶
- class textworld.logic.model.Inform7Node(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- parts = None¶
- class textworld.logic.model.Inform7PredicateNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- predicate = None¶
- source = None¶
- class textworld.logic.model.Inform7PredicatesNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- predicates = None¶
- class textworld.logic.model.Inform7TypeNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- definition = None¶
- kind = None¶
- class textworld.logic.model.ModelBase(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
tatsu.objectmodel.Node
- class textworld.logic.model.PlaceholderNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- name = None¶
- type = None¶
- class textworld.logic.model.PredicateNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- name = None¶
- parameters = None¶
- class textworld.logic.model.PredicatesNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- predicates = None¶
- class textworld.logic.model.PropositionNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- arguments = None¶
- name = None¶
- class textworld.logic.model.ReverseRuleNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- lhs = None¶
- rhs = None¶
- class textworld.logic.model.ReverseRulesNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- reverse_rules = None¶
- class textworld.logic.model.RuleNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- name = None¶
- postconditions = None¶
- preconditions = None¶
- class textworld.logic.model.RulePreconditionNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- condition = None¶
- preserve = None¶
- class textworld.logic.model.RulesNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- rules = None¶
- class textworld.logic.model.SignatureNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- name = None¶
- types = None¶
- class textworld.logic.model.TypeNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- name = None¶
- parts = None¶
- supertypes = None¶
- class textworld.logic.model.VariableNode(ctx=None, ast=None, parseinfo=None, **kwargs)[source]¶
Bases:
textworld.logic.model.ModelBase
- name = None¶
- type = None¶
- class textworld.logic.parser.GameLogicBuffer(text, whitespace=None, nameguard=None, comments_re=None, eol_comments_re='#.*$', ignorecase=None, namechars='', **kwargs)[source]¶
Bases:
tatsu.buffering.Buffer
- class textworld.logic.parser.GameLogicParser(whitespace=None, nameguard=None, comments_re=None, eol_comments_re='#.*$', ignorecase=None, left_recursion=True, parseinfo=True, keywords=None, namechars='', buffer_class=<class 'textworld.logic.parser.GameLogicBuffer'>, **kwargs)[source]¶
Bases:
tatsu.parsing.Parser