-
Notifications
You must be signed in to change notification settings - Fork 0
/
Constraints.m
170 lines (141 loc) · 3.56 KB
/
Constraints.m
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
/***************************************************************************
Constraints.c
Constraint, variable, and other operations for DeltaBlue.
****************************************************************************/
#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import "List.h"
#import "Constraints.h"
#import "DeltaBlue.h"
#import "DBVariable.h"
#import "DBConstraint.h"
#import "DBSolver.h"
/******* Private *******/
static void Error(char*);
static void Error(errorString)
char* errorString;
{
printf("Constraints.c error: %s.\n", errorString);
exit(-1);
}
static void Noop(Constraint);
static void Noop(c)
Constraint c;
{
/* default execute procedure; does nothing */
};
/******* Variables *******/
Variable Variable_Create()
{
register Variable new;
new = (Variable) malloc(sizeof(VariableStruct));
if (new == NULL) Error("out of memory");
new->constraints = [NSMutableArray new];
new->mark = 0;
new->walkStrength = S_weakest;
new->stay = true;
return new;
}
Variable Variable_CreateConstant()
{
register Variable new;
new = (Variable) malloc(sizeof(VariableStruct));
if (new == NULL) Error("out of memory");
new->constraints = [NSMutableArray new];
new->mark = 0;
new->walkStrength = S_required;
new->stay = true;
return new;
}
void Variable_Destroy(v)
Variable v;
{
if (v->constraints == NULL) {
Error("bad VariableStruct; already freed?");
}
[v->constraints release];
v->constraints = NULL;
free(v);
}
/******* Constraints *******/
Constraint Constraint_Create(int variableCount, int strength)
{
register Constraint new;
int i;
new = (Constraint) calloc(1, sizeof(ConstraintStruct) + ((variableCount) * sizeof(Variable)));
if (new == NULL) Error("out of memory");
new->execute = Noop;
new->inputFlag = false;
new->strength = strength;
new->whichMethod = NO_METHOD;
new->methodCount = 0;
for (i = 0; i < 7; i++) {
new->methodOuts[i] = 0;
}
new->varCount = variableCount;
for (i = 0; i < new->varCount; i++) {
new->variables[i] = NULL;
}
new->methodBlocks = [NSMutableArray new];
return new;
}
void Constraint_Destroy(c)
Constraint c;
{
if (c->execute == NULL) {
Error("bad ConstraintStruct; already freed?");
}
c->execute = NULL;
free(c);
}
void Constraint_Print(c)
Constraint c;
{
int i, outIndex;
if (!SATISFIED(c)) {
printf("Unsatisfied(");
for (i = 0; i < c->varCount; i++) {
printf("%s",[[c->variables[i] description] UTF8String]);
printf(" ");
}
printf(")");
} else {
outIndex = c->methodOuts[c->whichMethod];
printf("Satisfied(");
for (i = 0; i < c->varCount; i++) {
if (i != outIndex) {
printf("%s",[[c->variables[i] description] UTF8String]);
printf(" ");
}
}
printf("-> ");
printf("%s",[[c->variables[outIndex] description] UTF8String]);
printf(")");
}
printf("\n");
}
/******* Miscellaneous Functions *******/
char* StrengthString(strength)
int strength;
{
static char temp[20];
switch (strength) {
case S_required: return "required";
case S_strongPreferred: return "strongPreferred";
case S_preferred: return "preferred";
case S_strongDefault: return "strongDefault";
case S_default: return "default";
case S_weakDefault: return "weakDefault";
case S_weakest: return "weakest";
default:
sprintf(temp, "strength[%d]", strength);
return temp;
}
}
void ExecutePlan(NSArray *plan)
{
for (DBConstraint *c in plan) {
[c execute];
}
}