BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
pred.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: pred.h
4 // Purpose: Predicate Data for Loading Data Files (only EDB predicates)
5 // Last Change: 04.08.2017
6 // Language: C++
7 // EMail: brass@informatik.uni-halle.de
8 // WWW: http://www.informatik.uni-halle.de/~brass/
9 // Address: Feldschloesschen 15, D-06120 Halle (Saale), GERMANY
10 // Copyright: (c) 2015-2017 by Stefan Brass
11 // License: See file "LICENSE" for copying conditions.
12 // Note: There is no warranty at all - this code may contain bugs.
13 // ============================================================================
14 
15 
26 //=============================================================================
27 // Include File Frame:
28 //=============================================================================
29 
30 #ifndef PRED_INCLUDED
31 #define PRED_INCLUDED
32 
33 //=============================================================================
34 // Used Types and Macros:
35 //=============================================================================
36 
37 #ifndef VER_INCLUDED
38 #include "../base/ver.h"
39 #endif
40 
41 #ifndef STR_INCLUDED
42 #include "../base/str.h"
43 #endif
44 
45 #ifndef CHECK_INCLUDED
46 #include "../base/check.h"
47 #endif
48 
49 #ifndef STRTAB_INCLUDED
50 #include "../dom/strtab.h"
51 #endif
52 
53 #ifndef BIND_INCLUDED
54 #include "../rel/bind.h"
55 #endif
56 
57 #ifndef REL_INCLUDED
58 #include "../rel/rel.h"
59 #endif
60 
61 #ifndef LIST_1_INCLUDED
62 #include "../rel/list_1.h"
63 #endif
64 
65 #ifndef LIST_2_INCLUDED
66 #include "../rel/list_2.h"
67 #endif
68 
69 #ifndef SET_1_INCLUDED
70 #include "../rel/set_1.h"
71 #endif
72 
73 #ifndef SET_2_INCLUDED
74 #include "../rel/set_2.h"
75 #endif
76 
77 #ifndef REL_N_N_INCLUDED
78 #include "../rel/rel_n_n.h"
79 #endif
80 
81 #ifndef ARGTYPE_INCLUDED
82 #include "argtype.h"
83 #endif
84 
85 
86 //=============================================================================
87 // Private Constants:
88 //=============================================================================
89 
90 //-----------------------------------------------------------------------------
91 // PRED_MAX_RELS: Maximal number of of data structures to store facts.
92 //-----------------------------------------------------------------------------
93 
94 static const int PRED_MAX_RELS = 5;
95 
96 //-----------------------------------------------------------------------------
97 // PRED_MAGIC: Magic number (identifies objects of this class).
98 //-----------------------------------------------------------------------------
99 
100 static const long PRED_MAGIC = 0x5052440AL; // 'PRD\n'
101 
102 
103 //=============================================================================
104 // Class for EDB Predicates (Predicate Data for Loading Data Files):
105 //=============================================================================
106 
107 class pred_c {
108  public:
109 
110 
111 //-----------------------------------------------------------------------------
112 // Constructor, Destructor:
113 //-----------------------------------------------------------------------------
114 
115  // Constructor:
116  pred_c(str_t pred_name, int pred_arity);
117 
118  // Constructor, Special Case for Predicates with one atom/string arg:
119  pred_c(str_t pred_name, strtab_t dom_1);
120 
121  // Constructor, Special Case for Predicates with two atom/string args:
122  pred_c(str_t pred_name, strtab_t dom_1, strtab_t dom_2);
123 
124  // Constructor, Special Case for Predicates with one numeric arg:
125  pred_c(str_t pred_name, argtype_t argtype_1);
126 
127  // Constructor, Special Case for Predicates with two numeric args:
128  pred_c(str_t pred_name, argtype_t argtype_1, argtype_t argtype_2);
129 
130  // Destructor:
131  ~pred_c();
132 
133 
134 //-----------------------------------------------------------------------------
135 // (Object) Methods:
136 //-----------------------------------------------------------------------------
137 
138  // name: Return name of predicate.
139  str_t name() {
140  CHECK_VALID("pred_c::name");
141  return Name;
142  }
143 
144  // arity: Return arity of predicate.
145  int arity() {
146  CHECK_VALID("pred_c::arity");
147  return Arity;
148  }
149 
150  // set_argtype: Set argument type.
151  void set_argtype(int arg_no, argtype_t arg_type) {
152  CHECK_VALID("pred_c::set_argtype");
153  CHECK(arg_no >= 0, "pred_c::set_argtype: arg_no is negative");
154  CHECK(arg_no < Arity, "pred_c::set_argtype: arg_no too large");
155  CHECK(ArgType[arg_no] == ARGTYPE_NULL,
156  "pred_c::set_argtype: argtype already set");
157  CHECK(argtype_valid(arg_type),
158  "pred_c::set_argtype: arg_type is invalid");
159  if(arg_no < VER_MAX_PRED_ARGS)
160  ArgType[arg_no] = arg_type;
161  }
162 
163  // argtype: Get type of argument.
164  argtype_t argtype(int arg_no) {
165  CHECK_VALID("pred_c::argtype");
166  CHECK(arg_no >= 0, "pred_c::argtype: arg_no is negative");
167  CHECK(arg_no < Arity, "pred_c::argtype: arg_no too large");
168  if(arg_no < VER_MAX_PRED_ARGS)
169  return ArgType[arg_no];
170  else
171  return ARGTYPE_NULL;
172  }
173 
174  // set_dom: Set domain for argument to string table.
175  void set_dom(int arg_no, strtab_t dom) {
176  CHECK_VALID("pred_c::set_dom");
177  CHECK(arg_no >= 0, "pred_c::set_dom: arg_no is negative");
178  CHECK(arg_no < Arity, "pred_c::set_dom: arg_no too large");
179  CHECK_PAR(dom, "pred_c::set_dom");
180  CHECK(Dom[arg_no] == STRTAB_NULL,
181  "pred_c::set_dom: arg_no is already set");
182  if(arg_no < VER_MAX_PRED_ARGS)
183  Dom[arg_no] = dom;
184  }
185 
186  // arg_dom: Get domain for argument to string table.
187  strtab_t arg_dom(int arg_no) {
188  CHECK_VALID("pred_c::arg_dom");
189  CHECK(arg_no >= 0, "pred_c::arg_dom: arg_no is negative");
190  CHECK(arg_no < Arity, "pred_c::arg_dom: arg_no too large");
191  if(arg_no < VER_MAX_PRED_ARGS)
192  return Dom[arg_no];
193  else
194  return STRTAB_NULL;
195  }
196 
197  // add_rel: Add relation to store facts of this predicate.
198  bool add_rel(rel_t rel, bind_t bind);
199 
200  // Special cases of add_rel:
201  bool add_rel(list_1_t rel) {
202  return add_rel(rel, BIND_F);
203  }
204 
205  bool add_rel(list_2_t rel) {
206  return add_rel(rel, BIND_FF);
207  }
208 
209  bool add_rel(set_1_t rel) {
210  return add_rel(rel, BIND_B);
211  }
212 
213  bool add_rel(set_2_t rel) {
214  return add_rel(rel, BIND_BB);
215  }
216 
217  bool add_rel(rel_n_n_t rel) {
218  return add_rel(rel, BIND_BF);
219  }
220 
221  // store_fact: Store fact in all relations attached to this predicate:
222  bool store_fact(int args[]);
223 
224  // hash_next: Return next predicate in hash chain.
225  pred_c* hash_next() {
226  CHECK_VALID("pred_c::hash_next");
227  return HashNext;
228  }
229 
230  // set_next: Set next predicate in hash chain.
231  void set_next(pred_c *next_pred) {
232  CHECK_VALID("pred_c::set_next");
233  HashNext = next_pred;
234  }
235 
236 
237 //-----------------------------------------------------------------------------
238 // Debugging Support:
239 //-----------------------------------------------------------------------------
240 
241 #if VER_DEBUG
242  // Integrity check:
243  public:
244  str_t check() const;
245 
246  // Magic number (identifies objects of this class for debugging).
247  private:
248  long Magic; // Must be "PRED_MAGIC".
249 #endif
250 
251 
252 #if VER_DUMP
253  public:
254  // Display data structure:
255  void dump(str_t headline = STR_NULL) const;
256 #endif
257 
258 //-----------------------------------------------------------------------------
259 // Copy-constructor and assignment operator are not supported for this class:
260 //-----------------------------------------------------------------------------
261 
262  private:
263 
264  pred_c(const pred_c& atom); // Not implemented
265  pred_c& operator=(const pred_c& atom); // Not implemented
266 
267 
268 //-----------------------------------------------------------------------------
269 // Private Object Members:
270 //-----------------------------------------------------------------------------
271 
272  private:
273 
274  // Name of the predicate:
275  str_t Name;
276 
277  // Arity (number of arguments) of the predicate:
278  int Arity;
279 
280  // Argument types:
281  argtype_t ArgType[VER_MAX_PRED_ARGS];
282 
283  // Domains for storing the arguments:
285 
286  // Number of relation data structures to recieve facts of this pred:
287  int NumRels;
288 
289  // Data structures for storing the extension of this predicate:
290  rel_t Rel[PRED_MAX_RELS];
291 
292  // Binding patterns for these relations:
293  bind_t Bind[PRED_MAX_RELS];
294 
295  // Link to next predicate in hash chain (see class load_c).
296  pred_c *HashNext;
297 
298 
299 //=============================================================================
300 // End of Class:
301 //=============================================================================
302 
303 };
304 
305 //-----------------------------------------------------------------------------
306 // Define pointer type:
307 //-----------------------------------------------------------------------------
308 
309 typedef pred_c *pred_t;
310 
311 //-----------------------------------------------------------------------------
312 // Define null pointer:
313 //-----------------------------------------------------------------------------
314 
315 #define PRED_NULL (static_cast<pred_t>(0))
316 
317 //=============================================================================
318 // End of Include File:
319 //=============================================================================
320 
321 #endif
322 
Definition: set_1.h:58
Definition: strtab.h:106
Definition: rel.h:72
#define VER_MAX_PRED_ARGS
Definition: ver.h:220
Definition: list_2.h:58
#define CHECK_VALID(EX)
Definition: check.h:85
const char * str_t
Definition: str.h:41
Definition: list_1.h:58
Definition: pred.h:107
#define STR_NULL
Definition: str.h:52
Enumeration type for types of predicate arguments.
Definition: rel_n_n.h:78
enum argtype_enum argtype_t
#define CHECK(EX, MSG)
Definition: check.h:69
#define CHECK_PAR(PAR, PLACE)
Definition: check.h:102
Definition: set_2.h:58
Definition: argtype.h:58