Logo AND Algorithmique Numérique Distribuée

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