AERA
g_monitor.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef g_monitor_h
86 #define g_monitor_h
87 
88 #include "monitor.h"
89 
90 
91 namespace r_exec {
92 
93 class PMDLController;
94 class PrimaryMDLController;
95 
96 class _GMonitor :
97  public Monitor {
98 protected:
99  Timestamp deadline_; // of the goal.
100  Timestamp sim_thz_timestamp_;
101  _Fact *goal_target_; // convenience; f1->object.
102  P<Fact> f_imdl_;
103  SimMode sim_mode_;
104 
105  uint32 volatile simulating_; // 32 bits alignment.
106 
107  // The first in the pair is the (fact (pred (fact (success...)))) or
108  // (fact (pred (|fact (success...)))) where the success's object is the (fact (goal ...)).
109  typedef std::list<std::pair<P<_Fact>, P<Sim> > > SolutionList;
110 
111  /* From the (fact (pred (fact (success...)))) of a solution, get the goal from
112  * the success object's (fact (goal ...)).
113  */
114  static Goal* get_solution_goal(_Fact* f_pred_f_success) {
115  Pred* pred = f_pred_f_success->get_pred();
116  if (!pred)
117  return NULL;
118  Success* success = pred->get_target()->get_success();
119  if (!success)
120  return NULL;
121  return success->get_object()->get_goal();
122  }
123 
124  class SimOutcomes {
125  public:
126  SolutionList mandatory_solutions;
127  SolutionList optional_solutions;
128  };
129 
130  // Simulated predictions of any goal success resulting from the simulation of the monitored goal.
131  SimOutcomes sim_successes_;
132  SimOutcomes sim_failures_;
133 
142  void store_simulated_outcome(_Fact *f_pred_f_success, Sim *sim);
143  void invalidate_sim_outcomes();
144 
145  _GMonitor(PMDLController *controller,
146  BindingMap *bindings,
147  Timestamp deadline,
148  Timestamp sim_thz_timestamp,
149  Fact *goal,
150  Fact *f_imdl); // goal is f0->g->f1->object.
151 public:
155  virtual bool signal(Pred* /* prediction */) { return false; }
156 };
157 
185 class GMonitor :
186  public _GMonitor {
187 protected:
188  _Fact *volatile predicted_evidence_; // f0->pred->f1->object; 32 bits alignment.
189  bool injected_goal_;
190 
191  void commit();
192 public:
193  GMonitor(PMDLController *controller,
194  BindingMap *bindings,
195  Timestamp deadline,
196  Timestamp sim_thz_timestamp,
197  Fact *goal,
198  Fact *f_imdl,
199  _Fact *predicted_evidence); // goal is f0->g->f1->object.
200 
201  bool reduce(_Fact *input) override; // returning true will remove the monitor form the controller.
202  virtual void update(Timestamp &next_target);
203 };
204 
216 class RMonitor :
217  public GMonitor {
218 public:
219  RMonitor(PrimaryMDLController *controller,
220  BindingMap *bindings,
221  Timestamp deadline,
222  Timestamp sim_thz_timestamp,
223  Fact *goal,
224  Fact *f_imdl);
225 
226  bool reduce(_Fact *input) override;
227  void update(Timestamp &next_target) override;
228  bool signal(Pred* prediction) override;
229 };
230 
231 // Monitors simulated goals.
232 class SGMonitor :
233  public _GMonitor {
234 protected:
235  void commit();
236 public:
237  SGMonitor(PrimaryMDLController *controller,
238  BindingMap *bindings,
239  Timestamp sim_thz_timestamp,
240  Fact *goal,
241  Fact *f_imdl); // goal is f0->g->f1->object.
242 
243  bool reduce(_Fact *input) override;
244  void update(Timestamp &next_target);
245 };
246 
247 // Monitors simulated requirements.
248 // Use for SIM_OPTIONAL and SIM_MANDATORY.
249 class SRMonitor :
250  public SGMonitor {
251 public:
252  SRMonitor(PrimaryMDLController *controller,
253  BindingMap *bindings,
254  Timestamp sim_thz_timestamp,
255  Fact *goal,
256  Fact *f_imdl);
257 
258  bool reduce(_Fact *input) override;
259  void update(Timestamp &next_target);
260 
261  bool signal(Pred* prediction) override;
262 };
263 
264 // Case A: target==actual goal and target!=f_imdl: simulations have been produced for all sub-goals.
265 // Wait until the STHZ, in the meantime:
266 // if input==goal target, assert success and abort: invalidate goal (this will invalidate the related simulations).
267 // 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.
268 // if input==pred goal target, do nothing.
269 // if input==pred |goal target, do nothing.
270 // if input==pred success/failure of any other goal and sim->super-goal==goal, store the simulation for decision at STHZ.
271 // if input==pred goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
272 // if input==pred |goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
273 // if input==pred goal target and sim->super-goal!=goal, predict success for the goal.
274 // if input==pred |goal target and sim->super-goal!=goal, predict failure for the goal.
275 // At STHZ, choose the best simulations if any, and commit to their sub-goals; kill the predictions for the discarded simulations.
276 //
277 // 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.
278 // Wait until the STHZ, in the meantime:
279 // if input==pred success/failure of any goal and sim->super-goal==goal, store the simulation for decision at STHZ.
280 // At STHZ, choose the best simulations if any, and commit to their sub-goals; kill the predictions for the discarded simulations.
281 //
282 // Case C: target==simulated goal and target!=f_imdl: simulations have been produced for all sub-goals.
283 // Wait until the STHZ, in the meantime:
284 // if input==goal target, predict success for the goal and abort: invalidate goal.
285 // if input==|goal target, predict failure for the goal and abort: invalidate goal.
286 // if input==pred success/failure of any other goal and sim->super-goal==goal, store the simulation for decision at STHZ.
287 // if input==pred goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
288 // if input==pred |goal target and sim->super-goal==goal, store the simulation for decision at STHZ.
289 // if input==pred goal target and sim->super-goal!=goal, predict success for the goal.
290 // if input==pred |goal target and sim->super-goal!=goal, predict failure for the goal.
291 //
292 // 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.
293 // Wait until the STHZ, in the meantime:
294 // if input==pred success/failure of any goal and sim->super-goal==goal, store the simulation for decision at STHZ.
295 // At STHZ, choose the best simulations if any, and commit to their sub-goals; kill the predictions for the discarded simulations.
296 }
297 
298 
299 #endif
r_exec::_GMonitor::SimOutcomes
Definition: g_monitor.h:124
r_exec::RMonitor::signal
bool signal(Pred *prediction) override
Definition: g_monitor.cpp:400
r_exec::BindingMap
Definition: binding_map.h:248
r_exec::Goal
Definition: factory.h:517
r_exec::Sim
Definition: factory.h:248
r_exec::_GMonitor::store_simulated_outcome
void store_simulated_outcome(_Fact *f_pred_f_success, Sim *sim)
Definition: g_monitor.cpp:113
r_exec::_Fact
Definition: factory.h:155
core::P
Definition: base.h:103
r_exec::SRMonitor
Definition: g_monitor.h:250
r_exec::SGMonitor
Definition: g_monitor.h:233
r_exec::Success
Definition: factory.h:607
r_exec::GMonitor
Definition: g_monitor.h:186
r_exec::RMonitor
Definition: g_monitor.h:217
r_exec::Pred
Definition: factory.h:394
r_exec::_GMonitor::signal
virtual bool signal(Pred *)
Definition: g_monitor.h:155
r_exec::PrimaryMDLController
Definition: mdl_controller.h:430
r_exec::_GMonitor
Definition: g_monitor.h:97
r_exec::Fact
Definition: factory.h:360
r_exec::SRMonitor::signal
bool signal(Pred *prediction) override
Definition: g_monitor.cpp:592
r_exec::Success::get_object
_Fact * get_object() const
Definition: factory.h:617
r_exec::Monitor
Definition: monitor.h:98
r_exec::PMDLController
Definition: mdl_controller.h:332