AERA
binding_map.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 binding_map_h
86 #define binding_map_h
87 
88 #include "controller.h"
89 #include "object.h"
90 #include "dll.h"
91 
92 
93 namespace r_exec {
94 
95 class BindingMap;
96 class AtomValue;
97 class StructureValue;
98 class ObjectValue;
99 
100 class r_exec_dll Value :
101  public _Object {
102 protected:
103  BindingMap *map_;
104  Value(BindingMap *map);
105 public:
106  virtual Value *copy(BindingMap *map) const = 0;
107  virtual void valuate(r_code::Code *destination, uint16 write_index, uint16 &extent_index) const = 0;
108  virtual bool match(const r_code::Code *object, uint16 index) = 0;
109  virtual Atom *get_code() = 0;
110  virtual r_code::Code *get_object() = 0;
111  virtual uint16 get_code_size() = 0;
112 
113  virtual bool intersect(const Value* /* v */) const { return false; }
114  virtual bool _intersect(const AtomValue* /* v */) const { return false; }
115  virtual bool _intersect(const StructureValue* /* v */) const { return false; }
116  virtual bool _intersect(const ObjectValue* /* v */) const { return false; }
117 
118  virtual bool contains(const Atom /* a */) const { return false; }
119  virtual bool contains(const Atom* /* s */) const { return false; }
120  virtual bool contains(const r_code::Code* /* o */) const { return false; }
121 
126  std::string trace_string() const {
127  P<LObject> code = new LObject();
128  uint16 extent_index = 1;
129  valuate(code, 0, extent_index);
130  return code->trace_string();
131  }
132 };
133 
134 class r_exec_dll BoundValue :
135  public Value {
136 protected:
137  BoundValue(BindingMap *map);
138 public:
139 };
140 
141 class r_exec_dll UnboundValue :
142  public Value {
143 private:
144  uint8 index_;
145 public:
146  UnboundValue(BindingMap *map, uint8 index);
147  ~UnboundValue();
148 
149  Value *copy(BindingMap *map) const override;
150  void valuate(r_code::Code *destination, uint16 write_index, uint16 &extent_index) const override;
151  bool match(const r_code::Code *object, uint16 index) override;
152  Atom *get_code() override;
153  r_code::Code *get_object() override;
154  uint16 get_code_size() override;
155 };
156 
157 class r_exec_dll AtomValue :
158  public BoundValue {
159 private:
160  Atom atom_;
161 public:
162  AtomValue(BindingMap *map, Atom atom);
163 
164  Value *copy(BindingMap *map) const override;
165  void valuate(r_code::Code *destination, uint16 write_index, uint16 &extent_index) const override;
166  bool match(const r_code::Code *object, uint16 index) override;
167  Atom *get_code() override;
168  r_code::Code *get_object() override;
169  uint16 get_code_size() override;
170 
171  bool intersect(const Value *v) const override;
172  bool _intersect(const AtomValue *v) const override;
173 
174  bool contains(const Atom a) const override;
175 };
176 
177 class r_exec_dll StructureValue :
178  public BoundValue {
179 private:
180  P<r_code::Code> structure_;
181  StructureValue(BindingMap* map, const r_code::Code* structure)
182  : StructureValue(map, &structure->code(0), 0) {}
183 public:
184  StructureValue(BindingMap *map, const r_code::Code *source, uint16 structure_index)
185  : StructureValue(map, &source->code(0), structure_index) {}
186  StructureValue(BindingMap *map, const Atom *source, uint16 structure_index);
187  StructureValue(BindingMap *map, Timestamp time);
188  StructureValue(BindingMap *map, std::chrono::microseconds duration);
189 
190  Value *copy(BindingMap *map) const override;
191  void valuate(r_code::Code* destination, uint16 write_index, uint16& extent_index) const override {
192  destination->code(write_index) = Atom::IPointer(extent_index);
193  copy_structure(destination, extent_index, &structure_->code(0), 0);
194  }
195 
204  static void copy_structure(
205  r_code::Code* destination, uint16& extent_index, const Atom* source, uint16 source_index);
206 
207  bool match(const r_code::Code *object, uint16 index) override;
208  Atom *get_code() override;
209  r_code::Code *get_object() override;
210  uint16 get_code_size() override;
211 
212  bool intersect(const Value *v) const override;
213  bool _intersect(const StructureValue *v) const override;
214 
215  bool contains(const Atom *s) const override;
216 };
217 
218 class r_exec_dll ObjectValue :
219  public BoundValue {
220 private:
221  const P<r_code::Code> object_;
222 public:
223  ObjectValue(BindingMap *map, r_code::Code *object);
224 
225  Value *copy(BindingMap *map) const override;
226  void valuate(r_code::Code *destination, uint16 write_index, uint16 &extent_index) const override;
227  bool match(const r_code::Code *object, uint16 index) override;
228  Atom *get_code() override;
229  r_code::Code *get_object() override;
230  uint16 get_code_size() override;
231 
232  bool intersect(const Value *v) const override;
233  bool _intersect(const ObjectValue *v) const override;
234 
235  bool contains(const r_code::Code *o) const override;
236 };
237 
238 typedef enum {
239  MATCH_SUCCESS_POSITIVE = 0,
240  MATCH_SUCCESS_NEGATIVE = 1,
241  MATCH_FAILURE = 2
242 }MatchResult;
243 
244 class _Fact;
245 class Fact;
246 
247 class r_exec_dll BindingMap :
248  public _Object {
249  friend class UnboundValue;
250 protected:
251  std::vector<P<Value> > map_; // indexed by vl-ptrs.
252 
253  uint32 unbound_values_;
254 
255  void add_unbound_value(uint8 id);
256 
257  uint16 first_index_; // index of the first value found in the first fact.
258  int16 fwd_after_index_; // tpl args (if any) are located before fwd_after_index.
259  int16 fwd_before_index_;
260 
261  bool match_fwd_timings(const _Fact *f_object);
262  bool match(const r_code::Code *object, uint16 o_base_index, uint16 o_index, const r_code::Code *pattern, uint16 p_index, uint16 o_arity);
263 
264  void abstract_member(const r_code::Code *object, uint16 index, r_code::Code *abstracted_object, uint16 write_index, uint16 &extent_index, int first_search_index = 0);
265  Atom get_atom_variable(Atom a);
266 
277  Atom get_structure_variable(const r_code::Code *object, uint16 index, int first_search_index = 0);
278  Atom get_object_variable(r_code::Code *object);
279 public:
280  BindingMap();
281  BindingMap(const BindingMap *source);
282  BindingMap(const BindingMap &source);
283  virtual ~BindingMap();
284 
285  BindingMap& operator =(const BindingMap &source);
286  void load(const BindingMap *source);
287 
288  virtual void clear();
289 
290  void init(const r_code::Code *object, uint16 index);
291 
292  static _Fact *abstract_f_ihlp(const _Fact *fact); // for icst and imdl.
293 
301  _Fact *abstract_fact(_Fact *fact, const _Fact *original, bool force_sync, int timing_vars_first_search_index = 0);
302  r_code::Code *abstract_object(r_code::Code *object, bool force_sync, int timing_vars_first_search_index = 0);
303 
304  void reset_fwd_timings(_Fact *reference_fact); // reset after and before from the timings of the reference object.
305 
316  bool match_fwd_timings(Timestamp after, Timestamp before) {
317  return match_timings(after, before, fwd_after_index_, fwd_before_index_);
318  }
319 
320  MatchResult match_fwd_lenient(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we are lenient about fact vs |fact.
321  bool match_fwd_strict(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we need sharp match.
322 
327  bool is_timestamp(uint16 i) const {
328  return i >= 0 && i < map_.size() && map_[i]->get_code() != NULL &&
329  map_[i]->get_code()[0].getDescriptor() == Atom::TIMESTAMP;
330  }
331  bool has_fwd_after() const { return is_timestamp(fwd_after_index_); }
332  bool has_fwd_before() const { return is_timestamp(fwd_before_index_); }
333  Timestamp get_fwd_after() const; // assumes the timings are valuated.
334  Timestamp get_fwd_before() const; // idem.
335 
336  bool match_object(const r_code::Code *object, const r_code::Code *pattern);
337  bool match_structure(const r_code::Code *object, uint16 o_base_index, uint16 o_index, const r_code::Code *pattern, uint16 p_index);
338  bool match_atom(Atom o_atom, Atom p_atom);
339 
340  void bind_variable(BoundValue *value, uint8 id);
341  void bind_variable(Atom *code, uint8 id, uint16 value_index, Atom *intermediate_results);
342 
343  Atom *get_value_code(uint16 id) const;
344  uint16 get_value_code_size(uint16 id) const;
345  uint16 get_first_index() const { return first_index_; }
346 
352  bool intersect(const BindingMap *bm) const;
353 
354  bool is_fully_specified() const;
355 
356  Atom *get_code(uint16 i) const { return map_[i]->get_code(); }
357  r_code::Code *get_object(uint16 i) const { return map_[i]->get_object(); }
358  int16 get_fwd_after_index() const { return fwd_after_index_; }
359  int16 get_fwd_before_index() const { return fwd_before_index_; }
360  bool scan_variable(uint16 id) const; // return true if id<first_index or map[id] is not an UnboundValue.
361 
374  bool match_timings(Timestamp after, Timestamp before, uint32 after_index, uint32 before_index);
375 
383  static uint16 get_abstracted_ihlp_exposed_args_index(const r_code::Code* ihlp);
384 
389  std::string trace_string(uint16 i) const { return map_[i]->trace_string(); }
390 };
391 
392 class r_exec_dll HLPBindingMap :
393  public BindingMap {
394 private:
395  int16 bwd_after_index_;
396  int16 bwd_before_index_;
397 
398  bool match_bwd_timings(const _Fact *f_object, const _Fact *f_pattern);
399 
400  bool need_binding(r_code::Code *pattern) const;
401 
406  void init_timing_indexes(const r_code::Code* source, int16& after_index, int16& before_index) {
407  if (source->code_size() <= FACT_BEFORE)
408  // We don't expect this.
409  return;
410  if (source->code(FACT_AFTER).getDescriptor() == Atom::VL_PTR)
411  after_index = source->code(FACT_AFTER).asIndex();
412  if (source->code(FACT_BEFORE).getDescriptor() == Atom::VL_PTR)
413  before_index = source->code(FACT_BEFORE).asIndex();
414  }
415 
421  void add_unbound_values(const r_code::Code* hlp, uint16 structure_index);
422 
431  void build_ihlp_structure(
432  const r_code::Code* hlp, uint16 hlp_structure_index, r_code::Code* ihlp, uint16& extent_index) const;
433 
442  void init_from_ihlp_args(
443  const r_code::Code* hlp, uint16 hlp_args_index, const r_code::Code* ihlp, uint16 ihlp_args_index);
444 public:
445  HLPBindingMap();
446  HLPBindingMap(const HLPBindingMap *source);
447  HLPBindingMap(const HLPBindingMap &source);
448  ~HLPBindingMap();
449 
450  HLPBindingMap& operator =(const HLPBindingMap &source);
451  void load(const HLPBindingMap *source);
452  void clear() override;
453 
454  void init_from_hlp(const r_code::Code *hlp, const r_code::Code* packed_hlp);
455  void init_from_f_ihlp(const _Fact *f_ihlp);
456  Fact *build_f_ihlp(r_code::Code *hlp, uint16 opcode, bool wr_enabled) const; // return f->ihlp.
457  r_code::Code *bind_pattern(r_code::Code *pattern) const;
458 
466  int16 get_ihlp_exposed_args_position(int16 index) const;
467 
468  void reset_bwd_timings(_Fact *reference_fact); // idem for the last 2 unbound variables (i.e. timings of the second pattern in a mdl).
469 
470  MatchResult match_bwd_lenient(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we are lenient about fact vs |fact.
471  bool match_bwd_strict(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we need sharp match.
472 
473  bool has_bwd_after() const { return is_timestamp(bwd_after_index_); }
474  bool has_bwd_before() const { return is_timestamp(bwd_before_index_); }
475  Timestamp get_bwd_after() const; // assumes the timings are valuated.
476  Timestamp get_bwd_before() const; // idem.
477 };
478 }
479 
480 
481 #endif
r_exec::UnboundValue
Definition: binding_map.h:142
r_exec::BindingMap::trace_string
std::string trace_string(uint16 i) const
Definition: binding_map.h:389
r_exec::BindingMap
Definition: binding_map.h:248
r_exec::BoundValue
Definition: binding_map.h:135
r_code::Atom
Definition: atom.h:104
r_exec::BindingMap::is_timestamp
bool is_timestamp(uint16 i) const
Definition: binding_map.h:327
r_exec::_Fact
Definition: factory.h:155
r_exec::LObject
Definition: r_exec/object.h:195
core::P
Definition: base.h:103
r_code::Code
Definition: r_code/object.h:224
core::_Object
Definition: base.h:131
r_exec::ObjectValue
Definition: binding_map.h:219
r_exec::HLPBindingMap
Definition: binding_map.h:393
r_exec::StructureValue
Definition: binding_map.h:178
r_exec::BindingMap::match_fwd_timings
bool match_fwd_timings(Timestamp after, Timestamp before)
Definition: binding_map.h:316
r_exec::Value::trace_string
std::string trace_string() const
Definition: binding_map.h:126
r_exec::Fact
Definition: factory.h:360
r_exec::Value
Definition: binding_map.h:101
r_exec::AtomValue
Definition: binding_map.h:158