-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTMLKB.c
7135 lines (6883 loc) · 256 KB
/
TMLKB.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 <assert.h>
#include "TMLKB.h"
#include "util.h"
#include "Node.h"
#define MAX_LINE_LENGTH 10000
#define MAX_NAME_LENGTH 1000
#define TRUE 1
#define FALSE 0
#define SUBCLASS 0
#define SUBPART 1
#define RELATION 2
#define ATTRIBUTE 3
#define EMPTY_LINE -1
#define ERROR -2
#define relWeight(relVals,rel) (rel->hard == 0 ? (((relVals[0] != 0) ? relVals[0]*rel->nwt : 0.0) \
+((relVals[1] != 0) ? relVals[1]*rel->pwt : 0.0) \
+((relVals[2] != 0) ? relVals[2]*logsum_float(rel->pwt,rel->nwt) : 0.0)) : \
((rel->hard == 1) ? (relVals[0] != 0 ? log(0.0) : 0.0) : (relVals[1] != 0 ? log(0.0) : 0.0)))
float attrWeight(Node* node, TMLAttribute* attr) {
TMLAttrValue* attrvalue;
TMLAttrValue* tmp;
float* args;
float sum;
int* mask = node->attrValues[attr->idx];
if (node->assignedAttr[attr->idx] != NULL) {
return node->assignedAttr[attr->idx]->wt;
}
args = (float*)malloc(sizeof(float)*attr->nvals);
if (mask == NULL) {
HASH_ITER(hh, attr->vals, attrvalue, tmp) {
args[attrvalue->idx] = attrvalue->wt;
}
} else {
HASH_ITER(hh, attr->vals, attrvalue, tmp) {
if (mask[attrvalue->idx] < 0) {
args[attrvalue->idx] = log(0.0);
} else {
args[attrvalue->idx] = attrvalue->wt;
}
}
}
sum = logsumarr_float(args, attr->nvals);
free(args);
return sum;
}
/**
* Sets up a new TMLKB struct
*/
TMLKB* TMLKBNew() {
TMLKB* kb = (TMLKB*)malloc(sizeof(TMLKB));
kb->root = NULL;
kb->classes = NULL;
kb->classNameToPtr = NULL;
kb->objectNameToPtr = NULL;
kb->objectPathToPtr = NULL;
kb->classToObjPtrs = NULL;
kb->objToRelFactStrs = NULL;
kb->topcl = NULL;
kb->numClasses = 0;
kb->logZ = 0.0;
kb->edits = NULL;
kb->mapSet = 0;
// Learning parameters
// TODO: read these from somewhere
kb->scPct = 1;
kb->relPctT = 1;
kb->relPctF = 1;
kb->l0 = 1.0;
kb->l1 = 1.0;
return kb;
}
/**
* Adds a KBEdit (i.e., information regarding a change to the TML KB)
* to the KBEdit stack.
*
* @param node node in the SPN the change is related to
* @param subclIdx If a change of a subclass of node's class, the index of that subclass
* @param relStr If an added relation fact, the name of the relation
* @param relIdx If an added relation fact, the index of the relation in the array of rels
* @param pol Polarity of the change
* @param prev Stack of KBEdits to push onto
* @return the updated KBEdit stack
*/
KBEdit* addKBEdit(Node* node, int subclIdx, char* relStr, int relIdx, int valIdx, int pol, KBEdit* prev) {
KBEdit* newEdit = (KBEdit*)malloc(sizeof(KBEdit));
newEdit->prev = prev;
newEdit->node = node;
newEdit->subclIdx = subclIdx;
newEdit->relStr = relStr;
newEdit->relIdx = relIdx;
newEdit->valIdx = valIdx;
newEdit->pol = pol;
return newEdit;
}
KBEdit* addAndCopyKBEdit(KBEdit* copy, KBEdit* prev) {
KBEdit* newEdit = (KBEdit*)malloc(sizeof(KBEdit));
newEdit->prev = prev;
newEdit->node = copy->node;
newEdit->subclIdx = copy->subclIdx;
newEdit->relStr = copy->relStr;
newEdit->relIdx = copy->relIdx;
newEdit->valIdx = copy->valIdx;
newEdit->pol = copy->pol;
return newEdit;
}
/**
* Splits a string of the form %s.%d into its string and number parts
* If string is not of that form, returns a copy of the string and
* num is set to 1.
*
* @param str string to split
* @param num stores the number %d that was split in the string
* @return the %s part of str
*/
char* findBasePartName(char* str, int* num) {
char buffer[MAX_NAME_LENGTH+1];
int correctScan = sscanf(str, "%[^][][%d]", buffer, num);
if (correctScan == 1) {
*num = 1;
return strdup(buffer);
} else if (correctScan == 0) {
*num = 1;
return strdup(str);
}
return strdup(buffer);
}
/**
* Create the shortest pathname for a node by finding its
* closest ancestor with a name and starting the pathname from there.
*/
char* createBestPathname(TMLKB* kb, Node* node) {
char* period = strrchr(node->pathname, '.');
Node* par;
char* newper;
char best[MAX_NAME_LENGTH+1];
char partRel[MAX_NAME_LENGTH+1];
TMLPart* part = NULL;
TMLClass* cl;
Node* tmpnode;
while (period != NULL) {
*period = '\0';
HASH_FIND(hh_path, kb->objectPathToPtr, node->pathname, strlen(node->pathname), par);
if (par == NULL)
HASH_FIND(hh, kb->objectNameToPtr, node->pathname, strlen(node->pathname), par);
if (par->name != NULL) {
sscanf(period+1, "%[^[]", partRel);
cl = node->cl;
tmpnode = node;
while (TRUE) {
part = getPart(cl, partRel);
if (part != NULL) break;
if (cl->nsubcls == 0 || tmpnode->assignedSubcl == -1) break;
if (tmpnode->subclMask == NULL) tmpnode = tmpnode->subcl;
else tmpnode = &(tmpnode->subcl[tmpnode->assignedSubcl]);
cl = tmpnode->cl;
}
if (part != NULL && part->maxNumParts == 1) {
newper = strrchr(node->pathname, '[');
if (newper == NULL) {
sprintf(best, "%s.%s", par->name, period+1);
*period = '.';
return strdup(best);
} else {
*newper = '\0';
sprintf(best, "%s.%s", par->name, period+1);
*period = '.';
*newper = '[';
return strdup(best);
}
}
sprintf(best, "%s.%s", par->name, period+1);
*period = '.';
return strdup(best);
}
newper = strrchr(node->pathname, '.');
*period = '.';
period = newper;
}
sscanf(period+1, "%[^[]", partRel);
cl = node->cl;
tmpnode = node;
while (TRUE) {
part = getPart(cl, partRel);
if (part != NULL) break;
if (cl->nsubcls == 0 || tmpnode->assignedSubcl == -1) break;
if (tmpnode->subclMask == NULL) tmpnode = tmpnode->subcl;
else tmpnode = &(tmpnode->subcl[tmpnode->assignedSubcl]);
cl = tmpnode->cl;
}
if (part != NULL && part->maxNumParts == 1) {
newper = strrchr(node->pathname, '[');
if (newper == NULL) {
return strdup(node->pathname);
} else {
*newper = '\0';
period = strdup(node->pathname);
*newper = '[';
return period;
}
} else return strdup(node->pathname);
}
char* createNamedRelStr(TMLKB* kb, Node* node, char* normalizedStr) {
char* partName = strchr(normalizedStr, '(');
char* comma = strchr(normalizedStr, ',');
char* newcomma;
Node* part;
int end = 0;
char ret_even[MAX_LINE_LENGTH+1];
char ret_odd[MAX_LINE_LENGTH+1];
char pathname[MAX_NAME_LENGTH+1];
int odd = 1;
if (partName == NULL) return strdup(normalizedStr);
*partName = '\0';
if (partName == NULL) return strdup(normalizedStr);
if (comma == NULL) {
comma = strchr(normalizedStr, ')');
if (comma == NULL) return strdup(normalizedStr);
*comma = '\0';
partName++;
if (partName == NULL) {
sprintf(ret_even, "%s()", normalizedStr);
partName--;
*partName = '(';
*comma = ')';
return strdup(ret_even);
}
HASH_FIND(hh_path, kb->objectPathToPtr, partName, strlen(partName), part);
if (part == NULL) {
if (node->pathname != NULL)
sprintf(pathname, "%s.%s", node->pathname, partName);
else
sprintf(pathname, "%s.%s", node->name, partName);
HASH_FIND(hh_path, kb->objectPathToPtr, pathname, strlen(pathname), part);
}
if (part == NULL) {
partName--;
*partName = '(';
*comma = ')';
return strdup(normalizedStr);
}
if (part->name != NULL && strchr(part->name, '.') == NULL)
sprintf(ret_even, "%s(%s)", normalizedStr, part->name);
else
sprintf(ret_even, "%s(%s)", normalizedStr, partName);
partName--;
*partName = '(';
*comma = ')';
return strdup(ret_even);
}
sprintf(ret_even, "%s(", normalizedStr);
*partName = '(';
partName++;
do {
*comma = '\0';
HASH_FIND(hh_path, kb->objectPathToPtr, partName, strlen(partName), part);
if (part == NULL) {
if (node->pathname != NULL)
sprintf(pathname, "%s.%s", node->pathname, partName);
else
sprintf(pathname, "%s.%s", node->name, partName);
HASH_FIND(hh_path, kb->objectPathToPtr, pathname, strlen(pathname), part);
}
if (part == NULL) {
if (end == 0) *comma = ',';
else *comma = ')';
return strdup(normalizedStr);
}
if (odd == 1) {
if (end == 0) {
sprintf(ret_odd, "%s%s,", ret_even, (part->name == NULL || strchr(part->name, '.') != NULL) ? partName : part->name);
} else {
sprintf(ret_odd, "%s%s)", ret_even, (part->name == NULL || strchr(part->name, '.') != NULL) ? partName : part->name);
}
odd = 0;
} else {
if (end == 0) {
sprintf(ret_even, "%s%s,", ret_odd, (part->name == NULL || strchr(part->name, '.') != NULL) ? partName : part->name);
} else {
sprintf(ret_even, "%s%s)", ret_odd, (part->name == NULL || strchr(part->name, '.') != NULL) ? partName : part->name);
}
odd = 1;
}
partName = comma+1;
if (end == 0) *comma = ',';
else *comma = ')';
if (end == 0) {
comma++;
newcomma = strchr(comma, ',');
if (newcomma == NULL) {
newcomma = strchr(comma, ')');
if (newcomma == NULL) {
return strdup(normalizedStr);
}
end = 1;
}
comma = newcomma;
} else break;
} while (TRUE);
if (odd == 1)
return strdup(ret_even);
else
return strdup(ret_odd);
}
/**
* Takes a relation name, object, and a set of argument nodes and creates a normalized string
* of that relation (e.g., the whole relation literal with no spaces).
* Ex. relation = "Parent", object="Smiths", argNodes=[Node(Anna,Person),Node(Bob,Person)]
* If addBase == 1, return "Parent(Smiths,Anna,Bob)"
* If addBase == 0, return "Parent(Anna,Bob)
*
* @param kb TML KB
* @param relation name of the relation
* @param object name of the object
* @param argNodes list of pointers to the nodes of the arguments of the relation literal
* @param nargs length of argNodes array
* @param addBase determines if the object name should be added to the relation string
* @return normalized string of the relation ground literal
*/
char* createNormalizedRelStr(TMLKB* kb, char* relation, char* object, Node** argNodes, int nargs, int addBase, int useNames) {
int a;
char ret_even[MAX_LINE_LENGTH+1];
char ret_odd[MAX_LINE_LENGTH+1];
char* partName;
// Node* obj;
char* partPtr;
if (nargs == 0) {
if (addBase == 1)
snprintf(ret_even, MAX_LINE_LENGTH, "%s(%s)", relation, object);
else
snprintf(ret_even, MAX_LINE_LENGTH, "%s()", relation);
return strdup(ret_even);
}
if (addBase == 1)
sprintf(ret_even, "%s(%s,", relation, object);
else
sprintf(ret_even, "%s(", relation);
for (a = 0; a < nargs; a++) {
if (useNames != 1 || argNodes[a]->name == NULL)
partName = argNodes[a]->pathname;
else
partName = argNodes[a]->name;
partPtr = strrchr(partName, '.');
if (partPtr == NULL) partPtr = partName;
else partPtr++;
if (strchr(partName, '[') != NULL || (useNames == 1 && strchr(partName, '.') == NULL)) {
if (a % 2 == 1) {
if (a != nargs-1)
sprintf(ret_even, "%s%s,", ret_odd, partPtr);
else
sprintf(ret_even, "%s%s)", ret_odd, partPtr);
} else {
if (a != nargs-1)
sprintf(ret_odd, "%s%s,", ret_even, partPtr);
else
sprintf(ret_odd, "%s%s)", ret_even, partPtr);
}
} else {
if (a % 2 == 1) {
if (a != nargs-1)
sprintf(ret_even, "%s%s[1],", ret_odd, partPtr);
else
sprintf(ret_even, "%s%s[1])", ret_odd, partPtr);
} else {
if (a != nargs-1)
sprintf(ret_odd, "%s%s[1],", ret_even, partPtr);
else
sprintf(ret_odd, "%s%s[1])", ret_even, partPtr);
}
}
}
if (nargs % 2 == 1) return strdup(ret_odd);
return strdup(ret_even);
}
/**
* Splits a relation rule/fact string into its argument's strings.
* Returns the normalized version of relStr where all the whitespace is removed.
*
* @param relStr string of the relation's arguments
* @param relation string of relation name
* @param nargs returns the number of arguments found
* @param args returns an array of the argument strings
* @return the normalized string
*/
char* splitRelArgsAndCreateNormalizedRelStr(TMLKB* kb, char* relStr, char* relation, int* nargs, char*** args, int useNames) {
int n = 0;
char obj_fmt_str[25];
char* iter = relStr;
int correctScan;
char obj[MAX_NAME_LENGTH+1];
char ret_even[MAX_LINE_LENGTH+1];
char ret_odd[MAX_LINE_LENGTH+1];
int a = 0;
Node* node;
char* name;
char* partPtr;
// If relStr is NULL, the relation has no arguments
if (relStr == NULL) {
snprintf(ret_even, MAX_LINE_LENGTH, "%s()", relation);
*nargs = 0;
*args = NULL;
return strdup(ret_even);
}
sprintf(ret_even, "%s(", relation);
snprintf(obj_fmt_str, 25, " %%%d[]a-zA-Z0-9.[:?] ", MAX_NAME_LENGTH);
// Counts how many arguments there are
while (TRUE) {
iter = strchr(iter, ',');
if (iter != NULL) { iter++; n++; }
else {n++; break; }
}
*nargs = n;
*args = (char**)malloc(n*sizeof(char*));
iter = relStr;
// Adds arguments to the normalized string as they are read in
for (a = 0; a < n; a++) {
correctScan = sscanf(iter, obj_fmt_str, obj);
if (correctScan != 1) {
return NULL;
}
HASH_FIND_STR(kb->objectNameToPtr, obj, node);
if (node == NULL) {
node = findNodeFromAnonName(kb, NULL, obj, 0);
if (node == NULL || node->pathname == NULL || useNames == 1)
name = obj;
else {
name = node->pathname;
partPtr = strrchr(name, '.');
if (partPtr == NULL) partPtr = name;
else partPtr++;
name = partPtr;
}
} else if (node->pathname == NULL || useNames == 1) {
name = obj;
} else {
name = node->pathname;
partPtr = strrchr(name, '.');
if (partPtr == NULL) partPtr = name;
else partPtr++;
name = partPtr;
}
(*args)[a] = strdup(obj);
if (a % 2 == 1) {
if (a != n-1)
sprintf(ret_even, "%s%s,", ret_odd, name);
else
sprintf(ret_even, "%s%s)", ret_odd, name);
} else {
if (a != n-1)
sprintf(ret_odd, "%s%s,", ret_even, name);
else
sprintf(ret_odd, "%s%s)", ret_even, name);
}
iter = strchr(iter, ',');
if (iter != NULL) iter++;
}
if (n % 2 == 1) return strdup(ret_odd);
return strdup(ret_even);
}
/**
* Takes a path name through the TML KB and finds the node associated with it
*
* @param kb TML KB
* @param base initial node in the path
* @param name path name
* @param init If init == 1, initialize nodes along the path (and the found node)
* @return the node referred to by the path
*/
Node* findNodeFromAnonName(TMLKB* kb, Node* base, const char* name, int init) {
char* period = strchr(name, '.');
char* nextperiod;
Node* node;
char part_fmt_str[50];
char part_fmt_str2[50];
char partname[MAX_NAME_LENGTH+1];
int partnum = -1;
int correctScan;
TMLPart* part;
int maxParts = -1; // unnecessary. needed for findPartDown()
// If there is no period in the name, then name refers to either
// (1) an object
// (2) a part name of the base object
snprintf(part_fmt_str, 50, "%%%d[^][][%%d]", MAX_NAME_LENGTH);
snprintf(part_fmt_str2, 50, "%%%d[^][]", MAX_NAME_LENGTH);
if (period == NULL) {
HASH_FIND_STR(kb->objectNameToPtr, name, node);
if (node == NULL && base != NULL) {
correctScan = sscanf(name, part_fmt_str, partname, &partnum);
if (correctScan == 2) {
if (init == 1)
node = findAndInitPartDown(kb, base, partname, partnum, base->name);
else
node = findPartDown(base, partname, partnum, 0, &maxParts);
} else {
correctScan = sscanf(name, part_fmt_str2, partname);
partnum = 1;
if (correctScan == 1) {
if (init == 1)
node = findAndInitPartDown(kb, base, partname, partnum, base->name);
else
node = findPartDown(base, partname, partnum, 0, &maxParts);
}
}
}
return node;
}
*period = '\0';
HASH_FIND_STR(kb->objectNameToPtr, name, node);
if (node == NULL) {
*period = '.';
return NULL;
}
// find next node in path for each "." in the name
do {
nextperiod = strchr(period+1, '.');
if (nextperiod == NULL) {
correctScan = sscanf(period+1, part_fmt_str, partname, &partnum);
if (correctScan == 2) {
if (init == 1) {
if (node->pathname == NULL)
node = findAndInitPartDown(kb, node, partname, partnum, node->name);
else
node = findAndInitPartDown(kb, node, partname, partnum, node->pathname);
} else
node = findPartDown(node, partname, partnum, 0, &maxParts);
} else {
if (init == 1) {
if (node->pathname == NULL)
node = findAndInitPartDown(kb, node, partname, 1, node->name);
else
node = findAndInitPartDown(kb, node, partname, 1, node->pathname);
} else
node = findPartDown(node, partname, 1, 0, &maxParts);
}
*period = '.';
return node;
}
*nextperiod = '\0';
correctScan = sscanf(period+1, part_fmt_str, partname, &partnum);
if (correctScan == 2) {
if (init == 1) {
if (node->pathname == NULL)
node = findAndInitPartDown(kb, node, partname, partnum, node->name);
else
node = findAndInitPartDown(kb, node, partname, partnum, node->pathname);
} else
node = findPartDown(node, partname, partnum, 0, &maxParts);
} else {
if (init == 1) {
if (node->pathname == NULL)
node = findAndInitPartDown(kb, node, partname, 1, node->name);
else
node = findAndInitPartDown(kb, node, partname, 1, node->pathname);
} else
node = findPartDown(node, partname, 1, 0, &maxParts);
}
*period = '.';
if (node == NULL) {
*nextperiod = '.';
return NULL;
}
period = nextperiod;
} while (TRUE);
}
/**
* Recursive helper method that updates the class of an object in the SPN.
*
* Recursively creates nodes for new known classes of an object.
* If tmlFactFile is null, then this is being called from interactive mode to add a new fact.
* In this case, a KBEdit is added to the stack of edits kb->edits.
*
* @param kb TMLKB struct
* @param name name of the object
* @param topNode node for this object with the previously finest class information known
* @param cl current class for the object we are updating
* @param finecl new class information that we know (i.e., Is(name,finecl) )
* @param tmlFactFile file with TML Facts (needed to close the file if an error occurs)
* @param linenum current line number of file (needed for errors)
* @return pointer for the object:subcl node
*/
Node* updateClassForNodeRecHelper(TMLKB* kb, KBEdit** editPtr, char* name, Node* topNode, TMLClass* cl, TMLClass* finecl, FILE* tmlFactFile, int linenum) {
int i, j;
Node* ret = NULL;
Node* obj;
Node* par;
int numRel;
TMLPart* part;
TMLPart* tmppart;
TMLRelation* rel;
TMLRelation* tmprel;
int* subclMask;
QNode* qnode;
QNode* classToObjPtrsList;
KBEdit* edits = (editPtr == NULL) ? NULL : *editPtr;
// If the class's parent is the same as the previously known finest class information
// for the object, create a node for the object:cl pair.
// Otherwise, recursively create the next coarsest node and return the pointer to
// the node for the object:cl pair.
if (cl->par == topNode->cl) {
if (topNode->subcl == NULL) {
topNode->subcl = (Node*)malloc(sizeof(Node));
topNode->subclMask = NULL;
topNode->changed = 1;
obj = topNode->subcl;
obj->par = NULL;
obj->name = NULL;
addParent(obj, topNode);
} else {
if (topNode->subclMask == NULL) {
topNode->subclMask = (int*)malloc(sizeof(int)*topNode->cl->nsubcls);
subclMask = topNode->subclMask;
for (i = 0; i < topNode->cl->nsubcls; i++) {
*subclMask = 1;
subclMask++;
}
}
topNode->assignedSubcl = cl->subclIdx;
obj = &(topNode->subcl[cl->subclIdx]);
if (editPtr != NULL) {
edits = addKBEdit(topNode, cl->subclIdx, NULL, -1, -1, 1, edits);
classToObjPtrsList = kb->classToObjPtrs[cl->id];
qnode = (QNode*)malloc(sizeof(QNode));
qnode->ptr = obj;
qnode->next = classToObjPtrsList;
kb->classToObjPtrs[cl->id] = qnode;
}
if (editPtr != NULL) *editPtr = edits;
return obj;
}
} else if (cl->par->level == topNode->cl->level && cl->par != topNode->cl) {
if (tmlFactFile == NULL) {
printf("Error on line %d in fact file: Object %s has been assigned two mismatching classes: %s and %s.\n", linenum, name, topNode->cl->name, finecl->name);
freeTMLKB(kb);
fclose(tmlFactFile);
exit(1);
}
printf("Object %s is of class %s which conflicts with new class information.\n", name, topNode->cl->name);
return NULL;
} else obj = updateClassForNodeRecHelper(kb, editPtr, name, topNode, cl->par, finecl, tmlFactFile, linenum);
if (obj == NULL) return NULL;
if (obj->name != NULL) {
if (cl == finecl) return obj;
if (obj->cl->par != NULL) {
par = *(obj->par);
par->assignedSubcl = cl->subclIdx;
if (par->subclMask == NULL) {
par->subclMask = (int*)malloc(sizeof(int)*par->cl->nsubcls);
subclMask = par->subclMask;
for (i = 0; i < par->cl->nsubcls; i++) {
*subclMask = 1;
subclMask++;
}
}
if (editPtr != NULL) {
kb->edits = addKBEdit(par, cl->subclIdx, NULL, -1, -1, 1, kb->edits);
classToObjPtrsList = kb->classToObjPtrs[cl->id];
qnode = (QNode*)malloc(sizeof(QNode));
qnode->ptr = obj;
qnode->next = classToObjPtrsList;
kb->classToObjPtrs[cl->id] = qnode;
}
if (editPtr != NULL) *editPtr = edits;
return &(par->subcl[cl->subclIdx]);
}
return obj;
}
// Set up obj:cl node
obj->cl = cl;
if (strchr(name, '.') == NULL)
obj->name = name;
obj->changed = 1;
i = 0;
obj->part = (Node***)malloc(sizeof(Node**)*cl->nparts);
HASH_ITER(hh, cl->part, part, tmppart) {
obj->part[i] = (Node**)malloc(sizeof(Node*)*part->n);
for (j = 0; j < part->n; j++)
obj->part[i][j] = NULL;
i++;
}
obj->relValues = (int**)malloc(sizeof(int*)*cl->nrels);
i = 0;
HASH_ITER(hh, cl->rel, rel, tmprel) {
obj->relValues[i] = (int*)malloc(sizeof(int)*3);
if (rel->hard == 0) {
obj->relValues[i][0] = 0;
obj->relValues[i][1] = 0;
obj->relValues[i][2] = rel->numposs;
} else if (rel->hard == 1) {
obj->relValues[i][0] = 0;
obj->relValues[i][1] = rel->numposs;
obj->relValues[i][2] = 0;
} else {
obj->relValues[i][0] = rel->numposs;
obj->relValues[i][1] = 0;
obj->relValues[i][2] = 0;
}
i++;
}
obj->assignedAttr = (TMLAttrValue**)malloc(sizeof(TMLAttrValue**)*cl->nattr);
for (i = 0; i < cl->nattr; i++)
obj->assignedAttr[i] = NULL;
obj->attrValues = (int**)malloc(sizeof(int*)*cl->nattr);
for (i = 0; i < cl->nattr; i++)
obj->attrValues[i] = NULL;
if (cl == finecl) {
obj->subcl = NULL;
obj->assignedSubcl = -1;
obj->subclMask = NULL;
ret = obj;
} else {
obj->subcl = (Node*)malloc(sizeof(Node));
addParent(obj->subcl, obj);
obj->subcl->name = NULL;
obj->assignedSubcl = -1;
ret = obj->subcl;
obj->subclMask = NULL;
}
if (obj->cl->par != NULL) (*(obj->par))->assignedSubcl = cl->subclIdx;
if (editPtr != NULL) *editPtr = edits;
return ret;
}
/**
* Recursive helper method that updates the SPN from a !Is(X,C) fact
*
* Recursively creates nodes for new known classes of an object.
* If tmlFactFile is null, then this is being called from interactive mode to add a new fact.
* In this case, a KBEdit is added to the stack of edits kb->edits.
*
* @param kb TMLKB struct
* @param name name of the object
* @param topNode node for this object with the previously finest class information known
* @param cl current class for the object we are updating
* @param finecl new class information that we know (i.e., !Is(name,finecl) )
* @param tmlFactFile file with TML Facts (needed to close the file if an error occurs)
* @param linenum current line number of file (needed for errors)
* @return pointer for the object:subcl node
*/
Node* blockClassForNodeRecHelper(TMLKB* kb, KBEdit** editPtr, char* name, Node* topNode, TMLClass* cl, TMLClass* finecl, FILE* tmlFactFile, int linenum) {
int i, j;
Node* ret = NULL;
Node* obj;
Node* subcl;
int numRel;
TMLPart* part;
TMLPart* tmppart;
TMLRelation* rel;
TMLRelation* tmprel;
int* subclMask;
KBEdit* edits = editPtr == NULL ? NULL : *editPtr;
if (cl->par == topNode->cl) {
if (topNode->subcl == NULL) {
topNode->subcl = (Node*)malloc(sizeof(Node)*topNode->cl->nsubcls);
topNode->subclMask = (int*)malloc(sizeof(int)*topNode->cl->nsubcls);
for (i = 0; i < topNode->cl->nsubcls; i++) {
topNode->subclMask[i] = 1;
topNode->subcl[i].par = NULL;
addParent(&(topNode->subcl[i]), topNode);
topNode->subcl[i].changed = 1;
topNode->subcl[i].name = NULL;
}
} else {
if (topNode->subclMask == NULL) {
topNode->subclMask = (int*)malloc(sizeof(int)*topNode->cl->nsubcls);
subclMask = topNode->subclMask;
for (i = 0; i < topNode->cl->nsubcls; i++) {
*subclMask = 1;
subclMask++;
}
}
}
if (finecl->par == topNode->cl) {
j = 0;
for (i = 0; i < topNode->cl->nsubcls; i++) {
subcl = &(topNode->subcl[i]);
if (subcl->cl == finecl) {
if (tmlFactFile == NULL) {
if (topNode->subclMask[i] < 0) {
printf("Nothing done. Class already blocked from previous evidence.\n");
return NULL;
}
else topNode->subclMask[i] = -1;
} else
topNode->subclMask[i] = 0;
} else {
if (topNode->subclMask[i] == 1) j = 1;
}
}
if (j == 0) {
if (tmlFactFile != NULL) {
printf("Error on line %d in fact file: Blocked classes block all subclasses of class %s.\n",
linenum, topNode->cl->name);
return NULL;
} else {
printf("Nothing done. Blocked classes block all subclasses of class %s.\n",
topNode->cl->name);
topNode->subclMask[finecl->subclIdx] = 1;
return NULL;
}
}
if (editPtr == NULL)
edits = addKBEdit(topNode, finecl->subclIdx, NULL, -1, -1, 0, edits);
if (editPtr != NULL) *editPtr = edits;
return topNode;
}
return &(topNode->subcl[cl->subclIdx]);
} else if (cl->par->level == topNode->cl->level && cl->par == topNode->cl) {
if (tmlFactFile == NULL) printf("Nothing done. Class already blocked from previous evidence.\n");
return NULL; // Redundant warning
} else obj = blockClassForNodeRecHelper(kb, editPtr, name, topNode, cl->par, finecl, tmlFactFile, linenum);
if (obj == NULL) return NULL; // block was found to be redundant. Nothing to be done.
if (obj->name != NULL) {
if (finecl->par == obj->cl) {
if (obj->subclMask == NULL) {
obj->subclMask = (int*)malloc(sizeof(int)*cl->nsubcls);
subclMask = obj->subclMask;
for (i = 0; i < obj->cl->nsubcls; i++) {
*subclMask = 1;
subclMask++;
}
}
if (tmlFactFile == NULL) obj->subclMask[finecl->subclIdx] = 0;
else if (obj->subclMask[i] == 1) obj->subclMask[finecl->subclIdx] = -1;
else {
printf("Nothing done. Class already blocked from previous evidence.\n");
return NULL;
}
j = 0;
for (i = 0; i < obj->cl->nsubcls; i++) {
if (obj->subclMask[i] == 1) j = 1;
}
if (j == 0) {
if (tmlFactFile != NULL) {
printf("Error on line %d in fact file: Blocked classes block all subclasses of class %s.\n",
linenum, obj->cl->name);
return NULL;
} else {
printf("Nothing done. Blocked classes block all subclasses of class %s.\n",
topNode->cl->name);
obj->subclMask[finecl->subclIdx] = 1;
return NULL;
}
}
if (editPtr == NULL)
edits = addKBEdit(obj, finecl->subclIdx, NULL, -1, -1, 0, edits);
if (editPtr != NULL) *editPtr = edits;
return obj;
}
if (editPtr != NULL) *editPtr = edits;
return &(obj->subcl[cl->subclIdx]);
}
if (cl != finecl) {
obj->cl = cl;
if (strchr(name, '.') == NULL)
obj->name = name;
obj->changed = 1;
i = 0;
HASH_ITER(hh, cl->part, part, tmppart) {
obj->part[i] = (Node**)malloc(sizeof(Node*)*part->n);
for (j = 0; j < part->n; j++)
obj->part[i][j] = NULL;
i++;
}
obj->relValues = (int**)malloc(sizeof(int*)*cl->nrels);
i = 0;
HASH_ITER(hh, cl->rel, rel, tmprel) {
obj->relValues[i] = (int*)malloc(sizeof(int)*3);
if (rel->hard == 0) {
obj->relValues[i][0] = 0;
obj->relValues[i][1] = 0;
obj->relValues[i][2] = rel->numposs;
} else if (rel->hard == 1) {
obj->relValues[i][0] = 0;
obj->relValues[i][1] = rel->numposs;
obj->relValues[i][2] = 0;
} else {
obj->relValues[i][0] = rel->numposs;
obj->relValues[i][1] = 0;
obj->relValues[i][2] = 0;
}
i++;
}
obj->assignedAttr = (TMLAttrValue**)malloc(sizeof(TMLAttrValue**)*cl->nattr);
for (i = 0; i < cl->nattr; i++)
obj->assignedAttr[i] = NULL;
obj->attrValues = (int**)malloc(sizeof(int*)*cl->nattr);
for (i = 0; i < cl->nattr; i++)
obj->attrValues[i] = NULL;
if (obj->subcl == NULL) {
obj->subcl = (Node*)malloc(obj->cl->nsubcls*sizeof(Node));
for (i = 0; i < cl->nsubcls; i++) {
obj->subcl[i].par = NULL;
addParent(&(obj->subcl[i]), obj);
obj->subcl[i].changed = 1;
obj->subcl[i].name = NULL;
}
}
if (obj->subclMask == NULL) {
obj->subclMask = (int*)malloc(sizeof(int)*cl->nsubcls);
subclMask = obj->subclMask;
for (i = 0; i < topNode->cl->nsubcls; i++) {
*subclMask = 1;
subclMask++;
}
}
for (i = 0; i < cl->nsubcls; i++) {
if (cl->subcl[i] == finecl) {
obj->subclMask[i] = 0;
} else
obj->subclMask[i] = 1;
}
obj->assignedSubcl = -1;
if (editPtr != NULL) *editPtr = edits;
return &(obj->subcl[cl->subclIdx]);
}
if (editPtr != NULL) *editPtr = edits;
return obj;
}
/**
* Updates the SPN to reflect new knowledge about an object's class.
* If new information is redundant, method returns the node defined
* by the coarsest type information for that object.
*
* @param kb TML KB
* @param name name of the object
* @param node node for the object with the coarsest class information
* @param cl class introduced in a fact Is(name,cl)
* @param tmlFactFile TML fact file (needed to close if there is an error)
* @param linenum current line number in the fact file (used for errors)
* @return node for this object with the coarsest type information
*/
Node* updateClassForNode(TMLKB* kb, KBEdit** editPtr, char* name, Node* node, TMLClass* cl, FILE* tmlFactFile, int linenum) {
int i;
TMLClass* parcl = cl->par;
Node* initNode = node;
if (node->cl->level == cl->level) {
if (node->cl != cl) {
if (tmlFactFile != NULL) {
printf("Error on line %d in fact file: Object %s has been assigned two mismatching classes: %s and %s.\n", linenum, name, node->cl->name, cl->name);
freeTMLKB(kb);
fclose(tmlFactFile);
exit(1);
}
printf("Object %s is of class %s which conflicts with new class information.\n", name, node->cl->name);
return NULL;
}
if (tmlFactFile == NULL) {
printf("Information is redundant. Object %s has already been assigned class %s.\n", name, cl->name);
return NULL;
}
return initNode; // Redundant information
}
while (node->assignedSubcl != -1) {
if (node->subclMask == NULL)
node = node->subcl;
else