AERA
overlay.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 overlay_h
86 #define overlay_h
87 
88 #include "../submodules/CoreLibrary/CoreLibrary/base.h"
89 #include "../submodules/CoreLibrary/CoreLibrary/utils.h"
90 #include "../r_code/object.h"
91 #include "controller.h"
92 #include "reduction_job.h"
93 #include "dll.h"
94 
95 namespace r_exec {
96 
97 class _Context;
98 class IPGMContext;
99 class HLPContext;
100 class Controller;
101 class View;
102 
103 class r_exec_dll Overlay :
104  public _Object {
105  friend class _Context;
106  friend class IPGMContext;
107  friend class HLPContext;
108 protected:
109  volatile uint32 invalidated_;
110 
111  Controller *controller_;
112 
113  r_code::resized_vector<r_code::Atom> values_; // value array: stores the results of computations.
114  // Copy of the pgm/hlp code. Will be patched during matching and evaluation:
115  // any area indexed by a vl_ptr will be overwritten with:
116  // the evaluation result if it fits in a single atom,
117  // a ptr to the value array if the result is larger than a single atom,
118  // a ptr to an input if the result is a pattern input.
119  r_code::Atom *code_;
120  uint16 code_size_;
121  std::vector<uint16> patch_indices_; // indices where patches are applied; used for rollbacks.
122  uint16 value_commit_index_; // index of the last computed value_+1; used for rollbacks.
123 
124  void load_code();
125  void patch_code(uint16 index, r_code::Atom value);
126  uint16 get_last_patch_index();
127  void unpatch_code(uint16 patch_index);
128 
129  void rollback(); // reset the overlay to the last commited state: unpatch code and values.
130  void commit(); // empty the patch_indices_ and set value_commit_index_ to values.size().
131 
132  r_code::Code *get_core_object() const; // pgm, mdl, cst.
133 
134  Overlay();
135  Overlay(Controller *c, bool load_code = true);
136 public:
137  Overlay(size_t values_size);
138  virtual ~Overlay();
139 
140  virtual void reset(); // reset to original state.
141  virtual Overlay *reduce(r_exec::View *input); // returns an offspring in case of a match.
142 
143  void invalidate() { invalidated_ = 1; }
144  virtual bool is_invalidated() { return invalidated_ == 1; }
145 
146  r_code::Code* get_object() const;
147  r_exec::View* get_view() const;
148 
149  r_code::Code *build_object(r_code::Atom head) const;
150  const r_code::Atom* values() const { return &values_[0]; }
151 };
152 
160 class r_exec_dll DefeasibleValidity :
161  public _Object {
162 public:
166  DefeasibleValidity() : invalidated_(0) {}
167 
172  bool is_invalidated() { return invalidated_ != 0; }
173 
177  void invalidate() { invalidated_ = 1; }
178 
179 private:
180  volatile uint32 invalidated_; // 32 bit alignment.
181 };
182 
183 template<class T> class CriticalSectionList {
184 public:
185  CriticalSection CS_;
186  r_code::list<T> list_;
187 };
188 
189 }
190 
191 
192 #endif
r_code::resized_vector< r_code::Atom >
r_exec::IPGMContext
Definition: context.h:102
r_exec::Overlay
Definition: overlay.h:104
r_code::Atom
Definition: atom.h:104
r_exec::DefeasibleValidity::DefeasibleValidity
DefeasibleValidity()
Definition: overlay.h:166
r_exec::DefeasibleValidity
Definition: overlay.h:161
r_code::Code
Definition: r_code/object.h:224
core::CriticalSection
Definition: submodules/CoreLibrary/CoreLibrary/utils.h:233
core::_Object
Definition: base.h:131
r_exec::DefeasibleValidity::invalidate
void invalidate()
Definition: overlay.h:177
r_exec::View
Definition: view.h:102
r_exec::DefeasibleValidity::is_invalidated
bool is_invalidated()
Definition: overlay.h:172
r_exec::Controller
Definition: controller.h:104
r_code::list
Definition: list.h:99
r_exec::CriticalSectionList
Definition: overlay.h:183
r_exec::_Context
Definition: _context.h:96
r_exec::HLPContext
Definition: hlp_context.h:97