Logo AND Algorithmique Numérique Distribuée

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