AERA
class.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 "class.h"
86 #include "segments.h"
87 
88 using namespace std;
89 using namespace r_code;
90 
91 namespace r_comp {
92 
93 const char *Class::Expression = "xpr";
94 const char *Class::Type = "type";
95 
96 bool Class::has_offset() const {
97 
98  switch (atom_.getDescriptor()) {
99  case Atom::OBJECT:
100  case Atom::GROUP:
101  case Atom::INSTANTIATED_PROGRAM:
102  case Atom::INSTANTIATED_CPP_PROGRAM:
103  case Atom::S_SET:
104  case Atom::MARKER: return true;
105  default: return false;
106  }
107 }
108 
109 Class::Class(ReturnType t) : type(t), str_opcode("undefined") {
110 }
111 
112 Class::Class(Atom atom,
113  std::string str_opcode,
114  vector<StructureMember> r,
115  ReturnType t) : atom_(atom),
116  str_opcode(str_opcode),
117  things_to_read(r),
118  type(t),
119  use_as(StructureMember::I_CLASS) {
120 }
121 
122 bool Class::is_pattern(Metadata *metadata) const {
123 
124  return (metadata->classes_.find("ptn")->second.atom_ == atom_) || (metadata->classes_.find("|ptn")->second.atom_ == atom_);
125 }
126 
127 bool Class::is_fact(Metadata *metadata) const {
128 
129  return (metadata->classes_.find("fact")->second.atom_ == atom_) || (metadata->classes_.find("|fact")->second.atom_ == atom_);
130 }
131 
132 bool Class::get_member_index(Metadata *metadata, std::string &name, uint16 &index, Class *&p) const {
133 
134  for (uint16 i = 0; i < things_to_read.size(); ++i)
135  if (things_to_read[i].name_ == name) {
136 
137  index = (has_offset() ? i + 1 : i); // in expressions the lead r-atom is at 0; in objects, members start at 1
138  if (things_to_read[i].used_as_expression()) // the class is: [::a-class]
139  p = NULL;
140  else
141  p = things_to_read[i].get_class(metadata);
142  return true;
143  }
144  return false;
145 }
146 
147 std::string Class::get_member_name(uint32 index) {
148 
149  return things_to_read[has_offset() ? index - 1 : index].name_;
150 }
151 
152 ReturnType Class::get_member_type(const uint16 index) {
153 
154  return things_to_read[has_offset() ? index - 1 : index].get_return_type();
155 }
156 
157 Class *Class::get_member_class(Metadata *metadata, const std::string &name) {
158 
159  for (uint16 i = 0; i < things_to_read.size(); ++i)
160  if (things_to_read[i].name_ == name)
161  return things_to_read[i].get_class(metadata);
162  return NULL;
163 }
164 
165 void Class::write(word32 *storage) {
166 
167  storage[0] = atom_.atom_;
168  r_code::Write(storage + 1, str_opcode);
169  uint32 offset = 1 + r_code::GetSize(str_opcode);
170  storage[offset++] = type;
171  storage[offset++] = use_as;
172  storage[offset++] = things_to_read.size();
173  for (uint32 i = 0; i < things_to_read.size(); ++i) {
174 
175  things_to_read[i].write(storage + offset);
176  offset += things_to_read[i].get_size();
177  }
178 }
179 
180 void Class::read(word32 *storage) {
181 
182  atom_ = storage[0];
183  r_code::Read(storage + 1, str_opcode);
184  uint32 offset = 1 + r_code::GetSize(str_opcode);
185  type = (ReturnType)storage[offset++];
186  use_as = (StructureMember::Iteration)storage[offset++];
187  uint32 member_count = storage[offset++];
188  for (uint32 i = 0; i < member_count; ++i) {
189 
190  StructureMember m;
191  m.read(storage + offset);
192  things_to_read.push_back(m);
193  offset += m.get_size();
194  }
195 }
196 
197 uint32 Class::get_size() { // see segments.cpp for the RAM layout
198 
199  uint32 size = 4; // atom, return type, usage, number of members
200  size += r_code::GetSize(str_opcode);
201  for (uint32 i = 0; i < things_to_read.size(); ++i)
202  size += things_to_read[i].get_size();
203  return size;
204 }
205 }
r_code::Atom
Definition: atom.h:104