-
Notifications
You must be signed in to change notification settings - Fork 1
/
allen.c
321 lines (293 loc) · 7.89 KB
/
allen.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
/*
* Name : allen.c
* Project : Allen temporal logic: seminar schedule verifier
* Author : David Hildenbrand, Tobias Schoknecht
* Copyright : David Hildenbrand, Tobias Schoknecht 2012
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* [email protected] and [email protected] wrote this file.
* As long as you retain this notice you can do whatever you want with this
* stuff. If we meet some day, and you think this stuff is worth it, you can
* buy us a beer in return David Hildenbrand, Tobias Schoknecht
* ----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "allen.h"
const allen_relation allen_table[REL_COUNT][REL_COUNT] = { { Aeq, Asm, Abi, Ad,
Adi, Ao, Aoi, Am, Ami, As, Asi, Af, Afi }, { Asm, Asm, All, Asm | Ao | Am | Ad
| As, Asm, Asm, Asm | Ao | Am | Ad | As, Asm, Asm | Ao | Am | Ad | As,
Asm, Asm, Asm | Ao | Am | Ad | As, Asm }, { Abi, All, Abi, Abi | Aoi
| Ami | Ad | Af, Abi, Abi | Aoi | Ami | Ad | Af, Abi, Abi | Aoi | Ami
| Ad | Af, Abi, Abi | Aoi | Ami | Ad | Af, Abi, Abi, Abi }, { Ad, Asm,
Abi, Ad, All, Asm | Ao | Am | Ad | As, Abi | Aoi | Ami | Ad | Af, Asm,
Abi, Ad, Abi | Aoi | Ami | Ad | Af, Ad, Asm | Ao | Am | Ad | As }, {
Adi, Asm | Ao | Am | Adi | Afi, Asm | Aoi | Ami | Adi | Asi, Aeq | Ad | Adi | Ao
| Aoi | As | Asi | Af | Afi, Adi, Ao | Adi | Afi, Aoi | Adi | Asi, Ao
| Adi | Afi, Aoi | Adi | Asi, Ao | Adi | Afi,
Adi, Aoi | Adi | Asi, Adi }, { Ao, Asm, Abi | Aoi | Ami | Adi | Asi, Ao | Ad
| As, Asm | Ao | Am | Adi | Afi, Asm | Ao | Am, Aeq | Ad | Adi | Ao
| Aoi | As | Asi | Af | Afi, Asm, Aoi | Adi | Asi, Ao, Adi | Afi | Ao,
Ad | As | Ao, Asm | Ao | Am }, { Aoi, Asm | Ao | Am | Adi | Afi, Abi,
Aoi | Ad | Af, Asm | Aoi | Ami | Adi | Asi, Aeq | Ad | Adi | Ao | Aoi | As | Asi
| Af | Afi, Abi | Aoi | Ami, Ao | Adi | Afi, Abi, Aoi | Ad | Af, Abi
| Aoi | Ami, Aoi, Aoi | Asi | Adi }, { Am, Asm,
Abi | Aoi | Ami | Adi | Asi, Ao | Ad | As, Asm, Asm, Ao | Ad | As, Asm,
Aeq | Af | Afi, Am, Am, Ad | As | Ao, Asm }, { Ami, Asm | Ao | Am | Adi | Afi,
Abi, Aoi | Ad | Af, Abi, Aoi | Ad | Af, Abi, Aeq | As | Asi,
Abi, Ad | Af | Aoi, Abi, Ami, Ami }, { As, Asm, Abi, Ad, Asm | Ao | Am
| Adi | Afi, Asm | Ao | Am, Aoi | Ad | Af, Asm, Ami, As, Aeq | As | Asi,
Ad, Asm | Ao | Am }, { Asi, Asm | Ao | Am | Adi | Afi, Abi, Aoi | Ad | Af, Adi,
Ao | Adi | Afi, Aoi, Ao | Adi | Afi, Ami, Aeq | As | Asi,
Asi, Aoi, Adi }, { Af, Asm, Abi, Ad, Abi | Aoi | Ami | Adi | Asi, Ao
| Ad | As, Abi | Aoi | Ami, Am, Abi, Ad, Abi | Aoi | Ami, Af, Aeq | Af
| Afi }, { Afi, Asm, Abi | Aoi | Ami | Adi | Asi, Ao | Ad | As, Adi, Ao,
Aoi | Adi | Asi, Am, Aoi | Adi | Asi, Ao, Adi, Aeq | Af | Afi, Afi } };
allen_relation reverse_allen_rel(allen_relation source) {
allen_relation destination = 0x0;
if (source & Aeq)
destination |= Aeq;
if (source & Asm)
destination |= Abi;
if (source & Abi)
destination |= Asm;
if (source & Ad)
destination |= Adi;
if (source & Adi)
destination |= Ad;
if (source & Ao)
destination |= Aoi;
if (source & Aoi)
destination |= Ao;
if (source & Am)
destination |= Ami;
if (source & Ami)
destination |= Am;
if (source & As)
destination |= Asi;
if (source & Asi)
destination |= As;
if (source & Af)
destination |= Afi;
if (source & Afi)
destination |= Af;
return destination;
}
char* allen_rel_to_ascii(allen_relation source) {
char buffer[128];
unsigned short pos = 0;
if (source & Aeq) {
buffer[pos++] = '=';
buffer[pos++] = ' ';
}
if (source & Asm) {
buffer[pos++] = '<';
buffer[pos++] = ' ';
}
if (source & Abi) {
buffer[pos++] = '>';
buffer[pos++] = ' ';
}
if (source & Ad) {
buffer[pos++] = 'd';
buffer[pos++] = ' ';
}
if (source & Adi) {
buffer[pos++] = 'd';
buffer[pos++] = 'i';
buffer[pos++] = ' ';
}
if (source & Ao) {
buffer[pos++] = 'o';
buffer[pos++] = ' ';
}
if (source & Aoi) {
buffer[pos++] = 'o';
buffer[pos++] = 'i';
buffer[pos++] = ' ';
}
if (source & Am) {
buffer[pos++] = 'm';
buffer[pos++] = ' ';
}
if (source & Ami) {
buffer[pos++] = 'm';
buffer[pos++] = 'i';
buffer[pos++] = ' ';
}
if (source & As) {
buffer[pos++] = 's';
buffer[pos++] = ' ';
}
if (source & Asi) {
buffer[pos++] = 's';
buffer[pos++] = 'i';
buffer[pos++] = ' ';
}
if (source & Af) {
buffer[pos++] = 'f';
buffer[pos++] = ' ';
}
if (source & Afi) {
buffer[pos++] = 'f';
buffer[pos++] = 'i';
buffer[pos++] = ' ';
}
char* copy = NULL;
if (pos == 0) {
copy = (char*) malloc(4 * sizeof(char));
copy = strcpy(copy,"NIL");
} else {
copy = (char*) malloc((pos + 1) * sizeof(char));
buffer[--pos] = '\0';
copy = strcpy(copy,buffer);
}
return copy;
}
allen_relation allen_rel_from_ascii(char *source) {
int i;
allen_relation destination = 0;
if (source == NULL)
return 0;
for (i = 0; source[i] != '\0'; ++i) {
switch (source[i]) {
case '=':
destination |= Aeq;
break;
case '<':
destination |= Asm;
break;
case '>':
destination |= Abi;
break;
case 'd':
if (source[i + 1] == 'i') {
destination |= Adi;
} else
destination |= Ad;
break;
case 'm':
if (source[i + 1] == 'i') {
destination |= Ami;
} else
destination |= Am;
break;
case 'o':
if (source[i + 1] == 'i') {
destination |= Aoi;
} else
destination |= Ao;
break;
case 's':
if (source[i + 1] == 'i') {
destination |= Asi;
} else
destination |= As;
break;
case 'f':
if (source[i + 1] == 'i') {
destination |= Afi;
} else
destination |= Af;
break;
default:
break;
};
}
return destination;
}
allen_relation conjunct_allen_rel(allen_relation a, allen_relation b) {
return a | b;
}
allen_relation substract_allen_rel(allen_relation a, allen_relation b) {
return a & (~b);
}
allen_relation intersect_allen_rel(allen_relation a, allen_relation b) {
return a & b;
}
allen_relation allen_p_function(allen_relation a, allen_relation b) {
allen_relation destination = 0x0, temp;
short i, j;
for (i = 0; i < REL_COUNT; a >>= 1, ++i) {
if (a & 0x1) {
temp = b;
for (j = 0; j < REL_COUNT; temp >>= 1, ++j) {
if (temp & 0x1) {
destination = conjunct_allen_rel(destination,
allen_table[i][j]);
}
}
}
}
return destination;
}
short check_allen_rel_consistency(allen_relation ab, allen_relation bc,
allen_relation ac) {
allen_relation calculated_ac = allen_p_function(ab, bc);
if (intersect_allen_rel(ac, calculated_ac) == 0)
return 1;
else
return 0;
}
allen_relation allen_rel_from_intervals(unsigned int starta, unsigned int stopa,
unsigned int startb, unsigned int stopb) {
allen_relation destination = 0;
if (starta > stopa) {
unsigned int temp = starta;
starta = stopa;
stopa = temp;
}
if (startb > stopb) {
unsigned int temp = startb;
startb = stopb;
stopb = temp;
}
if (starta == startb) {
if (stopa == stopb)
destination |= Aeq;
else if (stopa < stopb)
destination |= As;
else
destination |= Asi;
} else if (stopa == stopb) {
if (starta > startb)
destination |= Af;
else
destination |= Afi;
} else if (starta < startb) {
if (stopa < startb)
destination |= Asm;
else if (stopa == startb)
destination |= Am;
else if (stopa > stopb)
destination |= Adi;
else
destination |= Ao;
} else if (startb < starta) {
if (stopb < starta)
destination |= Abi;
else if (stopb == starta)
destination |= Ami;
else if (stopb > stopa)
destination |= Ad;
else
destination |= Aoi;
}
return destination;
}
void print_allen_table(FILE* file, char delimiter) {
int i, j;
if (file == NULL)
return;
fprintf(file, "\n");
for (i = 0; i < REL_COUNT; ++i) {
for (j = 0; j < REL_COUNT; ++j) {
char* rel = allen_rel_to_ascii(allen_table[i][j]);
fprintf(file, "%s%c", rel, delimiter);
free(rel);
}
fprintf(file, "\n");
}
fflush(file);
}