Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
useless includes
[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   printf("(");
172   switch(label->type){
173     case 0:
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       break;
178     case 1:
179       xbt_automaton_exp_label_display(label->u.or_and.left_exp);
180       printf(" && ");
181       xbt_automaton_exp_label_display(label->u.or_and.right_exp);
182       break;
183     case 2:
184       printf("!");
185       xbt_automaton_exp_label_display(label->u.exp_not);
186       break;
187     case 3:
188       printf("%s", label->u.predicat);
189       break;
190     case 4:
191       printf("1");
192       break;
193     default:
194       break;
195   }
196   printf(")");
197 }
198
199 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
200   return a->current_state;
201 }
202
203 static int call_simple_function(void* function)
204 {
205   return ((int (*)(void)) function)();
206 }
207
208 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
209   xbt_automaton_propositional_symbol_t prop_symb = NULL;
210   prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
211   prop_symb->pred = xbt_strdup(id);
212   prop_symb->callback                            = &call_simple_function;
213   prop_symb->data = fct;
214   prop_symb->free_function = NULL;
215   xbt_dynar_push(a->propositional_symbols, &prop_symb);
216   return prop_symb;
217 }
218
219 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a, const char* id, int* value)
220 {
221   xbt_automaton_propositional_symbol_t prop_symb = NULL;
222   prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
223   prop_symb->pred = xbt_strdup(id);
224   prop_symb->callback = NULL;
225   prop_symb->data = value;
226   prop_symb->free_function = NULL;
227   xbt_dynar_push(a->propositional_symbols, &prop_symb);
228   return prop_symb;
229 }
230
231 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_callback(
232   xbt_automaton_t a, const char* id,
233   xbt_automaton_propositional_symbol_callback_type callback,
234   void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
235 {
236   xbt_automaton_propositional_symbol_t prop_symb = NULL;
237   prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
238   prop_symb->pred = xbt_strdup(id);
239   prop_symb->callback = callback;
240   prop_symb->data = data;
241   prop_symb->free_function = free_function;
242   xbt_dynar_push(a->propositional_symbols, &prop_symb);
243   return prop_symb;
244 }
245
246 XBT_PUBLIC(int) xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
247 {
248   if (symbol->callback)
249     return (symbol->callback)(symbol->data);
250   else
251     return *(int*) symbol->data;
252 }
253
254 XBT_PUBLIC(xbt_automaton_propositional_symbol_callback_type) xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
255 {
256   return symbol->callback;
257 }
258
259 XBT_PUBLIC(void*) xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
260 {
261   return symbol->data;
262 }
263
264 XBT_PUBLIC(const char*) xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
265 {
266   return symbol->pred;
267 }
268
269 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
270
271   /* single id for each state, id and type sufficient for comparison*/
272
273   if(strcmp(s1->id, s2->id))
274     return 1;
275
276   if(s1->type != s2->type)
277     return 1;
278
279   return 0;
280
281 }
282
283 int xbt_automaton_transition_compare(const void *t1, const void *t2){
284
285   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
286     return 1;
287   
288   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
289     return 1;
290
291   if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
292     return 1;
293
294   return 0;
295   
296 }
297
298 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
299
300   if(l1->type != l2->type)
301     return 1;
302
303   switch(l1->type){
304   case 0 : // OR 
305   case 1 : // AND
306     if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
307       return 1;
308     else
309       return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
310     break;
311   case 2 : // NOT
312     return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
313     break;
314   case 3 : // predicat
315     return (strcmp(l1->u.predicat, l2->u.predicat));
316     break;
317   case 4 : // 1
318     return 0;
319     break;
320   default :
321     return -1;
322     break;
323   }
324 }
325
326 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
327   unsigned int nb_elem = xbt_dynar_length(s1);
328
329   for (unsigned int cursor = 0; cursor < nb_elem; cursor++) {
330     int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
331     int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
332     if(*iptr1 != *iptr2)
333       return 1;
334   } 
335
336   return 0;
337 }
338
339 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
340 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
341 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
342
343 void xbt_automaton_state_free(xbt_automaton_state_t s){
344   if (s != NULL) {
345     xbt_free(s->id);
346     xbt_dynar_free(&(s->in));
347     xbt_dynar_free(&(s->out));
348     xbt_free(s);
349   }
350 }
351
352 void xbt_automaton_state_free_voidp(void *s){
353   xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
354 }
355
356 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
357   if(t){
358     xbt_automaton_exp_label_free(t->label);
359     xbt_free(t);
360     t = NULL;
361   }
362 }
363
364 void xbt_automaton_transition_free_voidp(void *t){
365   xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
366 }
367
368 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
369   if(e){
370     switch(e->type){
371     case 0:
372     case 1:
373       xbt_automaton_exp_label_free(e->u.or_and.left_exp);
374       xbt_automaton_exp_label_free(e->u.or_and.right_exp);
375       break;
376     case 2:
377       xbt_automaton_exp_label_free(e->u.exp_not);
378       break;
379     case 3:
380       xbt_free(e->u.predicat);
381       break;
382     default:
383       break;
384     }
385     xbt_free(e);
386     e = NULL;
387   }
388 }
389
390 void xbt_automaton_exp_label_free_voidp(void *e){
391   xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
392 }
393
394 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
395   if(ps){
396     xbt_free(ps->pred);
397     xbt_free(ps);
398     ps = NULL;
399   }
400 }
401
402 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
403   xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
404   if (symbol->free_function)
405     symbol->free_function(symbol->data);
406   xbt_free(symbol->pred);
407   xbt_automaton_propositional_symbol_free(symbol);
408 }
409
410 void xbt_automaton_free(xbt_automaton_t a){
411   if(a){
412     xbt_dynar_free(&(a->propositional_symbols));
413     xbt_dynar_free(&(a->transitions));
414     xbt_dynar_free(&(a->states));
415     xbt_automaton_state_free(a->current_state);
416     xbt_free(a);
417     a = NULL;
418   }
419 }