rescuecore
Class Memory

java.lang.Object
  extended byrescuecore.Memory
All Implemented Interfaces:
RescueConstants, java.io.Serializable
Direct Known Subclasses:
ArrayMemory, HashMemory

public abstract class Memory
extends java.lang.Object
implements RescueConstants, java.io.Serializable

This class holds an agents view of the world

See Also:
Serialized Form

Field Summary
 
Fields inherited from interface rescuecore.RescueConstants
AGENT_TYPE_AMBULANCE_CENTER, AGENT_TYPE_AMBULANCE_TEAM, AGENT_TYPE_ANY, AGENT_TYPE_ANY_AGENT, AGENT_TYPE_ANY_BUILDING, AGENT_TYPE_ANY_MOBILE, AGENT_TYPE_CIVILIAN, AGENT_TYPE_FIRE_BRIGADE, AGENT_TYPE_FIRE_STATION, AGENT_TYPE_POLICE_FORCE, AGENT_TYPE_POLICE_OFFICE, AK_ACKNOWLEDGE, AK_CLEAR, AK_CONNECT, AK_EXTINGUISH, AK_LOAD, AK_MOVE, AK_RESCUE, AK_REST, AK_SAY, AK_TELL, AK_UNLOAD, BYTE_SIZE, COMMAND_ACKNOWLEDGE, COMMAND_CLEAR, COMMAND_CONNECT, COMMAND_EXTINGUISH, COMMAND_LOAD, COMMAND_MOVE, COMMAND_RESCUE, COMMAND_REST, COMMAND_SAY, COMMAND_TELL, COMMAND_UNLOAD, FIERYNESS_BURNING, FIERYNESS_BURNT_OUT, FIERYNESS_HEATING, FIERYNESS_INFERNO, FIERYNESS_MODERATELY_BURNT, FIERYNESS_NOT_BURNT, FIERYNESS_SLIGHTLY_BURNT, FIERYNESS_VERY_BURNT, GK_CONNECT_ERROR, GK_CONNECT_OK, HEADER_NULL, INT_SIZE, INTERNAL_TYPE_AMBULANCE_CENTER, INTERNAL_TYPE_AMBULANCE_TEAM, INTERNAL_TYPE_ANY_BUILDING, INTERNAL_TYPE_ANY_HUMANOID, INTERNAL_TYPE_ANY_PLATOON, INTERNAL_TYPE_BUILDING, INTERNAL_TYPE_CAR, INTERNAL_TYPE_CIVILIAN, INTERNAL_TYPE_FIRE_BRIGADE, INTERNAL_TYPE_FIRE_STATION, INTERNAL_TYPE_NODE, INTERNAL_TYPE_POLICE_FORCE, INTERNAL_TYPE_POLICE_OFFICE, INTERNAL_TYPE_REFUGE, INTERNAL_TYPE_RIVER, INTERNAL_TYPE_RIVER_NODE, INTERNAL_TYPE_ROAD, INTERNAL_TYPE_WORLD, KA_CONNECT_ERROR, KA_CONNECT_OK, KA_HEAR, KA_SENSE, KG_ACKNOWLEDGE, KG_CONNECT, KG_UPDATE, KS_COMMANDS, KS_CONNECT_ERROR, KS_CONNECT_OK, KS_UPDATE, KV_CONNECT_ERROR, KV_CONNECT_OK, KV_UPDATE, MAX_CENTER_MESSAGES, MAX_EXTINGUISH_DISTANCE, MAX_EXTINGUISH_POWER, MAX_HP, MAX_PLATOON_MESSAGES, MAX_RESCUE_DISTANCE, MAX_WATER, PROPERTY_BLOCK, PROPERTY_BROKENNESS, PROPERTY_BUILDING_APEXES, PROPERTY_BUILDING_AREA_GROUND, PROPERTY_BUILDING_AREA_TOTAL, PROPERTY_BUILDING_ATTRIBUTES, PROPERTY_BUILDING_CODE, PROPERTY_BUILDING_SHAPE_ID, PROPERTY_BURIEDNESS, PROPERTY_CARS_PASS_TO_HEAD, PROPERTY_CARS_PASS_TO_TAIL, PROPERTY_DAMAGE, PROPERTY_DIRECTION, PROPERTY_EDGES, PROPERTY_ENTRANCES, PROPERTY_FIERYNESS, PROPERTY_FLOORS, PROPERTY_HEAD, PROPERTY_HP, PROPERTY_HUMANS_PASS_TO_HEAD, PROPERTY_HUMANS_PASS_TO_TAIL, PROPERTY_IGNITION, PROPERTY_LATITUDE, PROPERTY_LENGTH, PROPERTY_LINES_TO_HEAD, PROPERTY_LINES_TO_TAIL, PROPERTY_LONGITUDE, PROPERTY_MAX, PROPERTY_MEDIAN_STRIP, PROPERTY_NULL, PROPERTY_POCKET_TO_TURN_ACROSS, PROPERTY_POSITION, PROPERTY_POSITION_EXTRA, PROPERTY_POSITION_HISTORY, PROPERTY_REPAIR_COST, PROPERTY_ROAD_KIND, PROPERTY_SHORTCUT_TO_TURN, PROPERTY_SIGNAL, PROPERTY_SIGNAL_TIMING, PROPERTY_STAMINA, PROPERTY_START_TIME, PROPERTY_STRETCHED_LENGTH, PROPERTY_TAIL, PROPERTY_TYPE_BOOLEAN, PROPERTY_TYPE_INT, PROPERTY_TYPE_INT_ARRAY, PROPERTY_WATER_QUANTITY, PROPERTY_WIDTH, PROPERTY_WIDTH_FOR_WALKERS, PROPERTY_WIND_DIRECTION, PROPERTY_WIND_FORCE, PROPERTY_X, PROPERTY_Y, SAY_LENGTH, SHORT_SIZE, SK_ACKNOWLEDGE, SK_CONNECT, SK_UPDATE, TELL_LENGTH, TYPE_AMBULANCE_CENTER, TYPE_AMBULANCE_TEAM, TYPE_BUILDING, TYPE_CAR, TYPE_CIVILIAN, TYPE_FIRE_BRIGADE, TYPE_FIRE_STATION, TYPE_NODE, TYPE_NULL, TYPE_POLICE_FORCE, TYPE_POLICE_OFFICE, TYPE_REFUGE, TYPE_RIVER, TYPE_RIVER_NODE, TYPE_ROAD, TYPE_WORLD, VK_ACKNOWLEDGE, VK_CONNECT
 
