replicode
binding_map.h
Go to the documentation of this file.
1 // binding_map.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 binding_map_h
32 #define binding_map_h
33 
34 #include "object.h"
35 #include "CoreLibrary/dll.h"
36 
37 
38 namespace r_exec {
39 
40 class BindingMap;
41 class AtomValue;
42 class StructureValue;
43 class ObjectValue;
44 
46  public _Object {
47 protected:
49  Value(BindingMap *map);
50 public:
51  virtual Value *copy(BindingMap *map) const = 0;
52  virtual void valuate(Code *destination, uint16_t write_index, uint16_t &extent_index) const = 0;
53  virtual bool match(const Code *object, uint16_t index) = 0;
54  virtual Atom *get_code() = 0;
55  virtual Code *get_object() = 0;
56  virtual uint16_t get_code_size() = 0;
57 
58  virtual bool intersect(const Value *v) const {
59  return false;
60  }
61  virtual bool _intersect(const AtomValue *v) const {
62  return false;
63  }
64  virtual bool _intersect(const StructureValue *v) const {
65  return false;
66  }
67  virtual bool _intersect(const ObjectValue *v) const {
68  return false;
69  }
70 
71  virtual bool contains(const Atom a) const {
72  return false;
73  }
74  virtual bool contains(const Atom *s) const {
75  return false;
76  }
77  virtual bool contains(const Code *o) const {
78  return false;
79  }
80 };
81 
83  public Value {
84 protected:
85  BoundValue(BindingMap *map);
86 public:
87 };
88 
90  public Value {
91 private:
92  uint8_t index;
93 public:
94  UnboundValue(BindingMap *map, uint8_t index);
95  ~UnboundValue();
96 
97  Value *copy(BindingMap *map) const;
98  void valuate(Code *destination, uint16_t write_index, uint16_t &extent_index) const;
99  bool match(const Code *object, uint16_t index);
100  Atom *get_code();
101  Code *get_object();
102  uint16_t get_code_size();
103 };
104 
106  public BoundValue {
107 private:
109 public:
110  AtomValue(BindingMap *map, Atom atom);
111 
112  Value *copy(BindingMap *map) const;
113  void valuate(Code *destination, uint16_t write_index, uint16_t &extent_index) const;
114  bool match(const Code *object, uint16_t index);
115  Atom *get_code();
116  Code *get_object();
117  uint16_t get_code_size();
118 
119  bool intersect(const Value *v) const;
120  bool _intersect(const AtomValue *v) const;
121 
122  bool contains(const Atom a) const;
123 };
124 
126  public BoundValue {
127 private:
129  StructureValue(BindingMap *map, const Code *structure);
130 public:
131  StructureValue(BindingMap *map, const Code *source, uint16_t structure_index);
132  StructureValue(BindingMap *map, Atom *source, uint16_t structure_index);
133  StructureValue(BindingMap *map, uint64_t time);
134 
135  Value *copy(BindingMap *map) const;
136  void valuate(Code *destination, uint16_t write_index, uint16_t &extent_index) const;
137  bool match(const Code *object, uint16_t index);
138  Atom *get_code();
139  Code *get_object();
140  uint16_t get_code_size();
141 
142  bool intersect(const Value *v) const;
143  bool _intersect(const StructureValue *v) const;
144 
145  bool contains(const Atom *s) const;
146 };
147 
149  public BoundValue {
150 private:
152 public:
153  ObjectValue(BindingMap *map, Code *object);
154 
155  Value *copy(BindingMap *map) const;
156  void valuate(Code *destination, uint16_t write_index, uint16_t &extent_index) const;
157  bool match(const Code *object, uint16_t index);
158  Atom *get_code();
159  Code *get_object();
160  uint16_t get_code_size();
161 
162  bool intersect(const Value *v) const;
163  bool _intersect(const ObjectValue *v) const;
164 
165  bool contains(const Code *o) const;
166 };
167 
168 typedef enum {
172 } MatchResult;
173 
174 class _Fact;
175 class Fact;
176 
178  public _Object {
179  friend class UnboundValue;
180 protected:
181  std::vector<P<Value> > map; // indexed by vl-ptrs.
182 
184 
185  void add_unbound_value(uint8_t id);
186 
187  uint16_t first_index; // index of the first value found in the first fact.
188  int16_t fwd_after_index; // tpl args (if any) are located before fwd_after_index.
190 
191  bool match_timings(uint64_t stored_after, uint64_t stored_before, uint64_t after, uint64_t before, uint64_t destination_after_index, uint64_t destination_before_index);
192  bool match_fwd_timings(const _Fact *f_object, const _Fact *f_pattern);
193  bool match(const Code *object, uint16_t o_base_index, uint16_t o_index, const Code *pattern, uint16_t p_index, uint16_t o_arity);
194 
195  void abstract_member(Code *object, uint16_t index, Code *abstracted_object, uint16_t write_index, uint16_t &extent_index);
196  Atom get_atom_variable(Atom a);
197  Atom get_structure_variable(Code *object, uint16_t index);
198  Atom get_object_variable(Code *object);
199 public:
200  BindingMap();
201  BindingMap(const BindingMap *source);
202  BindingMap(const BindingMap &source);
203  virtual ~BindingMap();
204 
205  BindingMap& operator =(const BindingMap &source);
206  void load(const BindingMap *source);
207 
208  virtual void clear();
209 
210  void init(Code *object, uint16_t index);
211 
212  _Fact *abstract_f_ihlp(_Fact *fact) const; // for icst and imdl.
213  _Fact *abstract_fact(_Fact *fact, _Fact *original, bool force_sync);
214  Code *abstract_object(Code *object, bool force_sync);
215 
216  void reset_fwd_timings(_Fact *reference_fact); // reset after and before from the timings of the reference object.
217 
218  MatchResult match_fwd_lenient(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we are lenient about fact vs |fact.
219  bool match_fwd_strict(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we need sharp match.
220 
221  uint64_t get_fwd_after() const; // assumes the timings are valuated.
222  uint64_t get_fwd_before() const; // idem.
223 
224  bool match_object(const Code *object, const Code *pattern);
225  bool match_structure(const Code *object, uint16_t o_base_index, uint16_t o_index, const Code *pattern, uint16_t p_index);
226  bool match_atom(Atom o_atom, Atom p_atom);
227 
228  void bind_variable(BoundValue *value, uint8_t id);
229  void bind_variable(Atom *code, uint8_t id, uint16_t value_index, Atom *intermediate_results);
230 
231  Atom *get_value_code(uint16_t id);
232  uint16_t get_value_code_size(uint16_t id);
233 
234  bool intersect(BindingMap *bm);
235  bool is_fully_specified() const;
236 
237  Atom *get_code(uint16_t i) const {
238  return map[i]->get_code();
239  }
240  Code *get_object(uint16_t i) const {
241  return map[i]->get_object();
242  }
243  uint16_t get_fwd_after_index() const {
244  return fwd_after_index;
245  }
246  uint16_t get_fwd_before_index() const {
247  return fwd_before_index;
248  }
249  bool scan_variable(uint16_t id) const; // return true if id<first_index or map[id] is not an UnboundValue.
250 };
251 
253  public BindingMap {
254 private:
257 
258  bool match_bwd_timings(const _Fact *f_object, const _Fact *f_pattern);
259 
260  bool need_binding(Code *pattern) const;
261  void init_from_pattern(const Code *source, int16_t position); // first source is f->obj.
262 public:
263  HLPBindingMap();
264  HLPBindingMap(const HLPBindingMap *source);
265  HLPBindingMap(const HLPBindingMap &source);
266  ~HLPBindingMap();
267 
268  HLPBindingMap& operator =(const HLPBindingMap &source);
269  void load(const HLPBindingMap *source);
270  void clear();
271 
272  void init_from_hlp(const Code *hlp);
273  void init_from_f_ihlp(const _Fact *f_ihlp);
274  Fact *build_f_ihlp(Code *hlp, uint16_t opcode, bool wr_enabled) const; // return f->ihlp.
275  Code *bind_pattern(Code *pattern) const;
276 
277  void reset_bwd_timings(_Fact *reference_fact); // idem for the last 2 unbound variables (i.e. timings of the second pattern in a mdl).
278 
279  MatchResult match_bwd_lenient(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we are lenient about fact vs |fact.
280  bool match_bwd_strict(const _Fact *f_object, const _Fact *f_pattern); // use for facts when we need sharp match.
281 
282  uint64_t get_bwd_after() const; // assumes the timings are valuated.
283  uint64_t get_bwd_before() const; // idem.
284 };
285 }
286 
287 
288 #endif
Definition: binding_map.h:45
virtual bool intersect(const Value *v) const
Definition: binding_map.h:58
Definition: factory.h:98
Definition: base.h:75
Definition: binding_map.h:177
uint16_t first_index
Definition: binding_map.h:187
#define dll_export
Definition: dll.h:44
std::vector< P< Value > > map
Definition: binding_map.h:181
Definition: binding_map.h:82
Definition: binding_map.h:105
Definition: binding_map.h:169
Definition: binding_map.h:125
Definition: _context.cpp:34
MatchResult
Definition: binding_map.h:168
uint16_t get_fwd_before_index() const
Definition: binding_map.h:246
uint8_t index
Definition: binding_map.h:92
Definition: binding_map.h:252
int16_t fwd_before_index
Definition: binding_map.h:189
bool match(const IPGMContext &input, const IPGMContext &pattern)
Definition: context.cpp:452
BindingMap * map
Definition: binding_map.h:48
Definition: binding_map.h:148
const P< Code > object
Definition: binding_map.h:151
Definition: atom.h:45
P< Code > structure
Definition: binding_map.h:128
int16_t bwd_before_index
Definition: binding_map.h:256
Atom atom
Definition: binding_map.h:108
virtual bool _intersect(const AtomValue *v) const
Definition: binding_map.h:61
int16_t bwd_after_index
Definition: binding_map.h:255
virtual bool contains(const Atom *s) const
Definition: binding_map.h:74
uint16_t get_fwd_after_index() const
Definition: binding_map.h:243
int16_t fwd_after_index
Definition: binding_map.h:188
Atom * get_code(uint16_t i) const
Definition: binding_map.h:237
Definition: binding_map.h:171
Definition: object.h:172
virtual bool _intersect(const ObjectValue *v) const
Definition: binding_map.h:67
virtual bool contains(const Code *o) const
Definition: binding_map.h:77
virtual bool _intersect(const StructureValue *v) const
Definition: binding_map.h:64
uint64_t unbound_values
Definition: binding_map.h:183
Definition: factory.h:174
Definition: binding_map.h:89
Definition: binding_map.h:170
Code * get_object(uint16_t i) const
Definition: binding_map.h:240
virtual bool contains(const Atom a) const
Definition: binding_map.h:71