AERA
image.tpl.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 <iostream>
86 #include "atom.h"
87 
88 
89 namespace r_code {
90 
91 template<class I> Image<I> *Image<I>::Build(Timestamp timestamp, uint32 map_size, uint32 code_size, uint32 names_size) {
92 
93  I *image = new(map_size + code_size) I(timestamp, map_size, code_size, names_size);
94  return (Image<I> *)image;
95 }
96 
97 template<class I> Image<I> *Image<I>::Read(std::ifstream &stream) {
98 
99  Timestamp timestamp;
100  uint32 map_size;
101  uint32 code_size;
102  uint32 names_size;
103  stream.read((char *)&timestamp, sizeof(uint64));
104  stream.read((char *)&map_size, sizeof(uint32));
105  stream.read((char *)&code_size, sizeof(uint32));
106  stream.read((char *)&names_size, sizeof(uint32));
107  Image *image = Build(timestamp, map_size, code_size, names_size);
108  stream.read((char *)image->data(), image->get_size() * sizeof(word32));
109  return image;
110 }
111 
112 template<class I> void Image<I>::Write(Image<I> *image, std::ofstream &stream) {
113 
114  Timestamp timestamp = image->timestamp();
115  uint32 map_size = image->map_size();
116  uint32 code_size = image->code_size();
117  uint32 names_size = image->names_size();
118  stream.write((char *)&timestamp, sizeof(uint64));
119  stream.write((char *)&map_size, sizeof(uint32));
120  stream.write((char *)&code_size, sizeof(uint32));
121  stream.write((char *)&names_size, sizeof(uint32));
122  stream.write((char *)image->data(), image->get_size() * sizeof(word32));
123 }
124 
125 template<class I> Image<I>::Image() : I() {
126 }
127 
128 template<class I> Image<I>::~Image() {
129 }
130 
131 template<class I> uint32 Image<I>::get_size() const {
132 
133  return I::map_size() + I::code_size() + I::names_size();
134 }
135 
136 template<class I> uint32 Image<I>::getObjectCount() const {
137 
138  return I::map_size();
139 }
140 
141 template<class I> word32 *Image<I>::get_object(uint32 i) {
142 
143  return I::data() + I::data(i);
144 }
145 
146 template<class I> word32 *Image<I>::getCodeSegment() {
147 
148  return I::data() + I::map_size();
149 }
150 
151 template<class I> uint32 Image<I>::getCodeSegmentSize() const {
152 
153  return I::code_size();
154 }
155 
156 template<class I> void Image<I>::trace() const {
157 
158  std::cout << "---Image---\n";
159  std::cout << "Size: " << get_size() << std::endl;
160  std::cout << "Object Map Size: " << I::map_size() << std::endl;
161  std::cout << "Code Segment Size: " << I::code_size() << std::endl;
162  std::cout << "Names Size: " << I::names_size() << std::endl;
163 
164  uint32 i = 0;
165 
166  std::cout << "===Object Map===" << std::endl;
167  for (; i < I::map_size(); ++i)
168  std::cout << i << " " << I::data(i) << std::endl;
169 
170  // at this point, i is at the first word32 of the first object in the code segment
171  std::cout << "===Code Segment===" << std::endl;
172  uint32 code_start = I::map_size();
173  for (uint32 j = 0; j < code_start; ++j) { // read object map: data[data[j]] is the first word32 of an object, data[data[j]+5] is the first atom
174 
175  uint32 object_axiom = I::data(I::data(j));
176  uint32 object_code_size = I::data(I::data(j) + 1);
177  uint32 object_reference_set_size = I::data(I::data(j) + 2);
178  uint32 object_marker_set_size = I::data(I::data(j) + 3);
179  uint32 object_view_set_size = I::data(I::data(j) + 4);
180  std::cout << "---object---\n";
181  std::cout << i++;
182  std::cout << i++ << " code size: " << object_code_size << std::endl;
183  std::cout << i++ << " reference set size: " << object_reference_set_size << std::endl;
184  std::cout << i++ << " marker set size: " << object_marker_set_size << std::endl;
185  std::cout << i++ << " view set size: " << object_view_set_size << std::endl;
186 
187  std::cout << "---code---\n";
188  for (; i < I::data(j) + 5 + object_code_size; ++i) {
189 
190  std::cout << i << " ";
191  Atom::TraceContext context;
192  ((Atom *)&I::data(i))->trace(context, std::cout);
193  std::cout << std::endl;
194  }
195 
196  std::cout << "---reference set---\n";
197  for (; i < I::data(j) + 5 + object_code_size + object_reference_set_size; ++i)
198  std::cout << i << " " << I::data(i) << std::endl;
199 
200  std::cout << "---marker set---\n";
201  for (; i < I::data(j) + 5 + object_code_size + object_reference_set_size + object_marker_set_size; ++i)
202  std::cout << i << " " << I::data(i) << std::endl;
203 
204  std::cout << "---view set---\n";
205  for (uint32 k = 0; k < object_view_set_size; ++k) {
206 
207  uint32 view_code_size = I::data(i);
208  uint32 view_reference_set_size = I::data(i + 1);
209 
210  std::cout << "view[" << k << "]\n";
211  std::cout << i++ << " code size: " << view_code_size << std::endl;
212  std::cout << i++ << " reference set size: " << view_reference_set_size << std::endl;
213 
214  std::cout << "---code---\n";
215  uint32 l;
216  for (l = 0; l < view_code_size; ++i, ++l) {
217 
218  std::cout << i << " ";
219  Atom::TraceContext context;
220  ((Atom *)&I::data(i))->trace(context, std::cout);
221  std::cout << std::endl;
222  }
223 
224  std::cout << "---reference set---\n";
225  for (l = 0; l < view_reference_set_size; ++i, ++l)
226  std::cout << i << " " << I::data(i) << std::endl;
227  }
228  }
229 }
230 }