Constructor Summary
protected Memory()
          Construct a new empty memory
 
Method Summary
 void add(RescueObject o, int timestamp)
          Add a new object
 void addMemoryListener(MemoryListener l)
          Add a MemoryListener that will be informed of adds and updates
 boolean canPass(Road road, int positionExtra, Node target)
          Test whether an agent can pass a particular road in a certain direction
 RescueObject[] findBuildingNeighbours(Building b)
          Find the neighbours of a Building.
 RescueObject[] findNeighbours(RescueObject o)
          Find the neighbours of a RescueObject.
 RescueObject[] findNodeNeighbours(Node node)
          Find the neighbours of a Node.
 RescueObject[] findRoadNeighbours(Road road)
          Find the neighbours of a Road.
abstract  RescueObject[] getAllObjects()
          Get all objects in memory
abstract  java.util.Collection getAllObjectsAsCollection()
          Get all objects in memory as a Collection
 int getAngle(RescueObject from, RescueObject to)
          Get the angle in arc-seconds from one object to another
 Node getClosestNode(RescueObject o)
          Find the closest Node to a RescueObject.
 Node getClosestNode(Road road, int positionExtra)
          Get the node id (either head or tail of the given road) that is closest to the position given
 double getDistance(RescueObject o1, RescueObject o2)
          Get the Euclidean distance between two objects
 int getHeight()
          Get the height of the world
abstract  RescueObject[] getObjectsOfInternalType(int type)
          Get all objects of a particular internal type
abstract  RescueObject[] getObjectsOfType(int type)
          Get all objects of a particular type
 int getWidth()
          Get the width of the world
 int[] getXY(RescueObject o)
          Get the X and Y coordinates of an object
