Logo AND Algorithmique Numérique Distribuée

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