Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
015444104c61ace9b96f5221266494a670938ea5
[simgrid.git] / src / xbt / automaton / automaton.c
1 /* automaton - representation of büchi automaton */
2
3 /* Copyright (c) 2011-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/automaton.h"
10 #include <stdio.h> /* printf */
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(int type, ...){
57   xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
58   label->type = type;
59   xbt_automaton_exp_label_t left;
60   xbt_automaton_exp_label_t right;
61   xbt_automaton_exp_label_t exp_not;
62   char *p;
63   va_list ap;
64   va_start(ap, type);
65   switch(type){
66   case 0 :
67     left = va_arg(ap, xbt_automaton_exp_label_t);
68     right = va_arg(ap, xbt_automaton_exp_label_t);
69     label->u.or_and.left_exp = left;
70     label->u.or_and.right_exp = right;
71     break;
72   case 1 :
73     left = va_arg(ap, xbt_automaton_exp_label_t);
74     right = va_arg(ap, xbt_automaton_exp_label_t);
75     label->u.or_and.left_exp = left;
76     label->u.or_and.right_exp = right;
77     break;
78   case 2 :
79     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
80     label->u.exp_not = exp_not;
81     break;
82   case 3 :
83     p = va_arg(ap, char*);
84     label->u.predicat = xbt_strdup(p);
85     break;
86   }
87   va_end(ap);
88   return label;
89 }
90
91 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
92   return a->states;
93 }
94
95 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
96   return a->transitions;
97 }
98
99 xbt_automaton_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_automaton_state_t src, xbt_automaton_state_t dst){
100   xbt_automaton_transition_t transition;
101   unsigned int cursor;
102   xbt_dynar_foreach(src->out, cursor, transition){
103     if((transition->src == src) && (transition->dst == dst))
104       return transition;
105   }
106   return NULL;
107 }
108
109 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
110   return t->src;
111 }
112
113 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
114   return t->dst;
115 }
116
117 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
118   t->src = src;
119   xbt_dynar_push(src->out,&t);
120 }
121
122 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
123   t->dst = dst;
124   xbt_dynar_push(dst->in,&t);
125 }
126
127 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
128   return s->out;
129 }
130
131 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
132   return s->in;
133 }
134
135 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
136   xbt_automaton_state_t state = NULL;
137   unsigned int cursor = 0;
138   xbt_dynar_foreach(a->states, cursor, state){
139    if(strcmp(state->id, id)==0)
140      return state;
141   }
142   return NULL;
143 }
144
145 void xbt_automaton_display(xbt_automaton_t a){
146   unsigned int cursor;
147   xbt_automaton_state_t state = NULL;
148
149   printf("\n\nCurrent state: %s\n", a->current_state->id);
150
151   printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
152
153   xbt_dynar_foreach(a->states, cursor, state)
154     printf("ID: %s, type: %d\n", state->id, state->type);
155
156   xbt_automaton_transition_t transition;
157   printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
158
159   xbt_dynar_foreach(a->transitions, cursor, transition){
160     printf("label:");
161     xbt_automaton_exp_label_display(transition->label);
162     printf(", %s -> %s\n", transition->src->id, transition->dst->id);
163   }
164 }
165
166 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
167   printf("(");
168   switch(label->type){
169     case 0:
170       xbt_automaton_exp_label_display(label->u.or_and.left_exp);
171       printf(" || ");
172       xbt_automaton_exp_label_display(label->u.or_and.right_exp);
173       break;
174     case 1:
175       xbt_automaton_exp_label_display(label->u.or_and.left_exp);
176       printf(" && ");
177       xbt_automaton_exp_label_display(label->u.or_and.right_exp);
178       break;
179     case 2:
180       printf("!");
181       xbt_automaton_exp_label_display(label->u.exp_not);
182       break;
183     case 3:
184       printf("%s", label->u.predicat);
185       break;
186     case 4:
187       printf("1");
188       break;
189     default:
190       break;
191   }
192   printf(")");
193 }
194
195 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
196   return a->current_state;
197 }
198
199 static int call_simple_function(void* function)
200 {
201   return ((int (*)(void)) function)();
202 }
203
204 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
205   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
206   prop_symb->pred = xbt_strdup(id);
207   prop_symb->callback                            = &call_simple_function;
208   prop_symb->data = fct;
209   prop_symb->free_function = NULL;
210   xbt_dynar_push(a->propositional_symbols, &prop_symb);
211   return prop_symb;
212 }
213
214 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a, const char* id, int* value)
215 {
216   xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
217   prop_symb->pred = xbt_strdup(id);
218   prop_symb->callback = NULL;
219   prop_symb->data = value;
220   prop_symb->free_function = NULL;
221   xbt_dynar_push(a->propositional_symbols, &prop_symb);
222   return prop_symb;
223 }
224
225 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_callback(
226   xbt_automaton_t a, const char* id,
227   xbt_automaton_propositional_symbol_callback_type callback,
228   void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
229 {
230   xbt_automaton_propositional_symbol_t prop_symb = NULL;
231   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_free(symbol->pred);
401   xbt_automaton_propositional_symbol_free(symbol);
402 }
403
404 void xbt_automaton_free(xbt_automaton_t a){
405   if(a){
406     xbt_dynar_free(&(a->propositional_symbols));
407     xbt_dynar_free(&(a->transitions));
408     xbt_dynar_free(&(a->states));
409     xbt_automaton_state_free(a->current_state);
410     xbt_free(a);
411     a = NULL;
412   }
413 }