AERA
controller.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 controller_h
86 #define controller_h
87 
88 #include "../submodules/CoreLibrary/CoreLibrary/base.h"
89 #include "../submodules/CoreLibrary/CoreLibrary/utils.h"
90 #include "../r_code/object.h"
91 #include "mem_output.h"
92 #include "dll.h"
93 #include "reduction_job.h"
94 
95 namespace r_exec {
96 
97 class Overlay;
98 class View;
99 
100 // Upon invocation of take_input() the overlays older than tsc are killed, assuming stc>0; otherwise, overlays live unitl the ipgm dies.
101 // Controllers are built at loading time and at the view's injection time.
102 // Derived classes must expose a function: void reduce(r_code::_View*input); (called by reduction jobs).
103 class r_exec_dll Controller :
104  public _Object {
105 protected:
106  volatile uint32 invalidated_; // 32 bit alignment.
107  volatile uint32 activated_; // 32 bit alignment.
108 
109  std::chrono::microseconds time_scope_;
110 
111  r_code::_View* view_;
112 
113  CriticalSection reductionCS_;
114 
115  virtual void take_input(r_exec::View* /* input */) {}
116  template<class C> void __take_input(r_exec::View* input) { // utility: to be called by sub-classes.
117 
118  ReductionJob<C>* j = new ReductionJob<C>(input, (C*)this);
119 #ifdef WITH_DETAIL_OID
120  OUTPUT_LINE((TraceLevel)0, " make ReductionJob " << j->get_job_id() <<
121  "(" << j->get_detail_oid() << "): controller(" << get_detail_oid() << ")->reduce(View(fact_" <<
122  input->object_->get_oid() << ")) for " << get_core_object()->get_oid());
123 #endif
124  push_reduction_job(j);
125  }
126 
127  static void push_reduction_job(_ReductionJob* j);
128 
129  Controller(r_code::_View* view);
130 public:
131  virtual ~Controller();
132 
133  std::chrono::microseconds get_tsc() { return time_scope_; }
134 
135  virtual void invalidate() { invalidated_ = 1; }
136  bool is_invalidated() { return invalidated_ == 1; };
137  void activate(bool a) { activated_ = (a ? 1 : 0); }
138  bool is_activated() const { return activated_ == 1; }
139  bool is_alive() const { return invalidated_ == 0 && activated_ == 1; }
140 
141  virtual r_code::Code* get_core_object() const = 0;
142 
143  r_code::Code* get_object() const { return view_->object_; } // return the reduction object (e.g. ipgm, icpp_pgm, cst, mdl).
144  r_exec::View* get_view() const { return (r_exec::View*)view_; } // return the reduction object's view.
145 
146  void _take_input(r_exec::View* input); // called by the rMem at update time and at injection time.
147 
148  virtual void gain_activation() { activate(true); }
149  virtual void lose_activation() { activate(false); }
150 
151  void set_view(View* view);
152 
153  void debug(View* /* input */) {}
154 };
155 
156 class r_exec_dll OController :
157  public Controller {
158 protected:
159  r_code::list<P<Overlay> > overlays_;
160 
161  OController(r_code::_View* view);
162 public:
163  virtual ~OController();
164 };
165 
166 }
167 
168 #endif
r_code::_View
Definition: r_code/object.h:164
r_code::Code
Definition: r_code/object.h:224
core::CriticalSection
Definition: submodules/CoreLibrary/CoreLibrary/utils.h:233
core::_Object
Definition: base.h:131
r_exec::_ReductionJob
Definition: reduction_job.h:96
r_exec::View
Definition: view.h:102
r_exec::OController
Definition: controller.h:157
r_exec::ReductionJob
Definition: reduction_job.h:111
r_exec::Controller
Definition: controller.h:104
r_code::list
Definition: list.h:99