abstract  RescueObject lookup(int id)
          Look up a RescueObject by id
 void remove(RescueObject o)
          Remove an object from the memory
 void removeMemoryListener(MemoryListener l)
          Remove a MemoryListener
 void update(java.io.DataInput in, int timestep)
          Update our memory from an Objects structure
 void updateFromKA_SENSE(byte[] data, MutableInteger index, int timestep)
          Deprecated. Replaced by @{link update(DataInput, int)}
 void updateFromKS_UPDATE(byte[] data, MutableInteger index, int timestep)
          Deprecated. Replaced by @{link update(DataInput, int)}
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Memory

protected Memory()
Construct a new empty memory

Method Detail

getWidth

public int getWidth()
Get the width of the world

Returns:
The width of the world

getHeight

public int getHeight()
Get the height of the world

Returns:
The height of the world

addMemoryListener

public void addMemoryListener(MemoryListener l)
Add a MemoryListener that will be informed of adds and updates

Parameters:
l - The MemoryListener to add

removeMemoryListener

public void removeMemoryListener(MemoryListener l)
Remove a MemoryListener

Parameters:
l - The MemoryListener to remove

lookup

public abstract RescueObject lookup(int id)
Look up a RescueObject by id

Parameters:
id - The id of the object we want
Returns:
The object with the given id, or null if there are no objects with that id

getAllObjects

public abstract RescueObject[] getAllObjects()
Get all objects in memory

Returns:
An array of all objects in memory

getAllObjectsAsCollection

public abstract java.util.Collection getAllObjectsAsCollection()
Get all objects in memory as a Collection

Returns:
A Collection of all objects in memory

getObjectsOfType

public abstract RescueObject[] getObjectsOfType(int type)
Get all objects of a particular type

Parameters:
type - The type we want
Returns:
An array of all objects of the given type
See Also:
RescueConstants.TYPE_CIVILIAN, RescueConstants.TYPE_FIRE_BRIGADE, RescueConstants.TYPE_AMBULANCE_TEAM, RescueConstants.TYPE_POLICE_FORCE, RescueConstants.TYPE_ROAD, RescueConstants.TYPE_NODE, RescueConstants.TYPE_RIVER, RescueConstants.TYPE_RIVER_NODE, RescueConstants.TYPE_BUILDING, RescueConstants.TYPE_REFUGE, RescueConstants.TYPE_FIRE_STATION, RescueConstants.TYPE_AMBULANCE_CENTER, RescueConstants.TYPE_POLICE_OFFICE, RescueConstants.TYPE_WORLD, RescueConstants.TYPE_CAR

getObjectsOfInternalType

public abstract RescueObject[] getObjectsOfInternalType(int type)
Get all objects of a particular internal type

Parameters:
type - The type we want
Returns:
An array of all objects of the given internal type
See Also:
RescueConstants.INTERNAL_TYPE_CIVILIAN, RescueConstants.INTERNAL_TYPE_FIRE_BRIGADE, RescueConstants.INTERNAL_TYPE_AMBULANCE_TEAM, RescueConstants.INTERNAL_TYPE_POLICE_FORCE, RescueConstants.INTERNAL_TYPE_CAR, RescueConstants.INTERNAL_TYPE_BUILDING, RescueConstants.INTERNAL_TYPE_REFUGE, RescueConstants.INTERNAL_TYPE_FIRE_STATION, RescueConstants.INTERNAL_TYPE_POLICE_OFFICE, RescueConstants.INTERNAL_TYPE_AMBULANCE_CENTER, RescueConstants.INTERNAL_TYPE_ROAD, RescueConstants.INTERNAL_TYPE_NODE, RescueConstants.INTERNAL_TYPE_RIVER, RescueConstants.INTERNAL_TYPE_RIVER_NODE, RescueConstants.INTERNAL_TYPE_WORLD, RescueConstants.INTERNAL_TYPE_ANY_BUILDING, RescueConstants.INTERNAL_TYPE_ANY_HUMANOID

add

public void add(RescueObject o,
                int timestamp)
Add a new object

Parameters:
o - The new object
timestamp - The time that this object is added

remove

public void remove(RescueObject o)
Remove an object from the memory

Parameters:
o - The object to be removed

update

public final void update(java.io.DataInput in,
                         int timestep)
                  throws UnknownPropertyException,
                         UnknownTypeException,
                         java.io.IOException
Update our memory from an Objects structure

Parameters:
in - The DataInput to read data from
timestep - The current timestep
Throws:
UnknownTypeException - if an unknown object type appears
UnknownPropertyException - if an unknown property shows up
java.io.IOException - if there is an IO problem

