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