View Javadoc

1   package cz.cuni.amis.pogamut.sposh.engine;
2   
3   import cz.cuni.amis.pogamut.sposh.engine.timer.SystemClockTimer;
4   import cz.cuni.amis.pogamut.sposh.engine.timer.ITimer;
5   import cz.cuni.amis.pogamut.sposh.elements.PoshPlan;
6   import cz.cuni.amis.pogamut.sposh.executor.IWorkExecutor;
7   import java.util.Set;
8   import java.util.logging.Logger;
9   
10  /**
11   * This class is responsible for executing the valid posh plan.
12   * 
13   * @author Honza
14   */
15  public class PoshEngine {
16  
17      public enum EvaluationResult {
18          GOAL_SATISFIED,
19          ELEMENT_FIRED,
20          NO_ELEMENT_FIRED
21      }
22      
23      public static class EvaluationResultInfo {
24      	
25      	public EvaluationResult result;
26      	public FireResult.Type type;
27      	
28      	public EvaluationResultInfo(EvaluationResult result, FireResult.Type type) {
29      		this.result = result;
30      		this.type = type;
31      	}
32      	
33      }
34  
35      protected PoshPlan plan;
36      protected ITimer timer;
37      protected Logger log;
38      private DCExecutor dcExecutor;
39  
40      public PoshEngine(PoshPlan plan) {
41          this(plan, new SystemClockTimer());
42      }
43  
44      public PoshEngine(PoshPlan plan, ITimer timer) {
45          this(plan, timer, null);
46      }
47  
48      public PoshEngine(PoshPlan plan, ITimer timer, Logger log) {
49          this.plan = plan;
50          this.timer = timer;
51          this.log = log;
52          reset();
53      }
54  
55      /**
56       * Reset the posh engine, all stacks and variables will be reseted.
57       * Use this to return engine to former state, it had when first initialized.
58       */
59      public final synchronized void reset() {
60          dcExecutor = new DCExecutor(plan, new VariableContext(), timer, log);
61      }
62  
63      public synchronized EvaluationResultInfo evaluatePlan(IWorkExecutor workExecuter) {
64          return dcExecutor.fire(workExecuter);
65      }
66  
67      ElementStackTrace getStackForDE(String name) {
68          return dcExecutor.getStackForDE(name);
69      }
70  
71      
72      ElementStackTrace getStackForDE(int index) {
73          return dcExecutor.getStackForDE(index);
74      }
75      
76      public Logger getLog() {
77      	return log;
78      }
79  
80      /**
81       * Get set of action names utilized by this engine.
82       * @return
83       */
84      public Set<String> getActions() {
85          return plan.getActionsNames();
86      }
87  
88      /**
89       * Get set of sense names utilized by this engine.
90       * @return
91       */
92      public Set<String> getSenses() {
93          return plan.getSensesNames();
94      }
95  
96      /**
97       * Get plan of this engine (serialize the parsed plane and return it)
98       * @return
99       */
100     public String getPlan() {
101         return plan.toString();
102     }
103 
104     /**
105      * Return serialized state of the engine. 
106      * @return
107      */
108     public String getState() {
109         StringBuilder sb = new StringBuilder();
110         for (DEExecutor de : dcExecutor.deExecutors) {
111             sb.append("/Root:");
112             sb.append(plan.getDriveCollection().getName());
113             sb.append("/Drive:");
114             sb.append(de.getName());
115             TriggerResult triggerResult = de.getTriggerResult();
116             if (triggerResult != null) {
117                 for (SenseResult senseResult : triggerResult) {
118                     sb.append('[');
119                     sb.append(senseResult.name);
120                     sb.append(':');
121                     sb.append(senseResult.wasSuccessful() ? '1' : '0');
122                     sb.append(']');
123                 }
124             }
125             sb.append(de.getStackTrace().toString());
126             sb.append('\n');
127         }
128         return sb.toString();
129     }
130 }