Class LogicalWorld
- All Implemented Interfaces:
Named
world model implements the internal logic of a
story world using logical propositions and describes its
elements using a rule-based describer.- Author:
- Stephen G. Ware
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionLogicalWorld(AssetBuilder builder) Constructs a logical world model from the assets defined in an asset builder.LogicalWorld(String name, Collection<Entity> entities, Collection<Variable> variables, Collection<Action> actions, Collection<Ending> endings) Constructs a logical world model with the given assets. -
Method Summary
Modifier and TypeMethodDescriptionprotected static final RoleDetermines whose turn it is based on the history of a session.describe()Returns a long string describing every element of a story world model for use in debugging.protected Turn[]getChoices(Role role, Turn[] history, State state, Ending ending) Returns an array of choices available to a given role based on the current state of a story world, or null if no choices are available.getCondition(Ending ending) Returns a logical proposition that defines when an ending should occur.Returns the describer this world model uses to describe its objects.getDescription(Object object, Role to) Returns a phrase or sentence to describe an object to the given role using this world models'describer.Effect[]getEffects(Action action) Returns an array of effects which express how an action modifies the state of a story world when it happens.protected EndingDetermines whether the story has ended and, if so, returns the ending.protected StringgetEntityDescription(Role role, Turn[] history, State state, Entity entity) Returns a longer, more helpful description of an entity based on the current state of a story world.protected Entity[]getEntityDescriptions(Role role, Turn[] history, State state) Returns an array of entitiesvisibleto a given role which haveuseful descriptions.getInitialValue(Variable variable) Returns the value a given variable has when the story first begins.getPrecondition(Action action) Returns a logical proposition that defines when an action can be taken.protected StatusCalculates the ending (if any), entity descriptions, and turns available to include in a status.getVisibility(Action action) Returns a logical proposition that defines when theplayerwould see an action happen.getVisibility(Entity entity) Returns a logical proposition that defines when theplayercan see an entity.getVisibility(Variable variable) Returns a logical proposition that defines when theplayercan see a varaiable's value.booleanisAvailable(Role role, Action action, State state) Checks whether the given action is available is available to the given role in the given state.booleanChecks whether the given action would be seen by the given role if it happened in the given state.booleanChecks whether the given role can see the given entity in the given state.booleanChecks whether the given role can see the value of the given variable in the given state.static LogicalWorldReads a serialized logical world model from a file.static LogicalWorldReads a serialized logical world model from some input source.voidsetCondition(Ending ending, Proposition condition) Sets the logical proposition that defines when an ending should occur.voidsetDescriber(Describer describer) Sets the describer used by this world model to describe its objects.voidsetEffects(Action action, Effect... effects) Sets the arrays of effects which express how an action modifies the state of a story world when it happens.voidsetInitialValue(Variable variable, Value value) Sets the value a given variable has when the story first begins.voidsetPrecondition(Action action, Proposition precondition) Sets the logical proposition that defines when an action can be taken.voidsetVisibility(Action action, Proposition visibility) Sets the logical proposition that defines when theplayercan see an action happen.voidsetVisibility(Entity entity, Proposition visibility) Sets the logical proposition that defines when theplayercan see an entity.voidsetVisibility(Variable variable, Proposition visibility) Sets the logical proposition that defines when theplayercan see the value of a variable.Returns the initial state of the story world for a given role as a status object.transition(Status status, State actual, Turn turn) Given the current status of a story world (actual or perceived), the actual current status of a story world, and a turn, this method simulates that turn and returns the new perceived status of the story world.voidSerializes this logical world model to a file.voidSerializes this logical world model to an output stream.Methods inherited from class World
contains, contains, contains, contains, getAction, getAction, getAction, getActions, getEnding, getEnding, getEnding, getEndings, getEntities, getEntity, getEntity, getName, getPlayer, getVariable, getVariable, getVariable, getVariables, requireAction, requireAction, requireAction, requireEnding, requireEnding, requireEnding, requireEntity, requireEntity, requireVariable, requireVariable, requireVariable, toString, validate, validate, validate, validate, validate
-
Constructor Details
-
LogicalWorld
public LogicalWorld(String name, Collection<Entity> entities, Collection<Variable> variables, Collection<Action> actions, Collection<Ending> endings) Constructs a logical world model with the given assets.- Parameters:
name- a unique name for the story worldentities- a collection of objects that exist in the worldvariables- a collection of variables whose values can change and whose current values reflect the current state of the worldactions- a collection of ways the world state can changeendings- a collection of ways a story in this world can end- Throws:
IllegalArgumentException- if the provided assets do not meet the required format, such as two assets of the same type having the same name
-
LogicalWorld
Constructs a logical world model from the assets defined in an asset builder.- Parameters:
builder- the asset builder that defines the assets to be used in this story world- Throws:
IllegalArgumentException- if the provided assets do not meet the required format, such as two assets of the same type having the same name
-
-
Method Details
-
isVisible
Checks whether the given role can see the given entity in the given state. Thegame mastercan always see all entities. Whether theplayercan see an entity depends onthe entity's visibility.- Parameters:
role- the role whose ability to see is being checkedentity- the entity whose viability is being checkedstate- the state in which the check will occur- Returns:
- true if the role can see the entity in the given state, false otherwise
-
getVisibility
Returns a logical proposition that defines when theplayercan see an entity. If no proposition has beenset, this method returns the constantfalse.- Parameters:
entity- an entity from this story world- Returns:
- a proposition that will evaluate to true in states where the player can see the entity and false in states where the player cannot see the entity
-
setVisibility
Sets the logical proposition that defines when theplayercan see an entity.- Parameters:
entity- an entity from this story worldvisibility- a logical proposition that evaluates to true in states where the player can see the entity and false in states where the player cannot see the entity
-
getInitialValue
-
setInitialValue
-
isVisible
Checks whether the given role can see the value of the given variable in the given state. Thegame mastercan always see all variable values. Whether theplayercan see a variable's value depends onthe variable's visibility.- Parameters:
role- the role whose ability to see is being checkedvariable- the variable whose visibility is being checkedstate- the state in which the check will occur- Returns:
- true if the role can see the variable's value in the given state, false otherwise
-
getVisibility
Returns a logical proposition that defines when theplayercan see a varaiable's value. If no proposition has beenset, this method returns the constanttrue.Note that if a player
sees an actionthat updates the value of a variable, they will observe the update, even if the variable is not visible. Variable visibility is intended for situations where a variable changed because of an action that the player did not observe, but the player should (either immediately or later) see this change.For example, suppose there are three rooms: A, B, and C. The player is in room A and a second non-player character is in room C. Suppose the location of the second character is visible to the player when the player and that second character are in the same room together. Now suppose the second character walks to room B. The player does not see this action happen, so they do not know about the change to that character's location, and the player's status continues to report that the second character is in room C. Now suppose the player walks into room B. The player should notice the other character. This is accomplished via variable visibility. The player's status will now report that the second character is in room B, because the variable for that character's location is visible.
When a player cannot see a variable's value, the player's
statusshould report the last known value of that variable.- Parameters:
variable- a variable from this story world- Returns:
- a proposition that will evaluate to true in states where the player can see the variable's value and false in states where the player cannot see the variable's value
-
setVisibility
Sets the logical proposition that defines when theplayercan see the value of a variable.- Parameters:
variable- a variable from this story worldvisibility- a logical proposition that evaluates to true in states where the player can see the variable's value and false in states where the player cannot see the variable's value
-
isAvailable
Checks whether the given action is available is available to the given role in the given state. An action is available to a role whenits preconditionis satisfied in the current state and the roleconsentsto the action (i.e. has to approve taking the action).- Parameters:
role- the role whose consent is being checkedaction- the action whose precondition is being checkedstate- the state in which the check will occur- Returns:
- true if the action is possible and the role consents to it, false otherwise
-
getPrecondition
Returns a logical proposition that defines when an action can be taken. If no proposition has beenset, this method returns the constanttrue.- Parameters:
action- an action from this story world- Returns:
- a proposition that will evaluate to true in states where the action can be taken and false in states where the action cannot be taken
-
setPrecondition
Sets the logical proposition that defines when an action can be taken.- Parameters:
action- an action from this story worldprecondition- a logical proposition that evaluates to true in states where the action can be taken and false in states where the action cannot be taken
-
getEffects
Returns an array of effects which express how an action modifies the state of a story world when it happens. If no effects have beenset, this method returns an empty array.- Parameters:
action- an action from this story world- Returns:
- an (possibly empty) array of effects that express how the action modifies a state
-
setEffects
-
isVisible
Checks whether the given action would be seen by the given role if it happened in the given state. Thegame mastercan always see all actions. Whether theplayercan see a an action occur depends onthe action's visibility.- Parameters:
role- the role whose ability to see is being checkedaction- the action whose visibility is being checkedstate- the state in which the check will occur- Returns:
- true if the role would see the action occurs in the given state, false otherwise
-
getVisibility
Returns a logical proposition that defines when theplayerwould see an action happen. If no proposition has beenset, this method returns the constanttrue.When the player sees an action, it means the action will appear in the
historyof the story world and the changes to the state caused by the action'seffectsare reflected in the state. When the player does not see an action, theirstatuswill appear as if the action did not happen, meaning it will not appear in the history and the variables changed by the action's effects will not be reflected in the current state.- Parameters:
action- an action from this story world- Returns:
- a proposition that will evaluate to true in states where the player would see the action occur and false in states where the player would not see the action occur
-
setVisibility
Sets the logical proposition that defines when theplayercan see an action happen.- Parameters:
action- an action from this story worldvisibility- a logical proposition that evaluates to true in states where the player would see the action occur and false in states where the player would not see the action occur
-
getCondition
Returns a logical proposition that defines when an ending should occur. If no proposition has beenset, this method returns the constantfalse. These conditions are checked an action changes the story state, in order byending. If any conditions evaluates to true, the story immediately ends with the corresponding ending.- Parameters:
ending- an ending from this story world- Returns:
- a proposition that will evaluate to true in states where the ending occurs and false in states where the ending does not occur
-
setCondition
Sets the logical proposition that defines when an ending should occur.- Parameters:
ending- an ending from this story worldcondition- a logical proposition that evaluates to true in states where the ending should occur and false in states where the ending should not occur
-
getDescription
Returns a phrase or sentence to describe an object to the given role using this world models'describer.If the object given is an
AssignmentorTurn, the description will be asentence. For all other objects, the description will be aphrase.- Parameters:
object- the object to describerto- the role to whom the object will be described- Returns:
- a description of the object as a phrase or sentence
-
getDescriber
Returns the describer this world model uses to describe its objects.- Returns:
- the describer used by this world model
-
setDescriber
Sets the describer used by this world model to describe its objects.- Parameters:
describer- the new describer this world model should use to describe its objects
-
start
Description copied from class:WorldModelReturns the initial state of the story world for a given role as a status object. For thegame master, the initial status should completely and accurately define the initial state of all variables and entities. For theplayer, the initial status should should define the initial state of all variables and entities that the player knows about at the start of the story. The initial status should have nohistory.- Specified by:
startin classWorldModel- Parameters:
role- the role whose initial status is desired- Returns:
- the initial status of the story world for that role
-
transition
Description copied from class:WorldModelGiven the current status of a story world (actual or perceived), the actual current status of a story world, and a turn, this method simulates that turn and returns the new perceived status of the story world. The status returned will be for the samerole as the first status argument. Since thegame mastershould always know the actual status of the story world, when calculating a new status for the game master, the first two status arguments should be the same. When calculating a new status for theplayer, the first status argument should be the player's perceived status and the second should be the actual status available to the game master. The status returned will be the new status as perceived by that role (the new actual status for the game master or the new perceived status for the player).- Specified by:
transitionin classWorldModel- Parameters:
status- the current status of the story world as perceived by the status' participantactual- the actual current status of the story worldturn- the turn to be taken- Returns:
- the new story world status as perceived by the status' participant
-
getStatus
Calculates the ending (if any), entity descriptions, and turns available to include in a status.- Parameters:
role- the role for the statushistory- the history of turns so far in the sessionactual- the actual current stateobserved- the perceived current state- Returns:
- the story world status implied by those inputs
-
getEnding
Determines whether the story has ended and, if so, returns the ending.- Parameters:
role- the role to whom the ending (if any) will be describedhistory- the history of turns so far in the sessionstate- the current state of the story world- Returns:
- an ending, if one has been reached, or null
-
getEntityDescriptions
Returns an array of entitiesvisibleto a given role which haveuseful descriptions.- Parameters:
role- the role for whom visibility will be checked and to whom the entities will be describedhistory- the history of turns so far in the sessionstate- the current state as perceived by the given role- Returns:
- an array of entities that are currently visible to that role with helpful descriptions
-
getEntityDescription
Returns a longer, more helpful description of an entity based on the current state of a story world.- Parameters:
role- the role to whom the entity is being describedhistory- the history of turns so far in the sessionstate- the current state of the story worldentity- the entity being described- Returns:
- a description of the entity that includes relevant details from the current state
-
getChoices
Returns an array of choices available to a given role based on the current state of a story world, or null if no choices are available.- Parameters:
role- the role to whom the choices are available and to whom the choices will be describedhistory- the history of turns so far in the sessionstate- the current actual state of the story worldending- the ending of the story, if any- Returns:
- an array of choices available to the given role, or null if no choices are available
-
current
-
describe
Returns a long string describing every element of a story world model for use in debugging.- Returns:
- a long string describing every element of this story world model
-
read
Reads a serialized logical world model from some input source.- Parameters:
reader- the input source- Returns:
- a logical world model
- Throws:
IOException- if a problem occurs when reading from the input sourcecom.google.gson.JsonIOException- if there was a problem reading JSON data from the Readercom.google.gson.JsonSyntaxException- if the JSON read from the source cannot be parsed as a logical world model
-
read
Reads a serialized logical world model from a file.- Parameters:
file- the file to which a logical world model has been serialized- Returns:
- a logical world model
- Throws:
IOException- if a problem occurs when reading from the filecom.google.gson.JsonIOException- if there was a problem reading JSON data from the Readercom.google.gson.JsonSyntaxException- if the JSON read from the source cannot be parsed as a logical world model
-
write
Serializes this logical world model to an output stream.- Parameters:
writer- the output to which the logical world model will be serialized- Throws:
IOException- if a problem occurs when writing to the output sourcecom.google.gson.JsonIOException- if a problem occurs when writing JSON data to the output source
-
write
Serializes this logical world model to a file.- Parameters:
file- the file to which the logical world model will be serialized- Throws:
IOException- if a problem occurs when writing to the output sourcecom.google.gson.JsonIOException- if a problem occurs when writing JSON data to the output source
-