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.
- 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[Proposition]¶
All the new propositions being introduced by this action.
- Return type:
Collection
[Proposition
]
- property all_propositions: Collection[Proposition]¶
All the pre- and post-conditions.
- Return type:
Collection
[Proposition
]
- property removed: Collection[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.- 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 is_negation: bool¶
- Return type:
bool
- 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:
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¶
- property is_negation: bool¶
- Return type:
bool
- 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.- 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.
- 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:
- classmethod parse_conjunctive_query(expr)[source]¶
Parse a conjunctive query expression framed as a rule.
A conjunctive query is similar to a Rule’s precondition, but it doesn’t allow carry-over predicate (i.e., starting with a $).
- Parameters:
expr (
str
) – The string to parse, in the formname :: 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 added: Collection[Predicate]¶
All the new predicates being introduced by this rule.
- Return type:
Collection
[Predicate
]
- class textworld.logic.Signature(name, types)[source]¶
Bases:
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.
- 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.
- 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.- 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[Proposition]¶
All the facts in the current state.
- Return type:
Iterable
[Proposition
]
- class textworld.logic.Type(name, parents)[source]¶
Bases:
object
A variable type.
- property ancestors: Iterable[Type]¶
The ancestors of this type (not including itself).
- Return type:
Iterable
[Type
]
- property children: Iterable[str]¶
The names of the direct children of this type.
- Return type:
Iterable
[str
]
- property descendants: Iterable[Type]¶
The descendants of this type (not including itself).
- 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(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, name=None, postconditions=None, preconditions=None)[source]¶
Bases:
ModelBase
- name: Any = None¶
- postconditions: Any = None¶
- preconditions: Any = None¶
- class textworld.logic.model.ActionPreconditionNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, condition=None, preserve=None)[source]¶
Bases:
ModelBase
- condition: Any = None¶
- preserve: Any = None¶
- class textworld.logic.model.AliasNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, lhs=None, rhs=None)[source]¶
Bases:
ModelBase
- lhs: Any = None¶
- rhs: Any = None¶
- class textworld.logic.model.ConstraintsNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, constraints=None)[source]¶
Bases:
ModelBase
- constraints: Any = None¶
- class textworld.logic.model.DocumentNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, types=None)[source]¶
Bases:
ModelBase
- types: Any = None¶
- class textworld.logic.model.GameLogicModelBuilderSemantics(context=None, types=None)[source]¶
Bases:
ModelBuilderSemantics
- class textworld.logic.model.Inform7CodeNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, code=None)[source]¶
Bases:
ModelBase
- code: Any = None¶
- class textworld.logic.model.Inform7CommandNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, command=None, event=None, rule=None)[source]¶
Bases:
ModelBase
- command: Any = None¶
- event: Any = None¶
- rule: Any = None¶
- class textworld.logic.model.Inform7CommandsNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, commands=None)[source]¶
Bases:
ModelBase
- commands: Any = None¶
- class textworld.logic.model.Inform7Node(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, parts=None)[source]¶
Bases:
ModelBase
- parts: Any = None¶
- class textworld.logic.model.Inform7PredicateNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, predicate=None, source=None)[source]¶
Bases:
ModelBase
- predicate: Any = None¶
- source: Any = None¶
- class textworld.logic.model.Inform7PredicatesNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, predicates=None)[source]¶
Bases:
ModelBase
- predicates: Any = None¶
- class textworld.logic.model.Inform7TypeNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, definition=None, kind=None)[source]¶
Bases:
ModelBase
- definition: Any = None¶
- kind: Any = None¶
- class textworld.logic.model.ModelBase(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None)[source]¶
Bases:
Node
- class textworld.logic.model.PlaceholderNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, name=None, type=None)[source]¶
Bases:
ModelBase
- name: Any = None¶
- type: Any = None¶
- class textworld.logic.model.PredicateNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, name=None, parameters=None)[source]¶
Bases:
ModelBase
- name: Any = None¶
- parameters: Any = None¶
- class textworld.logic.model.PredicatesNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, predicates=None)[source]¶
Bases:
ModelBase
- predicates: Any = None¶
- class textworld.logic.model.PropositionNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, arguments=None, name=None)[source]¶
Bases:
ModelBase
- arguments: Any = None¶
- name: Any = None¶
- class textworld.logic.model.ReverseRuleNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, lhs=None, rhs=None)[source]¶
Bases:
ModelBase
- lhs: Any = None¶
- rhs: Any = None¶
- class textworld.logic.model.ReverseRulesNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, reverse_rules=None)[source]¶
Bases:
ModelBase
- reverse_rules: Any = None¶
- class textworld.logic.model.RuleNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, name=None, postconditions=None, preconditions=None)[source]¶
Bases:
ModelBase
- name: Any = None¶
- postconditions: Any = None¶
- preconditions: Any = None¶
- class textworld.logic.model.RulePreconditionNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, condition=None, preserve=None)[source]¶
Bases:
ModelBase
- condition: Any = None¶
- preserve: Any = None¶
- class textworld.logic.model.RulesNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, rules=None)[source]¶
Bases:
ModelBase
- rules: Any = None¶
- class textworld.logic.model.SignatureNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, name=None, types=None)[source]¶
Bases:
ModelBase
- name: Any = None¶
- types: Any = None¶
- class textworld.logic.model.TypeNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, name=None, parts=None, supertypes=None)[source]¶
Bases:
ModelBase
- name: Any = None¶
- parts: Any = None¶
- supertypes: Any = None¶
- class textworld.logic.model.VariableNode(_parent=None, _children=None, ast=None, ctx=None, parseinfo=None, name=None, type=None)[source]¶
Bases:
ModelBase
- name: Any = None¶
- type: Any = None¶
- class textworld.logic.parser.GameLogicBuffer(text, /, config=None, **settings)[source]¶
Bases:
Buffer