replicode
factory.h
Go to the documentation of this file.
1 // factory.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef factory_h
32 #define factory_h
33 
34 #include "binding_map.h"
35 #include "overlay.h"
36 #include "CoreLibrary/dll.h"
37 
38 
39 namespace r_exec {
40 
41 // No instances of the following classes can transmited; for now: facts and icst will be.
42 
43 // Notification markers are not put in their references marker sets.
44 // They are not used to propagate saliency changes.
45 // They are encoded as Atom::Object instead of Atom::Marker.
46 
48  public LObject {
49 public:
50  MkNew(r_code::Mem *m, Code *object);
51 };
52 
54  public LObject {
55 public:
56  MkLowRes(r_code::Mem *m, Code *object);
57 };
58 
60  public LObject {
61 public:
62  MkLowSln(r_code::Mem *m, Code *object);
63 };
64 
66  public LObject {
67 public:
68  MkHighSln(r_code::Mem *m, Code *object);
69 };
70 
72  public LObject {
73 public:
74  MkLowAct(r_code::Mem *m, Code *object);
75 };
76 
78  public LObject {
79 public:
80  MkHighAct(r_code::Mem *m, Code *object);
81 };
82 
84  public LObject {
85 public:
86  MkSlnChg(r_code::Mem *m, Code *object, double value);
87 };
88 
90  public LObject {
91 public:
92  MkActChg(r_code::Mem *m, Code *object, double value);
93 };
94 
95 class Pred;
96 class Goal;
97 
99  public LObject {
100 private:
101  static bool MatchAtom(Atom lhs, Atom rhs);
102  static bool MatchStructure(const Code *lhs, uint16_t lhs_base_index, uint16_t lhs_index, const Code *rhs, uint16_t rhs_index);
103  static bool Match(const Code *lhs, uint16_t lhs_base_index, uint16_t lhs_index, const Code *rhs, uint16_t rhs_index, uint16_t lhs_arity);
104  static bool CounterEvidence(const Code *lhs, const Code *rhs);
105 protected:
106  _Fact();
107  _Fact(SysObject *source);
108  _Fact(_Fact *f);
109  _Fact(uint16_t opcode, Code *object, uint64_t after, uint64_t before, double confidence, double psln_thr);
110 public:
111  static bool MatchObject(const Code *lhs, const Code *rhs);
112 
113  virtual bool is_invalidated();
114 
115  bool is_fact() const;
116  bool is_anti_fact() const;
117  void set_opposite() const;
118  _Fact *get_absentee() const;
119 
120  bool match_timings_sync(const _Fact *evidence) const;
121  bool match_timings_overlap(const _Fact *evidence) const;
122  bool match_timings_inclusive(const _Fact *evidence) const;
123 
124  MatchResult is_evidence(const _Fact *target) const;
125  MatchResult is_timeless_evidence(const _Fact *target) const;
126 
127  uint64_t get_after() const;
128  uint64_t get_before() const;
129  float get_cfd() const;
130 
131  void set_cfd(double cfd);
132 
133  Pred *get_pred() const;
134  Goal *get_goal() const;
135 
136  void trace() const;
137 };
138 
139 typedef enum {
140  SIM_ROOT = 0,
143 } SimMode;
144 
146  public _Object {
147 private:
148  uint64_t volatile invalidated; // 32 bits alignment.
149 public:
150  Sim();
151  Sim(Sim *s); // is_requirement=false (not copied).
152  Sim(SimMode mode, uint64_t thz, Fact *super_goal, bool opposite, Controller *root); // use for SIM_ROOT.
153  Sim(SimMode mode, uint64_t thz, Fact *super_goal, bool opposite, Controller *root, Controller *sol, double sol_cfd, uint64_t sol_deadline); // USE for SIM_MANDATORY or SIM_OPTIONAL.
154 
155  void invalidate();
156  bool is_invalidated();
157 
159 
160  bool opposite; // of the goal the sim is attached to, i.e. the result of the match during controller->reduce(); the confidence is in the goal target.
161 
162  SimMode mode; // if SIM_MANDATORY or SIM_OPTIONAL: qualifies a sub-goal of the branch's root.
163  uint64_t thz; // simulation time allowance (this is not the goal deadline); 0 indicates no time for simulation.
164  P<Fact> super_goal; // of the goal the sim is attached to.
165  P<Controller> root; // controller that produced the simulation branch root (SIM_ROOT): identifies the branch.
166  P<Controller> sol; // controller that produced a sub-goal of the branch's root: identifies the model that can be a solution for the super-goal.
167  double sol_cfd; // confidence of the solution goal.
168  uint64_t sol_before; // deadline of the solution goal.
169 };
170 
171 // Caveat: instances of Fact can becone instances of AntiFact (set_opposite() upon MATCH_SUCCESS_NEGATIVE during backward chaining).
172 // In particular, is_fact() and is_anti_fact() are based on the opcode, not on the class.
173 // Do not store any data in this class.
175  public _Fact {
176 public:
177  void *operator new(size_t s);
178  Fact();
179  Fact(SysObject *source);
180  Fact(Fact *f);
181  Fact(Code *object, uint64_t after, uint64_t before, double confidence, double psln_thr);
182 };
183 
184 // Caveat: as for Fact.
186  public _Fact {
187 public:
188  void *operator new(size_t s);
189  AntiFact();
190  AntiFact(SysObject *source);
191  AntiFact(AntiFact *f);
192  AntiFact(Code *object, uint64_t after, uint64_t before, double confidence, double psln_thr);
193 };
194 
195 // Goals and predictions:
196 // When positive evidences are found for a goal/prediction, said object is invalidated: in g-monitors and p-monitors, respectively.
197 // Idem for negative evidences In such cases, an absentee (absence of the expected fact) is injected.
198 //
199 // A negative evidence is either:
200 // (a) a fact that is either the absence of the target (absentee) or a fact asserting a state for the target which is not the expected one (it is assumed that an object can be in only one state WRT a given attribute; and recurse in icst) or,
201 // (b) a prediction holding a fact that is a counter evidence for the target with a confidence higher than said target.
202 //
203 // When a decision ground is invalidated, the subsequent is also invalidated: said invalidation is performed in the p-monitors and g-monitors.
204 // When a super-goal is invalidated (simulated or actual), sub-goals are also invalidated: said invalidation is performed in the g-monitors of the sub-goals.
205 //
206 // Invalidation checks are performed at both _take_input() time and reduce() time as the invalidation may occur during the transit in the pipe.
207 
209  public LObject {
210 public:
211  Pred();
212  Pred(SysObject *source);
213  Pred(_Fact *target, double psln_thr);
214 
215  bool is_invalidated();
216  bool grounds_invalidated(_Fact *evidence);
217 
218  _Fact *get_target() const;
219 
220  std::vector<P<_Fact> > grounds; // f1->obj; predictions that were used to build this predictions (i.e. antecedents); empty if simulated.
221  std::vector<P<Sim> > simulations;
222 
223  bool is_simulation() const;
224  Sim *get_simulation(Controller *root) const; // return true if there is a simulation for the goal.
225 };
226 
228  public LObject {
229 public:
230  Goal();
231  Goal(SysObject *source);
232  Goal(_Fact *target, Code *actor, double psln_thr);
233 
234  bool invalidate();
235  bool is_invalidated();
236  bool ground_invalidated(_Fact *evidence);
237 
238  bool is_requirement() const;
239 
240  bool is_self_goal() const;
241  bool is_drive() const;
242 
243  _Fact *get_target() const;
244  _Fact *get_super_goal() const;
245  Code *get_actor() const;
246 
248  P<_Fact> ground; // f->p->f->imdl (weak requirement) that allowed backward chaining, if any.
249 
250  double get_strength(uint64_t now) const; // goal->target->cfd/(before-now).
251 };
252 
254  public LObject {
255 public:
256  MkRdx();
257  MkRdx(SysObject *source);
258  MkRdx(Code *imdl_fact, Code *input, Code *output, double psln_thr, BindingMap *binding_map); // for mdl.
259 
260  P<BindingMap> bindings; // NULL when produced by programs.
261 };
262 
264  public LObject {
265 public:
266  Success();
267  Success(_Fact *object, _Fact *evidence, double psln_thr);
268 };
269 
271  public LObject {
272 public:
273  Perf();
274  Perf(uint64_t reduction_job_avg_latency, int64_t d_reduction_job_avg_latency, uint64_t time_job_avg_latency, int64_t d_time_job_avg_latency);
275 };
276 
278  public LObject {
279 public:
280  ICST();
281  ICST(SysObject *source);
282 
283  bool is_invalidated();
284 
285  bool contains(_Fact *component, uint16_t &component_index) const;
286 
288  std::vector<P<_Fact> > components; // the inputs that triggered the building of the icst.
289 };
290 }
291 
292 
293 #endif
Definition: factory.h:208
Definition: factory.h:98
P< BindingMap > bindings
Definition: factory.h:287
Definition: factory.h:141
std::vector< P< _Fact > > components
Definition: factory.h:288
Definition: factory.h:145
Definition: base.h:75
bool is_requirement
Definition: factory.h:158
Definition: binding_map.h:177
P< Fact > super_goal
Definition: factory.h:164
Definition: factory.h:253
bool now(const Context &context, uint16_t &index)
Definition: operator.cpp:55
uint64_t volatile invalidated
Definition: factory.h:148
uint64_t thz
Definition: factory.h:163
bool opposite
Definition: factory.h:160
#define dll_export
Definition: dll.h:44
Definition: object.h:328
std::vector< P< Sim > > simulations
Definition: factory.h:221
Definition: factory.h:142
Definition: _context.cpp:34
std::vector< P< _Fact > > grounds
Definition: factory.h:220
double sol_cfd
Definition: factory.h:167
MatchResult
Definition: binding_map.h:168
Definition: base.h:47
Definition: factory.h:71
uint64_t sol_before
Definition: factory.h:168
Definition: factory.h:77
P< Controller > sol
Definition: factory.h:166
Definition: factory.h:83
Definition: factory.h:185
Definition: factory.h:59
SimMode
Definition: factory.h:139
SimMode mode
Definition: factory.h:162
Definition: atom.h:45
Definition: factory.h:140
P< BindingMap > bindings
Definition: factory.h:260
Definition: object.h:76
Definition: factory.h:277
Definition: factory.h:53
Definition: object.h:172
Definition: overlay.h:50
Definition: factory.h:227
Definition: object.h:133
P< Controller > root
Definition: factory.h:165
Definition: factory.h:65
Definition: factory.h:263
P< _Fact > ground
Definition: factory.h:248
Definition: factory.h:174
Definition: factory.h:89
Definition: factory.h:47
Definition: factory.h:270
P< Sim > sim
Definition: factory.h:247