AERA
r_code/object.h
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 #ifndef r_code_object_h
86 #define r_code_object_h
87 
88 #include <ostream>
89 #include <sstream>
90 #include <unordered_set>
91 #include "atom.h"
92 #include "resized_vector.h"
93 #include "list.h"
94 #include "replicode_defs.h"
95 
96 #include "../submodules/CoreLibrary/CoreLibrary/base.h"
97 #include "utils.h"
98 
99 
100 using namespace core;
101 
102 namespace r_code {
103 
104 // I/O from/to r_code::Image ////////////////////////////////////////////////////////////////////////
105 
106 class dll_export ImageObject {
107 public:
109  r_code::resized_vector<uint16> references_;
110 
111  virtual void write(word32 *data) = 0;
112  virtual void read(word32 *data) = 0;
113  virtual void trace(std::ostream& out) const = 0;
114 };
115 
116 class _View;
117 
118 class dll_export SysView :
119  public ImageObject {
120 public:
121  SysView();
122  SysView(_View *source);
123 
124  void write(word32 *data) override;
125  void read(word32 *data) override;
126  uint32 get_size() const;
127  void trace(std::ostream& out) const override;
128 #ifdef WITH_DETAIL_OID
129  int detail_oid_;
130 #endif
131 };
132 
133 class Code;
134 
135 class dll_export SysObject :
136  public ImageObject {
137 private:
138  static uint32 lastOID_;
139 public:
140  r_code::resized_vector<uint32> markers_; // indexes in the relocation segment
142 
143  uint32 oid_;
144 #ifdef WITH_DETAIL_OID
145  uint64 detail_oid_;
146 #endif
147 
148  SysObject();
149  SysObject(Code *source);
150  ~SysObject();
151 
152  void write(word32 *data) override;
153  void read(word32 *data) override;
154  uint32 get_size();
155  void trace(std::ostream& out)const override;
156  void trace() const;
157 };
158 
159 // Interfaces for r_exec classes ////////////////////////////////////////////////////////////////////////
160 
161 class Object;
162 
163 class dll_export _View :
164  public _Object {
165 protected:
166  Atom code_[VIEW_CODE_MAX_SIZE]; // dimensioned to hold the largest view (group view): head atom, iptr to ijt, sln, res, rptr to grp, rptr to org, vis, cov, 3 atoms for ijt's timestamp; oid is the last word32 (not an atom).
167 public:
168  Code *references_[2]; // does not include the viewed object; no smart pointer here (a view is held by a group and holds a ref to said group in references[0]).
169  P<Code> object_; // viewed object.
170 
171  _View() : object_(NULL) {
172 
173  references_[0] = references_[1] = NULL;
174  }
175 
176  _View(SysView *source, Code *object) {
177 
178  for (uint16 i = 0; i < source->code_.size(); ++i)
179  code_[i] = source->code_[i];
180  references_[0] = references_[1] = NULL;
181  object_ = object;
182  }
183 
184  virtual ~_View() {}
185 
186  Atom &code(uint16 i) { return code_[i]; }
187  Atom code(uint16 i) const { return code_[i]; }
188 
189  typedef enum {
190  SYNC_ONCE = 0,
191  SYNC_PERIODIC = 1,
192  SYNC_HOLD = 2,
193  SYNC_AXIOM = 3,
194  SYNC_ONCE_AXIOM = 4
195  }SyncMode;
196 
197  SyncMode get_sync() const { return (SyncMode)(uint32)code_[VIEW_SYNC].asFloat(); }
198  Timestamp get_ijt() const { return Utils::GetTimestamp(code_ + code_[VIEW_IJT].asIndex()); }
199  void set_ijt(Timestamp ijt) { Utils::SetTimestamp(code_ + code_[VIEW_IJT].asIndex(), ijt); }
200 
201  class Hash {
202  public:
203  size_t operator ()(_View *v) const {
204  return (size_t)(Code *)v->references_[0]; // i.e. the group the view belongs to.
205  }
206  };
207 
208  class Equal {
209  public:
210  bool operator ()(const _View *lhs, const _View *rhs) const {
211  return lhs->references_[0] == rhs->references_[0];
212  }
213  };
214 
215  class Less {
216  public:
217  bool operator ()(const _View *lhs, const _View *rhs) const {
218  return lhs->get_ijt() < rhs->get_ijt();
219  }
220  };
221 };
222 
223 class dll_export Code :
224  public _Object {
225 public:
226  static const int32 null_storage_index = -1;
227  static const uint32 CodeMarkersInitialSize = 8;
228 protected:
229  int32 storage_index_; // -1: not stored; >= 0: index of the object in a vector-based container.
230 
231  void load(SysObject *source) {
232 
233  for (uint16 i = 0; i < source->code_.size(); ++i)
234  code(i) = source->code_[i];
235  set_oid(source->oid_);
236  }
237  template<class V> _View *build_view(SysView *source) {
238 
239  return new V(source, this);
240  }
241 public:
242  void set_strorage_index(int32 i) { storage_index_ = i; }
243  bool is_registered() const { return storage_index_ > null_storage_index; }
244  int32 get_storage_index() const { return storage_index_; }
245 
246  virtual uint32 get_oid() const = 0;
247  virtual void set_oid(uint32 oid) = 0;
248 
249  virtual Atom &code(uint16 i) = 0;
250  virtual const Atom &code(uint16 i) const = 0;
251  virtual uint16 code_size() const = 0;
252  virtual void resize_code(uint16 new_size) = 0;
253  bool includes(Atom a) const {
254  for (uint16 i = 0; i < code_size(); ++i) {
255  if (code(i) == a)
256  return true;
257  }
258  return false;
259  }
260  virtual void set_reference(uint16 i, Code *object) = 0;
261  virtual Code *get_reference(uint16 i) const = 0;
262  virtual uint16 references_size() const = 0;
263  virtual void clear_references() = 0;
264  virtual void set_references(std::vector<P<Code> > &new_references) = 0;
265 
266  virtual bool is_compact() const { return false; }
267  virtual bool is_invalidated() { return false; }
268  virtual bool invalidate() { return false; }
269 
270  r_code::list<Code *> markers_;
271  std::unordered_set<_View *, _View::Hash, _View::Equal> views_; // indexed by groups.
272 
273  virtual _View *build_view(SysView *source) = 0;
274 
275  virtual void acq_views() {}
276  virtual void rel_views() {}
277  virtual void acq_markers() {}
278  virtual void rel_markers() {}
279 
280  virtual float32 get_psln_thr() { return 1; }
281 
282  Code() : storage_index_(null_storage_index) { markers_.reserve(CodeMarkersInitialSize); }
283  virtual ~Code() {}
284 
285  virtual void mod(uint16 /* member_index */, float32 /* value */) {};
286  virtual void set(uint16 /* member_index */, float32 /* value */) {};
287  virtual _View *get_view(Code* /* group */, bool /* lock */) { return NULL; }
288  virtual void add_reference(Code* /* object */) {} // called only on local objects.
289  void remove_marker(Code *m) {
290 
291  acq_markers();
292  markers_.remove(m);
293  rel_markers();
294  }
295 
299  void trace(uint16 i, std::ostream& out, Atom::TraceContext& context) const {
300  Atom atom = code(i);
301  atom.trace(context, out);
302  if (atom.getDescriptor() == Atom::R_PTR) {
303  if (atom.asIndex() < references_size()) {
304  out << " -> " << get_reference(atom.asIndex())->get_oid();
305 #ifdef WITH_DETAIL_OID
306  out << "(" << get_reference(atom.asIndex())->get_detail_oid() << ")";
307 #endif
308  }
309  else
310  out << " (unassigned) ";
311  }
312  }
313 
317  void trace(std::ostream& out) const {
318 
319  out << "--------\n";
320  Atom::TraceContext context;
321  for (uint16 i = 0; i < code_size(); ++i) {
322 
323  out << i << "\t";
324  trace(i, out, context);
325  out << std::endl;
326  }
327  out << "OID: " << get_oid();
328 #ifdef WITH_DETAIL_OID
329  out << "(" << get_detail_oid() << ")";
330 #endif
331  out << std::endl;
332  }
333 
334  void trace() const { trace(std::cout); }
335 
339  std::string trace_string() const {
340  std::ostringstream out;
341  trace(out);
342  return out.str();
343  }
344 
348  std::string trace_string(uint16 i) const {
349  Atom::TraceContext context;
350  std::ostringstream out;
351  trace(i, out, context);
352  return out.str();
353  }
354 
361  void r_trace(std::ostream& out) const;
362 
363  void r_trace() const { r_trace(std::cout); }
364 
368  std::string r_trace_string() const {
369  std::ostringstream out;
370  r_trace(out);
371  return out.str();
372  }
373 };
374 
375 // Implementation for local objects (non distributed).
376 class dll_export LocalObject :
377  public Code {
378 private:
379  uint32 oid_;
381  r_code::resized_vector<P<Code> > references_;
382 public:
383  LocalObject() : Code() {}
384  LocalObject(SysObject *source) : Code() {
385 
386  load(source);
387  }
388  virtual ~LocalObject() {}
389 
390  _View *build_view(SysView *source) override {
391 
392  return Code::build_view<_View>(source);
393  }
394 
395  uint32 get_oid() const override { return oid_; }
396  void set_oid(uint32 oid) override { oid_ = oid; }
397 
398  Atom &code(uint16 i) override { return code_[i]; }
399  const Atom &code(uint16 i) const override { return code_[i]; }
400  uint16 code_size() const override {
401  // There can't be more than 65536 code bytes. Explicitly cast to the return type.
402  return (uint16)code_.size();
403  }
404  void resize_code(uint16 new_size) override { code_.resize(new_size); }
405  void set_reference(uint16 i, Code *object) override { references_[i] = object; }
406  Code *get_reference(uint16 i) const override { return references_[i]; }
407  uint16 references_size() const override {
408  // There can't be more than 65536 references. Explicitly cast to the return type.
409  return (uint16)references_.size();
410  }
411  void clear_references() override { references_.clear(); }
412  void set_references(std::vector<P<Code> > &new_references) override {
413  references_.clear();
414  for (size_t i = 0; i < new_references.size(); ++i)
415  references_.push_back(new_references[i]);
416  }
417  void add_reference(Code *object) override { references_.push_back(object); }
418 };
419 
420 class dll_export Mem {
421 protected:
422  static Mem *singleton_;
423  Mem();
424 public:
425  static Mem *Get();
426 
427  virtual Code *build_object(SysObject *source) const = 0;
428  virtual void delete_object(Code *object) = 0;
429 };
430 }
431 
432 
433 #endif
r_code::resized_vector
Definition: resized_vector.h:137
r_code::Code::trace_string
std::string trace_string() const
Definition: r_code/object.h:339
r_code::_View::Hash
Definition: r_code/object.h:201
r_code::Atom::TraceContext
Definition: atom.h:112
r_code::_View
Definition: r_code/object.h:164
r_code::Atom
Definition: atom.h:104
r_code::Code::trace_string
std::string trace_string(uint16 i) const
Definition: r_code/object.h:348
r_code::SysView
Definition: r_code/object.h:119
r_code::_View::Equal
Definition: r_code/object.h:208
r_code::SysObject
Definition: r_code/object.h:136
r_code::Code::trace
void trace(uint16 i, std::ostream &out, Atom::TraceContext &context) const
Definition: r_code/object.h:299
core::P
Definition: base.h:102
r_code::_View::Less
Definition: r_code/object.h:215
r_code::Code
Definition: r_code/object.h:224
core::_Object
Definition: base.h:130
r_code::ImageObject
Definition: r_code/object.h:106
r_code::LocalObject
Definition: r_code/object.h:377
r_code::Code::trace
void trace(std::ostream &out) const
Definition: r_code/object.h:317
r_code::Mem
Definition: r_code/object.h:420
r_code::list
Definition: list.h:99
r_code::Code::r_trace_string
std::string r_trace_string() const
Definition: r_code/object.h:368