replicode
g_monitor.h
Go to the documentation of this file.
1 // g_monitor.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef g_monitor_h
32 #define g_monitor_h
33 
34 #include "monitor.h"
35 
36 
37 namespace r_exec {
38 
39 class PMDLController;
40 class PrimaryMDLController;
41 
42 class _GMonitor:
43  public Monitor {
44 protected:
45  uint64_t deadline; // of the goal.
47  _Fact *goal_target; // convenience; f1->object.
50 
51  uint64_t volatile simulating; // 32 bits alignment.
52 
53  typedef std::list<std::pair<P<Goal>, P<Sim> > > SolutionList;
54 
55  class SimOutcomes {
56  public:
57  SolutionList mandatory_solutions;
58  SolutionList optional_solutions;
59  };
60 
61 // Simulated predictions of any goal success resulting from the simulation of the monitored goal.
64 
65  void store_simulated_outcome(Goal *affected_goal, Sim *sim, bool success); // store the outcomes of any goal affected by the simulation of the monitored goal.
67 
70  uint64_t deadline,
71  uint64_t sim_thz,
72  Fact *goal,
73  Fact *f_imdl); // goal is f0->g->f1->object.
74 public:
75  virtual bool signal(bool simulation) {
76  return false;
77  }
78 };
79 
80 // Monitors goals (other than requirements).
81 // Use for SIM_ROOT.
82 // Is aware of any predicted evidence for the goal target: if at construction time such an evidence is known, the goal is not injected.
83 // Reporting a success or failure to the controller invalidates the goal; reporting a predicted success also does.
84 // Reporting a predicted failure injects the goal if it has not been already, invalidates it otherwise (a new goal will be injected).
85 // The monitor still runs after reporting a predicted success.
86 // The monitor does not run anymore if the goal is invalidated (case of a predicted success, followed by a predicted failure).
87 // Wait for the time horizon; in the meantime:
88 // actual inputs:
89 // if an input is an evidence for the target, report a success.
90 // if an input is a counter-evidence of the target, report a failure.
91 // if an input is a predicted evidence for the target, report a predicted success.
92 // if an input is a predicted counter-evidence for the target, report a predicted failure.
93 // if there is a predicted evidence for the target that becomes invalidated, report a predicted failure.
94 // simulated predictions: catch only those that are a simulation for the monitored goal.
95 // if an input is an evidence of the goal target, simulate a prediction of the goal success.
96 // if an input is an evidence of the goal target, simulate a prediction of the goal failure.
97 // store any simulated prediction of success/failure for any goal.
98 // At the time horizon:
99 // simulation mode:
100 // commit to the appropriate solutions for the goal.
101 // mode become actual.
102 // time horizon becomes the goal deadline.
103 // actual mode
104 // if the goal is not invalidated, report a failure.
105 class GMonitor:
106  public _GMonitor {
107 protected:
108  _Fact *volatile predicted_evidence; // f0->pred->f1->object; 32 bits alignment.
110 
111  void commit();
112 public:
117  Fact *goal,
118  Fact *f_imdl,
119  _Fact *predicted_evidence); // goal is f0->g->f1->object.
120 
121  virtual bool reduce(_Fact *input); // returning true will remove the monitor form the controller.
122  virtual void update(uint64_t &next_target);
123 };
124 
125 // Monitors actual requirements.
126 // Use for SIM_ROOT.
127 // target==f_imdl; this means we need to fullfill some requirements:
128 // Wait until the deadline of the goal, in the meantime:
129 // each time the monitor is signalled (i.e. a new pred->f_imdl has been produced), check if chaining is allowed:
130 // if no, do nothing.
131 // if yes: assert success and abort: the model will bind its rhs with the bm retrieved from the pred->f_imdl; this will kill the monitor and a new one will be built for the bound rhs sub-goal.
132 // At the deadline, assert failure.
133 class RMonitor:
134  public GMonitor {
135 public:
140  Fact *goal,
141  Fact *f_imdl);
142 
143  bool reduce(_Fact *input);
144  void update(uint64_t &next_target);
145  bool signal(bool simulation);
146 };
147 
148 // Monitors simulated goals.
149 class SGMonitor:
150  public _GMonitor {
151 protected:
152  void commit();
153 public:
157  Fact *goal,
158  Fact *f_imdl); // goal is f0->g->f1->object.
159 
160  bool reduce(_Fact *input);
161  void update(uint64_t &next_target);
162 };
163 
164 // Monitors simulated requirements.
165 // Use for SIM_OPTIONAL and SIM_MANDATORY.
166 class SRMonitor:
167  public SGMonitor {
168 public:
172  Fact *goal,
173  Fact *f_imdl);
174 
175  bool reduce(_Fact *input);
176  void update(uint64_t &next_target);
177  bool signal(bool simulation);
178 };
179 
180 // Case A: target==actual goal and target!=f_imdl: simulations have been produced for all sub-goals.
181 // Wait until the STHZ, in the meantime:
182 // if input==goal target, assert success and abort: invalidate goal (this will invalidate the related simulations).
183 // if input==|goal target, assert failure and abort: the super-goal will probably fail and so on, until some drives fail, which will trigger their re-injection.
184 // if input==pred goal target, do nothing.
185 // if input==pred |goal target, do nothing.
186 // if input==pred success/failure of any other goal and sim->super-goal==goal, store the simulation for decision at STHZ.
187 // if input==pred goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
188 // if input==pred |goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
189 // if input==pred goal target and sim->super-goal!=goal, predict success for the goal.
190 // if input==pred |goal target and sim->super-goal!=goal, predict failure for the goal.
191 // At STHZ, choose the best simulations if any, and commit to their sub-goals; kill the predictions for the discarded simulations.
192 //
193 // Case B: target==f_imdl; this means we need to fullfill some requirements: simulations have been produced for all the sub-goals of f_imdl.
194 // Wait until the STHZ, in the meantime:
195 // if input==pred success/failure of any goal and sim->super-goal==goal, store the simulation for decision at STHZ.
196 // At STHZ, choose the best simulations if any, and commit to their sub-goals; kill the predictions for the discarded simulations.
197 //
198 // Case C: target==simulated goal and target!=f_imdl: simulations have been produced for all sub-goals.
199 // Wait until the STHZ, in the meantime:
200 // if input==goal target, predict success for the goal and abort: invalidate goal.
201 // if input==|goal target, predict failure for the goal and abort: invalidate goal.
202 // if input==pred success/failure of any other goal and sim->super-goal==goal, store the simulation for decision at STHZ.
203 // if input==pred goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
204 // if input==pred |goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
205 // if input==pred goal target and sim->super-goal!=goal, predict success for the goal.
206 // if input==pred |goal target and sim->super-goal!=goal, predict failure for the goal.
207 //
208 // Case D: target==simulated f_imdl; this means we need to fullfill some requirements: simulations have been produced for all the sub-goals of f_imdl.
209 // Wait until the STHZ, in the meantime:
210 // if input==pred success/failure of any goal and sim->super-goal==goal, store the simulation for decision at STHZ.
211 // At STHZ, choose the best simulations if any, and commit to their sub-goals; kill the predictions for the discarded simulations.
212 }
213 
214 
215 #endif
Definition: g_monitor.h:42
RMonitor(PrimaryMDLController *controller, BindingMap *bindings, uint64_t deadline, uint64_t sim_thz, Fact *goal, Fact *f_imdl)
Definition: g_monitor.cpp:291
SolutionList optional_solutions
Definition: g_monitor.h:58
Definition: factory.h:98
bool reduce(_Fact *input)
Definition: g_monitor.cpp:421
Definition: factory.h:145
_GMonitor(PMDLController *controller, BindingMap *bindings, uint64_t deadline, uint64_t sim_thz, Fact *goal, Fact *f_imdl)
Definition: g_monitor.cpp:39
void store_simulated_outcome(Goal *affected_goal, Sim *sim, bool success)
Definition: g_monitor.cpp:56
void invalidate_sim_outcomes()
Definition: g_monitor.cpp:85
uint64_t sim_thz
Definition: g_monitor.h:46
bool signal(bool simulation)
Definition: g_monitor.cpp:486
bool signal(bool simulation)
Definition: g_monitor.cpp:308
Definition: g_monitor.h:105
Definition: binding_map.h:177
SimOutcomes sim_failures
Definition: g_monitor.h:63
virtual bool reduce(_Fact *input)
Definition: g_monitor.cpp:177
_Fact *volatile predicted_evidence
Definition: g_monitor.h:108
Definition: g_monitor.h:149
Definition: g_monitor.h:55
Definition: _context.cpp:34
uint64_t deadline
Definition: g_monitor.h:45
P< BindingMap > bindings
Definition: monitor.h:45
SimOutcomes sim_successes
Definition: g_monitor.h:62
SGMonitor(PrimaryMDLController *controller, BindingMap *bindings, uint64_t sim_thz, Fact *goal, Fact *f_imdl)
Definition: g_monitor.cpp:378
uint64_t volatile simulating
Definition: g_monitor.h:51
virtual bool signal(bool simulation)
Definition: g_monitor.h:75
void commit()
Definition: g_monitor.cpp:123
Definition: base.h:47
P< Fact > f_imdl
Definition: g_monitor.h:48
SRMonitor(PrimaryMDLController *controller, BindingMap *bindings, uint64_t sim_thz, Fact *goal, Fact *f_imdl)
Definition: g_monitor.cpp:472
_Fact * goal_target
Definition: g_monitor.h:47
bool reduce(_Fact *input)
Definition: g_monitor.cpp:325
SimMode sim_mode
Definition: g_monitor.h:49
SimMode
Definition: factory.h:139
std::list< std::pair< P< Goal >, P< Sim > > > SolutionList
Definition: g_monitor.h:53
void update(uint64_t &next_target)
Definition: g_monitor.cpp:531
Definition: g_monitor.h:133
void update(uint64_t &next_target)
Definition: g_monitor.cpp:462
Definition: mdl_controller.h:306
bool reduce(_Fact *input)
Definition: g_monitor.cpp:501
Definition: g_monitor.h:166
void commit()
Definition: g_monitor.cpp:393
Definition: mdl_controller.h:214
Definition: factory.h:227
SolutionList mandatory_solutions
Definition: g_monitor.h:57
void update(uint64_t &next_target)
Definition: g_monitor.cpp:357
bool injected_goal
Definition: g_monitor.h:109
Definition: factory.h:174
GMonitor(PMDLController *controller, BindingMap *bindings, uint64_t deadline, uint64_t sim_thz, Fact *goal, Fact *f_imdl, _Fact *predicted_evidence)
Definition: g_monitor.cpp:104
MDLController * controller
Definition: monitor.h:48
Definition: monitor.h:42
virtual void update(uint64_t &next_target)
Definition: g_monitor.cpp:270