Logo AND Algorithmique Numérique Distribuée

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