cz.cuni.amis.pogamut.base.communication.worldview.impl
Class AbstractWorldView

java.lang.Object
  extended by cz.cuni.amis.pogamut.base.communication.worldview.impl.AbstractWorldView
All Implemented Interfaces:
IWorldChangeEventInput, IWorldView, IComponent
Direct Known Subclasses:
EventDrivenWorldView

public abstract class AbstractWorldView
extends java.lang.Object
implements IWorldView

Abstract world view is implementing some of the tedious things every WorldView will surely implement -> maps for holding the references to all world objects either according to their id and type (class). It also implements a map of listeners for events that may be generated in the world.

For raising new IWorldEvent in descendants call protected method raiseEvent(IWorldEvent event), that is preventing recursion.

Note that there is a big advantage in how the listeners are called and how objects are stored.

The event notifying method (raiseEvent()) is respecting the class/interface hierarchy thus informing listeners hooked on all levels of the hierarchy.

The items are stored according the the class/interface hierarchy as well!

Example:You have interface ItemEvent (extends IWorldObjectEvent) and it's implementation WeaponEvent and HealthEvent. Perheps you want to listen for all events on WeaponEvent, so you will create IWorldEventListener<WeaponEvent>. But hey - you may want to listen on both WeaponEvent and HealthEvent (and perheps any ItemEvent there is), that's easy - just create IWorldEventListener<ItemEvent> and you will receive both WeaponEvent and HealthEvent. That's because during event handling we're probing the event class ancestors / interfaces and informing all listeners on all class-hierarchy levels.

Ultimately you may create IWorldEventListener<IWorldEvent> to catch all events the world view produces (be careful because it may cause serious performance hit if you process these events slowly).

Same goes for storing the items under it's class in the 'worldObjects'.


Field Summary
static Token COMPONENT_ID
           
protected  IComponentControlHelper control
           
protected  ComponentController<IComponent> controller
           
protected  IComponentBus eventBus
           
