Package jsl.modeling.elements.spatial
Class SpatialElement
- java.lang.Object
-
- jsl.modeling.elements.spatial.SpatialElement
-
- All Implemented Interfaces:
PositionIfc
,SpatialElementIfc
,GetNameIfc
,IdentityIfc
public class SpatialElement extends java.lang.Object implements SpatialElementIfc
A SpatialElement represents an object that is within a spatial model. A spatial element can be in one and only one spatial model at a time. A spatial model may contain many spatial elements. Spatial elements can be added to and removed from a spatial model. A spatial element has a position within a spatial model. The position should allow the distance between spatial elements to be computed within a spatial model. If the position of the spatial element is changed then the spatial element should notify any position observers and should indicate to its spatial model that the position has changed. A spatial element should start in one spatial model. The starting spatial model is the element's initial model. A spatial element may move to other spatial models during a simulation and thus should always have a current spatial model. A spatial element may be related to a ModelElement.
-
-
Field Summary
Fields Modifier and Type Field Description static int
CHANGED_POSITION
An enum to indicate that an element changed its position within a spatial model
-
Constructor Summary
Constructors Constructor Description SpatialElement(SpatialModel spatialModel)
Creates a spatial element with the given spatial model.SpatialElement(SpatialModel spatialModel, double x, double y)
Creates a spatial element at the given (x, y, 0.0) coordinatesSpatialElement(SpatialModel spatialModel, double x, double y, double z)
Creates a spatial element at the given (x,y,z) coordinatesSpatialElement(SpatialModel spatialModel, double x, double y, double z, java.lang.String name)
Creates a spatial element at the given (x,y,z) coordinatesSpatialElement(SpatialModel spatialModel, double x, double y, java.lang.String name)
Creates a spatial element at the given (x, y, 0.0) coordinatesSpatialElement(SpatialModel spatialModel, java.lang.String name)
Creates a spatial element with the given parent and spatial model.SpatialElement(SpatialModel spatialModel, CoordinateIfc coordinate)
Creates a spatial element with the given parent and spatial model.SpatialElement(SpatialModel spatialModel, CoordinateIfc coordinate, java.lang.String name)
Creates a spatial element with the given parent and spatial model.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
attachPositionObserver(ObserverIfc observer)
Implementor of this interface should allow Observers to be attached.void
changeSpatialModel(SpatialModel spatialModel, CoordinateIfc coordinate)
Changes the spatial model for this element and places the element at the supplied coordinate within the new spatial model.double
distanceTo(CoordinateIfc coordinate)
This is a "convenience" method for getting the distance from this element to the supplied coordinate within the underlying spatial modeldouble
distanceTo(SpatialElementIfc element)
This is a "convenience" method for getting the distance from this element to the supplied element within the underlying spatial model Requirement: The elements must be in the same spatial model.int
getId()
CoordinateIfc
getInitialPosition()
SpatialModel
getInitialSpatialModel()
Returns the spatial model that should hold this element at the beginning of each replication of a simulationModelElement
getModelElement()
Gets the ModelElement associated with this spatial element May be nulljava.lang.String
getName()
int
getObserverState()
Observers can call this to get an integer representing the state of the element after the observers have been notifiedCoordinateIfc
getPosition()
CoordinateIfc
getPreviousPosition()
SpatialModel
getSpatialModel()
Returns the current spatial model that contains this elementvoid
initializeSpatialElement()
This method should be called to initialize the spatial element prior to running a simulationboolean
isPositionEqualTo(CoordinateIfc coordinate)
Returns true if the position of this element is the same as supplied coordinate within the underlying spatial model.boolean
isPositionEqualTo(SpatialElementIfc element)
Returns true if the position of this element is the same as the position of the supplied element within the underlying spatial model.protected void
notifyObservers(int observerState)
protected void
notifyObservers(java.lang.Object theObserved, java.lang.Object arg)
void
removePositionObserver(ObserverIfc observer)
Remove the observer from this PositionIfcprotected void
setCurrentPosition(CoordinateIfc currentPosition)
void
setInitialPosition(CoordinateIfc initialPosition)
Sets the initial position of the element.protected void
setInitialSpatialModel(SpatialModel spatialModel)
Sets the initial spatial model for the position.protected void
setModelElement(ModelElement modelElement)
Sets the model element associated with this spatial element, may be nullprotected void
setName(java.lang.String str)
Sets the name of this elementprotected void
setObserverState(int observerState)
Can be called by sub-classes to set an indicator of the state of the position prior to notifying observersprotected void
setPreviousPosition(CoordinateIfc previousPosition)
protected void
setSpatialModel(SpatialModel spatialModel)
Sets the spatial model for the spatial element.java.lang.String
toString()
-
-
-
Constructor Detail
-
SpatialElement
public SpatialElement(SpatialModel spatialModel)
Creates a spatial element with the given spatial model. The CoordinateIfc will be the default within the supplied spatial model.- Parameters:
spatialModel
-
-
SpatialElement
public SpatialElement(SpatialModel spatialModel, CoordinateIfc coordinate)
Creates a spatial element with the given parent and spatial model. TThe CoordinateIfc must be valid within the supplied spatial model.- Parameters:
spatialModel
-coordinate
-
-
SpatialElement
public SpatialElement(SpatialModel spatialModel, java.lang.String name)
Creates a spatial element with the given parent and spatial model. TThe coordinate will be the default within the supplied spatial model.- Parameters:
spatialModel
-name
-
-
SpatialElement
public SpatialElement(SpatialModel spatialModel, double x, double y)
Creates a spatial element at the given (x, y, 0.0) coordinates- Parameters:
spatialModel
-x
-y
-
-
SpatialElement
public SpatialElement(SpatialModel spatialModel, double x, double y, java.lang.String name)
Creates a spatial element at the given (x, y, 0.0) coordinates- Parameters:
spatialModel
-x
-y
-name
-
-
SpatialElement
public SpatialElement(SpatialModel spatialModel, double x, double y, double z)
Creates a spatial element at the given (x,y,z) coordinates- Parameters:
spatialModel
-x
-y
-z
-
-
SpatialElement
public SpatialElement(SpatialModel spatialModel, double x, double y, double z, java.lang.String name)
Creates a spatial element at the given (x,y,z) coordinates- Parameters:
spatialModel
-x
-y
-z
-name
-
-
SpatialElement
public SpatialElement(SpatialModel spatialModel, CoordinateIfc coordinate, java.lang.String name)
Creates a spatial element with the given parent and spatial model. The coordinate must be valid within the supplied spatial model.- Parameters:
spatialModel
-coordinate
-name
-
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getName
public final java.lang.String getName()
- Specified by:
getName
in interfaceGetNameIfc
- Returns:
- a string representing the name of the object
-
getId
public final int getId()
- Specified by:
getId
in interfaceIdentityIfc
- Returns:
- an int representing the id of the object
-
getPosition
public final CoordinateIfc getPosition()
- Specified by:
getPosition
in interfacePositionIfc
- Specified by:
getPosition
in interfaceSpatialElementIfc
- Returns:
- Returns the current Position.
-
getInitialPosition
public final CoordinateIfc getInitialPosition()
- Specified by:
getInitialPosition
in interfaceSpatialElementIfc
- Returns:
- Returns the initialPosition.
-
getPreviousPosition
public final CoordinateIfc getPreviousPosition()
- Specified by:
getPreviousPosition
in interfaceSpatialElementIfc
- Returns:
- Returns the previousPosition.
-
getObserverState
public final int getObserverState()
Description copied from interface:SpatialElementIfc
Observers can call this to get an integer representing the state of the element after the observers have been notified- Specified by:
getObserverState
in interfaceSpatialElementIfc
- Returns:
- the observer state
-
distanceTo
public final double distanceTo(CoordinateIfc coordinate)
Description copied from interface:SpatialElementIfc
This is a "convenience" method for getting the distance from this element to the supplied coordinate within the underlying spatial model- Specified by:
distanceTo
in interfaceSpatialElementIfc
- Parameters:
coordinate
- the coordinate to check- Returns:
- the distance
-
isPositionEqualTo
public final boolean isPositionEqualTo(CoordinateIfc coordinate)
Description copied from interface:SpatialElementIfc
Returns true if the position of this element is the same as supplied coordinate within the underlying spatial model. This is not necessarily object reference equality, but rather whether or not the positions within the underlying spatial model can be considered the same (equivalent).- Specified by:
isPositionEqualTo
in interfaceSpatialElementIfc
- Parameters:
coordinate
- the coordinate to check- Returns:
- true if position is equal
-
distanceTo
public final double distanceTo(SpatialElementIfc element)
Description copied from interface:SpatialElementIfc
This is a "convenience" method for getting the distance from this element to the supplied element within the underlying spatial model Requirement: The elements must be in the same spatial model. The distance should be calculated by the spatial model. If they are not in the same spatial model this method will throw and IllegalArgumentException- Specified by:
distanceTo
in interfaceSpatialElementIfc
- Parameters:
element
- the element to check- Returns:
- the distance
-
isPositionEqualTo
public final boolean isPositionEqualTo(SpatialElementIfc element)
Description copied from interface:SpatialElementIfc
Returns true if the position of this element is the same as the position of the supplied element within the underlying spatial model. This is not necessarily object reference equality, but rather whether or not the positions within the underlying spatial model can be considered the same (equivalent). Requirement: The elements must be in the same spatial model. If they are not in the same spatial model, then this method should return false.- Specified by:
isPositionEqualTo
in interfaceSpatialElementIfc
- Parameters:
element
- the element to check- Returns:
- the distance
-
getSpatialModel
public final SpatialModel getSpatialModel()
Description copied from interface:SpatialElementIfc
Returns the current spatial model that contains this element- Specified by:
getSpatialModel
in interfaceSpatialElementIfc
- Returns:
- the spatial model
-
getInitialSpatialModel
public final SpatialModel getInitialSpatialModel()
Description copied from interface:SpatialElementIfc
Returns the spatial model that should hold this element at the beginning of each replication of a simulation- Specified by:
getInitialSpatialModel
in interfaceSpatialElementIfc
- Returns:
- the spatial model
-
changeSpatialModel
public final void changeSpatialModel(SpatialModel spatialModel, CoordinateIfc coordinate)
Description copied from interface:SpatialElementIfc
Changes the spatial model for this element and places the element at the supplied coordinate within the new spatial model. Throws IllegalArgumentException if the coordinate is not valid for the supplied spatial model. This spatial element becomes a child element of the new spatial model.- Specified by:
changeSpatialModel
in interfaceSpatialElementIfc
- Parameters:
spatialModel
- the spatial modelcoordinate
- the coordinate
-
getModelElement
public ModelElement getModelElement()
Description copied from interface:SpatialElementIfc
Gets the ModelElement associated with this spatial element May be null- Specified by:
getModelElement
in interfaceSpatialElementIfc
- Returns:
- the model element
-
initializeSpatialElement
public void initializeSpatialElement()
Description copied from interface:SpatialElementIfc
This method should be called to initialize the spatial element prior to running a simulation- Specified by:
initializeSpatialElement
in interfaceSpatialElementIfc
-
setInitialPosition
public final void setInitialPosition(CoordinateIfc initialPosition)
Description copied from interface:SpatialElementIfc
Sets the initial position of the element. This position should be used when initializeSpatialElement() is called, typically at the beginning of a replication.- Specified by:
setInitialPosition
in interfaceSpatialElementIfc
- Parameters:
initialPosition
- the coordinate
-
setSpatialModel
protected final void setSpatialModel(SpatialModel spatialModel)
Sets the spatial model for the spatial element. This method is used by addSpatialElement() within SpatialModel- Parameters:
spatialModel
-
-
setModelElement
protected void setModelElement(ModelElement modelElement)
Sets the model element associated with this spatial element, may be null- Parameters:
modelElement
-
-
setCurrentPosition
protected final void setCurrentPosition(CoordinateIfc currentPosition)
- Parameters:
currentPosition
- The currentPosition to set.
-
setPreviousPosition
protected final void setPreviousPosition(CoordinateIfc previousPosition)
- Parameters:
previousPosition
- The previousPosition to set.
-
setName
protected final void setName(java.lang.String str)
Sets the name of this element- Parameters:
str
- The name as a string.
-
setInitialSpatialModel
protected final void setInitialSpatialModel(SpatialModel spatialModel)
Sets the initial spatial model for the position. Since a element can move between spatial models during a simulation, there must be a way to remember which spatial model the element started with prior to re-running a simulation This method sets the initial spatial model. This only has an effect when the element is initialized. Calling this method during a simulation has no effect until the element is initialized. The current spatial model is not changed by this method. When the element is initialized, its current spatial model is set to the supplied spatial model.- Parameters:
spatialModel
-
-
setObserverState
protected final void setObserverState(int observerState)
Can be called by sub-classes to set an indicator of the state of the position prior to notifying observers- Parameters:
observerState
- The observerState to set.
-
notifyObservers
protected final void notifyObservers(int observerState)
- Parameters:
observerState
- The observerState to set.
-
attachPositionObserver
public void attachPositionObserver(ObserverIfc observer)
Description copied from interface:SpatialElementIfc
Implementor of this interface should allow Observers to be attached. For example, the observers should be notified when the position changes. It is the responsibility of implementers to properly notify the observers.- Specified by:
attachPositionObserver
in interfaceSpatialElementIfc
- Parameters:
observer
- the observer
-
removePositionObserver
public void removePositionObserver(ObserverIfc observer)
Description copied from interface:SpatialElementIfc
Remove the observer from this PositionIfc- Specified by:
removePositionObserver
in interfaceSpatialElementIfc
- Parameters:
observer
- the observer
-
notifyObservers
protected final void notifyObservers(java.lang.Object theObserved, java.lang.Object arg)
-
-