Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add default case to switch statements.
[simgrid.git] / src / xbt / automaton / automaton.c
1 /* automaton - representation of büchi automaton */
2
3 /* Copyright (c) 2011-2017. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/automaton.h"
9 #include <stdio.h> /* printf */
10 #include <xbt/log.h>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_automaton, xbt, "Automaton");
13
14 struct xbt_automaton_propositional_symbol{
15   char* pred;
16   /** Callback used to evaluate the value of the symbol */
17   int (*callback)(void*);
18   /** Additional data for the callback.
19       Alternatively it can be used as a pointer to the data. */
20   void* data;
21   /** Optional callback used to free the data field */
22   void (*free_function)(void*);
23 };
24
25 xbt_automaton_t xbt_automaton_new(void){
26   xbt_automaton_t automaton = xbt_new0(struct xbt_automaton, 1);
27   automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
28   automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
29   automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
30   return automaton;
31 }
32
33 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
34   xbt_automaton_state_t state = xbt_new0(struct xbt_automaton_state, 1);
35   state->type = type;
36   state->id = xbt_strdup(id);
37   state->in = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
38   state->out = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
39   xbt_dynar_push(a->states, &state);
40   return state;
41 }
42
43 xbt_automaton_transition_t xbt_automaton_transition_new(xbt_automaton_t a, xbt_automaton_state_t src, xbt_automaton_state_t dst, xbt_automaton_exp_label_t label){
44   xbt_automaton_transition_t transition = xbt_new0(struct xbt_automaton_transition, 1);
45   if(src != NULL){
46     xbt_dynar_push(src->out, &transition);
47     transition->src = src;
48   }
49   if(dst != NULL){
50     xbt_dynar_push(dst->in, &transition);
51     transition->dst = dst;
52   }
53   transition->label = label;
54   xbt_dynar_push(a->transitions, &transition);
55   return transition;
56 }
57
58 xbt_automaton_exp_label_t xbt_automaton_exp_label_new(int type, ...){
59   xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
60   label->type = type;
61   xbt_automaton_exp_label_t left;
62   xbt_automaton_exp_label_t right;
63   xbt_automaton_exp_label_t exp_not;
64   char *p;
65   va_list ap;
66   va_start(ap, type);
67   switch(type){
68   case 0 :
69     left = va_arg(ap, xbt_automaton_exp_label_t);
70     right = va_arg(ap, xbt_automaton_exp_label_t);
71     label->u.or_and.left_exp = left;
72     label->u.or_and.right_exp = right;
73     break;
74   case 1 :
75     left = va_arg(ap, xbt_automaton_exp_label_t);
76     right = va_arg(ap, xbt_automaton_exp_label_t);
77     label->u.or_and.left_exp = left;
78     label->u.or_and.right_exp = right;
79     break;
80   case 2 :
81     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
82     label->u.exp_not = exp_not;
83     break;
84   case 3 :
85     p = va_arg(ap, char*);
86     label->u.predicat = xbt_strdup(p);
87     break;
88   case 4:
89     break;
90   default:
91     XBT_DEBUG("Invalid type: %d", type);
92     xbt_free(label);
93     break;
94   }
95   va_end(ap);
96   return label;
97 }
98
99 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
100   return a->states;
101 }
102
103 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
104   return a->transitions;
105 }
106
107 xbt_automaton_transition_t xbt_automaton_get_transition(xbt_automaton_t XBT_ATTRIB_UNUSED a, xbt_automaton_state_t src,
108                                                         xbt_automaton_state_t dst)
109 {
110   xbt_automaton_transition_t transition;
111   unsigned int cursor;
112   xbt_dynar_foreach(src->out, cursor, transition){
113     if((transition->src == src) && (transition->dst == dst))
114       return transition;
115   }
116   return NULL;
117 }
118
119 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
120   return t->src;
121 }
122
123 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
124   return t->dst;
125 }
126
127 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
128   t->src = src;
129   xbt_dynar_push(src->out,&t);
130 }
131
132 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
133   t->dst = dst;
134   xbt_dynar_push(dst->in,&t);
135 }
136
137 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
138   return s->out;
139 }
140
141 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
142   return s->in;
143 }
144
145 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
146   xbt_automaton_state_t state = NULL;
147   unsigned int cursor = 0;
148   xbt_dynar_foreach(a->states, cursor, state){
149    if(strcmp(state->id, id)==0)
150      return state;
151   }
152   return NULL;
153 }
154
155 void xbt_automaton_display(xbt_automaton_t a){
156   unsigned int cursor;
157   xbt_automaton_state_t state = NULL;
158
159   printf("\n\nCurrent state: %s\n", a->current_state->id);
160
161   printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
162
163   xbt_dynar_foreach(a->states, cursor, state)
164     printf("ID: %s, type: %d\n", state->id, state->type);
165
166   xbt_automaton_transition_t transition;
167   printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
168
169   xbt_dynar_foreach(a->transitions, cursor, transition){
170     printf("label:");
171     xbt_automaton_exp_label_display(transition->label);
172     printf(", %s -> %s\n", transition->src->id, transition->dst->id);
173   }
174 }
175
176 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
177   printf("(");
178   switch(label->type){
179     case 0:
180       xbt_automaton_exp_label_display(label->u.or_and.left_exp);
181       printf(" || ");
182       xbt_automaton_exp_label_display(label->u.or_and.right_exp);
183       break;
184     case 1:
185       xbt_automaton_exp_label_display(label->u.or_and.left_exp);
186       printf(" && ");
187       xbt_automaton_exp_label_display(label->u.or_and.right_exp);
188       break;
189     case 2:
190       printf("!");
191       xbt_automaton_exp_label_display(label->u.exp_not);
192       break;
193     case 3:
194       printf("%s", label->u.predicat);
195       break;
196     case 4:
197       printf("1");
198       break;
199     default:
200       break;
201   }
202   printf(")");
203 }
204
205 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
206   return a->current_state;
207 }
208
209 static int call_simple_function(void* function)
210 {
211   return ((int (*)(void)) function)();
212 }
213
214 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
215   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
216   prop_symb->pred = xbt_strdup(id);
217   prop_symb->callback                            = &call_simple_function;
218   prop_symb->data = fct;
219   prop_symb->free_function = NULL;
220   xbt_dynar_push(a->propositional_symbols, &prop_symb);
221   return prop_symb;
222 }
223
224 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a, const char* id, int* value)
225 {
226   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
227   prop_symb->pred = xbt_strdup(id);
228   prop_symb->callback = NULL;
229   prop_symb->data = value;
230   prop_symb->free_function = NULL;
231   xbt_dynar_push(a->propositional_symbols, &prop_symb);
232   return prop_symb;
233 }
234
235 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_callback(
236   xbt_automaton_t a, const char* id,
237   xbt_automaton_propositional_symbol_callback_type callback,
238   void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
239 {
240   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
241   prop_symb->pred = xbt_strdup(id);
242   prop_symb->callback = callback;
243   prop_symb->data = data;
244   prop_symb->free_function = free_function;
245   xbt_dynar_push(a->propositional_symbols, &prop_symb);
246   return prop_symb;
247 }
248
249 XBT_PUBLIC(int) xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
250 {
251   if (symbol->callback)
252     return (symbol->callback)(symbol->data);
253   else
254     return *(int*) symbol->data;
255 }
256
257 XBT_PUBLIC(xbt_automaton_propositional_symbol_callback_type) xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
258 {
259   return symbol->callback;
260 }
261
262 XBT_PUBLIC(void*) xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
263 {
264   return symbol->data;
265 }
266
267 XBT_PUBLIC(const char*) xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
268 {
269   return symbol->pred;
270 }
271
272 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
273
274   /* single id for each state, id and type sufficient for comparison*/
275
276   if(strcmp(s1->id, s2->id))
277     return 1;
278
279   if(s1->type != s2->type)
280     return 1;
281
282   return 0;
283
284 }
285
286 int xbt_automaton_transition_compare(const void *t1, const void *t2){
287
288   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
289     return 1;
290
291   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
292     return 1;
293
294   if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
295     return 1;
296
297   return 0;
298
299 }
300
301 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
302
303   if(l1->type != l2->type)
304     return 1;
305
306   switch(l1->type){
307   case 0 : // OR
308   case 1 : // AND
309     if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
310       return 1;
311     else
312       return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
313     break;
314   case 2 : // NOT
315     return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
316     break;
317   case 3 : // predicat
318     return (strcmp(l1->u.predicat, l2->u.predicat));
319     break;
320   case 4 : // 1
321     return 0;
322     break;
323   default :
324     return -1;
325     break;
326   }
327 }
328
329 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
330   unsigned int nb_elem = xbt_dynar_length(s1);
331
332   for (unsigned int cursor = 0; cursor < nb_elem; cursor++) {
333     int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
334     int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
335     if(*iptr1 != *iptr2)
336       return 1;
337   }
338
339   return 0;
340 }
341
342 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
343 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
344 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
345
346 void xbt_automaton_state_free(xbt_automaton_state_t s){
347   if (s != NULL) {
348     xbt_free(s->id);
349     xbt_dynar_free(&(s->in));
350     xbt_dynar_free(&(s->out));
351     xbt_free(s);
352   }
353 }
354
355 void xbt_automaton_state_free_voidp(void *s){
356   xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
357 }
358
359 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
360   if(t){
361     xbt_automaton_exp_label_free(t->label);
362     xbt_free(t);
363     t = NULL;
364   }
365 }
366
367 void xbt_automaton_transition_free_voidp(void *t){
368   xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
369 }
370
371 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
372   if(e){
373     switch(e->type){
374     case 0:
375     case 1:
376       xbt_automaton_exp_label_free(e->u.or_and.left_exp);
377       xbt_automaton_exp_label_free(e->u.or_and.right_exp);
378       break;
379     case 2:
380       xbt_automaton_exp_label_free(e->u.exp_not);
381       break;
382     case 3:
383       xbt_free(e->u.predicat);
384       break;
385     default:
386       break;
387     }
388     xbt_free(e);
389     e = NULL;
390   }
391 }
392
393 void xbt_automaton_exp_label_free_voidp(void *e){
394   xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
395 }
396
397 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
398   if(ps){
399     xbt_free(ps->pred);
400     xbt_free(ps);
401     ps = NULL;
402   }
403 }
404
405 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
406   xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
407   if (symbol->free_function)
408     symbol->free_function(symbol->data);
409   xbt_automaton_propositional_symbol_free(symbol);
410 }
411
412 void xbt_automaton_free(xbt_automaton_t a){
413   if(a){
414     xbt_dynar_free(&(a->propositional_symbols));
415     xbt_dynar_free(&(a->transitions));
416     xbt_dynar_free(&(a->states));
417     xbt_automaton_state_free(a->current_state);
418     xbt_free(a);
419     a = NULL;
420   }
421 }