Logo AND Algorithmique Numérique Distribuée

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