forked from mclumd/alma-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alma_kb.c
1843 lines (1671 loc) · 70.1 KB
/
alma_kb.c
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "alma_kb.h"
#include "alma_proc.h"
#include "alma_print.h"
static long next_index;
static void make_clause(alma_node *node, clause *c) {
// Note: predicate null assignment necessary for freeing of notes without losing predicates
if (node != NULL) {
if (node->type == FOL) {
// Neg lit case for NOT
if (node->fol->op == NOT) {
c->neg_count++;
c->neg_lits = realloc(c->neg_lits, sizeof(*c->neg_lits) * c->neg_count);
c->neg_lits[c->neg_count-1] = node->fol->arg1->predicate;
node->fol->arg1->predicate = NULL;
}
// Case of node is OR
else {
make_clause(node->fol->arg1, c);
make_clause(node->fol->arg2, c);
}
if (c->tag == NONE)
c->tag = node->fol->tag;
}
// Otherwise, only pos lit left
else {
c->pos_count++;
c->pos_lits = realloc(c->pos_lits, sizeof(*c->pos_lits) * c->pos_count);
c->pos_lits[c->pos_count-1] = node->predicate;
node->predicate = NULL;
}
}
}
static void find_variable_names(tommy_array *list, alma_term *term, int id_from_name) {
switch (term->type) {
case VARIABLE: {
for (tommy_size_t i = 0; i < tommy_array_size(list); i++) {
if (id_from_name) {
if (strcmp(term->variable->name, tommy_array_get(list, i)) == 0)
return;
}
else {
if (term->variable->id == *(long long *)tommy_array_get(list, i))
return;
}
}
if (id_from_name) {
tommy_array_insert(list, term->variable->name);
}
else {
long long *id = malloc(sizeof(*id));
*id = term->variable->id;
tommy_array_insert(list, id);
}
break;
}
case CONSTANT: {
return;
}
case FUNCTION: {
for (int i = 0; i < term->function->term_count; i++) {
find_variable_names(list, term->function->terms+i, id_from_name);
}
}
}
}
static void set_variable_names(tommy_array *list, alma_term *term, int id_from_name) {
switch (term->type) {
case VARIABLE: {
for (tommy_size_t i = 0; i < tommy_array_size(list); i++) {
if (id_from_name) {
if (strcmp(term->variable->name, tommy_array_get(list, i)) == 0)
term->variable->id = variable_id_count + i;
}
else {
if (term->variable->id == *(long long *)tommy_array_get(list, i))
term->variable->id = variable_id_count + i;
}
}
break;
}
case CONSTANT: {
return;
}
case FUNCTION: {
for (int i = 0; i < term->function->term_count; i++) {
set_variable_names(list, term->function->terms+i, id_from_name);
}
}
}
}
// Given a clause, assign the ID fields of each variable
// Two cases:
// 1) If clause is result of resolution, replace existing matching IDs with fresh values each
// 2) Otherwise, give variables with the same name matching IDs
// Fresh ID values drawn from variable_id_count global variable
static void set_variable_ids(clause *c, int id_from_name, binding_list *bs_bindings) {
tommy_array vars;
tommy_array_init(&vars);
for (int i = 0; i < c->pos_count; i++) {
for (int j = 0; j < c->pos_lits[i]->term_count; j++) {
find_variable_names(&vars, c->pos_lits[i]->terms+j, id_from_name);
}
}
for (int i = 0; i < c->neg_count; i++) {
for (int j = 0; j < c->neg_lits[i]->term_count; j++) {
find_variable_names(&vars, c->neg_lits[i]->terms+j, id_from_name);
}
}
for (int i = 0; i < c->pos_count; i++) {
for (int j = 0; j < c->pos_lits[i]->term_count; j++) {
set_variable_names(&vars, c->pos_lits[i]->terms+j, id_from_name);
}
}
for (int i = 0; i < c->neg_count; i++) {
for (int j = 0; j < c->neg_lits[i]->term_count; j++) {
set_variable_names(&vars, c->neg_lits[i]->terms+j, id_from_name);
}
}
// If bindings for a backsearch have been passed in, update variable names for them as well
if (bs_bindings) {
for (int i = 0; i < bs_bindings->num_bindings; i++) {
set_variable_names(&vars, bs_bindings->list[i].term, id_from_name);
}
}
variable_id_count += tommy_array_size(&vars);
if (!id_from_name) {
for (int i = 0; i < tommy_array_size(&vars); i++)
free(tommy_array_get(&vars, i));
}
tommy_array_done(&vars);
}
static void init_ordering_rec(fif_info *info, alma_node *node, int *next, int *pos, int *neg) {
if (node->type == FOL) {
// Neg lit case for NOT
if (node->fol->op == NOT) {
info->ordering[(*next)++] = (*neg)--;
}
// Case of node is OR
else {
init_ordering_rec(info, node->fol->arg1, next, pos, neg);
init_ordering_rec(info, node->fol->arg2, next, pos, neg);
}
}
// Otherwise, pos lit
else {
info->ordering[(*next)++] = (*pos)++;
}
}
// Given a node for a fif formula, record inorder traversal ofpositive and negative literals
static void init_ordering(fif_info *info, alma_node *node) {
int next = 0;
int pos = 0;
int neg = -1;
// Fif node must necessarily at top level have an OR -- ignore second branch of with conclusion
init_ordering_rec(info, node->fol->arg1, &next, &pos, &neg);
}
// Comparison used by qsort of clauses -- orders by increasing function name and increasing arity
static int function_compare(const void *p1, const void *p2) {
alma_function **f1 = (alma_function **)p1;
alma_function **f2 = (alma_function **)p2;
int compare = strcmp((*f1)->name, (*f2)->name);
if (compare == 0)
return (*f1)->term_count - (*f2)->term_count;
else
return compare;
}
// Flattens a single alma node and adds its contents to collection
// Recursively calls when an AND is found to separate conjunctions
void flatten_node(alma_node *node, tommy_array *clauses, int print) {
if (node->type == FOL && node->fol->op == AND) {
if (node->fol->arg1->type == FOL)
node->fol->arg1->fol->tag = node->fol->tag;
flatten_node(node->fol->arg1, clauses, print);
if (node->fol->arg2->type == FOL)
node->fol->arg2->fol->tag = node->fol->tag;
flatten_node(node->fol->arg2, clauses, print);
}
else {
clause *c = malloc(sizeof(*c));
c->pos_count = c->neg_count = 0;
c->pos_lits = c->neg_lits = NULL;
c->parent_set_count = c->children_count = 0;
c->parents = NULL;
c->children = NULL;
c->tag = NONE;
c->fif = NULL;
make_clause(node, c);
set_variable_ids(c, 1, NULL);
// If clause is fif, initialize additional info
if (c->tag == FIF) {
c->fif = malloc(sizeof(*c->fif));
c->fif->premise_count = c->pos_count + c->neg_count - 1;
c->fif->ordering = malloc(sizeof(*c->fif->ordering) * c->fif->premise_count);
init_ordering(c->fif, node);
c->fif->conclusion = c->pos_lits[c->pos_count-1]; // Conclusion will always be last pos_lit
}
// Non-fif clauses can be sorted be literal for ease of resolution
// Fif clauses must retain original literal order to not affect their evaluation
if (c->tag != FIF) {
qsort(c->pos_lits, c->pos_count, sizeof(*c->pos_lits), function_compare);
qsort(c->neg_lits, c->neg_count, sizeof(*c->neg_lits), function_compare);
}
if (print) {
clause_print(c);
printf(" added\n");
}
tommy_array_insert(clauses, c);
}
}
void free_clause(clause *c) {
for (int i = 0; i < c->pos_count; i++)
free_function(c->pos_lits[i]);
for (int i = 0; i < c->neg_count; i++)
free_function(c->neg_lits[i]);
free(c->pos_lits);
free(c->neg_lits);
for (int i = 0; i < c->parent_set_count; i++)
free(c->parents[i].clauses);
free(c->parents);
free(c->children);
if (c->tag == FIF) {
free(c->fif->ordering);
free(c->fif);
}
free(c);
}
// Flattens trees into set of clauses (tommy_array must be initialized prior)
// trees argument freed here
void nodes_to_clauses(alma_node *trees, int num_trees, tommy_array *clauses, int print) {
for (int i = 0; i < num_trees; i++) {
flatten_node(trees+i, clauses, print);
free_alma_tree(trees+i);
}
free(trees);
}
// Reorders clause array to begin with fifs; for correctness of task generation
void fif_to_front(tommy_array *clauses) {
tommy_size_t loc = 0;
for (tommy_size_t i = 1; i < tommy_array_size(clauses); i++) {
clause *c = tommy_array_get(clauses, i);
if (c->tag == FIF) {
clause *tmp = tommy_array_get(clauses, loc);
tommy_array_set(clauses, loc, c);
tommy_array_set(clauses, i, tmp);
loc++;
}
}
}
void free_predname_mapping(void *arg) {
predname_mapping *entry = arg;
free(entry->predname);
free(entry->clauses);
// Note: clause entries ARE NOT FREED because they alias the clause objects freed in kb_halt
free(entry);
}
void free_fif_mapping(void *arg) {
fif_mapping *entry = arg;
free(entry->conclude_name);
free(entry->clauses);
// Note: clause entries ARE NOT FREED because they alias the clause objects freed in kb_halt
free(entry);
}
static void free_fif_task(fif_task *task) {
// Note: clause entries ARE NOT FREED because they alias the clause objects freed in kb_halt
cleanup_bindings(task->bindings);
free(task->unified_clauses);
free(task);
}
void free_fif_task_mapping(void *arg) {
fif_task_mapping *entry = arg;
free(entry->predname);
tommy_node *curr = tommy_list_head(&entry->tasks);
while (curr) {
fif_task *data = curr->data;
curr = curr->next;
free_fif_task(data);
}
free(entry);
}
// Accesses the literal in position i of fif clause c
alma_function* fif_access(clause *c, int i) {
int next = c->fif->ordering[i];
if (next < 0)
return c->neg_lits[-next - 1];
else
return c->pos_lits[next];
}
// Compare function to be used by tommy_hashlin_search for index_mapping
// Compares long arg to key of index_mapping
static int im_compare(const void *arg, const void *obj) {
return *(const long*)arg - ((const index_mapping*)obj)->key;
}
// Compare function to be used by tommy_hashlin_search for predname_mapping
// Compares string arg to predname of predname_mapping
int pm_compare(const void *arg, const void *obj) {
return strcmp((const char*)arg, ((const predname_mapping*)obj)->predname);
}
// Compare function to be used by tommy_hashlin_search for fif_mapping
// compares string arg to conclude_name of fif_mapping
static int fifm_compare(const void *arg, const void*obj) {
return strcmp((const char*)arg, ((const fif_mapping*)obj)->conclude_name);
}
// Compare function to be used by tommy_hashlin_search for fif_task_mapping
// Compares string arg to predname of fif_task_mapping
static int fif_taskm_compare(const void *arg, const void *obj) {
return strcmp((const char*)arg, ((const fif_task_mapping*)obj)->predname);
}
// Compare function to be used by tommy_hashlin_search for binding_mapping
// Compares long arg to key of binding_mapping
int bm_compare(const void *arg, const void *obj) {
return *(const long*)arg - ((const binding_mapping*)obj)->key;
}
// Given "name" and integer arity, returns "name/arity"
// Allocated cstring is later placed in predname_mapping, and eventually freed by kb_halt
char* name_with_arity(char *name, int arity) {
int arity_len = snprintf(NULL, 0, "%d", arity);
char *arity_str = malloc(arity_len+1);
snprintf(arity_str, arity_len+1, "%d", arity);
char *name_with_arity = malloc(strlen(name) + strlen(arity_str) + 2);
memcpy(name_with_arity, name, strlen(name));
name_with_arity[strlen(name)] = '/';
strcpy(name_with_arity + strlen(name) + 1, arity_str);
free(arity_str);
return name_with_arity;
}
// Inserts clause into the hashmap (with key based on lit), as well as the linked list
// If the map entry already exists, append; otherwise create a new one
static void map_add_clause(tommy_hashlin *map, tommy_list *list, alma_function *lit, clause *c) {
char *name = name_with_arity(lit->name, lit->term_count);
predname_mapping *result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
result->num_clauses++;
result->clauses = realloc(result->clauses, sizeof(*result->clauses) * result->num_clauses);
result->clauses[result->num_clauses-1] = c; // Aliases with pointer assignment
free(name); // Name not added into hashmap must be freed
}
else {
predname_mapping *entry = malloc(sizeof(*entry));
entry->predname = name;
entry->num_clauses = 1;
entry->clauses = malloc(sizeof(*entry->clauses));
entry->clauses[0] = c; // Aliases with pointer assignment
tommy_hashlin_insert(map, &entry->hash_node, entry, tommy_hash_u64(0, entry->predname, strlen(entry->predname)));
tommy_list_insert_tail(list, &entry->list_node, entry);
}
}
// Removes clause from hashmap and list if it exists
static void map_remove_clause(tommy_hashlin *map, tommy_list *list, alma_function *lit, clause *c) {
char *name = name_with_arity(lit->name, lit->term_count);
predname_mapping *result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
for (int i = 0; i < result->num_clauses; i++) {
if (result->clauses[i] == c) {
if (i < result->num_clauses-1)
result->clauses[i] = result->clauses[result->num_clauses-1];
result->num_clauses--;
// Shrink size of clause list of result
if (result->num_clauses > 0) {
result->clauses = realloc(result->clauses, sizeof(*result->clauses) * result->num_clauses);
}
// Remove predname_mapping from hashmap and linked list
else {
tommy_hashlin_remove_existing(map, &result->hash_node);
tommy_list_remove_existing(list, &result->list_node);
free_predname_mapping(result);
free(name);
return;
}
}
}
}
free(name);
}
// Returns first alma_function pointer for literal with given name in clause
// Searching positive or negative literals is decided by pos boolean
static alma_function* literal_by_name(clause *c, char *name, int pos) {
if (c != NULL) {
int count = pos ? c->pos_count : c->neg_count;
alma_function **lits = pos ? c->pos_lits : c->neg_lits;
for (int i = 0; i < count; i++)
if (strcmp(name, lits[i]->name) == 0)
return lits[i];
}
return NULL;
}
typedef struct var_matching {
int count;
long long *x;
long long *y;
} var_matching;
// Returns 0 if functions are equal while respecting x and y matchings based on matches arg; otherwise returns 1
// (Further detail in clauses_differ)
static int functions_differ(alma_function *x, alma_function *y, var_matching *matches) {
if (x->term_count == y->term_count && strcmp(x->name, y->name) == 0) {
for (int i = 0; i < x->term_count; i++) {
if (x->terms[i].type == y->terms[i].type) {
switch(x->terms[i].type) {
case VARIABLE: {
long long xval = x->terms[i].variable->id;
long long yval = y->terms[i].variable->id;
// Look for matching variable in var_matching's x and y
for (int j = 0; j < matches->count; j++) {
// If only one of xval and yval matches, return unequal
if ((xval == matches->x[j] && yval != matches->y[j]) || (yval == matches->y[j] && xval != matches->x[j]))
return 1;
}
// No match was found, add a new one to the matching
matches->count++;
matches->x = realloc(matches->x, sizeof(*matches->x) * matches->count);
matches->x[matches->count -1] = xval;
matches->y = realloc(matches->y, sizeof(*matches->y) * matches->count);
matches->y[matches->count -1] = yval;
break;
}
case CONSTANT: {
if (strcmp(x->terms[i].constant->name, y->terms[i].constant->name) != 0)
return 1;
break;
}
case FUNCTION: {
if (functions_differ(x->terms[i].function, y->terms[i].function, matches))
return 1;
}
}
}
else
return 1;
}
// All arguments compared as equal; return 0
return 0;
}
return 1;
}
// Function to call when short-circuiting clauses_differ to properly free var_matching instance
static int release_matches(var_matching *matches, int retval) {
if (matches->x != NULL)
free(matches->x);
if (matches->y != NULL)
free(matches->y);
return retval;
}
// Returns 0 if clauses have equal positive and negative literal sets; otherwise returns 1
// Equality of variables is that there is a one-to-one correspondence in the sets of variables x and y use,
// based on each location where a variable maps to another
// Thus a(X, X) and a(X, Y) are here considered different
// Naturally, literal ordering has no effect on clauses differing
// Clauses x and y must have no duplicate literals (ensured by earlier call to TODO on the clauses)
static int clauses_differ(clause *x, clause *y) {
if (x->pos_count == y->pos_count && x->neg_count == y->neg_count){
var_matching matches;
matches.count = 0;
matches.x = NULL;
matches.y = NULL;
if (x->tag == FIF) {
for (int i = 0; i < x->fif->premise_count; i++) {
alma_function *xf = fif_access(x, i);
alma_function *yf = fif_access(y, i);
if (function_compare(&xf, &yf) || functions_differ(xf, yf, &matches))
return release_matches(&matches, 1);
}
}
else {
for (int i = 0; i < x->pos_count; i++) {
// TODO: account for case in which may have several literals with name
// Ignoring duplicate literal case, sorted literal lists allows comparing ith literals of each clause
if (function_compare(x->pos_lits+i, y->pos_lits+i) || functions_differ(x->pos_lits[i], y->pos_lits[i], &matches))
return release_matches(&matches, 1);
}
for (int i = 0; i < x->neg_count; i++) {
// TODO: account for case in which may have several literals with name
// Ignoring duplicate literal case, sorted literal lists allows comparing ith literals of each clause
if (function_compare(x->neg_lits+i, y->neg_lits+i) || functions_differ(x->neg_lits[i], y->neg_lits[i], &matches))
return release_matches(&matches, 1);
}
}
// All literals compared as equal; return 0
return release_matches(&matches, 0);
}
return 1;
}
// If c is found to be a clause duplicate, returns a pointer to that clause; null otherwise
// See comments preceding clauses_differ function for further detail
clause* duplicate_check(kb *collection, clause *c) {
if (c->tag == FIF) {
char *name = c->fif->conclusion->name;
fif_mapping *result = tommy_hashlin_search(&collection->fif_map, fifm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
for (int i = 0; i < result->num_clauses; i++) {
if (c->tag == result->clauses[i]->tag && !clauses_differ(c, result->clauses[i]))
return result->clauses[i];
}
}
}
else {
if (c->pos_count > 0) {
// If clause has a positive literal, all duplicate candidates must have that same positive literal
// Arbitrarily pick first positive literal as one to use; may be able to do smarter literal choice later
char *name = name_with_arity(c->pos_lits[0]->name, c->pos_lits[0]->term_count);
predname_mapping *result = tommy_hashlin_search(&collection->pos_map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
free(name);
if (result != NULL) {
for (int i = 0; i < result->num_clauses; i++) {
if (c->tag == result->clauses[i]->tag && !clauses_differ(c, result->clauses[i]))
return result->clauses[i];
}
}
}
else if (c->neg_count > 0) {
// If clause has a negative literal, all duplicate candidates must have that same negative literal
// Arbitrarily pick first negative literal as one to use; may be able to do smarter literal choice later
char *name = name_with_arity(c->neg_lits[0]->name, c->neg_lits[0]->term_count);
predname_mapping *result = tommy_hashlin_search(&collection->neg_map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
free(name);
if (result != NULL) {
for (int i = 0; i < result->num_clauses; i++) {
if (c->tag == result->clauses[i]->tag && !clauses_differ(c, result->clauses[i]))
return result->clauses[i];
}
}
}
}
return NULL;
}
// Given a new clause, add to the KB and maps
void add_clause(kb *collection, clause *c) {
// TODO: call to duplicate literal filtering (add function and such)
// Add clause to overall clause list and index map
index_mapping *ientry = malloc(sizeof(*ientry));
c->index = ientry->key = next_index++;
ientry->value = c;
c->learned = collection->time;
tommy_list_insert_tail(&collection->clauses, &ientry->list_node, ientry);
tommy_hashlin_insert(&collection->index_map, &ientry->hash_node, ientry, tommy_hash_u64(0, &ientry->key, sizeof(ientry->key)));
if (c->tag == FIF) {
char *name = c->fif->conclusion->name;
// Index into fif hashmap
fif_mapping *result = tommy_hashlin_search(&collection->fif_map, fifm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
result->num_clauses++;
result->clauses = realloc(result->clauses, sizeof(*result->clauses)*result->num_clauses);
result->clauses[result->num_clauses-1] = c;
}
else {
fif_mapping *entry = malloc(sizeof(*entry));
entry->conclude_name = malloc(strlen(name)+1);
strcpy(entry->conclude_name, name);
entry->num_clauses = 1;
entry->clauses = malloc(sizeof(*entry->clauses));
entry->clauses[0] = c;
tommy_hashlin_insert(&collection->fif_map, &entry->node, entry, tommy_hash_u64(0, entry->conclude_name, strlen(entry->conclude_name)));
}
}
else {
// If non-fif, indexes clause into pos/neg hashmaps/lists
for (int j = 0; j < c->pos_count; j++)
map_add_clause(&collection->pos_map, &collection->pos_list, c->pos_lits[j], c);
for (int j = 0; j < c->neg_count; j++)
map_add_clause(&collection->neg_map, &collection->neg_list, c->neg_lits[j], c);
}
}
// Remove res tasks using clause
static void remove_res_tasks(kb *collection, clause *c) {
for (tommy_size_t i = 0; i < tommy_array_size(&collection->res_tasks); i++) {
res_task *current_task = tommy_array_get(&collection->res_tasks, i);
if (current_task != NULL && (current_task->x == c || current_task->y == c)) {
// Removed task set to null; null checked for when processing later
tommy_array_set(&collection->res_tasks, i, NULL);
free(current_task);
}
}
}
// Removes c from children list of p
static void remove_child(clause *p, clause *c) {
if (p != NULL) {
for (int j = 0; j < p->children_count; j++) {
if (p->children[j] == c) {
if (j < p->children_count-1)
p->children[j] = p->children[p->children_count-1];
p->children_count--;
if (p->children_count > 0) {
p->children = realloc(p->children, sizeof(*p->children) * p->children_count);
}
else {
free(p->children);
p->children = NULL;
}
break;
}
}
}
}
// Given a clause already existing in KB, remove from data structures
// Note that fif removal, or removal of a singleton clause used in a fif rule, may be very expensive
void remove_clause(kb *collection, clause *c) {
if (c->tag == FIF) {
// fif must be removed from fif_map and all fif tasks using it deleted
char *name = c->fif->conclusion->name;
fif_mapping *fifm = tommy_hashlin_search(&collection->fif_map, fifm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (fifm != NULL) {
tommy_hashlin_remove_existing(&collection->fif_map, &fifm->node);
free_fif_mapping(fifm);
}
for (int i = 0; i < c->fif->premise_count; i++) {
alma_function *f = fif_access(c, i);
name = name_with_arity(f->name, f->term_count);
fif_task_mapping *tm = tommy_hashlin_search(&collection->fif_tasks, fif_taskm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (tm != NULL) {
tommy_node *curr = tommy_list_head(&tm->tasks);
while (curr) {
fif_task *currdata = curr->data;
curr = curr->next;
if (currdata->fif == c) {
tommy_list_remove_existing(&tm->tasks, &currdata->node);
free_fif_task(currdata);
}
}
}
free(name);
}
}
else {
// non-fif must be unindexed from pos/neg maps, have resolution tasks and fif tasks (if in any) removed
for (int i = 0; i < c->pos_count; i++)
map_remove_clause(&collection->pos_map, &collection->pos_list, c->pos_lits[i], c);
for (int i = 0; i < c->neg_count; i++)
map_remove_clause(&collection->neg_map, &collection->neg_list, c->neg_lits[i], c);
remove_res_tasks(collection, c);
// May be used in fif tasks if it's a singleton clause
if (c->pos_count + c->neg_count == 1) {
int compare_pos = c->neg_count == 1;
char *cname = compare_pos ? c->neg_lits[0]->name : c->pos_lits[0]->name;
int cterms = compare_pos ? c->neg_lits[0]->term_count : c->pos_lits[0]->term_count;
int count = 1;
char **names = malloc(sizeof(*names));
int prev_count = 0;
char **prev_names = NULL;
// Always process mapping for singleton's clause
names[0] = compare_pos ? name_with_arity(c->neg_lits[0]->name, c->neg_lits[0]->term_count) : name_with_arity(c->pos_lits[0]->name, c->pos_lits[0]->term_count);
// Check if fif tasks exist singleton
if (tommy_hashlin_search(&collection->fif_tasks, fif_taskm_compare, names[0], tommy_hash_u64(0, names[0], strlen(names[0]))) != NULL) {
// Check all fif clauses for containing premise matching c
tommy_size_t bucket_max = collection->fif_map.low_max + collection->fif_map.split;
for (tommy_size_t pos = 0; pos < bucket_max; ++pos) {
tommy_hashlin_node *node = *tommy_hashlin_pos(&collection->fif_map, pos);
while (node) {
fif_mapping *data = node->data;
node = node->next;
for (int i = 0; i < data->num_clauses; i++) {
for (int j = 0; j < data->clauses[i]->fif->premise_count; j++) {
alma_function *premise = fif_access(data->clauses[i], j);
// For each match, collect premises after singleton's location
if (strcmp(premise->name, cname) == 0 && premise->term_count == cterms) {
if (j > 0) {
prev_count++;
prev_names = realloc(prev_names, sizeof(*prev_names) * prev_count);
alma_function *pf = fif_access(data->clauses[i], j-1);
prev_names[prev_count-1] = name_with_arity(pf->name, pf->term_count);
}
names = realloc(names, sizeof(*names) * (count + data->clauses[i]->fif->premise_count - j- 1));
for (int k = j+1; k < data->clauses[i]->fif->premise_count; k++) {
premise = fif_access(data->clauses[i], k);
names[count] = name_with_arity(premise->name, premise->term_count);
count++;
}
break;
}
}
}
}
}
// fif_task_mappings have tasks deleted if they have unified with c
for (int i = 0; i < count; i++) {
fif_task_mapping *tm = tommy_hashlin_search(&collection->fif_tasks, fif_taskm_compare, names[i], tommy_hash_u64(0, names[i], strlen(names[i])));
if (tm != NULL) {
tommy_node *curr = tommy_list_head(&tm->tasks);
while (curr) {
fif_task *currdata = curr->data;
curr = curr->next;
if (currdata->to_unify == c) {
currdata->to_unify = NULL;
}
}
}
free(names[i]);
}
free(names);
for (int i = 0; i < prev_count; i++) {
fif_task_mapping *tm = tommy_hashlin_search(&collection->fif_tasks, fif_taskm_compare, prev_names[i], tommy_hash_u64(0, prev_names[i], strlen(prev_names[i])));
if (tm != NULL) {
tommy_node *curr = tommy_list_head(&tm->tasks);
while (curr) {
fif_task *currdata = curr->data;
curr = curr->next;
for (int j = 0; j < currdata->num_unified; j++) {
if (currdata->unified_clauses[j] == c->index) {
tommy_list_remove_existing(&tm->tasks, &currdata->node);
free_fif_task(currdata);
}
}
}
}
free(prev_names[i]);
}
free(prev_names);
}
else {
// Doesn't appear in fif tasks, no need to check
free(names[0]);
free(names);
}
}
}
index_mapping *result = tommy_hashlin_search(&collection->index_map, im_compare, &c->index, tommy_hash_u64(0, &c->index, sizeof(c->index)));
tommy_list_remove_existing(&collection->clauses, &result->list_node);
tommy_hashlin_remove_existing(&collection->index_map, &result->hash_node);
free(result);
// Remove clause from the parents list of each of its children
for (int i = 0; i < c->children_count; i++) {
clause *child = c->children[i];
if (child != NULL) {
int new_count = child->parent_set_count;
// If a parent set has a clause matching, remove match
for (int j = 0; j < child->parent_set_count; j++) {
for (int k = 0; k < child->parents[j].count; k++) {
if (child->parents[j].clauses[k] == c) {
// If last parent in set, deallocate
if (child->parents[j].count == 1) {
new_count--;
child->parents[j].count = 0;
free(child->parents[j].clauses);
child->parents[j].clauses = NULL;
}
// Otherwise, reallocate without parent
else {
child->parents[j].count--;
child->parents[j].clauses[k] = child->parents[j].clauses[child->parents[j].count];
child->parents[j].clauses = realloc(child->parents[j].clauses, sizeof(*child->parents[j].clauses)*child->parents[j].count);
}
break;
}
}
}
if (new_count > 0) {
int loc = child->parent_set_count-1;
// Replace empty parent sets with clauses from end of parents
for (int j = child->parent_set_count-2; j >= 0; j--) {
if (child->parents[j].clauses == NULL) {
child->parents[j] = child->parents[loc];
loc--;
}
}
child->parents = realloc(child->parents, sizeof(*child->parents)*new_count);
}
else {
free(child->parents);
child->parents = NULL;
}
}
}
// Remove clause from the children list of each of its parents
for (int i = 0; i < c->parent_set_count; i++)
for (int j = 0; j < c->parents[i].count; j++)
remove_child(c->parents[i].clauses[j], c);
free_clause(c);
}
// Given a new fif clause, initializes fif task mappings held by fif_tasks for each premise of c
// Also places single fif_task into fif_task_mapping for first premise
void fif_task_map_init(kb *collection, clause *c) {
if (c->tag == FIF) {
for (int i = 0; i < c->fif->premise_count; i++) {
alma_function *f = fif_access(c, i);
char *name = name_with_arity(f->name, f->term_count);
fif_task_mapping *result = tommy_hashlin_search(&collection->fif_tasks, fif_taskm_compare, name, tommy_hash_u64(0, name, strlen(name)));
// If a task map entry doesn't exist for name of a literal, create one
if (result == NULL) {
result = malloc(sizeof(*result));
result->predname = name;
tommy_list_init(&result->tasks);
tommy_hashlin_insert(&collection->fif_tasks, &result->node, result, tommy_hash_u64(0, result->predname, strlen(result->predname)));
}
else
free(name);
// For first premise, initialze fif_task root for c
if (i == 0) {
fif_task *task = malloc(sizeof(*task));
task->fif = c;
task->bindings = malloc(sizeof(*task->bindings));
task->bindings->list = NULL;
task->bindings->num_bindings = 0;
task->premises_done = 0;
task->num_unified = 0;
task->unified_clauses = NULL;
task->to_unify = NULL;
task->proc_next = proc_valid(fif_access(c, 0));
tommy_list_insert_tail(&result->tasks, &task->node, task);
}
}
}
}
// Given a non-fif singleton clause, sets to_unify pointers for fif tasks that have next clause to process matching it
// If fif tasks have to_unify assigned; branches off new tasks as copies
void fif_tasks_from_clause(kb *collection, clause *c) {
if (c->fif == NONE && c->pos_count + c->neg_count == 1) {
char *name;
int pos = 1;
if (c->pos_count == 1)
name = name_with_arity(c->pos_lits[0]->name, c->pos_lits[0]->term_count);
else {
name = name_with_arity(c->neg_lits[0]->name, c->neg_lits[0]->term_count);
pos = 0;
}
fif_task_mapping *result = tommy_hashlin_search(&collection->fif_tasks, fif_taskm_compare, name, tommy_hash_u64(0, name, strlen(name)));
free(name);
if (result != NULL) {
int len = tommy_list_count(&result->tasks);
tommy_node *curr = tommy_list_head(&result->tasks);
// Process original list contents and deal with to_unify
int set_first = 0;
int i = 0;
while (curr && i < len) {
fif_task *data = curr->data;
// Only modify to_unify if next for fif isn't a procedure predicate
if (!data->proc_next) {
int sign_match = (pos && data->fif->fif->ordering[data->num_unified] < 0) || (!pos && data->fif->fif->ordering[data->num_unified] >= 0);
if (sign_match && !(data->num_unified == 0 && set_first)) {
if (data->to_unify == NULL)
data->to_unify = c;
else {
// If to_unify is already non-null, duplicate the task and assign to_unify of duplicate
fif_task *copy = malloc(sizeof(*copy));
copy->fif = data->fif;
copy->bindings = malloc(sizeof(*copy->bindings));
copy_bindings(copy->bindings, data->bindings);
copy->premises_done = data->premises_done;
copy->num_unified = data->num_unified;
copy->unified_clauses = malloc(sizeof(*data->unified_clauses) * data->num_unified);
memcpy(copy->unified_clauses, data->unified_clauses, sizeof(*data->unified_clauses) * data->num_unified);
copy->to_unify = c;
copy->proc_next = data->proc_next;
tommy_list_insert_tail(&result->tasks, ©->node, copy);
}
// Special case -- c should only -once- be put into to_unify of a task with none unified
set_first = 1;
}
}
curr = curr->next;
i++;
}
}
}
}
// Compare function to be used by tommy_hashlin_search for distrust_mapping
// Compares long arg to key of distrust_mapping
static int dm_compare(const void *arg, const void *obj) {
return *(const long*)arg - ((const distrust_mapping*)obj)->key;
}
int is_distrusted(kb *collection, long index) {
return tommy_hashlin_search(&collection->distrusted, dm_compare, &index, tommy_hash_u64(0, &index, sizeof(index))) != NULL;
}
// Checks if each of task's unified_clauses is distrusted
// Returns 1 if any are
static int fif_unified_distrusted(kb *collection, fif_task *task) {
for (int i = 0; i < task->num_unified; i++)
if (is_distrusted(collection, task->unified_clauses[i]))
return 1;
return 0;
}
// Called when each premise of a fif is satisfied
static clause* fif_conclude(kb *collection, fif_task *task, binding_list *bindings) {
clause *conclusion = malloc(sizeof(*conclusion));
conclusion->pos_count = 1;
conclusion->neg_count = 0;
conclusion->pos_lits = malloc(sizeof(*conclusion->pos_lits));
conclusion->neg_lits = NULL;
// Using task's overall bindings, obtain proper conclusion predicate
alma_function *conc_func = malloc(sizeof(*conc_func));
copy_alma_function(task->fif->fif->conclusion, conc_func);
for (int k = 0; k < conc_func->term_count; k++)
subst(bindings, conc_func->terms+k);
cleanup_bindings(bindings);
conclusion->pos_lits[0] = conc_func;
conclusion->parent_set_count = 1;
conclusion->parents = malloc(sizeof(*conclusion->parents));
conclusion->parents[0].count = task->num_unified + 1;
conclusion->parents[0].clauses = malloc(sizeof(*conclusion->parents[0].clauses) * conclusion->parents[0].count);
for (int i = 0; i < conclusion->parents[0].count-1; i++) {
long index = task->unified_clauses[i];
index_mapping *result = tommy_hashlin_search(&collection->index_map, im_compare, &index, tommy_hash_u64(0, &index, sizeof(index)));
conclusion->parents[0].clauses[i] = result->value;
}
conclusion->parents[0].clauses[conclusion->parents[0].count-1] = task->fif;
conclusion->children_count = 0;
conclusion->children = NULL;
conclusion->tag = NONE;
conclusion->fif = NULL;
set_variable_ids(conclusion, 1, NULL);
return conclusion;
}
// Continues attempting to unify from tasks set
// Task instances for which cannot be further progressed with current KB are added to stopped
// Completed tasks result in additions to new_clauses
static void fif_task_unify_loop(kb *collection, tommy_list *tasks, tommy_list *stopped, tommy_array *new_clauses) {
tommy_node *curr = tommy_list_head(tasks);
int len = tommy_list_count(tasks);
// Process original list contents
while (curr && len > 0) {
fif_task *next_task = curr->data;
curr = curr->next;
len--;
// Withdraw current task from set
tommy_list_remove_existing(tasks, &next_task->node);
int task_erased = 0;