Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
35af3be3e0d6b6e5b2ecd7c87c29b6f08ed89e95
[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/log.h>
11 #include <xbt/sysdep.h>
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_automaton, xbt, "Automaton");
14
15 struct xbt_automaton_propositional_symbol{
16   char* pred;
17   /** Callback used to evaluate the value of the symbol */
18   int (*callback)(void*);
19   /** Additional data for the callback.
20       Alternatively it can be used as a pointer to the data. */
21   void* data;
22   /** Optional callback used to free the data field */
23   void (*free_function)(void*);
24 };
25
26 xbt_automaton_t xbt_automaton_new(void){
27   xbt_automaton_t automaton = xbt_new0(struct xbt_automaton, 1);
28   automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
29   automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
30   automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
31   return automaton;
32 }
33
34 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
35   xbt_automaton_state_t state = xbt_new0(struct xbt_automaton_state, 1);
36   state->type = type;
37   state->id = xbt_strdup(id);
38   state->in = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
39   state->out = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
40   xbt_dynar_push(a->states, &state);
41   return state;
42 }
43
44 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){
45   xbt_automaton_transition_t 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 = xbt_new0(struct xbt_automaton_exp_label, 1);
61   label->type = type;
62   xbt_automaton_exp_label_t left;
63   xbt_automaton_exp_label_t right;
64   xbt_automaton_exp_label_t exp_not;
65   char *p;
66   va_list ap;
67   va_start(ap, type);
68   switch(type){
69   case 0 :
70     left = va_arg(ap, xbt_automaton_exp_label_t);
71     right = va_arg(ap, xbt_automaton_exp_label_t);
72     label->u.or_and.left_exp = left;
73     label->u.or_and.right_exp = right;
74     break;
75   case 1 :
76     left = va_arg(ap, xbt_automaton_exp_label_t);
77     right = va_arg(ap, xbt_automaton_exp_label_t);
78     label->u.or_and.left_exp = left;
79     label->u.or_and.right_exp = right;
80     break;
81   case 2 :
82     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
83     label->u.exp_not = exp_not;
84     break;
85   case 3 :
86     p = va_arg(ap, char*);
87     label->u.predicat = xbt_strdup(p);
88     break;
89   case 4:
90     break;
91   default:
92     XBT_DEBUG("Invalid type: %d", type);
93     xbt_free(label);
94     label = NULL;
95     break;
96   }
97   va_end(ap);
98   return label;
99 }
100
101 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
102   return a->states;
103 }
104
105 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
106   return a->transitions;
107 }
108
109 xbt_automaton_transition_t xbt_automaton_get_transition(XBT_ATTRIB_UNUSED xbt_automaton_t a, xbt_automaton_state_t src,
110                                                         xbt_automaton_state_t dst)
111 {
112   xbt_automaton_transition_t transition;
113   unsigned int cursor;
114   xbt_dynar_foreach(src->out, cursor, transition){
115     if((transition->src == src) && (transition->dst == dst))
116       return transition;
117   }
118   return NULL;
119 }
120
121 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
122   return t->src;
123 }
124
125 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
126   return t->dst;
127 }
128
129 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
130   t->src = src;
131   xbt_dynar_push(src->out,&t);
132 }
133
134 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
135   t->dst = dst;
136   xbt_dynar_push(dst->in,&t);
137 }
138
139 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
140   return s->out;
141 }
142
143 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
144   return s->in;
145 }
146
147 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
148   xbt_automaton_state_t state = NULL;
149   unsigned int cursor = 0;
150   xbt_dynar_foreach(a->states, cursor, state){
151    if(strcmp(state->id, id)==0)
152      return state;
153   }
154   return NULL;
155 }
156
157 void xbt_automaton_display(xbt_automaton_t a){
158   unsigned int cursor;
159   xbt_automaton_state_t state = NULL;
160
161   printf("\n\nCurrent state: %s\n", a->current_state->id);
162
163   printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
164
165   xbt_dynar_foreach(a->states, cursor, state)
166     printf("ID: %s, type: %d\n", state->id, state->type);
167
168   xbt_automaton_transition_t transition;
169   printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
170
171   xbt_dynar_foreach(a->transitions, cursor, transition){
172     printf("label:");
173     xbt_automaton_exp_label_display(transition->label);
174     printf(", %s -> %s\n", transition->src->id, transition->dst->id);
175   }
176 }
177
178 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
179   printf("(");
180   switch(label->type){
181     case 0:
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       break;
186     case 1:
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       break;
191     case 2:
192       printf("!");
193       xbt_automaton_exp_label_display(label->u.exp_not);
194       break;
195     case 3:
196       printf("%s", label->u.predicat);
197       break;
198     case 4:
199       printf("1");
200       break;
201     default:
202       break;
203   }
204   printf(")");
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,
217                                                                             int (*fct)(void))
218 {
219   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
220   prop_symb->pred = xbt_strdup(id);
221   prop_symb->callback                            = &call_simple_function;
222   prop_symb->data = fct;
223   prop_symb->free_function = NULL;
224   xbt_dynar_push(a->propositional_symbols, &prop_symb);
225   return prop_symb;
226 }
227
228 XBT_PUBLIC xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a,
229                                                                                                const char* id,
230                                                                                                int* value)
231 {
232   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
233   prop_symb->pred = xbt_strdup(id);
234   prop_symb->callback = NULL;
235   prop_symb->data = value;
236   prop_symb->free_function = NULL;
237   xbt_dynar_push(a->propositional_symbols, &prop_symb);
238   return prop_symb;
239 }
240
241 XBT_PUBLIC xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new_callback(
242     xbt_automaton_t a, const char* id, xbt_automaton_propositional_symbol_callback_type callback, void* data,
243     xbt_automaton_propositional_symbol_free_function_type free_function)
244 {
245   xbt_automaton_propositional_symbol_t 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
263 xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
264 {
265   return symbol->callback;
266 }
267
268 XBT_PUBLIC void* xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
269 {
270   return symbol->data;
271 }
272
273 XBT_PUBLIC const char* xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
274 {
275   return symbol->pred;
276 }
277
278 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
279
280   /* single id for each state, id and type sufficient for comparison*/
281
282   if(strcmp(s1->id, s2->id))
283     return 1;
284
285   if(s1->type != s2->type)
286     return 1;
287
288   return 0;
289
290 }
291
292 int xbt_automaton_transition_compare(const void *t1, const void *t2){
293
294   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
295     return 1;
296
297   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
298     return 1;
299
300   if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
301     return 1;
302
303   return 0;
304
305 }
306
307 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
308
309   if(l1->type != l2->type)
310     return 1;
311
312   int res;
313   switch(l1->type){
314   case 0 : // OR
315   case 1 : // AND
316     if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
317       res = 1;
318     else
319       res = xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
320     break;
321   case 2 : // NOT
322     res = xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
323     break;
324   case 3 : // predicat
325     res = strcmp(l1->u.predicat, l2->u.predicat);
326     break;
327   case 4 : // 1
328     res = 0;
329     break;
330   default :
331     res = -1;
332     break;
333   }
334   return res;
335 }
336
337 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
338   unsigned int nb_elem = xbt_dynar_length(s1);
339
340   for (unsigned int cursor = 0; cursor < nb_elem; cursor++) {
341     int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
342     int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
343     if(*iptr1 != *iptr2)
344       return 1;
345   }
346
347   return 0;
348 }
349
350 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
351 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
352 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
353
354 void xbt_automaton_state_free(xbt_automaton_state_t s){
355   if (s != NULL) {
356     xbt_free(s->id);
357     xbt_dynar_free(&(s->in));
358     xbt_dynar_free(&(s->out));
359     xbt_free(s);
360   }
361 }
362
363 void xbt_automaton_state_free_voidp(void *s){
364   xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
365 }
366
367 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
368   if(t){
369     xbt_automaton_exp_label_free(t->label);
370     xbt_free(t);
371     t = NULL;
372   }
373 }
374
375 void xbt_automaton_transition_free_voidp(void *t){
376   xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
377 }
378
379 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
380   if(e){
381     switch(e->type){
382     case 0:
383     case 1:
384       xbt_automaton_exp_label_free(e->u.or_and.left_exp);
385       xbt_automaton_exp_label_free(e->u.or_and.right_exp);
386       break;
387     case 2:
388       xbt_automaton_exp_label_free(e->u.exp_not);
389       break;
390     case 3:
391       xbt_free(e->u.predicat);
392       break;
393     default:
394       break;
395     }
396     xbt_free(e);
397     e = NULL;
398   }
399 }
400
401 void xbt_automaton_exp_label_free_voidp(void *e){
402   xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
403 }
404
405 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
406   if(ps){
407     xbt_free(ps->pred);
408     xbt_free(ps);
409     ps = NULL;
410   }
411 }
412
413 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
414   xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
415   if (symbol->free_function)
416     symbol->free_function(symbol->data);
417   xbt_automaton_propositional_symbol_free(symbol);
418 }
419
420 void xbt_automaton_free(xbt_automaton_t a){
421   if(a){
422     xbt_dynar_free(&(a->propositional_symbols));
423     xbt_dynar_free(&(a->transitions));
424     xbt_dynar_free(&(a->states));
425     xbt_automaton_state_free(a->current_state);
426     xbt_free(a);
427     a = NULL;
428   }
429 }