View Javadoc

1   package nl.tudelft.goal.unreal.actions;
2   
3   import cz.cuni.amis.utils.exception.PogamutException;
4   
5   /**
6    * Basic class for actions executed by the {@link UT3BotBehavior}. Before
7    * actions are executed they are are queued up in the {@link ActionQueue}. To
8    * ensure the queue does not overflow the ActionQueue checks which actions can
9    * replace other actions in the queue and if this action would have any effect.
10   * 
11   * Actions can be replaced when executing this action after them would have no
12   * effect. This is indicated by using the {@link #setReplaces(Class...)} method.
13   * 
14   * Actions have no effect if there is an action in the queue which prevents this
15   * action from having any effect. this is indicated by using the
16   * {@link #setBlockedBy(Class...)} .
17   * 
18   * 
19   */
20  public abstract class Action {
21  
22  	@SuppressWarnings("unchecked")
23  	private Class<? extends Action>[] replaces = new Class[0];
24  	@SuppressWarnings("unchecked")
25  	private Class<? extends Action>[] blocks = new Class[0];
26  
27  	public abstract void execute() throws PogamutException;
28  
29  	/**
30  	 * Sets which actions will have no effect if this action is executed after
31  	 * them.
32  	 * 
33  	 * @param replaces
34  	 */
35  	public void setReplaces(Class<? extends Action>... replaces) {
36  		this.replaces = replaces;
37  	}
38  
39  	/**
40  	 * Returns true if executing the argument action before this action will
41  	 * have no effect. This action overrides all effects of the argument action.
42  	 * 
43  	 * @param action
44  	 * @return
45  	 */
46  	public boolean replaces(Action action) {
47  
48  		for (Class<? extends Action> replace : replaces) {
49  			// Check if the class of the action is a (sub)class of the actions
50  			// this action replaces
51  			if (replace.isAssignableFrom(action.getClass())) {
52  				return true;
53  			}
54  		}
55  
56  		return false;
57  
58  	}
59  
60  	/**
61  	 * Sets which actions will prevent this action from having any effect if
62  	 * executed after them.
63  	 * 
64  	 * @param blocks
65  	 */
66  	public void setBlockedBy(Class<? extends Action>... blocks) {
67  		this.blocks = blocks;
68  	}
69  
70  	/**
71  	 * Returns true if executing the argument action does not impede the
72  	 * execution of this action. When false executing this action after the
73  	 * argument action will have no effect.
74  	 * 
75  	 * @param action
76  	 * @return
77  	 */
78  	public boolean hasEffect(Action action) {
79  
80  		for (Class<? extends Action> block : blocks) {
81  			// Check if the class of the action is a (sub)class of the actions
82  			// this action is blocked by.
83  			if (block.isAssignableFrom(action.getClass())) {
84  				return false;
85  			}
86  		}
87  
88  		return true;
89  
90  	}
91  
92  }