Repository navigation
Expand file tree
/
Copy pathTreeStatistics.c
More file actions
343 lines (312 loc) · 11.8 KB
/
Copy pathTreeStatistics.c
File metadata and controls
343 lines (312 loc) · 11.8 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
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "DataTypes.h"
#include "Utilities.h"
#include "Examine.h"
#include "Parsing.h"
#include "ListStatistics.h"
#include "List.h"
#include "Tree.h"
#include "TreeStatistics.h"
//-------------------------------------------------------------------------------------------------
void InitializeSolutionStatistics(SolutionStatisticsType * Statistics) {
Statistics->Type = nonszsoutput;
InitializeStatistics(&(Statistics->FormulaeStatistics));
Statistics->ForestDepth = 0;
Statistics->ForestLeaves = 0;
Statistics->FiniteDomainSize = 0;
}
//-------------------------------------------------------------------------------------------------
int TreeHasCycle(TREENODE Root) {
int Index;
//----A real tree out there? (1 = black)
if (Root->Visited == 1) {
return(0);
}
//----Been here before? (-1 = grey)
if (Root->Visited == -1) {
return(1);
}
//----Try all children
Root->Visited = -1;
for (Index = 0; Index < Root->NumberOfParents; Index++) {
if (TreeHasCycle(Root->Parents[Index])) {
return(1);
}
}
Root->Visited = 1;
return(0);
}
//-------------------------------------------------------------------------------------------------
int RootListHasCycle(ROOTLIST RootListHead) {
ResetRootListVisited(RootListHead);
while (RootListHead != NULL) {
if (TreeHasCycle(RootListHead->TheTree)) {
return(1);
}
RootListHead = RootListHead->Next;
}
return(0);
}
//-------------------------------------------------------------------------------------------------
int GetTreeDepth(TREENODE Tree) {
int Depth;
int index;
Depth = 0;
if (Tree->NumberOfParents > 0) {
for (index = 0;index < Tree->NumberOfParents;index++) {
Depth = MaximumOfInt(Depth,GetTreeDepth(Tree->Parents[index]));
}
Depth++;
}
return(Depth);
}
//-------------------------------------------------------------------------------------------------
int NewLeavesInTree(TREENODE Tree) {
int NewLeaves;
int index;
if (Tree->Visited) {
return(0);
} else {
Tree->Visited = 1;
if (Tree->NumberOfParents == 0) {
return(1);
} else {
NewLeaves = 0;
for (index = 0;index < Tree->NumberOfParents;index++) {
NewLeaves += NewLeavesInTree(Tree->Parents[index]);
}
return(NewLeaves);
}
}
}
//-------------------------------------------------------------------------------------------------
LISTNODE MakeListOfTreeAnnotatedFormulae(TREENODE Tree) {
LISTNODE NewNodesInThisTree;
int index;
if (Tree->Visited) {
return(NULL);
} else {
Tree->Visited = 1;
NewNodesInThisTree = NULL;
AddListNode(&NewNodesInThisTree,NULL,Tree->AnnotatedFormula);
for (index = 0;index < Tree->NumberOfParents;index++) {
NewNodesInThisTree = AppendListsOfAnnotatedTSTPNodes(MakeListOfTreeAnnotatedFormulae(
Tree->Parents[index]),NewNodesInThisTree);
}
return(NewNodesInThisTree);
}
}
//-------------------------------------------------------------------------------------------------
SolutionStatisticsType GetForestStatistics(SZSOutputType SolutionType,ROOTLIST Head,
SIGNATURE Signature) {
SolutionStatisticsType Statistics;
LISTNODE ListOfForestNodes;
ROOTLIST MovingHead;
InitializeSolutionStatistics(&Statistics);
Statistics.Type = SolutionType;
if (Head == NULL) {
Statistics.Type = nonszsoutput;
return(Statistics);
}
if (RootListHasCycle(Head)) {
ReportError("SemanticError","Cycle in a tree",0);
Statistics.Type = nonszsoutput;
return(Statistics);
}
ResetRootListVisited(Head);
MovingHead = Head;
while (MovingHead != NULL) {
Statistics.ForestDepth = MaximumOfInt(Statistics.ForestDepth,GetTreeDepth(
MovingHead->TheTree));
Statistics.ForestLeaves += NewLeavesInTree(MovingHead->TheTree);
MovingHead = MovingHead->Next;
}
ListOfForestNodes = NULL;
ResetRootListVisited(Head);
MovingHead = Head;
while (MovingHead != NULL) {
ListOfForestNodes = AppendListsOfAnnotatedTSTPNodes(ListOfForestNodes,
MakeListOfTreeAnnotatedFormulae(MovingHead->TheTree));
MovingHead = MovingHead->Next;
}
Statistics.FormulaeStatistics = GetListStatistics(ListOfForestNodes,Signature);
FreeListOfAnnotatedFormulae(&ListOfForestNodes,Signature);
return(Statistics);
}
//-------------------------------------------------------------------------------------------------
void PrintForestStatistics(FILE * Stream,SolutionStatisticsType Statistics) {
fprintf(Stream,"%% Derivation depth : %4d\n",Statistics.ForestDepth);
fprintf(Stream,"%% Number of leaves : %4d\n",Statistics.ForestLeaves);
PrintListStatistics(Stream,Statistics.FormulaeStatistics);
}
//-------------------------------------------------------------------------------------------------
void PrintFiniteModelStatistics(FILE * Stream,SolutionStatisticsType Statistics) {
fprintf(Stream,"%% Domain size : %4d\n",Statistics.FiniteDomainSize);
}
//-------------------------------------------------------------------------------------------------
int ExtractFiniteDomainSize(FORMULA Formula) {
int LHSize,RHSize;
//----If an equality atom, return 1
if (Formula->Type == atom && !strcmp("=",GetSymbol(Formula->FormulaUnion.Atom)) &&
GetArity(Formula->FormulaUnion.Atom) == 2) {
return(1);
} else if (Formula->Type == binary && Formula->FormulaUnion.BinaryFormula.Connective ==
disjunction) {
if (!(LHSize = ExtractFiniteDomainSize(Formula->FormulaUnion.BinaryFormula.LHS)) ||
!(RHSize = ExtractFiniteDomainSize(Formula->FormulaUnion.BinaryFormula.RHS))) {
return(0);
} else {
return(LHSize + RHSize);
}
} else {
return(0);
}
}
//-------------------------------------------------------------------------------------------------
int HasAFiniteDomain(LISTNODE Head) {
FORMULA Formula;
while (Head != NULL) {
if (GetRole(Head->AnnotatedFormula,NULL) == fi_domain) {
Formula = GetListNodeFormula(Head);
//----Check that it's a FOF universal
if (Head->AnnotatedFormula->Syntax != tptp_fof || Formula->Type != quantified ||
Formula->FormulaUnion.QuantifiedFormula.Quantifier != universal) {
return(0);
}
//----Top level equality is size 1
return(ExtractFiniteDomainSize(Formula->FormulaUnion.QuantifiedFormula.Formula));
}
Head = Head->Next;
}
return(0);
}
//-------------------------------------------------------------------------------------------------
int IsARefutation(ROOTLIST RootListHead) {
FORMULA Formula;
AnnotatedTSTPFormulaType * AnnotatedTSTPFormula;
//----All roots must be false for a refutation
while (RootListHead != NULL) {
AnnotatedTSTPFormula = GetTreeNodeAnnotatedTSTPFormula(RootListHead->TheTree);
if (CheckRole((*AnnotatedTSTPFormula).Status,axiom_like) ||
RootListHead->TheTree->NumberOfParents > 0) {
Formula = GetTreeNodeFormula(RootListHead->TheTree);
if (Formula->Type == atom && !strcmp("$false",GetSymbol(Formula->FormulaUnion.Atom))) {
return(1);
}
}
RootListHead = RootListHead->Next;
}
return(0);
}
//-------------------------------------------------------------------------------------------------
SZSOutputType GetSZSOutputType(LISTNODE Head,ROOTLIST * RootListHead,int * FiniteDomainSize,
SIGNATURE Signature,SZSOutputType ClaimedSZSOuput) {
String ErrorMessage;
*RootListHead = NULL;
if (Head == NULL) {
return(nonszsoutput);
//----Check for a finite domain
} else if ((*FiniteDomainSize = HasAFiniteDomain(Head)) > 0) {
//DEBUG printf("Found a finite domain\n");
if (ClaimedSZSOuput != nonszsoutput && !SZSOutputIsA(ClaimedSZSOuput,FMo) &&
!SZSOutputIsA(FMo,ClaimedSZSOuput)) {
sprintf(ErrorMessage,"SZS output says it's a %s, but found a finite domain model",
SZSOutputToUserString(ClaimedSZSOuput));
ReportError(NULL,ErrorMessage,0);
}
return(FMo);
//----Build a root list, but it might not really be one
} else if ((*RootListHead = BuildRootList(Head,Signature)) != NULL) {
//DEBUG printf("Built rootlist\n");
//----Refutation is a good root list
if (IsARefutation(*RootListHead)) {
//DEBUG printf("Rootlist is a refutation\n");
if (ClaimedSZSOuput != nonszsoutput && !SZSOutputIsA(ClaimedSZSOuput,Ref) &&
!SZSOutputIsA(Ref,ClaimedSZSOuput)) {
sprintf(ErrorMessage,"SZS output says it's a %s, but found a refutation",
SZSOutputToUserString(ClaimedSZSOuput));
ReportError(NULL,ErrorMessage,0);
}
return(Ref);
//----One tree in the forest is good
} else if ((*RootListHead)->Next == NULL) {
//DEBUG printf("Rootlist not a refutation\n");
if (ClaimedSZSOuput != nonszsoutput && !SZSOutputIsA(ClaimedSZSOuput,Der) &&
!SZSOutputIsA(Der,ClaimedSZSOuput)) {
sprintf(ErrorMessage,"SZS output says it's a %s, but found a derivation",
SZSOutputToUserString(ClaimedSZSOuput));
ReportError(NULL,ErrorMessage,0);
}
return(Der);
} else {
//DEBUG printf("Some kind of list\n");
FreeRootList(RootListHead,1,Signature);
if (SZSOutputIsA(ClaimedSZSOuput,Sat)) {
//TODO check it is a nice list of formulae
return(Sat);
} else if (SZSOutputIsA(ClaimedSZSOuput,Mod)) {
return(TMo);
} else {
return(nonszsoutput);
}
}
}
return(nonszsoutput);
}
//-------------------------------------------------------------------------------------------------
//----If the signature is non-NULL use it for symbols
SolutionStatisticsType GetSolutionStatistics(LISTNODE Head,SIGNATURE Signature,
ROOTLIST * RootListHead,SZSResultType ClaimedSZSResult,SZSOutputType ClaimedSZSOutput) {
SolutionStatisticsType Statistics;
Statistics.Type = GetSZSOutputType(Head,RootListHead,&(Statistics.FiniteDomainSize),Signature,
ClaimedSZSOutput);
//DEBUG printf("Soln type index is %d\n",Statistics.Type);
//DEBUG printf("Soln is a %s\n",SZSOutputToUserString(Statistics.Type));
switch (Statistics.Type) {
case CRf:
case Ref:
case Der:
Statistics = GetForestStatistics(Statistics.Type,*RootListHead,Signature);
break;
case FMo:
//----Got the size in GetSZSOutputType
break;
case TMo:
case Sat:
Statistics.FormulaeStatistics = GetListStatistics(Head,Signature);
break;
case Non:
case nonszsoutput:
break;
default:
break;
}
return(Statistics);
}
//-------------------------------------------------------------------------------------------------
void PrintSolutionStatistics(FILE * Stream,SolutionStatisticsType Statistics) {
fprintf(Stream,"%% SZS Type : %s\n",SZSOutputToUserString(Statistics.Type));
switch (Statistics.Type) {
case CRf:
case Ref:
case Der:
PrintForestStatistics(Stream,Statistics);
break;
case FMo:
PrintFiniteModelStatistics(Stream,Statistics);
break;
case TMo:
case Sat:
PrintListStatistics(Stream,Statistics.FormulaeStatistics);
break;
case Non:
case nonszsoutput:
break;
default:
break;
}
}
//-------------------------------------------------------------------------------------------------