Logo AND Algorithmique Numérique Distribuée

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