AERA
model_base.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 model_base_h
86 #define model_base_h
87 
88 #include "factory.h"
89 
90 
91 namespace r_exec {
92 
93 class _Mem;
94 
95 // TPX guess models: this list is meant for TPXs to (a) avoid re-guessing known failed models and,
96 // (b) avoid producing the same models in case they run concurrently.
97 // The black list contains bad models (models that were killed). This list is trimmed down on a time basis (black_thz) by the garbage collector.
98 // Each bad model is tagged with the last time it was successfully compared to. GC is performed by comparing this time to the thz.
99 // The white list contains models that are still alive and is trimmed down when models time out.
100 // Models are packed before insertion in the white list.
101 class ModelBase {
102  friend class _Mem;
103 private:
104  static ModelBase *singleton_;
105 
106  CriticalSection mdlCS_;
107 
108  std::chrono::microseconds thz_;
109 
110  class MEntry {
111  private:
112  static bool Match(r_code::Code *lhs, r_code::Code *rhs);
113  static uint32 _ComputeHashCode(_Fact *component); // use for lhs/rhs.
114  public:
115  static uint32 ComputeHashCode(r_code::Code *mdl, bool packed);
116 
117  MEntry();
118  MEntry(r_code::Code *mdl, bool packed);
119 
120  P<r_code::Code> mdl_;
121  bool packed_;
122  Timestamp touch_time_; // last time the mdl was successfully compared to.
123  uint32 hash_code_;
124 
125  bool match(const MEntry &e) const;
126 
127  class Hash {
128  public:
129  size_t operator ()(const MEntry& e) const { return e.hash_code_; }
130  };
131 
132  class Equal {
133  public:
134  bool operator ()(const MEntry& lhs, const MEntry& rhs) const { return lhs.match(rhs); }
135  };
136  };
137 
138  typedef std::unordered_set<MEntry, typename MEntry::Hash, typename MEntry::Equal> MdlSet;
139 
140  MdlSet black_list_; // mdls are already packed when inserted (they come from the white list).
141  MdlSet white_list_; // mdls are packed just before insertion.
142 
143  void set_thz(std::chrono::microseconds thz) { thz_ = thz; } // called by _Mem::start(); set to secondary_thz.
144  void trim_objects(); // called by _Mem::GC().
145 
146  ModelBase();
147 public:
148  static ModelBase *Get();
149 
150  void load(r_code::Code *mdl); // called by _Mem::load(); models with no views go to the black_list_.
151  void get_models(r_code::list<P<r_code::Code> > &models); // white_list_ first, black_list_ next.
152 
153  r_code::Code *check_existence(r_code::Code *mdl); // caveat: mdl is unpacked; return (a) NULL if the model is in the black list, (b) a model in the white list if the mdl has been registered there or (c) the mdl itself if not in the model base, in which case the mdl is added to the white list.
154  void check_existence(r_code::Code *m0, r_code::Code *m1, r_code::Code *&_m0, r_code::Code *&_m1); // m1 is a requirement on m0; _m0 and _m1 are the return values as defined above; m0 added only if m1 is not black listed.
155  void register_mdl_failure(r_code::Code *mdl); // moves the mdl from the white to the black list; happens to bad models.
156  void register_mdl_timeout(r_code::Code *mdl); // deletes the mdl from the white list; happen to models that have been unused for primary_thz.
157 };
158 }
159 
160 
161 #endif
r_exec::_Fact
Definition: factory.h:155
core::P< r_code::Code >
r_code::Code
Definition: r_code/object.h:224
r_exec::ModelBase::MEntry::Equal
Definition: model_base.h:132
core::CriticalSection
Definition: submodules/CoreLibrary/CoreLibrary/utils.h:233
r_exec::ModelBase
Definition: model_base.h:101
r_exec::ModelBase::MEntry::Hash
Definition: model_base.h:127
r_code::list
Definition: list.h:99
r_exec::_Mem
Definition: mem.h:118