Package repast.simphony.space.grid
Class AbstractGrid<T,U>
java.lang.Object
repast.simphony.space.projection.DefaultProjection<T>
repast.simphony.space.grid.AbstractGrid<T,U>
- All Implemented Interfaces:
Grid<T>,Projection<T>
- Direct Known Subclasses:
DefaultGrid,FastDenseSingleOccuGrid
Default implementation of an n-dimensional grid.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA little class used for getting rid of some hash table lookups. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CellAccessor<T,U> protected Map<T,AbstractGrid.PointHolder> protected GridDimensionsprotected Uprotected booleanprotected intprotected GridPointTranslatorFields inherited from class repast.simphony.space.projection.DefaultProjection
listeners, name -
Constructor Summary
ConstructorsConstructorDescriptionAbstractGrid(String name, GridAdder<T> adder, GridPointTranslator translator, CellAccessor<T, U> accessor, int... size) Constructs this space with the specified name, adder, translator, accessor and size.AbstractGrid(String name, GridAdder<T> adder, GridPointTranslator translator, CellAccessor<T, U> accessor, int[] size, int[] origin) Constructs this space with the specified name, adder, translator, accessor and size. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract Ubooleanevaluate(ProjectionPredicate predicate) Evaluate this Projection against the specified Predicate.getAdder()Gets the adder used by this space to add new objects.Gets the cell accessor used to control access to individual grid cells.Gets the dimensions of the grid.doublegetDistance(GridPoint point1, GridPoint point2) Gets the the euclidian distance between the GridPoints point1 and point2.doublegetDistanceSq(GridPoint point1, GridPoint point2) Gets the square of the euclidian distance between the GridPoints point1 and point2.Retrieves the rule being used for controlling what happens at or beyond the borders of the space.getLocation(Object obj) Gets the location of the specified object.getObjectAt(int... location) Gets the object at the specified location.Gets all the object currently in the space.getObjectsAt(int... location) Gets all the objects at the specified location.getRandomObjectAt(int... location) Gets a random object from among those at the specified location.protected int[]getTransformedLocation(int... location) booleanTrue if this grid is peridoic (in the sense that moving off one border makes you appear on the other one), otherwise false.moveByDisplacement(T object, int... displacement) Moves the specified object from its current location by the specified amount.moveByVector(T object, double distance, double... anglesInRadians) Moves the specifed object the specified distance from its current position along the specified angle.booleanMoves the specified object from its current location into the new location.protected voidprotected voidvoidSets the adder used by this space to add new objects.voidSets the rule to use for controlling what happens at or beyond the borders of the space.intsize()Gets the number of objects currently in the space.Methods inherited from class repast.simphony.space.projection.DefaultProjection
addProjectionListener, fireProjectionEvent, getName, getProjectionListeners, removeProjectionListenerMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface repast.simphony.space.projection.Projection
addProjectionListener, getName, getProjectionListeners, removeProjectionListener
-
Field Details
-
agentLocationMap
-
locationStorage
-
dimensions
-
adder
-
translator
-
accessor
-
ok
protected boolean ok -
size
protected int size
-
-
Constructor Details
-
AbstractGrid
public AbstractGrid(String name, GridAdder<T> adder, GridPointTranslator translator, CellAccessor<T, U> accessor, int... size) Constructs this space with the specified name, adder, translator, accessor and size. The size is the size of the space meaning [3, 3] is a 3x3 space.- Parameters:
name- the name of the spacesize- the dimensions of the space
-
AbstractGrid
public AbstractGrid(String name, GridAdder<T> adder, GridPointTranslator translator, CellAccessor<T, U> accessor, int[] size, int[] origin) Constructs this space with the specified name, adder, translator, accessor and size. The size is the size of the space meaning [3, 3] is a 3x3 space.- Parameters:
name- the name of the spacesize- the dimensions of the spaceorigin- the origin of the space
-
-
Method Details
-
createLocationStorage
-
moveTo
Moves the specified object from its current location into the new location. The object must previously have been introduced into the space. Objects are introduced into the space by adding them to the context of which this space is a projection.- Specified by:
moveToin interfaceGrid<T>- Parameters:
object-newLocation-- Returns:
- true if the move was successful, otherwise false.
- Throws:
SpatialException- if the object is not already in the space or if the number of dimensions in the location does not agree with the number in the space.
-
getLocation
Gets the location of the specified object.- Specified by:
getLocationin interfaceGrid<T>- Parameters:
obj-- Returns:
- the location of the specified object or null if the object is not in the space.
-
size
public int size()Gets the number of objects currently in the space. This does NOT include any objects that may have been added, but have NOT been moved to a space location. -
getGridPointTranslator
Retrieves the rule being used for controlling what happens at or beyond the borders of the space.- Specified by:
getGridPointTranslatorin interfaceGrid<T>- Returns:
- the rule for handling out of bounds coordinates
-
setGridPointTranslator
Sets the rule to use for controlling what happens at or beyond the borders of the space.- Specified by:
setGridPointTranslatorin interfaceGrid<T>- Parameters:
rule- the rule for handling out of bounds coordinates
-
getObjects
Gets all the object currently in the space. This does NOT include any objects that may have been added, but have NOT been moved to a space location.- Specified by:
getObjectsin interfaceGrid<T>- Returns:
- an iteratable over all the object currently in the space. This does NOT include any objects that may have been added, but have NOT been moved to a space location.
-
getObjectAt
Gets the object at the specified location.- Specified by:
getObjectAtin interfaceGrid<T>- Parameters:
location-- Returns:
- the object at the specified location.
-
getObjectsAt
Gets all the objects at the specified location. For a multi occupancy space this will be all the objects at that location. For a single occupancy space this will be the single object at that location.- Specified by:
getObjectsAtin interfaceGrid<T>- Parameters:
location-- Returns:
- the object at the specified location.
-
getTransformedLocation
protected int[] getTransformedLocation(int... location) -
getRandomObjectAt
Gets a random object from among those at the specified location. If this is a single occupancy space this will return the single object at that location, if any.- Specified by:
getRandomObjectAtin interfaceGrid<T>- Parameters:
location-- Returns:
- the object at the specified location.
-
moveByDisplacement
Moves the specified object from its current location by the specified amount. For examplemoveByDisplacement(object, 3, -2, 1)will move the object by 3 along the x-axis, -2 along the y and 1 along the z. The displacement argument can be less than the number of dimensions in the space in which case the remaining argument will be set to 0. For example,moveByDisplacement(object, 3)will move the object 3 along the x-axis and 0 along the y and z axes, assuming a 3D grid.- Specified by:
moveByDisplacementin interfaceGrid<T>- Parameters:
object- the object to movedisplacement- the amount to move the object- Returns:
- the new location if the move was successful, otherwise null
- Throws:
SpatialException- if the object is not already in the space or if the number of dimensions in the displacement does not agree with the number in the grid.
-
moveByVector
Moves the specifed object the specified distance from its current position along the specified angle. For example,moveByVector(object, 1, Grid.NORTH)will move the object 1 unit "north" up the y-axis, assuming a 2D grid. Similarly,grid.moveByVector(object, 2, 0, Math.toRadians(90), 0)will rotate 90 degrees around the y-axis, thus moving the object 2 units along the z-axis. Note that the radians / degrees are incremented in a anti-clockwise fashion, such that 0 degrees is "east", 90 degrees is "north", 180 is "west" and 270 is "south."- Specified by:
moveByVectorin interfaceGrid<T>- Parameters:
object- the object to movedistance- the distance to moveanglesInRadians- the angle to move along in radians. Note that Math.toRadians(degrees) is useful here- Returns:
- the new location or null if the move is not successful
- Throws:
SpatialException- if the object is not already in the space or if the number of angles is greater than the number of dimensions- See Also:
-
getDimensions
Gets the dimensions of the grid.- Specified by:
getDimensionsin interfaceGrid<T>- Returns:
- the dimensions of the grid.
-
setAdder
Sets the adder used by this space to add new objects. -
getAdder
Gets the adder used by this space to add new objects. -
getCellAccessor
Gets the cell accessor used to control access to individual grid cells.- Specified by:
getCellAccessorin interfaceGrid<T>- Returns:
- the cell accessor used to control access to individual grid cells.
-
removeAll
protected void removeAll() -
remove
-
isPeriodic
public boolean isPeriodic()True if this grid is peridoic (in the sense that moving off one border makes you appear on the other one), otherwise false. A 2D periodic grid is a torus. This is defined by the grid's border behavior which is determined by its GridPointTranslator.- Specified by:
isPeriodicin interfaceGrid<T>- Returns:
- true if this grid is periodic, otherwise false.
-
evaluate
Evaluate this Projection against the specified Predicate. This typically involves a double dispatch where the Projection calls back to the predicate, passing itself.- Specified by:
evaluatein interfaceProjection<T>- Overrides:
evaluatein classDefaultProjection<T>- Parameters:
predicate-- Returns:
- true if the predicate evaluates to true, otherwise false. False can also mean that the predicate is not applicable to this Projection. For example, a linked type predicate evaluated against a grid projection.
-
getDistance
Gets the the euclidian distance between the GridPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN- Specified by:
getDistancein interfaceGrid<T>- Parameters:
point1- the first pointpoint2- the second point- Returns:
- the euclidian distance between the GridPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN
-
getDistanceSq
Gets the square of the euclidian distance between the GridPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN- Specified by:
getDistanceSqin interfaceGrid<T>- Parameters:
point1- the first pointpoint2- the second point- Returns:
- the square of the euclidian distance between the GridPoints point1 and point2. If the points do not have the same dimension then this returns Double.NaN
-