replicode
cst_controller.h
Go to the documentation of this file.
1 // cst_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 cst_controller_h
32 #define cst_controller_h
33 
34 #include "hlp_overlay.h"
35 #include "hlp_controller.h"
36 #include "factory.h"
37 
38 
39 namespace r_exec {
40 
41 // All inputs are expected to be synchronized (within the time tolerance).
42 // The time value of each fact is therefore the same (within the time tolerance).
43 // Therefore, said time value is not listed in the argument list of the icst.
44 // It is held by the fact holding said icst.
45 // Inputs synchronized on state are treated as if they were produced continuously, i.e. Now().
46 // The confidence value for an icst is the lowest value taken from the matched inputs.
47 // No cmds or imdls in a cst.
48 //
49 // Forward chaining:
50 // output a prediction of icst instead of icst if at least one input is a prediction.
51 // output as many predictions of icst as we got simulated predictions for different goals.
52 class CSTOverlay:
53  public HLPOverlay {
54 protected:
55  uint64_t match_deadline; // before deadline after the last match.
56  double lowest_cfd; // among the inputs (forward chaining).
57 
58  std::vector<P<_Fact> > inputs;
59 
60  std::unordered_set<P<_Fact>, PHash<_Fact> > predictions; // f0->pred->f1->obj.
61  std::unordered_set<P<Sim>, PHash<Sim> > simulations;
62 
63  void inject_production();
64  void update(HLPBindingMap *map, _Fact *input, _Fact *bound_pattern);
65  CSTOverlay *get_offspring(HLPBindingMap *map, _Fact *input, _Fact *bound_pattern);
66 
67  CSTOverlay(const CSTOverlay *original);
68 public:
70  ~CSTOverlay();
71 
72  bool reduce(View *input, CSTOverlay *&offspring);
73 
74  void load_patterns();
75 
76  bool can_match(uint64_t now) const;
77 };
78 
79 // Backward chaining:
80 // if there are requirements, do nothing: these requirements will get the goal and abduce.
81 // else
82 // bind all patterns and look in the cache for positive evidences; for all bound patterns not matched in the cache, output a sub-goal (simulated or not, depending on the super-goal).
84  public HLPController {
85 private:
87  std::mutex m_mutex;
88 
89  void abduce(HLPBindingMap *bm, Fact *super_goal); // super_goal is f->g->f->icst.
90  void inject_goal(HLPBindingMap *bm,
91  Fact *super_goal, // f0->g->f1->icst.
92  _Fact *sub_goal_target, // f1.
93  Sim *sim,
94  uint64_t now,
95  double confidence,
96  Code *group) const;
97 
98  void kill_views();
99  void check_last_match_time(bool match); // kill if no match after primary_thz;
100 public:
102  ~CSTController();
103 
104  void take_input(r_exec::View *input);
105  void reduce(r_exec::View *input);
106 
107  Fact *get_f_ihlp(HLPBindingMap *bindings, bool wr_enabled) const;
108  Fact *get_f_icst(HLPBindingMap *bindings, std::vector<P<_Fact> > *inputs) const;
109 
110  void inject_icst(Fact *production, double confidence, uint64_t time_to_live) const; // here, resilience=time to live, in us.
111  bool inject_prediction(Fact *prediction, double confidence, uint64_t time_to_live) const; // here, resilience=time to live, in us; returns true if the prediction has actually been injected.
112 
113  void set_secondary_host(Group *host);
114  Group *get_secondary_host() const;
115 };
116 }
117 
118 
119 #endif
P< HLPBindingMap > bindings
Definition: hlp_overlay.h:47
Definition: hlp_controller.h:51
Definition: factory.h:98
void inject_goal(HLPBindingMap *bm, Fact *super_goal, _Fact *sub_goal_target, Sim *sim, uint64_t now, double confidence, Code *group) const
Definition: cst_controller.cpp:386
Definition: factory.h:145
Group * get_secondary_host() const
Definition: cst_controller.cpp:477
double lowest_cfd
Definition: cst_controller.h:56
void update(HLPBindingMap *map, _Fact *input, _Fact *bound_pattern)
Definition: cst_controller.cpp:130
void set_secondary_host(Group *host)
Definition: cst_controller.cpp:472
Definition: hlp_overlay.h:43
bool now(const Context &context, uint16_t &index)
Definition: operator.cpp:55
~CSTOverlay()
Definition: cst_controller.cpp:52
std::mutex m_mutex
Definition: cst_controller.h:87
std::vector< P< _Fact > > inputs
Definition: cst_controller.h:58
Fact * get_f_icst(HLPBindingMap *bindings, std::vector< P< _Fact > > *inputs) const
Definition: cst_controller.cpp:423
~CSTController()
Definition: cst_controller.cpp:279
Definition: _context.cpp:34
Fact * get_f_ihlp(HLPBindingMap *bindings, bool wr_enabled) const
Definition: cst_controller.cpp:418
CSTController(r_code::View *view)
Definition: cst_controller.cpp:253
void inject_production()
Definition: cst_controller.cpp:74
void take_input(r_exec::View *input)
Definition: cst_controller.cpp:282
Definition: base.h:47
Definition: group.h:49
Definition: binding_map.h:252
void load_patterns()
Definition: cst_controller.cpp:55
bool match(const IPGMContext &input, const IPGMContext &pattern)
Definition: context.cpp:452
Definition: utils.h:43
Definition: view.h:47
std::unordered_set< P< _Fact >, PHash< _Fact > > predictions
Definition: cst_controller.h:60
void abduce(HLPBindingMap *bm, Fact *super_goal)
Definition: cst_controller.cpp:346
void reduce(r_exec::View *input)
Definition: cst_controller.cpp:295
void kill_views()
Definition: cst_controller.cpp:482
Group * secondary_host
Definition: cst_controller.h:86
bool inject_prediction(Fact *prediction, double confidence, uint64_t time_to_live) const
Definition: cst_controller.cpp:431
Definition: cst_controller.h:52
r_code::View * view
Definition: overlay.h:58
std::unordered_set< P< Sim >, PHash< Sim > > simulations
Definition: cst_controller.h:61
bool can_match(uint64_t now) const
Definition: cst_controller.cpp:67
CSTOverlay * get_offspring(HLPBindingMap *map, _Fact *input, _Fact *bound_pattern)
Definition: cst_controller.cpp:119
Definition: object.h:100
bool reduce(View *input, CSTOverlay *&offspring)
Definition: cst_controller.cpp:152
uint64_t match_deadline
Definition: cst_controller.h:55
void check_last_match_time(bool match)
Definition: cst_controller.cpp:488
Definition: object.h:172
Definition: overlay.h:50
P< HLPBindingMap > bindings
Definition: hlp_controller.h:108
void inject_icst(Fact *production, double confidence, uint64_t time_to_live) const
Definition: cst_controller.cpp:446
Definition: factory.h:174
Definition: cst_controller.h:83
CSTOverlay(const CSTOverlay *original)
Definition: cst_controller.cpp:43