-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbintlog.h
More file actions
457 lines (355 loc) · 10.6 KB
/
dbintlog.h
File metadata and controls
457 lines (355 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
IL : Intlog Language
Object Oriented Prolog Project
Copyright (C) 1992-2021 - Ing. Capelli Carlo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef DBINTLOG_H_
#define DBINTLOG_H_
///////////////////////////////////////////////////////////////////
// IntLog database
//
// an object oriented approach, that implement
// encapsulation,
// multiple inheritance,
// polymorphism
//
// contains
// type names (other databases)
// from which inherit
// procedures (clauses with matched functor/arity)
// that can be declared exported/imported
//
// links between procedures, used at evaluation time,
// are resolved when clauses are added to database
// contains Clauses, VTables or ExtRef (external references)
//
// a VTable is a set of pair DbTag/EntryPtr
// (as DbTag use the data pointer)
//
// an exported/imported procedure add a VTable at declaration point
// while references are added when no ambiguity arise
///////////////////////////////////////////////////////////////////
#include "iafx.h"
#include "clause.h"
#include "builtin.h"
// storage classes
class e_DbList;
class DbIntlog;
class DbEntry;
class DbList;
class DbListSeek;
class DbVTable;
// iterators for external access
class DbEntryIter;
class DbIdArityIter;
class DbInherIter;
// debugging (external declared)
class ProofTracer;
class DbDisplayer;
//////////////////////////////////////
// linked entries for procedure(s)
// navigate virtual tables on succ()
//
class IAFX_API e_DbList : public e_slist {
public:
// get current procedure clause
const Clause* get() const {
assert(type == tClause || type == tLocData || type == tShared || type == tBuiltin);
return clause;
}
// fetch next element in procedure list
#if 0
const e_DbList* succ(const DbIntlog *dbs) const;
#else
const e_DbList* succ(const DbIntlog *dbs) const {
assert(this);
auto n = next();
return n ? n->fix_clause(dbs) : nullptr;
}
#endif
ostream& Display(ostream& s, int mode) const;
private:
enum eType {
tClause, // local clause definition
tVTable, // virtual table
tExtRef, // external chaining
tLocData, // asserted (local data)
tShared, // shared dynamic data
tBuiltin // builtin pointer
} type;
union {
Clause* clause; // instance data
DbVTable* table; // virtual links
const DbEntry* extref; // unambiguos link
BuiltIn* bltin; // system defined builtin
};
e_DbList(Clause *c) {
type = tClause;
clause = c;
}
e_DbList(DbVTable *t) {
type = tVTable;
table = t;
}
e_DbList(const DbEntry *e) {
type = tExtRef;
extref = e;
}
e_DbList(BuiltIn *bt) {
type = tBuiltin;
bltin = bt;
}
~e_DbList();
// search in VTables chainings
const e_DbList *fix_clause(const DbIntlog *) const;
const e_DbList* searchVTbl(const DbIntlog *) const;
const e_DbList *next() const {
return static_cast<const e_DbList *>(e_slist::next);
}
friend class DbEntry;
friend class DbEntryIter;
friend class DbIdArityIter;
friend class DbList;
friend class DbListSeek;
friend class DbIntlog;
friend class DbDisplayer;
};
//////////////////////////////////////
// a list of Clauses, VTables, ExtRef
//
class IAFX_API DbList : slist {
int match(e_slist *, void *) const;
e_DbList *seekptr(void* e) const {
return static_cast<e_DbList *>(slist::seekptr(e));
}
unsigned seek(void *e) const {
return slist::seek(e);
}
ostream& Display(ostream& s, int mode) const;
friend class DbIntlog;
friend class DbEntry;
friend class DbDisplayer;
};
///////////////////////////
// a procedure entry point
//
class DbEntry : e_hashtable {
public:
DbEntry(kstring f, int a) {
funct = f;
arity = a;
vProp = local;
}
~DbEntry() override;
e_DbList *get_first() const {
return static_cast<e_DbList*>(entries.get_first());
}
// inter DB entries scope properties
enum scopemode {
local = 0x01,
exported = 0x02,
import = 0x04,
dynamic = 0x08
};
ostream& Display(ostream& s, int mode) const;
private:
const char* getstr() const override {
return funct;
}
// proc identification
kstring funct;
int arity;
// actual scope tag for procedure
scopemode vProp;
// virtual table control (one for entry!)
DbVTable* addVTbl(unsigned = unsigned(-1));
void addExtRef(DbEntry*);
// e_DbList(s) list
DbList entries;
friend class DbIntlog;
friend class DbDisplayer;
};
/////////////////////
// a database object
//
class IAFX_API DbIntlog : protected hashtable, protected e_slist {
public:
// construct/destroy a database
DbIntlog();
DbIntlog(DbIntlog *);
DbIntlog(kstring, DbIntlog *);
~DbIntlog() override {}
///////// LOCAL STORE CONTROL ///////
// create entry for clause
virtual void Add(Clause *c, int first = 0, DbIntlog * = nullptr);
// return matching entry point, inserting if needed
DbEntry *GetEntryRef(Term);
// get first clause in procedure list entry
const e_DbList* StartProc(const DbEntry *dbe) const {
#if 0 //def _DEBUG
CCP tstring = dbe->funct;
#endif
return dbe->get_first()->fix_clause(this);
}
// delete matched by name/arity
virtual int Del(DbEntryIter&);
// search first clause matching term
int Search(Term, DbEntryIter &, DbIntlog *);
// release temporary invalidated clauses
virtual void ClearStatus(DbIntlog* = nullptr);
// remove entries indexed by file id
DbIntlog *RemoveFileClauses(kstring fileId, slistvptr &updated);
// and reinsert at named position
bool RestoreClause(Clause *);
////////// BUILTINS CONTROL //////////
// insert all builtins fron a table
void addtable(BuiltIn*, int);
// check if already in table
BuiltIn* is_builtin(CCP, int) const;
// check term is callable as builtin
BuiltIn* is_builtin(Term) const;
// keep root Builtin database
static DbIntlog *_pGlobalBuiltins;
protected:
// track deleted procs
slist m_deleted;
// override: compare kstring and arity
int keymatch(const e_hashtable *, const e_hashtable *) const override;
DbEntry* isin(DbEntry *e) const;
DbEntry* isin(DbEntry &e) const;
///////// INTERFACE CONTROL //////////
public:
// return DB identifier
kstring GetId() const {
return m_name;
}
// enable to change identifier
void SetId(kstring);
// get father
DbIntlog *GetFather() const {
return m_father;
}
// start definition of new local interface
DbIntlog *BeginInterface(kstring);
// terminate this interface, back to 'father'
DbIntlog *EndInterface();
// on qualified name, select direct DB
DbIntlog *FixPathName(Term *) const;
// look in local types list
DbIntlog *IsLocalInterface(kstring) const;
// make an entry, changing her properties as required
int ChangeEntryProperty(kstring, int, DbEntry::scopemode);
// open an inherited interface
int InheritInterface(DbIntlog *);
// check for inheritance relationship
int IsA(const DbIntlog *) const;
// get info about properties entries
struct EntryWithProp : e_slist {
kstring id;
int arity;
};
void EntriesWithProperty(DbEntry::scopemode, slist &) const;
// display with some option
enum {
Recurs = 0x01,
Header = 0x02,
Entries = 0x04,
PropInt = 0x08,
Arity = 0x10,
Clauses = 0x20,
Indent = 0x40,
Base = Recurs|Header|Entries|PropInt|Arity,
All = Base|Clauses
};
virtual ostream& Display(ostream& s, int mode = All) const;
protected:
// DB identifier (local unique)
kstring m_name;
// locally defined types (DbIntlog)
slist m_types;
// in which DB definition of this occurs
DbIntlog* m_father;
// inherited interfaces
slistvptr m_inherited;
// search entries in inherited interfaces
DbEntry* find_inherited_entry(DbEntry *, DbInherIter &) const;
// seek entry (if !found, insert)
DbEntry* make_entry(kstring, int);
// check if imported/exported from some inherited
void check_inherited_entries(DbEntry *);
friend class DbInherIter;
friend class DbIdArityIter;
friend class DbDisplayer;
};
inline BuiltIn* DbIntlog::is_builtin(Term t) const {
return is_builtin(t.get_funct(), t.get_arity());
}
//////////////////////////////
// a virtual table
// keep DB tag and pointers
//
class DbVTable : public slist {
public:
struct e_VTable : public e_slist {
DbIntlog* db; // tag owner DB
DbEntry* first; // pointer to first entry
};
void add(DbIntlog *, DbEntry *);
ostream& Display(ostream& s, int mode) const;
};
/////////////////////////////////////////////////////////////////
// iterators to gain access to subsequent matching clauses in DB
// initialize by call DbIntlog::Search()
//
class DbEntryIter {
public:
Clause* next();
Clause* curr() const {
return pcp? pcp->clause : nullptr;
}
DbIntlog* owner() const {
return own;
}
private:
e_DbList *pcc;
e_DbList *pcp;
DbIntlog *own;
DbIntlog *mod;
friend class DbIntlog;
};
class DbIdArityIter : hashtable_iter {
public:
DbIdArityIter(DbIntlog *, kstring, int);
Clause *next();
private:
int arity;
DbEntry *dbe;
e_DbList *pcp;
DbIntlog *own;
friend class DbIntlog;
};
/////////////////////////////
// iterate inherited members
//
class IAFX_API DbInherIter : slistvptr_iter {
public:
DbInherIter(const DbIntlog *db) : slistvptr_iter(db->m_inherited) {
}
DbIntlog* next() {
return reinterpret_cast<DbIntlog*>(slistvptr_iter::next());
}
private:
friend class DbIntlog;
};
#endif