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 intCHANGED_POSITIONAn 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 voidattachPositionObserver(ObserverIfc observer)Implementor of this interface should allow Observers to be attached.voidchangeSpatialModel(SpatialModel spatialModel, CoordinateIfc coordinate)Changes the spatial model for this element and places the element at the supplied coordinate within the new spatial model.doubledistanceTo(CoordinateIfc coordinate)This is a "convenience" method for getting the distance from this element to the supplied coordinate within the underlying spatial modeldoubledistanceTo(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.intgetId()CoordinateIfcgetInitialPosition()SpatialModelgetInitialSpatialModel()Returns the spatial model that should hold this element at the beginning of each replication of a simulationModelElementgetModelElement()Gets the ModelElement associated with this spatial element May be nulljava.lang.StringgetName()intgetObserverState()Observers can call this to get an integer representing the state of the element after the observers have been notifiedCoordinateIfcgetPosition()CoordinateIfcgetPreviousPosition()SpatialModelgetSpatialModel()Returns the current spatial model that contains this elementvoidinitializeSpatialElement()This method should be called to initialize the spatial element prior to running a simulationbooleanisPositionEqualTo(CoordinateIfc coordinate)Returns true if the position of this element is the same as supplied coordinate within the underlying spatial model.booleanisPositionEqualTo(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 voidnotifyObservers(int observerState)protected voidnotifyObservers(java.lang.Object theObserved, java.lang.Object arg)voidremovePositionObserver(ObserverIfc observer)Remove the observer from this PositionIfcprotected voidsetCurrentPosition(CoordinateIfc currentPosition)voidsetInitialPosition(CoordinateIfc initialPosition)Sets the initial position of the element.protected voidsetInitialSpatialModel(SpatialModel spatialModel)Sets the initial spatial model for the position.protected voidsetModelElement(ModelElement modelElement)Sets the model element associated with this spatial element, may be nullprotected voidsetName(java.lang.String str)Sets the name of this elementprotected voidsetObserverState(int observerState)Can be called by sub-classes to set an indicator of the state of the position prior to notifying observersprotected voidsetPreviousPosition(CoordinateIfc previousPosition)protected voidsetSpatialModel(SpatialModel spatialModel)Sets the spatial model for the spatial element.java.lang.StringtoString()
-
-
-
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:
toStringin classjava.lang.Object
-
getName
public final java.lang.String getName()
- Specified by:
getNamein interfaceGetNameIfc- Returns:
- a string representing the name of the object
-
getId
public final int getId()
- Specified by:
getIdin interfaceIdentityIfc- Returns:
- an int representing the id of the object
-
getPosition
public final CoordinateIfc getPosition()
- Specified by:
getPositionin interfacePositionIfc- Specified by:
getPositionin interfaceSpatialElementIfc- Returns:
- Returns the current Position.
-
getInitialPosition
public final CoordinateIfc getInitialPosition()
- Specified by:
getInitialPositionin interfaceSpatialElementIfc- Returns:
- Returns the initialPosition.
-
getPreviousPosition
public final CoordinateIfc getPreviousPosition()
- Specified by:
getPreviousPositionin interfaceSpatialElementIfc- Returns:
- Returns the previousPosition.
-
getObserverState
public final int getObserverState()
Description copied from interface:SpatialElementIfcObservers can call this to get an integer representing the state of the element after the observers have been notified- Specified by:
getObserverStatein interfaceSpatialElementIfc- Returns:
- the observer state
-
distanceTo
public final double distanceTo(CoordinateIfc coordinate)
Description copied from interface:SpatialElementIfcThis is a "convenience" method for getting the distance from this element to the supplied coordinate within the underlying spatial model- Specified by:
distanceToin interfaceSpatialElementIfc- Parameters:
coordinate- the coordinate to check- Returns:
- the distance
-
isPositionEqualTo
public final boolean isPositionEqualTo(CoordinateIfc coordinate)
Description copied from interface:SpatialElementIfcReturns 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:
isPositionEqualToin interfaceSpatialElementIfc- Parameters:
coordinate- the coordinate to check- Returns:
- true if position is equal
-
distanceTo
public final double distanceTo(SpatialElementIfc element)
Description copied from interface:SpatialElementIfcThis 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:
distanceToin interfaceSpatialElementIfc- Parameters:
element- the element to check- Returns:
- the distance
-
isPositionEqualTo
public final boolean isPositionEqualTo(SpatialElementIfc element)
Description copied from interface:SpatialElementIfcReturns 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:
isPositionEqualToin interfaceSpatialElementIfc- Parameters:
element- the element to check- Returns:
- the distance
-
getSpatialModel
public final SpatialModel getSpatialModel()
Description copied from interface:SpatialElementIfcReturns the current spatial model that contains this element- Specified by:
getSpatialModelin interfaceSpatialElementIfc- Returns:
- the spatial model
-
getInitialSpatialModel
public final SpatialModel getInitialSpatialModel()
Description copied from interface:SpatialElementIfcReturns the spatial model that should hold this element at the beginning of each replication of a simulation- Specified by:
getInitialSpatialModelin interfaceSpatialElementIfc- Returns:
- the spatial model
-
changeSpatialModel
public final void changeSpatialModel(SpatialModel spatialModel, CoordinateIfc coordinate)
Description copied from interface:SpatialElementIfcChanges 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:
changeSpatialModelin interfaceSpatialElementIfc- Parameters:
spatialModel- the spatial modelcoordinate- the coordinate
-
getModelElement
public ModelElement getModelElement()
Description copied from interface:SpatialElementIfcGets the ModelElement associated with this spatial element May be null- Specified by:
getModelElementin interfaceSpatialElementIfc- Returns:
- the model element
-
initializeSpatialElement
public void initializeSpatialElement()
Description copied from interface:SpatialElementIfcThis method should be called to initialize the spatial element prior to running a simulation- Specified by:
initializeSpatialElementin interfaceSpatialElementIfc
-
setInitialPosition
public final void setInitialPosition(CoordinateIfc initialPosition)
Description copied from interface:SpatialElementIfcSets the initial position of the element. This position should be used when initializeSpatialElement() is called, typically at the beginning of a replication.- Specified by:
setInitialPositionin 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:SpatialElementIfcImplementor 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:
attachPositionObserverin interfaceSpatialElementIfc- Parameters:
observer- the observer
-
removePositionObserver
public void removePositionObserver(ObserverIfc observer)
Description copied from interface:SpatialElementIfcRemove the observer from this PositionIfc- Specified by:
removePositionObserverin interfaceSpatialElementIfc- Parameters:
observer- the observer
-
notifyObservers
protected final void notifyObservers(java.lang.Object theObserved, java.lang.Object arg)
-
-