AERA
cst_controller.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 cst_controller_h
86 #define cst_controller_h
87 
88 #include "hlp_overlay.h"
89 #include "hlp_controller.h"
90 #include "factory.h"
91 
92 
93 namespace r_exec {
94 
95 // All inputs are expected to be synchronized (within the time tolerance).
96 // The time value of each fact is therefore the same (within the time tolerance).
97 // Therefore, said time value is not listed in the argument list of the icst.
98 // It is held by the fact holding said icst.
99 // Inputs synchronized on state are treated as if they were produced continuously, i.e. Now().
100 // The confidence value for an icst is the lowest value taken from the matched inputs.
101 // No cmds or imdls in a cst.
102 //
103 // Forward chaining:
104 // output a prediction of icst instead of icst if at least one input is a prediction.
105 // output as many predictions of icst as we got simulated predictions for different goals.
106 class CSTOverlay :
107  public HLPOverlay {
108 protected:
109  Timestamp match_deadline_; // before deadline after the last match.
110  float32 lowest_cfd_; // among the inputs (forward chaining).
111 
112  // The set of accumulated DefeasibleValidity from inputs, which is copied to each produced Pred.
113  std::set<P<DefeasibleValidity> > defeasible_validities_;
114 
115  std::unordered_set<P<_Fact>, r_code::PHash<_Fact> > predictions_; // f0->pred->f1->obj.
116  std::unordered_set<P<Sim>, r_code::PHash<Sim> > simulations_;
117  // This tracks whether promoting has already been done for this Sim. It needs to be a list of Sim
118  // because this overlay may be for the initial non-simulated icst which can be promoted for multiple sims.
119  std::vector<Sim*> promoted_in_sim_;
120 
121  r_code::list<P<_Fact> > axiom_patterns_;
122  r_code::list<P<_Fact> > non_axiom_patterns_;
123  std::vector<P<_Fact> > axiom_inputs_;
124  std::vector<P<_Fact> > non_axiom_inputs_;
125 
131  _Fact* inject_production(View* input);
132  void update(HLPBindingMap *map, _Fact *input, bool is_axiom);
133 
144  CSTOverlay *get_offspring(HLPBindingMap *map, _Fact *input, bool is_axiom, _Fact *bound_pattern = NULL);
145 
151  Sim* get_simulation(Controller *root) const;
152 
153  CSTOverlay(const CSTOverlay *original);
154 public:
155  CSTOverlay(Controller *c, HLPBindingMap *bindings);
156  ~CSTOverlay();
157 
158  bool reduce(View *input, CSTOverlay *&offspring);
159 
160  void load_patterns();
161 
162  bool can_match(Timestamp now) const;
163 
164  bool is_simulated() { return simulations_.size() > 0; }
165 
179  _Fact* bind_pattern(_Fact *input, HLPBindingMap* map, Sim* predictionSimulation, bool& is_axiom);
180 };
181 
182 // Backward chaining:
183 // if there are requirements, do nothing: these requirements will get the goal and abduce.
184 // else
185 // bind all patterns and look in the cache for positive evidences; for all bound patterns not matched in the cache, output a sub-goal (simulated or not, depending on the super-goal).
187  public HLPController {
188 private:
189  friend class CSTOverlay;
190  Group *secondary_host_;
191 
199  void abduce_simulated(HLPBindingMap *bm, Fact *f_super_goal);
200  void abduce(HLPBindingMap *bm, Fact *f_super_goal); // f_super_goal is f0->g->f1->icst or f0->g->|f1->icst.
201  void inject_goal(HLPBindingMap *bm,
202  Fact *f_super_goal, // f0->g->f1->icst or f0->g->|f1->icst.
203  _Fact *sub_goal_target, // f1.
204  Sim *sim,
205  Timestamp now,
206  float32 confidence,
207  r_code::Code *group) const;
208 
209  void kill_views() override;
210  void check_last_match_time(bool match) override; // kill if no match after primary_thz;
211 public:
213  ~CSTController();
214 
215  void take_input(r_exec::View *input) override;
216  void reduce(r_exec::View *input);
217 
218  Fact *get_f_ihlp(HLPBindingMap *bindings, bool wr_enabled) const override;
219  Fact *get_f_icst(HLPBindingMap *bindings, std::vector<P<_Fact> > *axiom_inputs, std::vector<P<_Fact> > *non_axiom_inputs) const;
220 
221  void inject_icst(Fact *production, float32 confidence, std::chrono::microseconds time_to_live) const; // here, resilience=time to live, in us.
222  bool inject_prediction(Fact *prediction, float32 confidence, std::chrono::microseconds time_to_live) const; // here, resilience=time to live, in us; returns true if the prediction has actually been injected.
223 
224  void set_secondary_host(Group *host);
225  Group *get_secondary_host() const;
226 };
227 }
228 
229 
230 #endif
r_code::_View
Definition: r_code/object.h:164
r_exec::Sim
Definition: factory.h:248
r_exec::_Fact
Definition: factory.h:155
core::P
Definition: base.h:103
r_code::Code
Definition: r_code/object.h:224
r_exec::CSTOverlay::inject_production
_Fact * inject_production(View *input)
Definition: cst_controller.cpp:137
r_exec::HLPController
Definition: hlp_controller.h:106
r_exec::View
Definition: view.h:102
r_exec::Group
Definition: group.h:108
r_exec::HLPBindingMap
Definition: binding_map.h:393
r_exec::HLPOverlay
Definition: hlp_overlay.h:98
r_exec::CSTController
Definition: cst_controller.h:187
r_exec::Controller
Definition: controller.h:104
r_exec::CSTOverlay::get_offspring
CSTOverlay * get_offspring(HLPBindingMap *map, _Fact *input, bool is_axiom, _Fact *bound_pattern=NULL)
Definition: cst_controller.cpp:200
r_code::list
Definition: list.h:99
r_exec::Fact
Definition: factory.h:360
r_exec::CSTOverlay
Definition: cst_controller.h:107
r_exec::CSTOverlay::get_simulation
Sim * get_simulation(Controller *root) const
Definition: cst_controller.cpp:502
r_exec::CSTOverlay::bind_pattern
_Fact * bind_pattern(_Fact *input, HLPBindingMap *map, Sim *predictionSimulation, bool &is_axiom)
Definition: cst_controller.cpp:448
r_code::PHash
Definition: r_code/utils.h:96