AERA
overlay.cpp
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 #include "overlay.h"
86 #include "controller.h"
87 #include "mem.h"
88 
89 using namespace std::chrono;
90 using namespace r_code;
91 
92 #define MAX_VALUE_SIZE 128
93 
94 namespace r_exec {
95 
96 Overlay::Overlay()
97  // MAX_VALUE_SIZE is the limit; if the array is resized later on, some contexts with data==VALUE_ARRAY
98  // may point to invalid adresses: case of embedded contexts with both data==VALUE_ARRAY.
99  : Overlay(MAX_VALUE_SIZE)
100 {}
101 
102 Overlay::Overlay(size_t values_size) : _Object(), invalidated_(0) {
103 
104  code_ = new r_code::Atom[1];
105  values_.resize(values_size);
106 }
107 
108 Overlay::Overlay(Controller *c, bool load_code) : _Object(), controller_(c), value_commit_index_(0), code_(NULL), invalidated_(0) {
109 
110  values_.resize(128);
111  if (load_code)
112  this->load_code();
113 }
114 
115 Overlay::~Overlay() {
116 
117  if (code_)
118  delete[] code_;
119 }
120 
121 Code* Overlay::get_object() const { return ((Controller*)controller_)->get_object(); }
122 r_exec::View* Overlay::get_view() const { return ((Controller*)controller_)->get_view(); }
123 
124 inline Code *Overlay::get_core_object() const {
125 
126  return controller_->get_core_object();
127 }
128 
129 void Overlay::load_code() {
130 
131  if (code_)
132  delete[] code_;
133 
134  Code *object = get_core_object();
135  // copy the original pgm/hlp code.
136  code_size_ = object->code_size();
137  code_ = new r_code::Atom[code_size_];
138  memcpy(code_, &object->code(0), code_size_ * sizeof(r_code::Atom));
139 }
140 
141 void Overlay::reset() {
142 
143  memcpy(code_, &get_object()->get_reference(0)->code(0), code_size_ * sizeof(r_code::Atom)); // restore code to prisitne copy.
144 }
145 
146 void Overlay::rollback() {
147 
148  Code *object = get_core_object();
149  Atom *original_code = &object->code(0);
150  for (uint16 i = 0; i < patch_indices_.size(); ++i) // upatch code.
151  code_[patch_indices_[i]] = original_code[patch_indices_[i]];
152  patch_indices_.clear();
153 
154  if (value_commit_index_ != values_.size()) { // shrink the values down to the last commit index.
155 
156  if (value_commit_index_ > 0)
157  values_.resize(value_commit_index_);
158  else
159  values_.clear();
160  value_commit_index_ = values_.size();
161  }
162 }
163 
164 void Overlay::commit() {
165 
166  patch_indices_.clear();
167  value_commit_index_ = values_.size();
168 }
169 
170 void Overlay::patch_code(uint16 index, Atom value) {
171 
172  code_[index] = value;
173  patch_indices_.push_back(index);
174 }
175 
176 uint16 Overlay::get_last_patch_index() {
177 
178  return patch_indices_.size();
179 }
180 
181 void Overlay::unpatch_code(uint16 patch_index) {
182 
183  Code *object = get_core_object();
184  Atom *original_code = &object->code(0);
185  for (uint16 i = patch_index; i < patch_indices_.size(); ++i)
186  code_[patch_indices_[i]] = original_code[patch_indices_[i]];
187  patch_indices_.resize(patch_index);
188 }
189 
190 Overlay *Overlay::reduce(r_exec::View *input) {
191 
192  return NULL;
193 }
194 
195 r_code::Code *Overlay::build_object(Atom head) const {
196 
197  return _Mem::Get()->build_object(head);
198 }
199 
201 
202 Controller::Controller(_View *view) : _Object(), invalidated_(0), activated_(0), view_(view) {
203 
204  if (!view)
205  return;
206 
207  switch (get_object()->code(0).getDescriptor()) {
208  case Atom::INSTANTIATED_PROGRAM:
209  case Atom::INSTANTIATED_INPUT_LESS_PROGRAM:
210  case Atom::INSTANTIATED_ANTI_PROGRAM:
211  time_scope_ = Utils::GetDuration<Code>(get_object(), IPGM_TSC);
212  break;
213  case Atom::INSTANTIATED_CPP_PROGRAM:
214  time_scope_ = Utils::GetDuration<Code>(get_object(), ICPP_PGM_TSC);
215  break;
216  }
217 }
218 
219 Controller::~Controller() {
220 }
221 
222 void Controller::set_view(View *view) {
223 
224  view_ = view;
225 }
226 
227 void Controller::_take_input(r_exec::View *input) { // called by groups at update and injection time.
228 
229  if (is_alive() && !input->object_->is_invalidated())
230  take_input(input);
231 }
232 
233 // Put this in the cpp file to avoid an include loop between controller.h and mem.h.
234 void Controller::push_reduction_job(_ReductionJob* j) {
235  _Mem::Get()->push_reduction_job(j);
236 }
237 
239 
240 OController::OController(_View *view) : Controller(view) {
241 }
242 
243 OController::~OController() {
244 }
245 }
r_code::_View
Definition: r_code/object.h:164
r_code::Atom
Definition: atom.h:104
r_code::Code
Definition: r_code/object.h:224
core::_Object
Definition: base.h:131
r_exec::View
Definition: view.h:102