AERA
hlp_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 hlp_controller_h
86 #define hlp_controller_h
87 
88 #include "overlay.h"
89 #include "binding_map.h"
90 #include "g_monitor.h"
91 #include "group.h"
92 #include "init.h"
93 
94 
95 namespace r_exec {
96 
97 typedef enum {
98  WEAK_REQUIREMENT_DISABLED = 0,
99  STRONG_REQUIREMENT_NO_WEAK_REQUIREMENT = 1,
100  STRONG_REQUIREMENT_DISABLED_WEAK_REQUIREMENT = 2,
101  WEAK_REQUIREMENT_ENABLED = 3,
102  NO_REQUIREMENT = 4
103 }ChainingStatus;
104 
106  public OController {
107 private:
108  uint32 strong_requirement_count_; // number of active strong requirements in the same group; updated dynamically.
109  uint32 weak_requirement_count_; // number of active weak requirements in the same group; updated dynamically.
110  uint32 requirement_count_; // sum of the two above.
111 protected:
112  class EvidenceEntry { // evidences.
113  private:
114  void load_data(_Fact *evidence);
115  public:
116  P<_Fact> evidence_;
117  Timestamp after_;
118  Timestamp before_;
119  float32 confidence_;
120 
121  EvidenceEntry();
122  EvidenceEntry(_Fact *evidence);
123  EvidenceEntry(_Fact *evidence, _Fact *payload);
124 
125  bool is_too_old(Timestamp now) const {
126  return evidence_->is_invalidated() ||
127  // If an instantaneous fact, require if to be strictly before now to be considered old,
128  // otherwise consider a time interval to be old if now is at the end of the interval.
129  (after_ == before_ ? (before_ < now) : (before_ <= now));
130  }
131  };
132 
133  class PredictedEvidenceEntry : // predicted evidences.
134  public EvidenceEntry {
135  public:
137  PredictedEvidenceEntry(_Fact *evidence);
138  };
139 
141  CriticalSectionList<PredictedEvidenceEntry> predicted_evidences_;
142 
143  template<class E> void _store_evidence(CriticalSectionList<E> *cache, _Fact *evidence) {
144 
145  E e(evidence);
146  cache->CS_.enter();
147  auto now = Now();
149  for (_e = cache->list_.begin(); _e != cache->list_.end();) {
150 
151  if ((*_e).evidence_ == e.evidence_) {
152  // Already stored.
153  cache->CS_.leave();
154  return;
155  }
156  if ((*_e).is_too_old(now)) // garbage collection.
157  _e = cache->list_.erase(_e);
158  else
159  ++_e;
160  }
161  cache->list_.push_front(e);
162  cache->CS_.leave();
163  }
164 
165  P<HLPBindingMap> bindings_;
166 
176  bool evaluate_bwd_guards(HLPBindingMap *bm, bool narrow_fwd_timings = false);
177 
178  MatchResult check_evidences(_Fact *target, _Fact *&evidence); // evidence with the match (positive or negative), get_absentee(target) otherwise.
179  MatchResult check_predicted_evidences(_Fact *target, _Fact *&evidence); // evidence with the match (positive or negative), NULL otherwise.
180 
187 
188  bool has_tpl_args_;
189  uint32 ref_count_; // used to detect _Object::refCount_ dropping down to 1 for hlp with tpl args.
190  bool is_orphan(); // true when there are tpl args and no requirements: the controller cannot execute anymore.
191 
192  std::vector<P<HLPController> > controllers_; // all controllers for models/states instantiated in the patterns; case of models: [0]==lhs, [1]==rhs.
193  Timestamp last_match_time_; // last time a match occurred (fwd), regardless of its outcome.
194  bool become_invalidated(); // true if one controller is invalidated or if all controllers pointing to this are invalidated.
195  virtual void kill_views() {}
196  virtual void check_last_match_time(bool match) = 0;
197 
198  HLPController(r_code::_View *view);
199 public:
200  virtual ~HLPController();
201 
202  void invalidate() override;
203 
204  r_code::Code *get_core_object() const override { return get_object(); } // cst or mdl.
205  r_code::Code *get_unpacked_object() const { // the unpacked version of the core object.
206 
207  r_code::Code *core_object = get_core_object();
208  return core_object->get_reference(core_object->references_size() - MDL_HIDDEN_REFS);
209  }
210 
211  void add_requirement(bool strong);
212  void remove_requirement(bool strong);
213 
214  uint32 get_requirement_count(uint32 &weak_requirement_count, uint32 &strong_requirement_count);
215  uint32 get_requirement_count();
216 
217  void store_evidence(_Fact *evidence) { _store_evidence<EvidenceEntry>(&evidences_, evidence); }
218  void store_predicted_evidence(_Fact *evidence) { _store_evidence <PredictedEvidenceEntry>(&predicted_evidences_, evidence); }
219 
220  virtual Fact *get_f_ihlp(HLPBindingMap *bindings, bool wr_enabled) const = 0;
221 
222  uint16 get_out_group_count() const;
223  r_code::Code *get_out_group(uint16 i) const; // i starts at 1.
224  Group *get_host() const { return (Group *)get_view()->get_host(); }
225  bool has_tpl_args() const { return has_tpl_args_; }
226 
227  bool inject_prediction(Fact *prediction, float32 confidence) const; // for simulated predictions.
228 };
229 }
230 
231 
232 #endif
r_code::list::const_iterator
Definition: list.h:266
r_exec::HLPController::evaluate_bwd_guards
bool evaluate_bwd_guards(HLPBindingMap *bm, bool narrow_fwd_timings=false)
Definition: hlp_controller.cpp:168
r_code::_View
Definition: r_code/object.h:164
r_exec::_Fact
Definition: factory.h:155
core::P
Definition: base.h:103
r_code::Code
Definition: r_code/object.h:224
r_exec::HLPController
Definition: hlp_controller.h:106
r_exec::HLPController::EvidenceEntry
Definition: hlp_controller.h:112
r_exec::OController
Definition: controller.h:157
r_exec::HLPController::inject_notification_into_out_groups
void inject_notification_into_out_groups(r_code::Code *origin, r_code::Code *marker) const
Definition: hlp_controller.cpp:301
r_exec::HLPController::PredictedEvidenceEntry
Definition: hlp_controller.h:134
r_exec::CriticalSectionList
Definition: overlay.h:183