File: consecutive.c

package info (click to toggle)
tablix2 0.3.5-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,024 kB
  • sloc: ansic: 24,593; xml: 13,161; sh: 10,409; makefile: 800; perl: 564; yacc: 289; sed: 16
file content (361 lines) | stat: -rw-r--r-- 9,883 bytes parent folder | download | duplicates (4)
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
/* TABLIX, PGA general timetable solver                                    */
/* Copyright (C) 2002-2006 Tomaz Solc                                      */

/* This program is free software; you can redistribute it and/or modify    */
/* it under the terms of the GNU General Public License as published by    */
/* the Free Software Foundation; either version 2 of the License, or       */
/* (at your option) any later version.                                     */

/* This program is distributed in the hope that it will be useful,         */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of          */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           */
/* GNU General Public License for more details.                            */

/* You should have received a copy of the GNU General Public License       */
/* along with this program; if not, write to the Free Software             */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

/* $Id: consecutive.c,v 1.11 2006-08-29 14:32:24 avian Exp $ */

/** @module
 *
 * @author Tomaz Solc
 * @author-email tomaz.solc@tablix.org
 *
 * @credits
 * Original idea for this module by Nick Robinson (npr@bottlehall.co.uk).
 * Module was later rewritten to serve as an example for the new updater 
 * function feature in 0.3.1 by Tomaz Solc.
 *
 * @brief Adds support for events that must be scheduled adjacent to one
 * another. This module uses updater functions, so the weight and mandatory
 * values are ignored.
 *
 * @ingroup School scheduling, Multiweek scheduling
 */

/** @tuple-restriction consecutive
 *
 * This restriction specifies that the repeats of the current event need to 
 * be scheduled consecutively. 
 *
 * Please note that this module distinguishes events by the 
 * assignments of constant resources and event names. The way events are 
 * defined in the XML file has no effect. The following two examples will 
 * therefore both result in one block of four consecutive "Lecture" events.
 *
 * Example 1:
 *
 * <event name ="Lecture" repeats="2">
 * 	<resource type="teacher"  name="A"/>
 * 	<resource type="class"  name="B"/>
 * 	<restriction type="consecutive"/>
 * </event>
 * <event name ="Lecture" repeats="2">
 * 	<resource type="teacher"  name="A"/>
 * 	<resource type="class"  name="B"/>
 * 	<restriction type="consecutive"/>
 * </event>
 *
 * Example 2:
 *
 * <event name ="Lecture" repeats="4">
 * 	<resource type="teacher"  name="A"/>
 * 	<resource type="class"  name="B"/>
 * 	<restriction type="consecutive"/>
 * </event>
 *
 * If you would like to have two blocks of two consecutive "Lecture" events, 
 * you must either change the names of two events like in the following example 
 * or use the periods-per-block restriction.
 * 
 * <event name ="Lecture 1" repeats="2">
 * 	<resource type="teacher"  name="A"/>
 * 	<resource type="class"  name="B"/>
 * 	<restriction type="consecutive"/>
 * </event>
 * <event name ="Lecture 2" repeats="2">
 * 	<resource type="teacher"  name="A"/>
 * 	<resource type="class"  name="B"/>
 * 	<restriction type="consecutive"/>
 * </event>
 */

/** @tuple-restriction periods-per-block
 *
 * This restriction specifies that the repeats of the current event need to 
 * be scheduled blocks of N consecutive events. If the number of repeats is
 * not divisible by N, then one block will have less than N events.
 *
 * Events in the following example will be scheduled in two blocks of three
 * consecutive events, two blocks of two consecutive events and one single
 * event.
 *
 * <event name ="Lecture" repeats="6">
 * 	<resource type="teacher" name="A"/>
 * 	<resource type="class" name="B"/>
 * 	<restriction type="periods-per-block">3</restriction>
 * </event>
 * <event name ="Lecture" repeats="5">
 * 	<resource type="teacher" name="A"/>
 * 	<resource type="class" name="B"/>
 * 	<restriction type="periods-per-block">2</restriction>
 * </event>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "module.h"

/* This structure describes a single consecutive block of events. */
struct cons_t {
	/* Array of tuple ids */
	int *tupleid;
	/* Number of tuple ids in the array */
	int tupleidnum;

	/* Maximum number of tuple IDs */
	int maxtupleidnum;

	struct cons_t *next;
};

/* Linked list describing all consecutive blocks */
static struct cons_t *cons=NULL;

static int time;

static int periods, days;

/* Adds a new cons_t structure to the linked list. */
static struct cons_t *cons_new(int maxtupleidnum)
{
	struct cons_t *dest;

	dest=malloc(sizeof(*dest));
	if(dest==NULL) return NULL;

	dest->tupleid=malloc(sizeof(*dest->tupleid)*maxtupleidnum);
	if(dest->tupleid==NULL) {
		free(dest);
		return NULL;
	}

	dest->tupleidnum=0;
	dest->maxtupleidnum=maxtupleidnum;

	dest->next=cons;

	cons=dest;

	return(dest);
}

