View Javadoc

1   package cz.cuni.amis.pogamut.sposh.executor;
2   
3   import cz.cuni.amis.pogamut.sposh.context.Context;
4   import cz.cuni.amis.pogamut.sposh.engine.VariableContext;
5   import cz.cuni.amis.pogamut.sposh.exceptions.MethodException;
6   import java.lang.reflect.InvocationTargetException;
7   
8   /**
9    * Base class for {@link IAction actions} that have parameters passed to them
10   * using the reflection API. The actions derived directly from {@link StateAction}
11   * must check that passed {@link VariableContext} contains expected parameters.
12   *
13   * To correctly implement action that can utilize the reflection for parameters,
14   * you must derive from this class and implement three methods: <tt>public void
15   * init</tt>, <tt>public ActionResult run</tt> and <tt>public void done</tt>.
16   * All parameters of these methods must be annotated by the {@link Param} with
17   * specified name of the variable contained in the {@link VariableContext}. Note
18   * that each action can have different parameters, as long as all parameters
19   * have annotations with {@link Param} and are all in the context.
20   *
21   * Only allowed parameters are of type {@link String}, {@link Integer} and {@link Double}.
22   *
23   * Example:
24   * <pre>
25   * class StayAction extends ParamsAction&lt;PreyContext&gt; {
26   *   public StayAction(Context ctx) {
27   *     super(ctx);
28   *   }
29   *
30   *   public void init(&064;Param("$enemy") String enemy, &064;Param("$distance") Double distance) {
31   *     ...
32   *   }
33   *
34   *   public ActionResult run(&064;Param("$teamname") String teamName) {
35   *     ...
36   *   }
37   *
38   *   public void done() {
39   *     ...
40   *   }
41   * }
42   *
43   * </pre>
44   *
45   * @see ParamsSense
46   * @see Param Annotation for parameters of the methods.
47   * @author Honza Havlicek
48   */
49  public abstract class ParamsAction<CONTEXT extends Context> extends StateAction<CONTEXT> {
50  
51      private static final String INIT_METHOD_NAME = "init";
52      private static final String RUN_METHOD_NAME = "run";
53      private static final String DONE_METHOD_NAME = "done";
54      private final ParamsMethod<Void> initMethod;
55      private final ParamsMethod<ActionResult> runMethod;
56      private final ParamsMethod<Void> doneMethod;
57  
58      public ParamsAction(CONTEXT ctx) {
59          super(ctx);
60  
61          initMethod = new ParamsMethod<Void>(getClass(), INIT_METHOD_NAME, Void.TYPE);
62          runMethod = new ParamsMethod<ActionResult>(getClass(), RUN_METHOD_NAME, ActionResult.class);
63          doneMethod = new ParamsMethod<Void>(getClass(), DONE_METHOD_NAME, Void.TYPE);
64      }
65  
66      @Override
67      public final void init(VariableContext params) {
68          try {
69              initMethod.invoke(this, params);
70          } catch (InvocationTargetException ex) {
71              throw new MethodException("",ex.getTargetException());
72          }
73      }
74  
75      @Override
76      public final ActionResult run(VariableContext params) {
77          try {
78              return runMethod.invoke(this, params);
79          } catch (InvocationTargetException ex) {
80              throw new MethodException("",ex.getTargetException());
81          }
82      }
83  
84      @Override
85      public final void done(VariableContext params) {
86          try {
87              doneMethod.invoke(this, params);
88          } catch (InvocationTargetException ex) {
89              throw new MethodException("",ex.getTargetException());
90          }
91      }
92  }