AERA
mem.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 "init.h"
86 #include "binding_map.h"
87 #include "../r_code/replicode_defs.h"
88 #include "operator.h"
89 #include "factory.h"
90 #include "cpp_programs.h"
91 #include "../r_code/utils.h"
92 #include <math.h>
93 
94 
95 namespace r_exec {
96 
97 template<class O, class S> MemExec<O, S>::MemExec() : S() {
98 }
99 
100 template<class O, class S> MemExec<O, S>::~MemExec() {
101 
102  if (state_ == RUNNING)
103  S::stop();
104  deleted_ = true;
105  objects_.clear();
106 }
107 
109 
110 template<class O, class S> r_code::Code *MemExec<O, S>::build_object(r_code::SysObject *source) const {
111 
112  Atom head = source->code_[0];
113  switch (head.getDescriptor()) {
114  case Atom::GROUP:
115  return new Group(source);
116  default: {
117  uint16 opcode = head.asOpcode();
118  if (opcode == Opcodes::Fact)
119  return new Fact(source);
120  else if (opcode == Opcodes::AntiFact)
121  return new AntiFact(source);
122  else if (opcode == Opcodes::Goal)
123  return new Goal(source);
124  else if (opcode == Opcodes::Pred)
125  return new Pred(source);
126  else if (opcode == Opcodes::ICst)
127  return new ICST(source);
128  else if (opcode == Opcodes::MkRdx)
129  return new MkRdx(source);
130  else if (opcode == Opcodes::MkActChg ||
131  opcode == Opcodes::MkHighAct ||
132  opcode == Opcodes::MkHighSln ||
133  opcode == Opcodes::MkLowAct ||
134  opcode == Opcodes::MkLowRes ||
135  opcode == Opcodes::MkLowSln ||
136  opcode == Opcodes::MkNew ||
137  opcode == Opcodes::MkSlnChg ||
138  opcode == Opcodes::Success ||
139  opcode == Opcodes::Perf)
140  return new r_code::LocalObject(source);
141  else
142  return new O(source);
143  }
144  }
145 }
146 
147 template<class O, class S> r_code::Code *MemExec<O, S>::_build_object(Atom head) const {
148 
149  r_code::Code *object = new O();
150  object->code(0) = head;
151  return object;
152 }
153 
154 template<class O, class S> r_code::Code *MemExec<O, S>::build_object(Atom head) const {
155 
156  r_code::Code *object;
157  switch (head.getDescriptor()) {
158  case Atom::GROUP:
159  object = new Group();
160  break;
161  default: {
162  uint16 opcode = head.asOpcode();
163  if (opcode == Opcodes::Fact)
164  object = new Fact();
165  else if (opcode == Opcodes::AntiFact)
166  object = new AntiFact();
167  else if (opcode == Opcodes::Pred)
168  object = new Pred();
169  else if (opcode == Opcodes::Goal)
170  object = new Goal();
171  else if (opcode == Opcodes::ICst)
172  object = new ICST();
173  else if (opcode == Opcodes::MkRdx)
174  object = new MkRdx();
175  else if (opcode == Opcodes::MkActChg ||
176  opcode == Opcodes::MkHighAct ||
177  opcode == Opcodes::MkHighSln ||
178  opcode == Opcodes::MkLowAct ||
179  opcode == Opcodes::MkLowRes ||
180  opcode == Opcodes::MkLowSln ||
181  opcode == Opcodes::MkNew ||
182  opcode == Opcodes::MkSlnChg ||
183  opcode == Opcodes::Success ||
184  opcode == Opcodes::Perf)
185  object = new r_code::LocalObject();
186  else if (O::RequiresPacking())
187  object = new r_code::LocalObject(); // temporary sand box for assembling code; will be packed into an O at injection time.
188  else
189  object = new O();
190  break;
191  }
192  }
193 
194  object->code(0) = head;
195  return object;
196 }
197 
199 
200 template<class O, class S> r_code::Code *MemExec<O, S>::check_existence(r_code::Code *object) {
201 
202  if (object->code(0).getDescriptor() == Atom::GROUP) // groups are always new.
203  return object;
204 
205  O *_object;
206  if (O::RequiresPacking()) // false if LObject, true for network-aware objects.
207  _object = O::Pack(object, this); // non compact form will be deleted (P<> in view) if not an instance of O; compact forms are left unchanged.
208  else
209  _object = (O *)object;
210 
211  return _object;
212 }
213 
214 template<class O, class S> void MemExec<O, S>::inject(O *object, View *view) {
215 
216  view->set_object(object);
217  S::inject_new_object(view);
218 }
219 }
r_code::SysObject
Definition: r_code/object.h:136
r_code::Code
Definition: r_code/object.h:224
r_code::LocalObject
Definition: r_code/object.h:377