replicode
pattern_extractor.h
Go to the documentation of this file.
1 // pattern_extractor.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 pattern_extractor_h
32 #define pattern_extractor_h
33 
34 #include "../r_code/time_buffer.h"
35 
36 #include "binding_map.h"
37 #include "guard_builder.h"
38 #include "cst_controller.h"
39 
40 
41 namespace r_exec {
42 
43 class AutoFocusController;
44 
45 class Input {
46 public:
47  P<BindingMap> bindings; // contains the values for the abstraction.
51  uint64_t ijt; // injection time.
52 
53  Input(View *input, _Fact *abstraction, BindingMap *bindings): bindings(bindings), abstraction(abstraction), input(input->object), eligible_cause(IsEligibleCause(input)), ijt(input->get_ijt()) {}
54  Input(): bindings(NULL), abstraction(NULL), input(NULL), eligible_cause(false), ijt(0) {}
55  Input(const Input &original): bindings(original.bindings), abstraction(original.abstraction), input(original.input), eligible_cause(original.eligible_cause), ijt(original.ijt) {}
56 
57  static bool IsEligibleCause(r_exec::View *view);
58 
59  class IsInvalidated { // for storage in time_buffers.
60  public:
61  bool operator()(Input &i, uint64_t time_reference, uint64_t thz) const {
62 
63  return (time_reference - i.ijt > thz);
64  }
65  };
66 };
67 
68 class CInput { // cached inputs.
69 public:
70  P<BindingMap> bindings; // contains the values for the abstraction.
73  bool injected;
74  uint64_t ijt; // injection time.
75  CInput(View *input, _Fact *abstraction, BindingMap *bindings): bindings(bindings), abstraction(abstraction), input(input), injected(false), ijt(input->get_ijt()) {}
76  CInput(): bindings(NULL), abstraction(NULL), input(NULL), injected(false), ijt(0) {}
77 
78  bool operator ==(const CInput &i) const {
79  return input == i.input;
80  }
81 
82  class IsInvalidated { // for storage in time_buffers.
83  public:
84  bool operator()(CInput &i, uint64_t time_reference, uint64_t thz) const {
85 
86  return (time_reference - i.ijt > thz);
87  }
88  };
89 };
90 
91 // Targeted Pattern eXtractor.
92 // Does nothing.
93 // Used for wr_enabled productions, for well-rated productions or when model acqusiiton is disabled.
95  public _Object {
96 protected:
98  P<_Fact> target; // goal or prediction target, or premise (CTPX); abstraction: lhs of a mdl for goals, rhs for predictions, premise for CTPX.
101 
102  P<CSTController> cst_hook; // in case the target is an icst.
103 
104  std::vector<P<BindingMap> > new_maps; // acquired (in the case the target's bm is not fully specified) while matching the target's bm with inputs.
105 
106  bool filter(View *input, _Fact *abstracted_input, BindingMap *bm);
107 
108  TPX(AutoFocusController *auto_focus, _Fact *target);
109 public:
110  TPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings);
111  virtual ~TPX();
112 
113  _Fact *get_pattern() const {
114  return abstracted_target;
115  }
117  return target_bindings;
118  }
119 
120  virtual bool take_input(View *view, _Fact *abstracted_input, BindingMap *bm);
121  virtual void signal(View *input) const;
122  virtual void ack_pred_success(_Fact *predicted_f);
123 };
124 
125 class ICST;
126 
128  public TPX {
129 private:
130  static const uint64_t InputsInitialSize = 16;
131 protected:
132  class Component { // for building csts.
133  public:
135  bool discarded;
137  Component(_Fact *object): object(object), discarded(false) {}
138  };
139 
140  r_code::list<Input> inputs; // time-controlled buffer (inputs older than tpx_time_horizon from now are discarded).
141  std::vector<P<Code> > mdls; // new mdls.
142  std::vector<P<Code> > csts; // new csts.
143  std::vector<P<_Fact> > icsts; // new icsts.
144 
145  void filter_icst_components(ICST *icst, uint64_t icst_index, std::vector<Component> &components);
146  _Fact *_find_f_icst(_Fact *component, uint16_t &component_index);
147  _Fact *find_f_icst(_Fact *component, uint16_t &component_index);
148  _Fact *find_f_icst(_Fact *component, uint16_t &component_index, Code *&cst);
149  Code *build_cst(const std::vector<Component> &components, BindingMap *bm, _Fact *main_component);
150 
151  Code *build_mdl_head(HLPBindingMap *bm, uint16_t tpl_arg_count, _Fact *lhs, _Fact *rhs, uint16_t &write_index);
152  void build_mdl_tail(Code *mdl, uint16_t write_index);
153 
154  void inject_hlps() const;
155  void inject_hlps(uint64_t analysis_starting_time);
156 
157  virtual std::string get_header() const = 0;
158 
159  _TPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings);
160  _TPX(AutoFocusController *auto_focus, _Fact *target);
161 public:
162  virtual ~_TPX();
163 
164  void debug(View *input) {};
165 };
166 
167 // Pattern extractor targeted at goal successes.
168 // Possible causes are younger than the production of the goal.
169 // Models produced are of the form: M1[cause -> goal_target], where cause can be an imdl and goal_target can be an imdl.
170 // M1 does not have template arguments.
171 // Commands are ignored (CTPX' job).
172 class dll_export GTPX: // target is the goal target.
173  public _TPX {
174 private:
175  P<Fact> f_imdl; // that produced the goal.
176 
177  std::vector<P<_Fact> > predictions; // successful predictions that may invalidate the need for model building.
178 
179  bool build_mdl(_Fact *cause, _Fact *consequent, GuardBuilder *guard_builder, uint64_t period);
180  bool build_mdl(_Fact *f_icst, _Fact *cause_pattern, _Fact *consequent, GuardBuilder *guard_builder, uint64_t period, Code *new_cst);
181 
182  std::string get_header() const;
183 public:
184  GTPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings, Fact *f_imdl);
185  ~GTPX();
186 
187  bool take_input(View *input, _Fact *abstracted_input, BindingMap *bm);
188  void signal(View *input) const;
189  void ack_pred_success(_Fact *predicted_f);
190  void reduce(View *input); // input is v->f->success(target,input) or v->|f->success(target,input).
191 };
192 
193 // Pattern extractor targeted at prediciton failures.
194 // Possible causes are older than the production of the prediction.
195 // Models produced are of the form: M1[cause -> |imdl M0] where M0 is the model that produced the failed prediction and cause can be an imdl.
196 // M1 does not have template arguments.
197 // Commands are ignored (CTPX' job).
198 class dll_export PTPX: // target is the prediction.
199  public _TPX {
200 private:
201  P<Fact> f_imdl; // that produced the prediction (and for which the PTPX will find strong requirements).
202 
203  bool build_mdl(_Fact *cause, _Fact *consequent, GuardBuilder *guard_builder, uint64_t period);
204  bool build_mdl(_Fact *f_icst, _Fact *cause_pattern, _Fact *consequent, GuardBuilder *guard_builder, uint64_t period, Code *new_cst);
205 
206  std::string get_header() const;
207 public:
208  PTPX(AutoFocusController *auto_focus, _Fact *target, _Fact *pattern, BindingMap *bindings, Fact *f_imdl);
209  ~PTPX();
210 
211  void signal(View *input) const;
212  void reduce(View *input); // input is v->f->success(target,input) or v->|f->success(target,input).
213 };
214 
215 // Pattern extractor targeted at changes of repeated input facts (SYMC_PERIODIC or SYNC_HOLD).
216 // Models produced are of the form: [premise -> [cause -> consequent]], i.e. M1:[premise -> imdl M0], M0:[cause -> consequent].
217 // M0 has template args, i.e the value of the premise and its after timestamp.
218 // N.B.: before-after=upr of the group the input comes from.
219 // The Consequent is a value different from the expected repetition of premise.
220 // The premise is an icst assembled from inputs synchronous with the input expected to repeat.
221 // Guards on values (not only on timings) are computed: this is the only TPX that does so.
222 // Inputs with SYNC_HOLD: I/O devices are expected to send changes on such inputs as soon as possible.
223 class CTPX:
224  public _TPX {
225 private:
228 
229  GuardBuilder *get_default_guard_builder(_Fact *cause, _Fact *consequent, uint64_t period);
230  GuardBuilder *find_guard_builder(_Fact *cause, _Fact *consequent, uint64_t period);
231 
232  bool build_mdl(_Fact *cause, _Fact *consequent, GuardBuilder *guard_builder, uint64_t period);
233  bool build_mdl(_Fact *f_icst, _Fact *cause_pattern, _Fact *consequent, GuardBuilder *guard_builder, uint64_t period);
234 
235  bool build_requirement(HLPBindingMap *bm, Code *m0, uint64_t period);
236 
237  std::string get_header() const;
238 public:
240  ~CTPX();
241 
242  void store_input(r_exec::View *input);
243  void reduce(r_exec::View *input); // asynchronous: build models of value change if not aborted asynchronously by ASTControllers.
244  void signal(r_exec::View *input); // spawns mdl/cst building (reduce()).
245 };
246 }
247 
248 
249 #endif
CInput(View *input, _Fact *abstraction, BindingMap *bindings)
Definition: pattern_extractor.h:75
BindingMap * get_bindings() const
Definition: pattern_extractor.h:116
bool build_mdl(_Fact *cause, _Fact *consequent, GuardBuilder *guard_builder, uint64_t period)
Definition: pattern_extractor.cpp:890
Component(_Fact *object)
Definition: pattern_extractor.h:137
Definition: factory.h:98
Component()
Definition: pattern_extractor.h:136
Definition: auto_focus.h:44
P< _Fact > abstraction
Definition: pattern_extractor.h:71
bool injected
Definition: pattern_extractor.h:73
std::vector< P< BindingMap > > new_maps
Definition: pattern_extractor.h:104
Definition: base.h:75
Definition: binding_map.h:177
Definition: guard_builder.h:39
Definition: pattern_extractor.h:82
static bool IsEligibleCause(r_exec::View *view)
Definition: pattern_extractor.cpp:40
P< Fact > f_imdl
Definition: pattern_extractor.h:201
CTPX(AutoFocusController *auto_focus, View *premise)
Definition: pattern_extractor.cpp:712
P< BindingMap > bindings
Definition: pattern_extractor.h:70
_Fact * object
Definition: pattern_extractor.h:134
#define dll_export
Definition: dll.h:44
Definition: pattern_extractor.h:94
bool operator()(Input &i, uint64_t time_reference, uint64_t thz) const
Definition: pattern_extractor.h:61
~CTPX()
Definition: pattern_extractor.cpp:715
GuardBuilder * get_default_guard_builder(_Fact *cause, _Fact *consequent, uint64_t period)
Definition: pattern_extractor.cpp:805
bool discarded
Definition: pattern_extractor.h:135
std::string get_header() const
Definition: pattern_extractor.cpp:971
Definition: _context.cpp:34
Definition: pattern_extractor.h:198
P< CSTController > cst_hook
Definition: pattern_extractor.h:102
bool build_requirement(HLPBindingMap *bm, Code *m0, uint64_t period)
Definition: pattern_extractor.cpp:922
Definition: pattern_extractor.h:59
Definition: pattern_extractor.h:68
P< Fact > f_imdl
Definition: pattern_extractor.h:175
Definition: pattern_extractor.h:127
std::vector< P< _Fact > > icsts
Definition: pattern_extractor.h:143
std::vector< P< Code > > csts
Definition: pattern_extractor.h:142
Definition: base.h:47
_Fact * get_pattern() const
Definition: pattern_extractor.h:113
Definition: binding_map.h:252
Definition: pattern_extractor.h:223
Input(const Input &original)
Definition: pattern_extractor.h:55
void debug(View *input)
Definition: pattern_extractor.h:164
Definition: pattern_extractor.h:132
bool operator()(CInput &i, uint64_t time_reference, uint64_t thz) const
Definition: pattern_extractor.h:84
P< _Fact > input
Definition: pattern_extractor.h:49
P< View > premise
Definition: pattern_extractor.h:227
P< BindingMap > bindings
Definition: pattern_extractor.h:47
Definition: view.h:47
r_code::list< Input > inputs
Definition: pattern_extractor.h:140
Definition: pattern_extractor.h:172
CInput()
Definition: pattern_extractor.h:76
P< View > input
Definition: pattern_extractor.h:72
bool operator==(const CInput &i) const
Definition: pattern_extractor.h:78
Definition: pattern_extractor.h:45
bool eligible_cause
Definition: pattern_extractor.h:50
P< _Fact > abstracted_target
Definition: pattern_extractor.h:100
void signal(r_exec::View *input)
Definition: pattern_extractor.cpp:729
void store_input(r_exec::View *input)
Definition: pattern_extractor.cpp:718
uint64_t ijt
Definition: pattern_extractor.h:51
P< BindingMap > target_bindings
Definition: pattern_extractor.h:99
P< _Fact > target
Definition: pattern_extractor.h:98
Definition: factory.h:277
Input()
Definition: pattern_extractor.h:54
std::vector< P< Code > > mdls
Definition: pattern_extractor.h:141
void reduce(r_exec::View *input)
Definition: pattern_extractor.cpp:736
Definition: list.h:42
Definition: object.h:172
uint64_t ijt
Definition: pattern_extractor.h:74
void reduce(const IPGMContext &context, const IPGMContext &input_set, const IPGMContext &section, std::vector< uint16_t > &input_indices, std::vector< uint16_t > &production_indices)
Definition: context.cpp:523
std::vector< P< _Fact > > predictions
Definition: pattern_extractor.h:177
P< _Fact > abstraction
Definition: pattern_extractor.h:48
Definition: factory.h:174
AutoFocusController * auto_focus
Definition: pattern_extractor.h:97
GuardBuilder * find_guard_builder(_Fact *cause, _Fact *consequent, uint64_t period)
Definition: pattern_extractor.cpp:823
bool stored_premise
Definition: pattern_extractor.h:226
Input(View *input, _Fact *abstraction, BindingMap *bindings)
Definition: pattern_extractor.h:53