Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
catch also some more sensible "nested code block"
[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 = NULL;
61   label = xbt_new0(struct xbt_automaton_exp_label, 1);
62   label->type = type;
63   xbt_automaton_exp_label_t left;
64   xbt_automaton_exp_label_t right;
65   xbt_automaton_exp_label_t exp_not;
66   char *p;
67   va_list ap;
68   va_start(ap, type);
69   switch(type){
70   case 0 :
71     left = va_arg(ap, xbt_automaton_exp_label_t);
72     right = va_arg(ap, xbt_automaton_exp_label_t);
73     label->u.or_and.left_exp = left;
74     label->u.or_and.right_exp = right;
75     break;
76   case 1 :
77     left = va_arg(ap, xbt_automaton_exp_label_t);
78     right = va_arg(ap, xbt_automaton_exp_label_t);
79     label->u.or_and.left_exp = left;
80     label->u.or_and.right_exp = right;
81     break;
82   case 2 :
83     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
84     label->u.exp_not = exp_not;
85     break;
86   case 3 :
87     p = va_arg(ap, char*);
88     label->u.predicat = xbt_strdup(p);
89     break;
90   }
91   va_end(ap);
92   return label;
93 }
94
95 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
96   return a->states;
97 }
98
99 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
100   return a->transitions;
101 }
102
103 xbt_automaton_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_automaton_state_t src, xbt_automaton_state_t dst){
104   xbt_automaton_transition_t transition;
105   unsigned int cursor;
106   xbt_dynar_foreach(src->out, cursor, transition){
107     if((transition->src == src) && (transition->dst == dst))
108       return transition;
109   }
110   return NULL;
111 }
112
113 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
114   return t->src;
115 }
116
117 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
118   return t->dst;
119 }
120
121 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
122   t->src = src;
123   xbt_dynar_push(src->out,&t);
124 }
125
126 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
127   t->dst = dst;
128   xbt_dynar_push(dst->in,&t);
129 }
130
131 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
132   return s->out;
133 }
134
135 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
136   return s->in;
137 }
138
139 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
140   xbt_automaton_state_t state = NULL;
141   unsigned int cursor = 0;
142   xbt_dynar_foreach(a->states, cursor, state){
143    if(strcmp(state->id, id)==0)
144      return state;
145   }
146   return NULL;
147 }
148
149 void xbt_automaton_display(xbt_automaton_t a){
150   unsigned int cursor;
151   xbt_automaton_state_t state = NULL;
152
153   printf("\n\nCurrent state: %s\n", a->current_state->id);
154
155   printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
156
157   xbt_dynar_foreach(a->states, cursor, state)
158     printf("ID: %s, type: %d\n", state->id, state->type);
159
160   xbt_automaton_transition_t transition;
161   printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
162
163   xbt_dynar_foreach(a->transitions, cursor, transition){
164     printf("label:");
165     xbt_automaton_exp_label_display(transition->label);
166     printf(", %s -> %s\n", transition->src->id, transition->dst->id);
167   }
168 }
169
170 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
171   switch(label->type){
172   case 0 :
173     printf("(");
174     xbt_automaton_exp_label_display(label->u.or_and.left_exp);
175     printf(" || ");
176     xbt_automaton_exp_label_display(label->u.or_and.right_exp);
177     printf(")");
178     break;
179   case 1 : 
180     printf("(");
181     xbt_automaton_exp_label_display(label->u.or_and.left_exp);
182     printf(" && ");
183     xbt_automaton_exp_label_display(label->u.or_and.right_exp);
184     printf(")");
185     break;
186   case 2 : 
187     printf("(!");
188     xbt_automaton_exp_label_display(label->u.exp_not);
189     printf(")");
190     break;
191   case 3 :
192     printf("(%s)",label->u.predicat);
193     break;
194   case 4 :
195     printf("(1)");
196     break;
197   }
198 }
199
200 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
201   return a->current_state;
202 }
203
204 static int call_simple_function(void* function)
205 {
206   return ((int (*)(void)) function)();
207 }
208
209 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
210   xbt_automaton_propositional_symbol_t prop_symb = NULL;
211   prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
212   prop_symb->pred = xbt_strdup(id);
213   prop_symb->callback = call_simple_function;
214   prop_symb->data = fct;
215   prop_symb->free_function = NULL;
216   xbt_dynar_push(a->propositional_symbols, &prop_symb);
217   return prop_symb;
218 }
219
220 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a, const char* id, int* value)
221 {
222   xbt_automaton_propositional_symbol_t prop_symb = NULL;
223   prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
224   prop_symb->pred = xbt_strdup(id);
225   prop_symb->callback = NULL;
226   prop_symb->data = value;
227   prop_symb->free_function = NULL;
228   xbt_dynar_push(a->propositional_symbols, &prop_symb);
229   return prop_symb;
230 }
231
232 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_callback(
233   xbt_automaton_t a, const char* id,
234   xbt_automaton_propositional_symbol_callback_type callback,
235   void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
236 {
237   xbt_automaton_propositional_symbol_t prop_symb = NULL;
238   prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
239   prop_symb->pred = xbt_strdup(id);
240   prop_symb->callback = callback;
241   prop_symb->data = data;
242   prop_symb->free_function = free_function;
243   xbt_dynar_push(a->propositional_symbols, &prop_symb);
244   return prop_symb;
245 }
246
247 XBT_PUBLIC(int) xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
248 {
249   if (symbol->callback)
250     return (symbol->callback)(symbol->data);
251   else
252     return *(int*) symbol->data;
253 }
254
255 XBT_PUBLIC(xbt_automaton_propositional_symbol_callback_type) xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
256 {
257   return symbol->callback;
258 }
259
260 XBT_PUBLIC(void*) xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
261 {
262   return symbol->data;
263 }
264
265 XBT_PUBLIC(const char*) xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
266 {
267   return symbol->pred;
268 }
269
270 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
271
272   /* single id for each state, id and type sufficient for comparison*/
273
274   if(strcmp(s1->id, s2->id))
275     return 1;
276
277   if(s1->type != s2->type)
278     return 1;
279
280   return 0;
281
282 }
283
284 int xbt_automaton_transition_compare(const void *t1, const void *t2){
285
286   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
287     return 1;
288   
289   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
290     return 1;
291
292   if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
293     return 1;
294
295   return 0;
296   
297 }
298
299 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
300
301   if(l1->type != l2->type)
302     return 1;
303
304   switch(l1->type){
305   case 0 : // OR 
306   case 1 : // AND
307     if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
308       return 1;
309     else
310       return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
311     break;
312   case 2 : // NOT
313     return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
314     break;
315   case 3 : // predicat
316     return (strcmp(l1->u.predicat, l2->u.predicat));
317     break;
318   case 4 : // 1
319     return 0;
320     break;
321   default :
322     return -1;
323     break;
324   }
325 }
326
327 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
328   int *iptr1, *iptr2;
329   unsigned int cursor;
330   unsigned int nb_elem = xbt_dynar_length(s1);
331
332   for(cursor=0;cursor<nb_elem;cursor++){
333     iptr1 = xbt_dynar_get_ptr(s1, cursor);
334     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){
348     xbt_free(s->id);
349     xbt_dynar_free(&(s->in));
350     xbt_dynar_free(&(s->out));
351     xbt_free(s);
352     s = NULL;
353   }
354 }
355
356 void xbt_automaton_state_free_voidp(void *s){
357   xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
358 }
359
360 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
361   if(t){
362     xbt_automaton_exp_label_free(t->label);
363     xbt_free(t);
364     t = NULL;
365   }
366 }
367
368 void xbt_automaton_transition_free_voidp(void *t){
369   xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
370 }
371
372 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
373   if(e){
374     switch(e->type){
375     case 0:
376     case 1:
377       xbt_automaton_exp_label_free(e->u.or_and.left_exp);
378       xbt_automaton_exp_label_free(e->u.or_and.right_exp);
379       break;
380     case 2:
381       xbt_automaton_exp_label_free(e->u.exp_not);
382       break;
383     case 3:
384       xbt_free(e->u.predicat);
385       break;
386     default:
387       break;
388     }
389     xbt_free(e);
390     e = NULL;
391   }
392 }
393
394 void xbt_automaton_exp_label_free_voidp(void *e){
395   xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
396 }
397
398 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
399   if(ps){
400     xbt_free(ps->pred);
401     xbt_free(ps);
402     ps = NULL;
403   }
404 }
405
406 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
407   xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
408   if (symbol->free_function)
409     symbol->free_function(symbol->data);
410   xbt_free(symbol->pred);
411   xbt_automaton_propositional_symbol_free(symbol);
412 }
413
414 void xbt_automaton_free(xbt_automaton_t a){
415   if(a){
416     xbt_dynar_free(&(a->propositional_symbols));
417     xbt_dynar_free(&(a->transitions));
418     xbt_dynar_free(&(a->states));
419     xbt_automaton_state_free(a->current_state);
420     xbt_free(a);
421     a = NULL;
422   }
423 }