AERA
object.cpp
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2023 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2023 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2023 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 "object.h"
86 #include "replicode_defs.h"
87 
88 #include <iostream>
89 
90 using namespace std;
91 
92 namespace r_code {
93 
94 #ifdef WITH_DETAIL_OID
95 // Start with a big number so it doesn't look like SysObject::detail_oid_.
96 static uint64 last_SysView_detail_oid = 1000;
97 #endif
98 SysView::SysView() {
99 #ifdef WITH_DETAIL_OID
100  detail_oid_ = ++last_SysView_detail_oid;
101  if (detail_oid_ == 0)
102  int set_breakpoint_here = 1;
103 #endif
104 }
105 
106 SysView::SysView(_View *source) {
107 
108 #ifdef WITH_DETAIL_OID
109  detail_oid_ = ++last_SysView_detail_oid;
110  if (detail_oid_ == 0)
111  int set_breakpoint_here = 1;
112 #endif
113  for (uint32 i = 0; i < VIEW_CODE_MAX_SIZE; ++i)
114  code_[i] = source->code(i);
115 
116  for (uint32 i = 0; i < 2; ++i) // to get the right size in Image::add_object().
117  if (source->references_[i])
118  references_.push_back(0);
119 }
120 
121 void SysView::write(word32 *data) {
122 
123  data[0] = code_.size();
124  data[1] = references_.size();
125  uint32 i = 0;
126  for (; i < code_.size(); ++i)
127  data[2 + i] = code_[i].atom_;
128  for (uint32 j = 0; j < references_.size(); ++j)
129  data[2 + i + j] = references_[j];
130 }
131 
132 void SysView::read(word32 *data) {
133 
134  uint32 code_size = data[0];
135  uint32 reference_set_size = data[1];
136  uint32 i;
137  uint32 j;
138  for (i = 0; i < code_size; ++i)
139  code_.push_back(Atom(data[2 + i]));
140  for (j = 0; j < reference_set_size; ++j)
141  references_.push_back(data[2 + i + j]);
142 }
143 
144 uint32 SysView::get_size() const {
145 
146  return 2 + code_.size() + references_.size();
147 }
148 
149 void SysView::trace(std::ostream& out) const {
150 
151  out << " code size: " << code_.size() << std::endl;
152  out << " reference set size: " << references_.size() << std::endl;
153  out << "---code---" << std::endl;
154  Atom::TraceContext context;
155  for (uint32 i = 0; i < code_.size(); ++i) {
156 
157  code_[i].trace(context, out);
158  out << std::endl;
159  }
160  out << "---reference set---" << std::endl;
161  for (uint32 i = 0; i < references_.size(); ++i)
162  out << references_[i] << std::endl;
163 }
164 
166 
167 uint32 SysObject::lastOID_ = 0;
168 
169 SysObject::SysObject() : oid_(lastOID_++) {
170 #ifdef WITH_DETAIL_OID
171  detail_oid_ = 0;
172 #endif
173 }
174 
175 SysObject::SysObject(Code *source) {
176 
177  uint32 i;
178  for (i = 0; i < source->code_size(); ++i)
179  code_[i] = source->code(i);
180 
181  unordered_set<_View *, _View::Hash, _View::Equal>::const_iterator v;
182  source->acq_views();
183  for (i = 0, v = source->views_.begin(); v != source->views_.end(); ++i, ++v)
184  views_[i] = new SysView(*v);
185  source->rel_views();
186 
187  oid_ = source->get_oid();
188 #ifdef WITH_DETAIL_OID
189  detail_oid_ = source->get_detail_oid();
190 #endif
191 
192  for (i = 0; i < source->references_size(); ++i) // to get the right size in Image::add_object().
193  references_.push_back(0);
194 }
195 
196 SysObject::~SysObject() {
197 
198  for (uint32 i = 0; i < views_.size(); ++i)
199  delete views_[i];
200 }
201 
202 void SysObject::write(word32 *data) {
203 
204  data[0] = oid_;
205  data[1] = code_.size();
206  data[2] = references_.size();
207  data[3] = markers_.size();
208  data[4] = views_.size();
209  uint32 i;
210  uint32 j;
211  uint32 k;
212  uint32 l;
213  for (i = 0; i < code_.size(); ++i)
214  data[5 + i] = code_[i].atom_;
215  for (j = 0; j < references_.size(); ++j)
216  data[5 + i + j] = references_[j];
217  for (k = 0; k < markers_.size(); ++k)
218  data[5 + i + j + k] = markers_[k];
219  uint32 offset = 0;
220  for (l = 0; l < views_.size(); ++l) {
221 
222  views_[l]->write(data + 5 + i + j + k + offset);
223  offset += views_[l]->get_size();
224  }
225 }
226 
227 void SysObject::read(word32 *data) {
228 
229  oid_ = data[0];
230  uint32 code_size = data[1];
231  uint32 reference_set_size = data[2];
232  uint32 marker_set_size = data[3];
233  uint32 view_set_size = data[4];
234  uint32 i;
235  uint32 j;
236  uint32 k;
237  uint32 l;
238  for (i = 0; i < code_size; ++i)
239  code_.push_back(Atom(data[5 + i]));
240  for (j = 0; j < reference_set_size; ++j)
241  references_.push_back(data[5 + i + j]);
242  for (k = 0; k < marker_set_size; ++k)
243  markers_.push_back(data[5 + i + j + k]);
244  uint32 offset = 0;
245  for (l = 0; l < view_set_size; ++l) {
246 
247  SysView *v = new SysView();
248  v->read(data + 5 + i + j + k + offset);
249  views_.push_back(v);
250  offset += v->get_size();
251  }
252 }
253 
254 uint32 SysObject::get_size() {
255 
256  uint32 view_set_size = 0;
257  for (uint32 i = 0; i < views_.size(); ++i)
258  view_set_size += views_[i]->get_size();
259  return 5 + code_.size() + references_.size() + markers_.size() + view_set_size;
260 }
261 
262 void SysObject::trace(std::ostream& out) const {
263 
264  out << "\n---object---\n";
265  out << oid_ << std::endl;
266  out << "code size: " << code_.size() << std::endl;
267  out << "reference set size: " << references_.size() << std::endl;
268  out << "marker set size: " << markers_.size() << std::endl;
269  out << "view set size: " << views_.size() << std::endl;
270  out << "\n---code---\n";
271  uint32 i;
272  Atom::TraceContext context;
273  for (i = 0; i < code_.size(); ++i) {
274 
275  out << i << " ";
276  code_[i].trace(context, out);
277  out << std::endl;
278  }
279  out << "\n---reference set---\n";
280  for (i = 0; i < references_.size(); ++i)
281  out << i << " " << references_[i] << std::endl;
282  out << "\n---marker set---\n";
283  for (i = 0; i < markers_.size(); ++i)
284  out << i << " " << markers_[i] << std::endl;
285  out << "\n---view set---\n";
286  for (uint32 k = 0; k < views_.size(); ++k) {
287 
288  out << "view[" << k << "]" << std::endl;
289  out << "reference set size: " << views_[k]->references_.size() << std::endl;
290  out << "-code-" << std::endl;
291  uint32 j;
292  for (j = 0; j < views_[k]->code_.size(); ++i, ++j) {
293 
294  out << j << " ";
295  views_[k]->code_[j].trace(context, out);
296  out << std::endl;
297  }
298  out << "-reference set-" << std::endl;
299  for (j = 0; j < views_[k]->references_.size(); ++i, ++j)
300  out << j << " " << views_[k]->references_[j] << std::endl;
301  }
302 }
303 
304 void SysObject::trace() const { trace(std::cout); }
305 
306 void Code::r_trace(ostream& out) const {
307  trace(out);
308 
309  if (references_size() < 1)
310  // Done recursing.
311  return;
312 
313  auto obj = get_reference(0);
314  if (obj->code_size() <= 2)
315  // Assume this is an ent or ont.
316  return;
317 
318  switch (obj->code(0).getDescriptor()) {
319  case Atom::MODEL:
320  case Atom::COMPOSITE_STATE:
321  // Don't trace these big structures.
322  return;
323  }
324 
325  out << endl;
326  obj->r_trace(out);
327 }
328 
330 
331 Mem *Mem::singleton_ = NULL;
332 
333 Mem::Mem() {
334 
335  singleton_ = this;
336 }
337 
338 Mem *Mem::Get() {
339 
340  return singleton_;
341 }
342 }
r_code::Mem
Definition: r_code/object.h:420