AERA
pgm_overlay.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 //_/_/ 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 pgm_overlay_h
86 #define pgm_overlay_h
87 
88 #include "overlay.h"
89 
90 
91 namespace r_exec {
92 
93 class PGMController;
94 class InputLessPGMController;
95 class IPGMContext;
96 
97 // Overlays for input-less programs.
98 // Base class for other programs (with inputs, anti-programs).
99 class r_exec_dll InputLessPGMOverlay :
100  public Overlay {
101  friend class PGMController;
102  friend class InputLessPGMController;
103  friend class IPGMContext;
104 protected:
105  std::vector<P<r_code::Code> > productions_; // receives the results of ins, inj and eje; views are retrieved (fvw) or built (reduction) in the value array.
106 
115  bool evaluate(uint16 index);
116 
117  virtual r_code::Code *get_mk_rdx(uint16 &extent_index) const;
118 
119  void patch_tpl_args(); // no views in tpl args; patches the ptn skeleton's first atom with IPGM_PTR with an index in the ipgm arg set; patches wildcards with similar IPGM_PTRs.
120  void patch_tpl_code(uint16 pgm_code_index, uint16 ipgm_code_index); // to recurse.
121  virtual void patch_input_code(uint16 pgm_code_index, uint16 input_index, uint16 input_code_index, int16 parent_index = -1); // defined in PGMOverlay.
122 
125 public:
126  virtual ~InputLessPGMOverlay();
127 
128  void reset() override; // reset to original state (pristine copy of the pgm code and empty value set).
129 
130  bool inject_productions(); // return true upon successful evaluation; no existence check in simulation mode.
131 };
132 
133 // Overlay with inputs.
134 // Several ReductionCores can attempt to reduce the same overlay simultaneously (each with a different input).
135 class r_exec_dll PGMOverlay :
136  public InputLessPGMOverlay {
137  friend class PGMController;
138  friend class IPGMContext;
139 private:
140  bool is_volatile_;
141  Timestamp birth_time_; // used for ipgms: overlays older than ipgm->tsc are killed; birth_time set to the time of the first match, 0 if no match occurred.
142 protected:
143  r_code::list<uint16> input_pattern_indices_; // stores the input patterns still waiting for a match: will be plucked upon each successful match.
144  std::vector<P<r_code::_View> > input_views_; // copies of the inputs; vector updated at each successful match.
145 
146  typedef enum {
147  SUCCESS = 0,
148  FAILURE = 1,
149  IMPOSSIBLE = 3 // when the input's class does not even match the object class in the pattern's skeleton.
150  }MatchResult;
151 
152  MatchResult match(r_exec::View *input, uint16 &input_index); // delegates to _match; input_index is set to the index of the pattern that matched the input.
153  bool check_guards(); // return true upon successful evaluation.
154 
155  MatchResult _match(r_exec::View *input, uint16 pattern_index); // delegates to __match.
156 
161  MatchResult __match(r_exec::View *input, uint16 pattern_index);
162 
163  r_code::Code *dereference_in_ptr(Atom a);
164  void patch_input_code(uint16 pgm_code_index, uint16 input_index, uint16 input_code_index, int16 parent_index = -1) override;
165 
166  r_code::Code *get_mk_rdx(uint16 &extent_index) const override;
167 
168  void init();
169 
171  PGMOverlay(PGMOverlay *original, uint16 last_input_index, uint16 value_commit_index); // copy from the original and rollback.
172 public:
173  virtual ~PGMOverlay();
174 
175  void reset() override {
176  InputLessPGMOverlay::reset();
177  patch_indices_.clear();
178  input_views_.clear();
179  input_pattern_indices_.clear();
180  init();
181  }
182 
183  Overlay *reduce(r_exec::View *input) override; // called upon the processing of a reduction job.
184 
185  r_code::Code *getInputObject(uint16 i) const;
186  r_code::_View *getInputView(uint16 i) const;
187 
188  Timestamp get_birth_time() const { return birth_time_; }
189 
190  bool is_invalidated() override;
191 };
192 
193 // Several ReductionCores can attempt to reduce the same overlay simultaneously (each with a different input).
194 // In addition, ReductionCores and signalling jobs can attempt to inject productions concurrently.
195 // Usues the same mk.rdx as for InputLessPGMOverlays.
196 class r_exec_dll AntiPGMOverlay :
197  public PGMOverlay {
198  friend class AntiPGMController;
199 private:
201  AntiPGMOverlay(AntiPGMOverlay *original, uint16 last_input_index, uint16 value_limit)
202  : PGMOverlay(original, last_input_index, value_limit) {}
203 public:
204  ~AntiPGMOverlay();
205 
206  Overlay *reduce(r_exec::View *input) override; // called upon the processing of a reduction job.
207 };
208 }
209 
210 
211 #include "pgm_overlay.inline.cpp"
212 
213 
214 #endif
r_exec::PGMController
Definition: pgm_controller.h:118
r_exec::IPGMContext
Definition: context.h:102
r_exec::Overlay
Definition: overlay.h:104
r_code::_View
Definition: r_code/object.h:164
r_code::Atom
Definition: atom.h:104
r_exec::InputLessPGMController
Definition: pgm_controller.h:108
r_code::Code
Definition: r_code/object.h:224
r_exec::AntiPGMOverlay
Definition: pgm_overlay.h:197
r_exec::View
Definition: view.h:102
r_exec::PGMOverlay
Definition: pgm_overlay.h:136
r_exec::Controller
Definition: controller.h:104
r_code::list< uint16 >
r_exec::InputLessPGMOverlay
Definition: pgm_overlay.h:100
r_exec::AntiPGMController
Definition: pgm_controller.h:132