View Javadoc

1   package cz.cuni.amis.pogamut.sposh.engine;
2   
3   import cz.cuni.amis.pogamut.sposh.elements.PoshElement;
4   import cz.cuni.amis.pogamut.sposh.elements.Sense;
5   import cz.cuni.amis.pogamut.sposh.elements.Trigger;
6   import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.Iterator;
10  import java.util.LinkedList;
11  import java.util.List;
12  import java.util.logging.Logger;
13  
14  /**
15   * Class that immutably stores the result of evaluation of trigger. Trigger is
16   * basically a list of sense calls (i.e. {@link SenseListExecutor}).
17   */
18  class TriggerResult implements Iterable<SenseResult> {
19  
20      private final List<SenseResult> senses;
21      private final boolean success;
22  
23      /**
24       * Create new result of trigger
25       *
26       * @param senses list of results, what did each sense ended up as
27       * @param success was the trigger as a whole successful
28       */
29      public TriggerResult(List<SenseResult> senses, boolean success) {
30          this.success = success;
31          this.senses = Collections.unmodifiableList(new ArrayList<SenseResult>(senses));
32      }
33  
34      /**
35       * Create new result of trigger, without specifying any senses.
36       *
37       * @param success was the trigger successful
38       */
39      public TriggerResult(boolean success) {
40          this(Collections.<SenseResult>emptyList(), success);
41      }
42  
43      public boolean wasSuccess() {
44          return success;
45      }
46  
47      /**
48       * Get iterator to the all senses of this trigger. Unmodifiable.
49       *
50       * @return iterator to the start of sens's list.
51       */
52      @Override
53      public Iterator<SenseResult> iterator() {
54          return senses.iterator();
55      }
56  }
57  
58  /**
59   * Executor that decicdes if goal or trigger are fulfilled. That happens only is
60   * all its senses are fired.
61   *
62   * @author Honza
63   */
64  final class SenseListExecutor<T extends PoshElement> extends AbstractExecutor {
65  
66      private List<SenseExecutor> sensesExecutors = new ArrayList<SenseExecutor>();
67  
68      /**
69       * Create an executor for triggers.
70       *
71       * @param trigger source of senses, can be null, then no senses will be
72       * used.
73       * @param ctx variable context that should be passed to the senses
74       * @param log logger to record actions of this executor, can be null
75       */
76      SenseListExecutor(Trigger<T> trigger, VariableContext ctx, Logger log) {
77          this(ctx, log);
78  
79          for (Sense sense : trigger) {
80              sensesExecutors.add(new SenseExecutor(sense, ctx, log));
81          }
82      }
83  
84      /**
85       * Create an executor without any senses (it will always return
86       * defaultReturn).
87       *
88       * @param ctx variable context that should be passed to the senses.
89       * @param log log to write debug info.
90       */
91      SenseListExecutor(VariableContext ctx, Logger log) {
92          super(ctx, log);
93      }
94  
95      /**
96       * Evaluate all senses until first one fails. If no senses were specified,
97       * consider it a fail.
98       *
99       * @param defaultReturn what to return if no senses were specified. In
100      * trigger true, in goal false
101      * @return Result of the senselist. The result is true if none of senses
102      * fails, false if at least one fails.
103      */
104     public TriggerResult fire(IWorkExecutor workExecuter, boolean defaultReturn) {
105         List<SenseResult> senses = new LinkedList<SenseResult>();
106 
107         for (SenseExecutor senseExecutor : sensesExecutors) {
108             defaultReturn = true;
109 
110             SenseResult res = senseExecutor.fire(workExecuter);
111             senses.add(res);
112 
113             if (!res.wasSuccessful()) {
114                 return new TriggerResult(senses, false);
115             }
116         }
117         return new TriggerResult(senses, defaultReturn);
118     }
119 }