updateFromKA_SENSE

public final void updateFromKA_SENSE(byte[] data,
                                     MutableInteger index,
                                     int timestep)
                              throws UnknownPropertyException,
                                     UnknownTypeException
Deprecated. Replaced by @{link update(DataInput, int)}

Update our memory from a KA_SENSE message

Parameters:
data - The body of the KA_SENSE
index - The index to start reading the message from. This index should be updated to point to the byte after the end of the message after decoding
timestep - The current timestep
Throws:
UnknownTypeException - if an unknown object type appears
UnknownPropertyException - if an unknown property shows up

updateFromKS_UPDATE

public final void updateFromKS_UPDATE(byte[] data,
                                      MutableInteger index,
                                      int timestep)
                               throws UnknownPropertyException,
                                      UnknownTypeException
Deprecated. Replaced by @{link update(DataInput, int)}

Update our memory from a KS_UPDATE message

Parameters:
data - The body of the KS_UPDATE
index - The index to start reading the message from. This index should be updated to point to the byte after the end of the message after decoding
timestep - The current timestep
Throws:
UnknownTypeException - if an unknown object type appears
UnknownPropertyException - if an unknown property shows up

getDistance

public double getDistance(RescueObject o1,
                          RescueObject o2)
Get the Euclidean distance between two objects

Parameters:
o1 - The first object
o2 - The second object
Returns:
The distance between the two objects

getAngle

public int getAngle(RescueObject from,
                    RescueObject to)
Get the angle in arc-seconds from one object to another

Parameters:
from - The first object
to - The second object
Returns:
The angle from 'from' to 'to', in arc-seconds

getXY

public int[] getXY(RescueObject o)
Get the X and Y coordinates of an object

Parameters:
o - The object of interest
Returns:
An array of two ints. The first element is the X coordinate, the second is Y. The return value may be null if the location of the object is unknown

getClosestNode

public Node getClosestNode(RescueObject o)
Find the closest Node to a RescueObject. If the given object is a Node then it is returned. If it is a road, then the head node is returned. If it is a building, then the entrance node closest to the center of the building is returned. If it is a mobile agent (civilian, fire brigade etc) then the closest node to it's location is returned

Parameters:
o - The object we want the closest node to
Returns:
The Node object that is closest to the given RescueObject

getClosestNode

public Node getClosestNode(Road road,
                           int positionExtra)
Get the node id (either head or tail of the given road) that is closest to the position given

Parameters:
road - The road that we want the head or tail of
positionExtra - The agent's position along the road
Returns:
Either the head or tail node of the given road, whichever is closest

findNeighbours

public RescueObject[] findNeighbours(RescueObject o)
Find the neighbours of a RescueObject. This method delegates to @{link #findNodeNeighbours(Node)}, @{link #findRoadNeighbours(Road)} or @{link #findBuildingNeighbours(Building)} for Nodes, Roads and Buildings respectively

Parameters:
o - The RescueObject we want the neighbours for
Returns:
An array of all neighbours of the given object

findNodeNeighbours

public RescueObject[] findNodeNeighbours(Node node)
Find the neighbours of a Node. This method returns all RescueObjects identified by this Nodes 'edges' property

Parameters:
node - The Node we want the neighbours for
Returns:
An array of all neighbours of the given Node

findRoadNeighbours

public RescueObject[] findRoadNeighbours(Road road)
Find the neighbours of a Road. This method returns the head and tail nodes connected to this Road

Parameters:
road - The Road we want the neighbours for
Returns:
An array containing the head and tail nodes, in that order

findBuildingNeighbours

public RescueObject[] findBuildingNeighbours(Building b)
Find the neighbours of a Building. This method returns all Nodes identified by this Buildings 'entrances' property

Parameters:
b - The Building we want the neighbours for
Returns:
An array of all Nodes identified by the 'entrances' property

canPass

public boolean canPass(Road road,
                       int positionExtra,
                       Node target)
Test whether an agent can pass a particular road in a certain direction

Parameters:
road - The road to test
positionExtra - The distance along the road where the agent is now
target - The target node - either the head or the tail of this road
Returns:
true if and only if the agent can pass in that direction - this will be true if the road is not blocked, or if the agent is on the near side of the blockage