AERA
operator.h
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 //_/_/ Copyright (c) 2023 Leonard M. Eberding
10 //_/_/ http://www.iiim.is
11 //_/_/
12 //_/_/ Copyright (c) 2010-2012 Eric Nivel
13 //_/_/ Center for Analysis and Design of Intelligent Agents
14 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
15 //_/_/ http://cadia.ru.is
16 //_/_/
17 //_/_/ Part of this software was developed by Eric Nivel
18 //_/_/ in the HUMANOBS EU research project, which included
19 //_/_/ the following parties:
20 //_/_/
21 //_/_/ Autonomous Systems Laboratory
22 //_/_/ Technical University of Madrid, Spain
23 //_/_/ http://www.aslab.org/
24 //_/_/
25 //_/_/ Communicative Machines
26 //_/_/ Edinburgh, United Kingdom
27 //_/_/ http://www.cmlabs.com/
28 //_/_/
29 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
30 //_/_/ University of Lugano and SUPSI, Switzerland
31 //_/_/ http://www.idsia.ch/
32 //_/_/
33 //_/_/ Institute of Cognitive Sciences and Technologies
34 //_/_/ Consiglio Nazionale delle Ricerche, Italy
35 //_/_/ http://www.istc.cnr.it/
36 //_/_/
37 //_/_/ Dipartimento di Ingegneria Informatica
38 //_/_/ University of Palermo, Italy
39 //_/_/ http://diid.unipa.it/roboticslab/
40 //_/_/
41 //_/_/
42 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
43 //_/_/
44 //_/_/ Redistribution and use in source and binary forms, with or without
45 //_/_/ modification, is permitted provided that the following conditions
46 //_/_/ are met:
47 //_/_/ - Redistributions of source code must retain the above copyright
48 //_/_/ and collaboration notice, this list of conditions and the
49 //_/_/ following disclaimer.
50 //_/_/ - Redistributions in binary form must reproduce the above copyright
51 //_/_/ notice, this list of conditions and the following disclaimer
52 //_/_/ in the documentation and/or other materials provided with
53 //_/_/ the distribution.
54 //_/_/
55 //_/_/ - Neither the name of its copyright holders nor the names of its
56 //_/_/ contributors may be used to endorse or promote products
57 //_/_/ derived from this software without specific prior
58 //_/_/ written permission.
59 //_/_/
60 //_/_/ - CADIA Clause: The license granted in and to the software
61 //_/_/ under this agreement is a limited-use license.
62 //_/_/ The software may not be used in furtherance of:
63 //_/_/ (i) intentionally causing bodily injury or severe emotional
64 //_/_/ distress to any person;
65 //_/_/ (ii) invading the personal privacy or violating the human
66 //_/_/ rights of any person; or
67 //_/_/ (iii) committing or preparing for any act of war.
68 //_/_/
69 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
70 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
71 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
72 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
73 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
74 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
76 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
77 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
78 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
80 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
81 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
82 //_/_/ OF SUCH DAMAGE.
83 //_/_/
84 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
85 
86 #ifndef operator_h
87 #define operator_h
88 
89 #include "../r_code/object.h"
90 #include "../r_code/utils.h"
91 #include "factory.h"
92 
93 #include "_context.h"
94 
95 
96 namespace r_exec {
97 
98 // Wrapper class for evaluation contexts.
99 // Template operator functions is not an option since some operators are defined in usr_operators.dll.
100 class dll_export Context {
101 private:
102  _Context *implementation_;
103 public:
104  Context(_Context *implementation) : implementation_(implementation) {}
105  ~Context() { delete implementation_; }
106 
107  _Context *get_implementation() const { return implementation_; }
108 
109  uint16 get_children_count() const { return implementation_->get_children_count(); }
110  Context get_child(uint16 index) const { return Context(implementation_->get_child_new(index)); }
111 
112  Context operator *() const { return Context(implementation_->dereference_new()); }
113  Context &operator =(const Context &c) {
114 
115  // Copy the existing implementation before deleting it.
116  _Context* copy = c.get_implementation()->clone();
117  delete implementation_;
118  implementation_ = copy;
119  return *this;
120  }
121 
122  bool operator ==(const Context &c) const { return implementation_->equal(c.get_implementation()); }
123  bool operator !=(const Context &c) const { return !implementation_->equal(c.get_implementation()); }
124 
125  Atom &operator [](uint16 i) const { return implementation_->get_atom(i); }
126 
127  virtual void setAtomicResult(Atom a) const { implementation_->setAtomicResult(a); }
128  virtual void setTimestampResult(Timestamp t) const { implementation_->setTimestampResult(t); }
129  virtual void setDurationResult(std::chrono::microseconds d) const { implementation_->setDurationResult(d); }
130  virtual uint16 setCompoundResultHead(Atom a) const { return implementation_->setCompoundResultHead(a); }
131  virtual void addCompoundResultPart(Atom a) const { implementation_->addCompoundResultPart(a); }
132 
133  void trace(std::ostream& out) const { return implementation_->trace(out); }
134 };
135 
136 class OpContext : public Context {
137 private:
138  std::vector<r_code::Atom> operation_results_;
139 
140  std::vector<Atom>& operation_results() const {
141  return const_cast<OpContext*>(this)->operation_results_;
142  }
143 
144 public:
145  OpContext(_Context* implementation) : Context(implementation) {}
146  ~OpContext() {}
147 
148  void setAtomicResult(Atom a) const override {
149  Context::setAtomicResult(a);
150  operation_results().push_back(a);
151  }
152  void setTimestampResult(Timestamp t) const override {
153  Context::setTimestampResult(t);
154  operation_results().resize(operation_results_.size() + 3);
155  uint16 value_index = operation_results().size() - 3;
156  r_code::Utils::SetTimestamp(&operation_results()[value_index], t);
157  }
158  void setDurationResult(std::chrono::microseconds d) const override {
159  Context::setDurationResult(d);
160  operation_results().resize(operation_results_.size() + 3);
161  uint16 value_index = operation_results().size() - 3;
162  r_code::Utils::SetDuration(&operation_results()[value_index], d);
163  }
164  uint16 setCompoundResultHead(Atom a) const override {
165  uint16 value_index = Context::setCompoundResultHead(a);
166  addCompoundResultPart(a);
167  return value_index;
168  }
169  void addCompoundResultPart(Atom a) const override{
170  Context::addCompoundResultPart(a);
171  operation_results().push_back(a);
172  }
173 
174  const std::vector<r_code::Atom>& result() { return operation_results_; }
175 
176  static std::vector<r_code::Atom> build_and_evaluate_expression(_Fact* q0, _Fact* q1, r_code::Atom op);
177  static P<r_code::LocalObject> build_expression_object(_Fact* q0, _Fact* q1, r_code::Atom op);
178  static std::vector<r_code::Atom> evaluate_expression(r_code::LocalObject* expression);
179 };
180 
181 bool red(const Context &context); // executive-dependent.
182 
183 bool syn(const Context &context);
184 
185 class Operator {
186 private:
187  static r_code::resized_vector<Operator> Operators_; // indexed by opcodes.
188 
189  bool(*operator_)(const Context &);
190  bool(*overload_)(const Context &);
191 public:
192  static void Register(uint16 opcode, bool(*op)(const Context &)); // first, register std operators; next register user-defined operators (may be registered as overloads).
193  static Operator Get(uint16 opcode) { return Operators_[opcode]; }
194  Operator() : operator_(NULL), overload_(NULL) {}
195  Operator(bool(*o)(const Context &)) : operator_(o), overload_(NULL) {}
196  ~Operator() {}
197 
198  void setOverload(bool(*o)(const Context &)) { overload_ = o; }
199 
200  bool operator ()(const Context &context) const {
201  if (operator_(context))
202  return true;
203  if (overload_)
204  return overload_(context);
205  return false;
206  }
207 
208  bool is_red() const { return operator_ == red; }
209  bool is_syn() const { return operator_ == syn; }
210 };
211 
212 // std operators ////////////////////////////////////////
213 
214 bool now(const Context &context);
215 
216 bool rnd(const Context &context);
217 
218 bool equ(const Context &context);
219 bool neq(const Context &context);
220 bool gtr(const Context &context);
221 bool lsr(const Context &context);
222 bool gte(const Context &context);
223 bool lse(const Context &context);
224 
225 bool add(const Context &context);
226 bool sub(const Context &context);
227 bool mul(const Context &context);
228 bool div(const Context &context);
229 
230 bool dis(const Context &context);
231 
232 bool ln(const Context &context);
233 bool exp(const Context &context);
234 bool log(const Context &context);
235 bool e10(const Context &context);
236 
237 bool ins(const Context &context); // executive-dependent.
238 
239 bool fvw(const Context &context); // executive-dependent.
240 
241 bool is_sim(const Context &context);
242 bool minimum(const Context &context);
243 bool maximum(const Context &context);
244 bool id(const Context& context);
245 }
246 
247 
248 #endif
r_code::resized_vector
Definition: resized_vector.h:137
r_code::Utils::SetDuration
static void SetDuration(Atom *iptr, std::chrono::microseconds duration)
Definition: r_code/utils.h:251
r_exec::Context
Definition: operator.h:100
r_exec::Operator
Definition: operator.h:185
r_code::Atom
Definition: atom.h:104
r_exec::_Fact
Definition: factory.h:155
core::P
Definition: base.h:103
r_exec::_Context::dereference_new
virtual _Context * dereference_new() const =0
r_exec::_Context::get_child_new
virtual _Context * get_child_new(uint16 index) const =0
r_code::LocalObject
Definition: r_code/object.h:377
r_exec::OpContext
Definition: operator.h:136
r_exec::_Context::setCompoundResultHead
uint16 setCompoundResultHead(Atom a) const
Definition: _context.cpp:112
r_exec::_Context
Definition: _context.h:96
r_code::Utils::SetTimestamp
static void SetTimestamp(Atom *iptr, Timestamp timestamp)
Definition: code_utils.cpp:134