replicode
hlp_controller.h
Go to the documentation of this file.
1 // hlp_controller.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 hlp_controller_h
32 #define hlp_controller_h
33 
34 #include "overlay.h"
35 #include "binding_map.h"
36 #include "g_monitor.h"
37 #include "group.h"
38 #include "init.h"
39 
40 
41 namespace r_exec {
42 
43 typedef enum {
48  NO_R = 4
50 
52  public OController {
53 private:
54  uint64_t strong_requirement_count; // number of active strong requirements in the same group; updated dynamically.
55  uint64_t weak_requirement_count; // number of active weak requirements in the same group; updated dynamically.
56  uint64_t requirement_count; // sum of the two above.
57 protected:
58  class EEntry { // evidences.
59  private:
60  void load_data(_Fact *evidence);
61  public:
65  double confidence;
66 
67  EEntry();
68  EEntry(_Fact *evidence);
69  EEntry(_Fact *evidence, _Fact *payload);
70 
71  bool is_too_old(uint64_t now) const {
72  return (evidence->is_invalidated() || before < now);
73  }
74  };
75 
76  class PEEntry: // predicted evidences.
77  public EEntry {
78  public:
79  PEEntry();
81  };
82 
83  template<class E> class Cache {
84  public:
85  std::mutex mutex;
87  };
88 
91 
92  template<class E> void _store_evidence(Cache<E> *cache, _Fact *evidence) {
93 
94  E e(evidence);
95  std::lock_guard<std::mutex> guard(cache->mutex);
98  for (_e = cache->evidences.begin(); _e != cache->evidences.end();) {
99 
100  if ((*_e).is_too_old(now)) // garbage collection.
101  _e = cache->evidences.erase(_e);
102  else
103  ++_e;
104  }
105  cache->evidences.push_front(e);
106  }
107 
109 
111 
112  MatchResult check_evidences(_Fact *target, _Fact *&evidence); // evidence with the match (positive or negative), get_absentee(target) otherwise.
113  MatchResult check_predicted_evidences(_Fact *target, _Fact *&evidence); // evidence with the match (positive or negative), NULL otherwise.
114 
116  uint64_t ref_count; // used to detect _Object::refCount dropping down to 1 for hlp with tpl args.
117  bool is_orphan(); // true when there are tpl args and no requirements: the controller cannot execute anymore.
118 
119  std::vector<P<HLPController> > controllers; // all controllers for models/states instantiated in the patterns; case of models: [0]==lhs, [1]==rhs.
120  uint64_t last_match_time; // last time a match occurred (fwd), regardless of its outcome.
121  bool become_invalidated(); // true if one controller is invalidated or if all controllers pointing to this are invalidated.
122  virtual void kill_views() {}
123  virtual void check_last_match_time(bool match) = 0;
124 
126 public:
127  virtual ~HLPController();
128 
129  void invalidate();
130 
132  return getObject(); // cst or mdl.
133  }
134  Code *get_unpacked_object() const { // the unpacked version of the core object.
135 
136  Code *core_object = get_core_object();
137  return core_object->get_reference(core_object->references_size() - MDL_HIDDEN_REFS);
138  }
139 
140  void add_requirement(bool strong);
141  void remove_requirement(bool strong);
142 
143  uint64_t get_requirement_count(uint64_t &weak_requirement_count, uint64_t &strong_requirement_count);
145 
146  void store_evidence(_Fact *evidence) {
147  _store_evidence<EEntry>(&evidences, evidence);
148  }
149  void store_predicted_evidence(_Fact *evidence) {
150  _store_evidence <PEEntry>(&predicted_evidences, evidence);
151  }
152 
153  virtual Fact *get_f_ihlp(HLPBindingMap *bindings, bool wr_enabled) const = 0;
154 
155  uint16_t get_out_group_count() const;
156  Code *get_out_group(uint16_t i) const; // i starts at 1.
157  Group *get_host() const;
158  bool has_tpl_args() const {
159  return _has_tpl_args;
160  }
161 
162  void inject_prediction(Fact *prediction, double confidence) const; // for simulated predictions.
163 };
164 }
165 
166 
167 #endif
const_iterator & end() const
Definition: list.h:288
Definition: hlp_controller.h:51
Definition: factory.h:98
ChainingStatus
Definition: hlp_controller.h:43
Code * get_unpacked_object() const
Definition: hlp_controller.h:134
MatchResult check_predicted_evidences(_Fact *target, _Fact *&evidence)
Definition: hlp_controller.cpp:151
uint64_t weak_requirement_count
Definition: hlp_controller.h:55
r_code::list< E > evidences
Definition: hlp_controller.h:86
virtual Fact * get_f_ihlp(HLPBindingMap *bindings, bool wr_enabled) const =0
void load_data(_Fact *evidence)
Definition: hlp_controller.cpp:229
bool become_invalidated()
Definition: hlp_controller.cpp:174
Code * get_out_group(uint16_t i) const
Definition: hlp_controller.cpp:102
Definition: hlp_controller.h:48
bool now(const Context &context, uint16_t &index)
Definition: operator.cpp:55
void invalidate()
Definition: hlp_controller.cpp:53
virtual Code * get_reference(uint16_t i) const =0
Definition: hlp_controller.h:58
Definition: hlp_controller.h:45
bool is_too_old(uint64_t now) const
Definition: hlp_controller.h:71
std::vector< P< HLPController > > controllers
Definition: hlp_controller.h:119
void add_requirement(bool strong)
Definition: hlp_controller.cpp:59
EEntry()
Definition: hlp_controller.cpp:216
Definition: hlp_controller.h:76
Definition: _context.cpp:34
uint64_t before
Definition: hlp_controller.h:64
void push_front(const T &t)
Definition: list.h:179
MatchResult
Definition: binding_map.h:168
Definition: hlp_controller.h:46
uint16_t get_out_group_count() const
Definition: hlp_controller.cpp:97
double confidence
Definition: hlp_controller.h:65
void remove_requirement(bool strong)
Definition: hlp_controller.cpp:69
virtual void check_last_match_time(bool match)=0
MatchResult check_evidences(_Fact *target, _Fact *&evidence)
Definition: hlp_controller.cpp:130
Definition: base.h:47
Definition: group.h:49
Definition: binding_map.h:252
void inject_prediction(Fact *prediction, double confidence) const
Definition: hlp_controller.cpp:119
bool match(const IPGMContext &input, const IPGMContext &pattern)
Definition: context.cpp:452
iterator begin()
Definition: list.h:282
Definition: overlay.h:172
void _store_evidence(Cache< E > *cache, _Fact *evidence)
Definition: hlp_controller.h:92
P< _Fact > evidence
Definition: hlp_controller.h:62
HLPController(r_code::View *view)
Definition: hlp_controller.cpp:38
uint64_t after
Definition: hlp_controller.h:63
Group * get_host() const
Definition: hlp_controller.cpp:114
Code * get_core_object() const
Definition: hlp_controller.h:131
r_code::View * view
Definition: overlay.h:58
Definition: hlp_controller.h:47
uint64_t get_requirement_count()
Definition: hlp_controller.cpp:89
Definition: list.h:216
#define MDL_HIDDEN_REFS
Definition: replicode_defs.h:165
virtual uint16_t references_size() const =0
dll_export uint64_t(* Now)()
Definition: init.cpp:49
Definition: hlp_controller.h:44
Definition: object.h:100
iterator erase(iterator &i)
Definition: list.h:291
uint64_t ref_count
Definition: hlp_controller.h:116
uint64_t last_match_time
Definition: hlp_controller.h:120
bool has_tpl_args() const
Definition: hlp_controller.h:158
Definition: object.h:172
virtual void kill_views()
Definition: hlp_controller.h:122
virtual ~HLPController()
Definition: hlp_controller.cpp:50
uint64_t requirement_count
Definition: hlp_controller.h:56
Cache< EEntry > evidences
Definition: hlp_controller.h:89
Definition: hlp_controller.h:83
P< HLPBindingMap > bindings
Definition: hlp_controller.h:108
PEEntry()
Definition: hlp_controller.cpp:238
bool _has_tpl_args
Definition: hlp_controller.h:115
bool evaluate_bwd_guards(HLPBindingMap *bm)
Definition: hlp_controller.cpp:109
void store_evidence(_Fact *evidence)
Definition: hlp_controller.h:146
Definition: factory.h:174
bool is_orphan()
Definition: hlp_controller.cpp:201
void store_predicted_evidence(_Fact *evidence)
Definition: hlp_controller.h:149
uint64_t strong_requirement_count
Definition: hlp_controller.h:54
Cache< PEEntry > predicted_evidences
Definition: hlp_controller.h:90
std::mutex mutex
Definition: hlp_controller.h:85
r_code::Code * getObject() const
Definition: overlay.h:91