AERA
guard_builder.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 guard_builder_h
86 #define guard_builder_h
87 
88 #include "factory.h"
89 
90 
91 namespace r_exec {
92 
93 class GuardBuilder :
94  public _Object {
95 public:
96  GuardBuilder();
97  virtual ~GuardBuilder();
98 
99  virtual void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const;
100 };
101 
102 // fwd: t2=t0+period, t3=t1+period.
103 // bwd: t0=t2-period, t1=t3-period.
105  public GuardBuilder {
106 protected:
107  std::chrono::microseconds period_;
108 
109  void write_guard(r_code::Code *mdl, uint16 l, uint16 r, uint16 opcode, std::chrono::microseconds offset, uint16 &write_index, uint16 &extent_index) const;
110  void _build(r_code::Code *mdl, uint16 t0, uint16 t1, uint16 &write_index) const;
111 public:
112  TimingGuardBuilder(std::chrono::microseconds period);
113  virtual ~TimingGuardBuilder();
114 
115  void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const override;
116 };
117 
118 // fwd: q1=q0+speed*period.
119 // bwd: speed=(q1-q0)/period, speed.after=q1.after-offset, speed.before=q1.before-offset.
121  public TimingGuardBuilder {
122 private:
123  std::chrono::microseconds offset_; // period-(speed.after-t0).
124 
125  void _build(r_code::Code *mdl, uint16 q0, uint16 t0, uint16 t1, uint16 &write_index) const;
126 public:
127  SGuardBuilder(std::chrono::microseconds period, std::chrono::microseconds offset);
128  ~SGuardBuilder();
129 
130  void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const override;
131 };
132 
133 // bwd: cmd.after=q1.after-offset, cmd.before=cmd.after+cmd_duration.
135  public TimingGuardBuilder {
136 protected:
137  std::chrono::microseconds offset_;
138  std::chrono::microseconds cmd_duration_;
139 
140  void _build(r_code::Code *mdl, uint16 q0, uint16 t0, uint16 t1, uint16 &write_index) const;
141 public:
147  NoArgCmdGuardBuilder(std::chrono::microseconds period, std::chrono::microseconds offset, std::chrono::microseconds cmd_duration);
149 
150  void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const override;
151 };
152 
153 // bwd: cmd.after=q1.after-period, cmd.before=q1.before-period.
155  public TimingGuardBuilder {
156 protected:
157  std::chrono::microseconds offset_;
158  uint16 cmd_arg_index_;
159 
160  void _build(r_code::Code *mdl, uint16 fwd_opcode, uint16 bwd_opcode, uint16 q0, uint16 t0, uint16 t1, uint16 &write_index) const;
161  void _build(r_code::Code *mdl, uint16 fwd_opcode, uint16 bwd_opcode, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const;
162 
163  CmdGuardBuilder(std::chrono::microseconds period, std::chrono::microseconds offset, uint16 cmd_arg_index);
164 public:
165  virtual ~CmdGuardBuilder();
166 };
167 
168 // fwd: q1=q0*cmd_arg.
169 // bwd: cmd_arg=q1/q0.
171  public CmdGuardBuilder {
172 public:
173  MCGuardBuilder(std::chrono::microseconds period, std::chrono::microseconds offset, float32 cmd_arg_index);
174  ~MCGuardBuilder();
175 
176  void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const override;
177 };
178 
179 // fwd: q1=q0+cmd_arg.
180 // bwd: cmd_arg=q1-q0.
182  public CmdGuardBuilder {
183 private:
184 
185 public:
186  ACGuardBuilder(std::chrono::microseconds period, std::chrono::microseconds offset, uint16 cmd_arg_index);
187  ~ACGuardBuilder();
188 
189  void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const override;
190 };
191 
192 // bwd: cause.after=t2-offset, cause.before=t3-offset.
194  public TimingGuardBuilder {
195 protected:
196  float32 constant_;
197  std::chrono::microseconds offset_;
198 
199  void _build(r_code::Code *mdl, uint16 fwd_opcode, uint16 bwd_opcode, uint16 q0, uint16 t0, uint16 t1, uint16 &write_index) const;
200  void _build(r_code::Code *mdl, uint16 fwd_opcode, uint16 bwd_opcode, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const;
201 
202  ConstGuardBuilder(std::chrono::microseconds period, float32 constant, std::chrono::microseconds offset);
203 public:
205 };
206 
207 // fwd: q1=q0*constant.
208 // bwd: q0=q1/constant.
210  public ConstGuardBuilder {
211 public:
212  MGuardBuilder(std::chrono::microseconds period, float32 constant, std::chrono::microseconds offset);
213  ~MGuardBuilder();
214 
215  void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const override;
216 };
217 
218 // fwd: q1=q0+constant.
219 // bwd: q0=q1-constant.
221  public ConstGuardBuilder {
222 public:
223  AGuardBuilder(std::chrono::microseconds period, float32 constant, std::chrono::microseconds offset);
224  ~AGuardBuilder();
225 
226  void build(r_code::Code *mdl, _Fact *premise_pattern, _Fact *cause_pattern, uint16 &write_index) const override;
227 };
228 
234  public TimingGuardBuilder {
235 protected:
236  std::chrono::microseconds offset_;
237  uint16 cmd_arg_index_;
238  P<_Fact> cause_;
239 public:
240  ConstBwdArgCmdGuardBuilder(std::chrono::microseconds period, std::chrono::microseconds offset, uint16 cmd_arg_index, _Fact* cause);
241  virtual ~ConstBwdArgCmdGuardBuilder();
242 
243  void build(r_code::Code* mdl, _Fact* premise_pattern, _Fact* cause_pattern, uint16& write_index) const override;
244 };
245 
246 }
247 
248 
249 #endif
r_exec::MCGuardBuilder
Definition: guard_builder.h:171
r_exec::ConstBwdArgCmdGuardBuilder
Definition: guard_builder.h:234
r_exec::ConstGuardBuilder
Definition: guard_builder.h:194
r_exec::ACGuardBuilder
Definition: guard_builder.h:182
r_exec::CmdGuardBuilder
Definition: guard_builder.h:155
r_exec::_Fact
Definition: factory.h:155
core::P
Definition: base.h:103
r_code::Code
Definition: r_code/object.h:224
r_exec::TimingGuardBuilder
Definition: guard_builder.h:105
core::_Object
Definition: base.h:131
r_exec::NoArgCmdGuardBuilder
Definition: guard_builder.h:135
r_exec::SGuardBuilder
Definition: guard_builder.h:121
r_exec::AGuardBuilder
Definition: guard_builder.h:221
r_exec::MGuardBuilder
Definition: guard_builder.h:210
r_exec::GuardBuilder
Definition: guard_builder.h:94
r_exec::NoArgCmdGuardBuilder::NoArgCmdGuardBuilder
NoArgCmdGuardBuilder(std::chrono::microseconds period, std::chrono::microseconds offset, std::chrono::microseconds cmd_duration)
Definition: guard_builder.cpp:261