/* This is the event restriction handler. */
int getevent(char *restriction, char *cont, tupleinfo *tuple)
{
	int tupleid;
	struct cons_t *cur;

	int maxtupleidnum, c;

	if(!strcmp("consecutive", restriction)) {
		/* "consecutive" restriction - groups of consecutive events
		 * have unlimited length. */

		if(strlen(cont)>0) {
			error(_("restriction '%s' does not take an "
						"argument"), restriction);
			return(-1);
		}
		maxtupleidnum=dat_tuplenum;
	} else if(!strcmp("periods-per-block", restriction)) {
		/* "periods-per-block" restriction - groups of consecutive
		 * events have limited length. */
		
        	c=sscanf(cont, "%d ", &maxtupleidnum);
	        if(c!=1||maxtupleidnum<1||maxtupleidnum>periods) {
	                error(_("Invalid number of periods for '%s' "
						"restriction"), restriction);
        	        return(-1);
	        }
	} else {
		assert(1==0);	
	}

	tupleid=tuple->tupleid;

	/* First we have to determine if the current event is a part of 
	 * consecutive group of events we already know. */

	cur=cons;
	while(cur!=NULL) {
		/* Since all events in a group should be equal, we only need
		 * to check against the first event in the group. Note the 
		 * a group always contains at least one event. */

		assert(cur->tupleidnum>0);

		if(tuple_compare(tupleid, cur->tupleid[0])) {

			/* Is this a group with the same maximum number of
			 * events? If not, we have to start a new group */

			if(cur->maxtupleidnum==maxtupleidnum) {
				/* Check if this group has space for one more 
				 * event */

				if(cur->tupleidnum<maxtupleidnum) {
					break;
				}
			}
		}
			
		cur=cur->next;
	}

	if(cur==NULL) {
		/* This event is a part of a new group */
		cur=cons_new(maxtupleidnum);

		/* Fail if we couldn't allocate memory */
		if(cur==NULL) {
			error(_("Can't allocate memory"));
			return -1;
		}

		cur->tupleid[0]=tupleid;
		cur->tupleidnum=1;
	} else {
		/* This event is a part of an existing group */
		cur->tupleid[cur->tupleidnum]=tupleid;
		cur->tupleidnum++;

		if(cur->tupleidnum>periods) {
			error(_("Number of consecutive events would exceed the "
						"number of periods in a day"));
			return -1;
		}
	}

	return 0;
}

/* This is the updater function. It makes sure that dependent event is 
 * scheduled one period later than the independent event. */
int updater(int src, int dst, int typeid, int resid)
{
	assert(typeid==time);

	return(resid+1);
}

/* This is the precalculate function. It is called after all restriction
 * handlers. We define updater functions here. */
int module_precalc(moduleoption *opt) 
{
	int n, tupleid;
	struct cons_t *cur;

	int *residlist;
	int residnum;

	if(cons==NULL) {
		/* The linked list is empty */
		info(_("module '%s' has been loaded, but not used"), 
							"consecutive.so");
	}

	/* We will use this buffer later for the domain_and() function */
	residlist=malloc(sizeof(*residlist)*periods*days);
	if(residlist==NULL) {
		error(_("Can't allocate memory"));
		return(-1);
	}

	/* We walk through all defined groups of event. */
	cur=cons;
	while(cur!=NULL) {

		/* For each event except the first we define an updater
		 * function. */

		for(n=1;n<cur->tupleidnum;n++) {
			tupleid=cur->tupleid[n];

			/* We have to check if this event is already dependent.
			 * If it is, we report an error. */

			if(updater_check(tupleid, time)) {
				error(_("Event '%s' already depends on another"
					" event"), dat_tuplemap[tupleid].name);
				free(residlist);
				return(-1);
			}

			/* First event in the group is truly independent 
			 * (at least as far as this module is concerned). The
			 * second event depends on the first. The third event
			 * depends on the second and so on. */

			updater_new(cur->tupleid[n-1], tupleid, time, updater);
		}

		/* Now we have to make sure that the first event in the group
		 * will be scheduled so early that the whole group will
		 * always fit on the same day. */

		/* This means that we have to eliminate the last tupleidnum
		 * periods on each day from its time domain. */

		residnum=0;
		for(n=0;n<days*periods;n++) {
			if(n%periods<=(periods-cur->tupleidnum)) {
				residlist[residnum]=n;
				residnum++;
			}
		}

		tupleid=cur->tupleid[0];
		domain_and(dat_tuplemap[tupleid].dom[time],residlist,residnum);

		cur=cur->next;
	}

	free(residlist);
	return(0);
}

/* This is a standard module initialization function. */
int module_init(moduleoption *opt) 
{
	int c;

	/* We store some info about the time resources in global variables. */
	time=restype_findid("time");
	if(time<0) {
		error(_("Resource type '%s' not found"), "time");
		return -1;
	}
	
	c=res_get_matrix(restype_find("time"), &days, &periods);
	if(c) {
		error(_("Resource type '%s' is not a matrix"), "time");
		return -1;
	}
	
	/* Definitions of the precalculate and event restriction handler
	 * functions. */
	precalc_new(module_precalc);

	handler_tup_new("consecutive", getevent);
	handler_tup_new("periods-per-block", getevent);

	return(0);
}