Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define XBT_PUBLIC without parameter, just like XBT_PRIVATE.
[simgrid.git] / src / xbt / automaton / automaton.c
1 /* automaton - representation of büchi automaton */
2
3 /* Copyright (c) 2011-2018. 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
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_automaton, xbt, "Automaton");
13
14 struct xbt_automaton_propositional_symbol{
15   char* pred;
16   /** Callback used to evaluate the value of the symbol */
17   int (*callback)(void*);
18   /** Additional data for the callback.
19       Alternatively it can be used as a pointer to the data. */
20   void* data;
21   /** Optional callback used to free the data field */
22   void (*free_function)(void*);
23 };
24
25 xbt_automaton_t xbt_automaton_new(void){
26   xbt_automaton_t automaton = xbt_new0(struct xbt_automaton, 1);
27   automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
28   automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
29   automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
30   return automaton;
31 }
32
33 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
34   xbt_automaton_state_t 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 = xbt_new0(struct xbt_automaton_transition, 1);
45   if(src != NULL){
46     xbt_dynar_push(src->out, &transition);
47     transition->src = src;
48   }
49   if(dst != NULL){
50     xbt_dynar_push(dst->in, &transition);
51     transition->dst = dst;
52   }
53   transition->label = label;
54   xbt_dynar_push(a->transitions, &transition);
55   return transition;
56 }
57
58 xbt_automaton_exp_label_t xbt_automaton_exp_label_new(int type, ...){
59   xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
60   label->type = type;
61   xbt_automaton_exp_label_t left;
62   xbt_automaton_exp_label_t right;
63   xbt_automaton_exp_label_t exp_not;
64   char *p;
65   va_list ap;
66   va_start(ap, type);
67   switch(type){
68   case 0 :
69     left = va_arg(ap, xbt_automaton_exp_label_t);
70     right = va_arg(ap, xbt_automaton_exp_label_t);
71     label->u.or_and.left_exp = left;
72     label->u.or_and.right_exp = right;
73     break;
74   case 1 :
75     left = va_arg(ap, xbt_automaton_exp_label_t);
76     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   case 2 :
81     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
82     label->u.exp_not = exp_not;
83     break;
84   case 3 :
85     p = va_arg(ap, char*);
86     label->u.predicat = xbt_strdup(p);
87     break;
88   case 4:
89     break;
90   default:
91     XBT_DEBUG("Invalid type: %d", type);
92     xbt_free(label);
93     label = NULL;
94     break;
95   }
96   va_end(ap);
97   return label;
98 }
99
100 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
101   return a->states;
102 }
103
104 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
105   return a->transitions;
106 }
107
108 xbt_automaton_transition_t xbt_automaton_get_transition(XBT_ATTRIB_UNUSED xbt_automaton_t a, xbt_automaton_state_t src,
109                                                         xbt_automaton_state_t dst)
110 {
111   xbt_automaton_transition_t transition;
112   unsigned int cursor;
113   xbt_dynar_foreach(src->out, cursor, transition){
114     if((transition->src == src) && (transition->dst == dst))
115       return transition;
116   }
117   return NULL;
118 }
119
120 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
121   return t->src;
122 }
123
124 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
125   return t->dst;
126 }
127
128 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
129   t->src = src;
130   xbt_dynar_push(src->out,&t);
131 }
132
133 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
134   t->dst = dst;
135   xbt_dynar_push(dst->in,&t);
136 }
137
138 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
139   return s->out;
140 }
141
142 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
143   return s->in;
144 }
145
146 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
147   xbt_automaton_state_t state = NULL;
148   unsigned int cursor = 0;
149   xbt_dynar_foreach(a->states, cursor, state){
150    if(strcmp(state->id, id)==0)
151      return state;
152   }
153   return NULL;
154 }
155
156 void xbt_automaton_display(xbt_automaton_t a){
157   unsigned int cursor;
158   xbt_automaton_state_t state = NULL;
159
160   printf("\n\nCurrent state: %s\n", a->current_state->id);
161
162   printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
163
164   xbt_dynar_foreach(a->states, cursor, state)
165     printf("ID: %s, type: %d\n", state->id, state->type);
166
167   xbt_automaton_transition_t transition;
168   printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
169
170   xbt_dynar_foreach(a->transitions, cursor, transition){
171     printf("label:");
172     xbt_automaton_exp_label_display(transition->label);
173     printf(", %s -> %s\n", transition->src->id, transition->dst->id);
174   }
175 }
176
177 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
178   printf("(");
179   switch(label->type){
180     case 0:
181       xbt_automaton_exp_label_display(label->u.or_and.left_exp);
182       printf(" || ");
183       xbt_automaton_exp_label_display(label->u.or_and.right_exp);
184       break;
185     case 1:
186       xbt_automaton_exp_label_display(label->u.or_and.left_exp);
187       printf(" && ");
188       xbt_automaton_exp_label_display(label->u.or_and.right_exp);
189       break;
190     case 2:
191       printf("!");
192       xbt_automaton_exp_label_display(label->u.exp_not);
193       break;
194     case 3:
195       printf("%s", label->u.predicat);
196       break;
197     case 4:
198       printf("1");
199       break;
200     default:
201       break;
202   }
203   printf(")");
204 }
205
206 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
207   return a->current_state;
208 }
209
210 static int call_simple_function(void* function)
211 {
212   return ((int (*)(void)) function)();
213 }
214
215 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id,
216                                                                             int (*fct)(void))
217 {
218   xbt_automaton_propositional_symbol_t 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,
228                                                                                                const char* id,
229                                                                                                int* value)
230 {
231   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
232   prop_symb->pred = xbt_strdup(id);
233   prop_symb->callback = NULL;
234   prop_symb->data = value;
235   prop_symb->free_function = NULL;
236   xbt_dynar_push(a->propositional_symbols, &prop_symb);
237   return prop_symb;
238 }
239
240 XBT_PUBLIC xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new_callback(
241     xbt_automaton_t a, const char* id, xbt_automaton_propositional_symbol_callback_type callback, void* data,
242     xbt_automaton_propositional_symbol_free_function_type free_function)
243 {
244   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
245   prop_symb->pred = xbt_strdup(id);
246   prop_symb->callback = callback;
247   prop_symb->data = data;
248   prop_symb->free_function = free_function;
249   xbt_dynar_push(a->propositional_symbols, &prop_symb);
250   return prop_symb;
251 }
252
253 XBT_PUBLIC int xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
254 {
255   if (symbol->callback)
256     return (symbol->callback)(symbol->data);
257   else
258     return *(int*) symbol->data;
259 }
260
261 XBT_PUBLIC xbt_automaton_propositional_symbol_callback_type
262 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   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       return 1;
316     else
317       return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
318     break;
319   case 2 : // NOT
320     return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
321     break;
322   case 3 : // predicat
323     return (strcmp(l1->u.predicat, l2->u.predicat));
324     break;
325   case 4 : // 1
326     return 0;
327     break;
328   default :
329     return -1;
330     break;
331   }
332 }
333
334 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
335   unsigned int nb_elem = xbt_dynar_length(s1);
336
337   for (unsigned int cursor = 0; cursor < nb_elem; cursor++) {
338     int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
339     int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
340     if(*iptr1 != *iptr2)
341       return 1;
342   }
343
344   return 0;
345 }
346
347 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
348 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
349 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
350
351 void xbt_automaton_state_free(xbt_automaton_state_t s){
352   if (s != NULL) {
353     xbt_free(s->id);
354     xbt_dynar_free(&(s->in));
355     xbt_dynar_free(&(s->out));
356     xbt_free(s);
357   }
358 }
359
360 void xbt_automaton_state_free_voidp(void *s){
361   xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
362 }
363
364 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
365   if(t){
366     xbt_automaton_exp_label_free(t->label);
367     xbt_free(t);
368     t = NULL;
369   }
370 }
371
372 void xbt_automaton_transition_free_voidp(void *t){
373   xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
374 }
375
376 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
377   if(e){
378     switch(e->type){
379     case 0:
380     case 1:
381       xbt_automaton_exp_label_free(e->u.or_and.left_exp);
382       xbt_automaton_exp_label_free(e->u.or_and.right_exp);
383       break;
384     case 2:
385       xbt_automaton_exp_label_free(e->u.exp_not);
386       break;
387     case 3:
388       xbt_free(e->u.predicat);
389       break;
390     default:
391       break;
392     }
393     xbt_free(e);
394     e = NULL;
395   }
396 }
397
398 void xbt_automaton_exp_label_free_voidp(void *e){
399   xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
400 }
401
402 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
403   if(ps){
404     xbt_free(ps->pred);
405     xbt_free(ps);
406     ps = NULL;
407   }
408 }
409
410 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
411   xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
412   if (symbol->free_function)
413     symbol->free_function(symbol->data);
414   xbt_automaton_propositional_symbol_free(symbol);
415 }
416
417 void xbt_automaton_free(xbt_automaton_t a){
418   if(a){
419     xbt_dynar_free(&(a->propositional_symbols));
420     xbt_dynar_free(&(a->transitions));
421     xbt_dynar_free(&(a->states));
422     xbt_automaton_state_free(a->current_state);
423     xbt_free(a);
424     a = NULL;
425   }
426 }