protected  LogCategory log
           
 
Constructor Summary
AbstractWorldView(ComponentDependencies dependencies, IComponentBus bus, IAgentLogger logger)
           
 
Method Summary
 void addEventListener(java.lang.Class<?> event, IWorldEventListener<?> listener)
          Adds listener to a specific event (Level A listeners).
 void addObjectListener(java.lang.Class<?> objectClass, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
          Adds listener to a specified 'event' that occurs on the specific 'objectClass' (Level C listeners).
 void addObjectListener(java.lang.Class<?> objectClass, IWorldObjectEventListener<?,?> listener)
          Adds listener to all events that happens on any object of the 'objectClass' (Level B listeners).
 void addObjectListener(WorldObjectId objectId, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
          Adds listener to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).
 void addObjectListener(WorldObjectId objectId, IWorldObjectEventListener<?,?> listener)
          Adds listener to all events that happens on object with specific 'objectId' (Level D listeners).
protected  void addWorldObject(IWorldObject worldObject)
          Method that adds a new world object to the object maps.
protected  void cleanUp()
          Cleans up internal data structures, called from start/stop/kill/reset methods.
 java.util.Map<WorldObjectId,IWorldObject> get()
          Returns map with objects inserted according to their id.
 IWorldObject get(WorldObjectId objectId)
          Returns a world object of the specific id (if exists inside the world view).
 java.util.Map<java.lang.Class,java.util.Map<WorldObjectId,IWorldObject>> getAll()
          Returns map of all objects that are present in the world view.
<T extends IWorldObject>
java.util.Map<WorldObjectId,T>
getAll(java.lang.Class<T> type)
          Returns map of all objects of a specific type that are present in the world view.
 Token getComponentId()
          Unique identification of the component.
 IComponentBus getEventBus()
           
 LogCategory getLog()
           
<T extends IWorldObject>
T
getSingle(java.lang.Class<T> cls)
          Returns the only instance of required object if present, if there are more instances of this object then IllegalArgumentException will be thrown.
 boolean isListening(java.lang.Class<?> objectClass, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
          Tests whether the 'listener' is listening at specified 'objectClass' for specified 'event' (Level C listeners).
 boolean isListening(java.lang.Class<?> eventClass, IWorldEventListener<?> listener)
          Tests whether the 'listener' is listening to a specific event (Level A listeners).
 boolean isListening(java.lang.Class<?> objectClass, IWorldObjectEventListener<?,?> listener)
          Tests whether the 'listener' is listening at specified 'objectClass' (Level B listeners).
 boolean isListening(IWorldEventListener<?> listener)
          Checks whether this listener is hooked to the world view (at any listener level).
 boolean isListening(WorldObjectId objectId, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
          Tests whether the 'listener' is listening to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).
 boolean isListening(WorldObjectId objectId, IWorldObjectEventListener<?,?> listener)
          Tests whether the 'listener' is listening at specified 'objectId' (Level D Listeners).
protected  boolean isPaused()
           
protected  boolean isRunning()
           
protected  void kill()
          Kills the world view.
protected  void pause()
          Pauses the world view.
protected  void prePause()
          Pre-pauses the world view.
protected  void preStop()
          Pre-stops the world view.
protected  void raiseEvent(IWorldEvent event)
          Process new IWorldEvent - notify all the listeners about it.
 void removeEventListener(java.lang.Class<?> eventClass, IWorldEventListener<?> listener)
          Removes listener from a specific event (Level A listeners).
 void removeListener(IWorldEventListener<?> listener)
          Removes listener from every listeners category (from every listener level).
 void removeObjectListener(java.lang.Class<?> objectClass, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
          Removes listener from specific 'objectClass' listening for specified 'event' (Level C listeners).
 void removeObjectListener(java.lang.Class<?> objectClass, IWorldObjectEventListener<?,?> listener)
          Removes listener from specific 'objectClass' listening for specified 'event' (Level B listeners).
 void removeObjectListener(WorldObjectId objectId, java.lang.Class<?> eventClass, IWorldObjectEventListener<?,?> listener)
          Removes listener to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).
 void removeObjectListener(WorldObjectId objectId, IWorldObjectEventListener<?,?> listener)
          Removes listener from objects with specified 'objectId' (Level D Listeners).
protected  void removeWorldObject(IWorldObject worldObject)
          Removes world object from the world view - this will be called from the descendants of the AbstractWorldView whenever world object should disappear from the world view (was destroyed in the world).
protected  void reset()
          Resets the world view so it is start()able again.
protected  void resume()
          Resumes the world view.
protected  void start(boolean startPaused)
          Starts the world view.
protected  void stop()
          Stops the world view.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface cz.cuni.amis.pogamut.base.communication.worldview.IWorldChangeEventInput
notify, notifyImmediately
 

Field Detail

COMPONENT_ID

public static final Token COMPONENT_ID

log

protected LogCategory log

eventBus

protected IComponentBus eventBus

controller

protected ComponentController<IComponent> controller

control

protected IComponentControlHelper control
Constructor Detail

AbstractWorldView

public AbstractWorldView(ComponentDependencies dependencies,
                         IComponentBus bus,
                         IAgentLogger logger)
Method Detail

cleanUp

protected void cleanUp()
Cleans up internal data structures, called from start/stop/kill/reset methods.

If you override this method, do not forget to call super.cleanUp().


start

protected void start(boolean startPaused)
Starts the world view.

If you override this method, do not forget to call super.start().


prePause

protected void prePause()
Pre-pauses the world view.

If you override this method, do not forget to call super.preStop().


pause

protected void pause()
Pauses the world view.

If you override this method, do not forget to call super.start().


resume

protected void resume()
Resumes the world view.

If you override this method, do not forget to call super.start().


preStop

protected void preStop()
Pre-stops the world view.

If you override this method, do not forget to call super.preStop().


stop

protected void stop()
Stops the world view.

If you override this method, do not forget to call super.stop().


kill

protected void kill()
Kills the world view.

If you override this method, do not forget to call super.stop().


reset

protected void reset()
Resets the world view so it is start()able again.

If you override this method, do not forget to call super.reset().


isRunning

protected boolean isRunning()

isPaused

protected boolean isPaused()

getComponentId

public Token getComponentId()
Description copied from interface: IComponent
Unique identification of the component.

Specified by:
getComponentId in interface IComponent
Returns:

getLog

public LogCategory getLog()

getEventBus

public IComponentBus getEventBus()
Specified by:
getEventBus in interface IWorldView

addEventListener

public void addEventListener(java.lang.Class<?> event,
                             IWorldEventListener<?> listener)
Description copied from interface: IWorldView
Adds listener to a specific event (Level A listeners). Note that the event listener must be able to handle events of the class 'event'.

It is the most general type of listener-registration allowing you to sniff any type of events.

Events passed to the listener are filtered only according to the 'event' class.

WARNING: even though the method does not require templated class and listener, you must be sure that 'listener' can accept 'eventClass'.

Specified by:
addEventListener in interface IWorldView
Parameters:
event - which event types you want to receive
listener - where you want to handle these events

addObjectListener

public void addObjectListener(java.lang.Class<?> objectClass,
                              IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Adds listener to all events that happens on any object of the 'objectClass' (Level B listeners).

Events passed to the listener are filtered according to the 'objectClass'.

WARNING: even though the method does not require templated classes and listener, you must be sure that 'listener' accepts all events (IWorldObjectEvent) for objects of 'objectClass'.

Specified by:
addObjectListener in interface IWorldView
Parameters:
objectClass - which object class you want to listen at
listener - where you want to handle these events

addObjectListener

public void addObjectListener(java.lang.Class<?> objectClass,
                              java.lang.Class<?> eventClass,
                              IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Adds listener to a specified 'event' that occurs on the specific 'objectClass' (Level C listeners).

Events passed to the listener are filtered according to the 'event' and 'objectClass' of the object the event happened upon.

WARNING: even though the method does not require templated classes and listener, you must be sure that 'listener' accepts 'eventClass' for objects of 'objectClass'.

eventClass can be any implementor of IWorldObjectEvent. E.g. WorldObjectAppearedEvent, WorldObjectDestroyedEvent, WorldObjectDisappearedEvent, WorldObjectFirstEncounteredEvent or WorldObjectUpdatedEvent.

Specified by:
addObjectListener in interface IWorldView
Parameters:
objectClass - which object class you want to listen at
eventClass - which event class you want to receive
listener - where you want to handle these events

addObjectListener

public void addObjectListener(WorldObjectId objectId,
                              IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Adds listener to all events that happens on object with specific 'objectId' (Level D listeners).

Events passed to the listener are filtered according to the 'objectId' of the object.

WARNING: you must ensure that 'listener' can accept the event raised on object of specified 'objectId'.

Specified by:
addObjectListener in interface IWorldView
Parameters:
objectId - which object you want to listen at
listener - where you want to handle events

addObjectListener

public void addObjectListener(WorldObjectId objectId,
                              java.lang.Class<?> eventClass,
                              IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Adds listener to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).

Events passed to the listener are filtered according to the 'event' and 'objectId' of the object.

WARNING: even though the method does not require templated classes and listener, you must be sure that 'listener' accepts 'eventClass' for objects of specified 'objectId'.

Specified by:
addObjectListener in interface IWorldView
Parameters:
objectId - which object you want to listen at
eventClass - which event classes you want to receive
listener - where you want to handle these events

isListening

public boolean isListening(java.lang.Class<?> eventClass,
                           IWorldEventListener<?> listener)
Description copied from interface: IWorldView
Tests whether the 'listener' is listening to a specific event (Level A listeners).

Specified by:
isListening in interface IWorldView
Parameters:
eventClass - which events you want to receive
listener - that is tested
Returns:
whether the listener is listening

isListening

public boolean isListening(java.lang.Class<?> objectClass,
                           IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Tests whether the 'listener' is listening at specified 'objectClass' (Level B listeners).

Specified by:
isListening in interface IWorldView
Parameters:
objectClass - where the listener is tested
listener - that is tested
Returns:
whether the listener is listening

isListening

public boolean isListening(java.lang.Class<?> objectClass,
                           java.lang.Class<?> eventClass,
                           IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Tests whether the 'listener' is listening at specified 'objectClass' for specified 'event' (Level C listeners).

Specified by:
isListening in interface IWorldView
Parameters:
objectClass - where the listener is tested
eventClass - where the listener is tested
listener - that is tested
Returns:
whether the listener is listening

isListening

public boolean isListening(WorldObjectId objectId,
                           IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Tests whether the 'listener' is listening at specified 'objectId' (Level D Listeners).

Specified by:
isListening in interface IWorldView
Parameters:
objectId - where the listener is tested
listener - that is tested
Returns:
whether the listener is listening

isListening

public boolean isListening(WorldObjectId objectId,
                           java.lang.Class<?> eventClass,
                           IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Tests whether the 'listener' is listening to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).

Specified by:
isListening in interface IWorldView
Parameters:
objectId - where the listener is tested
eventClass - what class is tested
listener - that is tested
Returns:
whether the listener is listening

isListening

public boolean isListening(IWorldEventListener<?> listener)
Description copied from interface: IWorldView
Checks whether this listener is hooked to the world view (at any listener level).

WARNING: Can be time consuming! (Iterating through all levels of listeners.)

Specified by:
isListening in interface IWorldView
Returns:

removeEventListener

public void removeEventListener(java.lang.Class<?> eventClass,
                                IWorldEventListener<?> listener)
Description copied from interface: IWorldView
Removes listener from a specific event (Level A listeners).

Specified by:
removeEventListener in interface IWorldView
Parameters:
eventClass - which events class you want to remove the listener from
listener - you want to remove

removeObjectListener

public void removeObjectListener(java.lang.Class<?> objectClass,
                                 IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Removes listener from specific 'objectClass' listening for specified 'event' (Level B listeners).

Specified by:
removeObjectListener in interface IWorldView
Parameters:
objectClass - class of objects you want the listener to remove from
listener - you want to remove

removeObjectListener

public void removeObjectListener(java.lang.Class<?> objectClass,
                                 java.lang.Class<?> eventClass,
                                 IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Removes listener from specific 'objectClass' listening for specified 'event' (Level C listeners).

Specified by:
removeObjectListener in interface IWorldView
Parameters:
objectClass - class of objects you want the listener to remove from
eventClass - which events class you want to remove the listener from
listener - you want to remove

removeObjectListener

public void removeObjectListener(WorldObjectId objectId,
                                 IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Removes listener from objects with specified 'objectId' (Level D Listeners).

Specified by:
removeObjectListener in interface IWorldView
Parameters:
objectId - id of object you want the listener to remove from
listener - you want to remove

removeObjectListener

public void removeObjectListener(WorldObjectId objectId,
                                 java.lang.Class<?> eventClass,
                                 IWorldObjectEventListener<?,?> listener)
Description copied from interface: IWorldView
Removes listener to a specified 'event' that occurs on the specific object with 'objectId' (Level E listeners).

Specified by:
removeObjectListener in interface IWorldView
Parameters:
objectId - id of object you want the listener to remove from
eventClass - event class you want to stop receiving in the listener
listener - you want to remove

removeListener

public void removeListener(IWorldEventListener<?> listener)
Description copied from interface: IWorldView
Removes listener from every listeners category (from every listener level).

WARNING: Can be time consuming! (Iterating through all levels of listeners.)

Specified by:
removeListener in interface IWorldView
Parameters:
listener - you want to remove from all listener levels

getAll

public java.util.Map<java.lang.Class,java.util.Map<WorldObjectId,IWorldObject>> getAll()
Description copied from interface: IWorldView
Returns map of all objects that are present in the world view.

WARNING: If you will do iteration over the map, you must synchronize on it.

Specified by:
getAll in interface IWorldView

getAll

public <T extends IWorldObject> java.util.Map<WorldObjectId,T> getAll(java.lang.Class<T> type)
Description copied from interface: IWorldView
Returns map of all objects of a specific type that are present in the world view.

WARNING: If you will do iteration over the map, you must synchronize on it.

Specified by:
getAll in interface IWorldView
Returns:

getSingle

public <T extends IWorldObject> T getSingle(java.lang.Class<T> cls)
Description copied from interface: IWorldView
Returns the only instance of required object if present, if there are more instances of this object then IllegalArgumentException will be thrown.

Should be used to obtain "utility" world objects such us "informations about the agent" (that is unique for the agent and as the world view should be used by only one agent...) or some "world statistics object".

Specified by:
getSingle in interface IWorldView
Parameters:
cls - Class of object to be retrieved
Returns:
the only object of given class

get

public IWorldObject get(WorldObjectId objectId)
Description copied from interface: IWorldView
Returns a world object of the specific id (if exists inside the world view). Otherwise, null is returned.

Note that there is no way to tell the correct type of returned object - you have to cast it to a correct class yourself.

Specified by:
get in interface IWorldView
Parameters:
objectId - objects's id
Returns:

get

public java.util.Map<WorldObjectId,IWorldObject> get()
Description copied from interface: IWorldView
Returns map with objects inserted according to their id.

WARNING: If you will do iteration over the map, you must synchronize on it.

Note that this map contains various objects, therefore you will somehow guess the correct class of the object from its id.

Specified by:
get in interface IWorldView
Returns:

addWorldObject

protected void addWorldObject(IWorldObject worldObject)
Method that adds a new world object to the object maps. It will be called from the descendant whenever new object is encountered for the first time.

Synchronized!

Parameters:
worldObject -

removeWorldObject

protected void removeWorldObject(IWorldObject worldObject)
Removes world object from the world view - this will be called from the descendants of the AbstractWorldView whenever world object should disappear from the world view (was destroyed in the world).

Synchronized!

Parameters:
worldObject -

raiseEvent

protected void raiseEvent(IWorldEvent event)
Process new IWorldEvent - notify all the listeners about it. Forbids recursion.

Use in the descendants to process new IWorldChangeEvent.

Does not catch any exceptions!

Synchronized!

Parameters:
event -

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object