AERA
pattern_extractor.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 //_/_/ Copyright (c) 2023 Leonard M. Eberding
10 //_/_/ http://www.iiim.is
11 //_/_/
12 //_/_/ Copyright (c) 2010-2012 Eric Nivel
13 //_/_/ Center for Analysis and Design of Intelligent Agents
14 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
15 //_/_/ http://cadia.ru.is
16 //_/_/
17 //_/_/ Part of this software was developed by Eric Nivel
18 //_/_/ in the HUMANOBS EU research project, which included
19 //_/_/ the following parties:
20 //_/_/
21 //_/_/ Autonomous Systems Laboratory
22 //_/_/ Technical University of Madrid, Spain
23 //_/_/ http://www.aslab.org/
24 //_/_/
25 //_/_/ Communicative Machines
26 //_/_/ Edinburgh, United Kingdom
27 //_/_/ http://www.cmlabs.com/
28 //_/_/
29 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
30 //_/_/ University of Lugano and SUPSI, Switzerland
31 //_/_/ http://www.idsia.ch/
32 //_/_/
33 //_/_/ Institute of Cognitive Sciences and Technologies
34 //_/_/ Consiglio Nazionale delle Ricerche, Italy
35 //_/_/ http://www.istc.cnr.it/
36 //_/_/
37 //_/_/ Dipartimento di Ingegneria Informatica
38 //_/_/ University of Palermo, Italy
39 //_/_/ http://diid.unipa.it/roboticslab/
40 //_/_/
41 //_/_/
42 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
43 //_/_/
44 //_/_/ Redistribution and use in source and binary forms, with or without
45 //_/_/ modification, is permitted provided that the following conditions
46 //_/_/ are met:
47 //_/_/ - Redistributions of source code must retain the above copyright
48 //_/_/ and collaboration notice, this list of conditions and the
49 //_/_/ following disclaimer.
50 //_/_/ - Redistributions in binary form must reproduce the above copyright
51 //_/_/ notice, this list of conditions and the following disclaimer
52 //_/_/ in the documentation and/or other materials provided with
53 //_/_/ the distribution.
54 //_/_/
55 //_/_/ - Neither the name of its copyright holders nor the names of its
56 //_/_/ contributors may be used to endorse or promote products
57 //_/_/ derived from this software without specific prior
58 //_/_/ written permission.
59 //_/_/
60 //_/_/ - CADIA Clause: The license granted in and to the software
61 //_/_/ under this agreement is a limited-use license.
62 //_/_/ The software may not be used in furtherance of:
63 //_/_/ (i) intentionally causing bodily injury or severe emotional
64 //_/_/ distress to any person;
65 //_/_/ (ii) invading the personal privacy or violating the human
66 //_/_/ rights of any person; or
67 //_/_/ (iii) committing or preparing for any act of war.
68 //_/_/
69 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
70 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
71 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
72 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
73 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
74 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
76 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
78 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
80 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
82 //_/_/ OF SUCH DAMAGE.
83 //_/_/
84 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
85 
86 #ifndef pattern_extractor_h
87 #define pattern_extractor_h
88 
89 #include "../r_code/time_buffer.h"
90 
91 #include "binding_map.h"
92 #include "guard_builder.h"
93 #include "cst_controller.h"
94 
95 
96 namespace r_exec {
97 
98 class AutoFocusController;
99 
100 class Input {
101 public:
102  P<BindingMap> bindings_; // contains the values for the abstraction.
103  P<_Fact> abstraction_;
104  P<_Fact> input_;
105  bool eligible_cause_;
106  Timestamp ijt_; // injection time.
107 
108  Input(View *input, _Fact *abstraction, BindingMap *bindings) : input_(input->object_), ijt_(input->get_ijt()), eligible_cause_(IsEligibleCause(input)), abstraction_(abstraction), bindings_(bindings) {}
109  Input() : input_(NULL), eligible_cause_(false), abstraction_(NULL), bindings_(NULL), ijt_(std::chrono::seconds(0)) {}
110  Input(const Input &original) : input_(original.input_), eligible_cause_(original.eligible_cause_), abstraction_(original.abstraction_), bindings_(original.bindings_), ijt_(original.ijt_) {}
111 
112  static bool IsEligibleCause(r_exec::View *view);
113 
114  class IsInvalidated { // for storage in time_buffers.
115  public:
116  bool operator ()(Input &i, Timestamp time_reference, std::chrono::microseconds thz) const {
117 
118  return (time_reference - i.ijt_ > thz);
119  }
120  };
121 };
122 
123 class CInput { // cached inputs.
124 public:
125  P<BindingMap> bindings_; // contains the values for the abstraction.
126  P<_Fact> abstraction_;
127  P<View> input_;
128  bool injected_;
129  Timestamp ijt_; // injection time.
130  CInput(View *input, _Fact *abstraction, BindingMap *bindings) : input_(input), abstraction_(abstraction), bindings_(bindings), injected_(false), ijt_(input->get_ijt()) {}
131  CInput() : input_(NULL), abstraction_(NULL), bindings_(NULL), injected_(false), ijt_(std::chrono::seconds(0)) {}
132 
133  bool operator ==(const CInput &i) const { return input_ == i.input_; }
134 
135  class IsInvalidated { // for storage in time_buffers.
136  public:
137  bool operator ()(CInput &i, Timestamp time_reference, std::chrono::microseconds thz) const {
138 
139  return (time_reference - i.ijt_ > thz);
140  }
141  };
142 };
143 
144 // Targeted Pattern eXtractor.
145 // Does nothing.
146 // Used for wr_enabled productions, for well-rated productions or when model acqusiiton is disabled.
147 class r_exec_dll TPX :
148  public _Object {
149 protected:
150  AutoFocusController *auto_focus_;
151  P<_Fact> target_; // goal or prediction target, or premise (CTPX); abstraction: lhs of a mdl for goals, rhs for predictions, premise for CTPX.
152  P<BindingMap> target_bindings_;
153  P<_Fact> abstracted_target_;
154 
155  P<CSTController> cst_hook_; // in case the target is an icst.
156 
157  std::vector<P<BindingMap> > new_maps_; // acquired (in the case the target's bm is not fully specified) while matching the target's bm with inputs.
158 
159  bool filter(View *input, _Fact *abstracted_input, BindingMap *bm);
160 
161  TPX(AutoFocusController *auto_focus, _Fact *target);
162 public:
163  TPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings);
164  virtual ~TPX();
165 
166  _Fact *get_pattern() const { return abstracted_target_; }
167  BindingMap *get_bindings() const { return target_bindings_; }
168 
169  virtual bool take_input(View *view, _Fact *abstracted_input, BindingMap *bm);
170  virtual void signal(View *input) const;
171  virtual void ack_pred_success(Success* success);
172 };
173 
174 class ICST;
175 
176 class r_exec_dll _TPX :
177  public TPX {
178 private:
179  static const uint32 InputsInitialSize = 16;
180 protected:
181  class Component { // for building csts.
182  public:
183  _Fact *object;
184  bool discarded;
185  Component() {}
186  Component(_Fact *object) : object(object), discarded(false) {}
187  };
188 
193  public:
194  FindFIcstResult(_Fact* f_icst, _Fact* component_pattern)
195  {
196  this->f_icst = f_icst;
197  this->component_pattern = component_pattern;
198  }
199 
200  P<_Fact> f_icst;
201  _Fact* component_pattern;
202  };
203 
204  r_code::list<Input> inputs_; // time-controlled buffer (inputs older than tpx_time_horizon from now are discarded).
205  std::vector<P<r_code::Code> > mdls_; // new mdls.
206  std::vector<P<r_code::Code> > csts_; // new csts.
207  std::vector<P<_Fact> > f_icsts_; // facts of new icsts.
208 
209  void filter_icst_components(ICST *icst, uint32 icst_index, std::vector<Component> &components);
210 
220  static _Fact* find_f_icst_component(_Fact* fact, const _Fact* component, int max_depth = 3);
221 
230  void _find_f_icst(_Fact *component, std::vector<FindFIcstResult>& results, bool find_multiple);
231 
240  void find_f_icst(_Fact *component, std::vector<FindFIcstResult>& results, bool find_multiple = false);
241 
252  void find_f_icst(_Fact *component, std::vector<FindFIcstResult>& results, P<r_code::Code> &new_cst, bool find_multiple = false);
253 
254  _Fact *make_f_icst(_Fact *component, _Fact*& component_pattern, P<r_code::Code> &new_cst);
255  r_code::Code *build_cst(const std::vector<Component> &components, BindingMap *bm, _Fact *main_component);
256 
257  static r_code::Code *build_mdl_head(HLPBindingMap *bm, uint16 tpl_arg_count, _Fact *lhs, _Fact *rhs, uint16 &write_index, bool allow_shared_timing_vars = true);
258  static r_code::Code* build_mdl_head_from_abstract(uint16 tpl_arg_count, r_code::Code* lhs, r_code::Code* rhs, uint16& write_index);
259  // check for mdl existence at the same time(ModelBase::mdlCS_ - wise).
260  bool build_requirement(HLPBindingMap* bm, r_code::Code* m0, std::chrono::microseconds period, const std::vector<FindFIcstResult>& results,
261  r_code::Code* new_cst);
262  void build_mdl_tail(r_code::Code *mdl, uint16 write_index);
263 
264  void inject_hlps() const;
265  void inject_hlps(Timestamp analysis_starting_time);
266 
267  virtual std::string get_header() const = 0;
268 
269  _TPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings);
270  _TPX(AutoFocusController *auto_focus, _Fact *target);
271 public:
272  virtual ~_TPX();
273 
274  void debug(View* /* input */) {};
275 };
276 
277 // Pattern extractor targeted at goal successes.
278 // Possible causes are younger than the production of the goal.
279 // Models produced are of the form: M1[cause -> goal_target], where cause can be an imdl and goal_target can be an imdl.
280 // M1 does not have template arguments.
281 // Commands are ignored (CTPX' job).
282 class r_exec_dll GTPX : // target is the goal target.
283  public _TPX {
284 private:
285  P<Fact> f_imdl_; // that produced the goal.
286 
287  std::vector<P<_Fact> > predictions_; // successful predictions that may invalidate the need for model building.
288 
289  bool build_mdl(_Fact *cause, _Fact* f_icst, _Fact *consequent, GuardBuilder *guard_builder, std::chrono::microseconds period);
290  bool build_mdl(_Fact *f_icst, _Fact *cause_pattern, _Fact *consequent, GuardBuilder *guard_builder, std::chrono::microseconds period, r_code::Code *new_cst);
291 
292  std::string get_header() const override;
293 public:
294  GTPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings, Fact *f_imdl);
295  ~GTPX();
296 
297  bool take_input(View *input, _Fact *abstracted_input, BindingMap *bm) override;
298  void signal(View *input) const override;
299 
305  void ack_pred_success(Success* success) override;
306  void reduce(View *input); // input is v->f->success(target,input) or v->|f->success(target,input).
307 };
308 
309 // Pattern extractor targeted at prediciton failures.
310 // Possible causes are older than the production of the prediction.
311 // Models produced are of the form: M1[cause -> |imdl M0] where M0 is the model that produced the failed prediction and cause can be an imdl.
312 // M1 does not have template arguments.
313 // Commands are ignored (CTPX' job).
314 class r_exec_dll PTPX : // target is the prediction.
315  public _TPX {
316 private:
317  P<Fact> f_imdl_; // that produced the prediction (and for which the PTPX will find strong requirements).
318 
319  bool build_mdl(_Fact *cause, _Fact *consequent, GuardBuilder *guard_builder, std::chrono::microseconds period);
320  bool build_mdl(_Fact *f_icst, _Fact *cause_pattern, _Fact *consequent, GuardBuilder *guard_builder, std::chrono::microseconds period, r_code::Code *new_cst);
321 
322  std::string get_header() const override;
323 public:
324  PTPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings, Fact *f_imdl);
325  ~PTPX();
326 
327  void signal(View *input) const override;
328  void reduce(View *input); // input is v->f->success(target,input) or v->|f->success(target,input).
329 };
330 
331 // Pattern extractor targeted at changes of repeated input facts (SYMC_PERIODIC or SYNC_HOLD).
332 // Models produced are of the form: [premise -> [cause -> consequent]], i.e. M1:[premise -> imdl M0], M0:[cause -> consequent].
333 // M0 has template args, i.e the value of the premise and its after timestamp.
334 // N.B.: before-after=upr of the group the input comes from.
335 // The Consequent is a value different from the expected repetition of premise.
336 // The premise is an icst assembled from inputs synchronous with the input expected to repeat.
337 // Guards on values (not only on timings) are computed: this is the only TPX that does so.
338 // Inputs with SYNC_HOLD: I/O devices are expected to send changes on such inputs as soon as possible.
339 class CTPX :
340  public _TPX {
341 private:
342  bool stored_premise_;
343  P<View> premise_;
344 
345  GuardBuilder *get_default_guard_builder(_Fact *cause, _Fact *consequent, std::chrono::microseconds period);
346  GuardBuilder *find_guard_builder(_Fact *cause, _Fact *consequent, std::chrono::microseconds period);
347 
348  bool build_mdl(_Fact *cause, _Fact *consequent, GuardBuilder *guard_builder, std::chrono::microseconds period);
349  bool build_mdl(_Fact *f_icst, _Fact *cause_pattern, _Fact *consequent, GuardBuilder *guard_builder, std::chrono::microseconds period);
350 
351  bool build_requirement(HLPBindingMap *bm, r_code::Code *m0, std::chrono::microseconds period);
352 
353  std::string get_header() const override;
354 public:
355  CTPX(AutoFocusController *auto_focus, View *premise);
356  ~CTPX();
357 
358  void store_input(r_exec::View *input);
359  void reduce(r_exec::View *input); // asynchronous: build models of value change if not aborted asynchronously by ASTControllers.
360  void signal(r_exec::View *input); // spawns mdl/cst building (reduce()).
361 };
362 }
363 
364 
365 #endif
r_exec::_TPX::find_f_icst
void find_f_icst(_Fact *component, std::vector< FindFIcstResult > &results, P< r_code::Code > &new_cst, bool find_multiple=false)
r_exec::GTPX
Definition: pattern_extractor.h:283
r_exec::_TPX::find_f_icst
void find_f_icst(_Fact *component, std::vector< FindFIcstResult > &results, bool find_multiple=false)
r_exec::CInput
Definition: pattern_extractor.h:123
r_exec::CTPX
Definition: pattern_extractor.h:340
r_exec::BindingMap
Definition: binding_map.h:248
r_exec::PTPX
Definition: pattern_extractor.h:315
r_exec::_Fact
Definition: factory.h:155
r_exec::_TPX::FindFIcstResult
Definition: pattern_extractor.h:192
r_exec::ICST
Definition: factory.h:655
core::P
Definition: base.h:103
r_code::Code
Definition: r_code/object.h:224
r_exec::TPX
Definition: pattern_extractor.h:148
r_exec::Success
Definition: factory.h:607
r_exec::AutoFocusController
Definition: auto_focus.h:99
core::_Object
Definition: base.h:131
r_exec::Input::IsInvalidated
Definition: pattern_extractor.h:114
r_exec::_TPX
Definition: pattern_extractor.h:177
r_exec::View
Definition: view.h:102
r_exec::HLPBindingMap
Definition: binding_map.h:393
r_exec::Input
Definition: pattern_extractor.h:100
r_exec::CInput::IsInvalidated
Definition: pattern_extractor.h:135
r_code::list
Definition: list.h:99
r_exec::Fact
Definition: factory.h:360
r_exec::GuardBuilder
Definition: guard_builder.h:94
r_exec::_TPX::Component
Definition: pattern_extractor.h:181