Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
please codacy
[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 = NULL;
217   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 = NULL;
232   prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
233   prop_symb->pred = xbt_strdup(id);
234   prop_symb->callback = callback;
235   prop_symb->data = data;
236   prop_symb->free_function = free_function;
237   xbt_dynar_push(a->propositional_symbols, &prop_symb);
238   return prop_symb;
239 }
240
241 XBT_PUBLIC(int) xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
242 {
243   if (symbol->callback)
244     return (symbol->callback)(symbol->data);
245   else
246     return *(int*) symbol->data;
247 }
248
249 XBT_PUBLIC(xbt_automaton_propositional_symbol_callback_type) xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
250 {
251   return symbol->callback;
252 }
253
254 XBT_PUBLIC(void*) xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
255 {
256   return symbol->data;
257 }
258
259 XBT_PUBLIC(const char*) xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
260 {
261   return symbol->pred;
262 }
263
264 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
265
266   /* single id for each state, id and type sufficient for comparison*/
267
268   if(strcmp(s1->id, s2->id))
269     return 1;
270
271   if(s1->type != s2->type)
272     return 1;
273
274   return 0;
275
276 }
277
278 int xbt_automaton_transition_compare(const void *t1, const void *t2){
279
280   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
281     return 1;
282
283   if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
284     return 1;
285
286   if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
287     return 1;
288
289   return 0;
290
291 }
292
293 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
294
295   if(l1->type != l2->type)
296     return 1;
297
298   switch(l1->type){
299   case 0 : // OR
300   case 1 : // AND
301     if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
302       return 1;
303     else
304       return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
305     break;
306   case 2 : // NOT
307     return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
308     break;
309   case 3 : // predicat
310     return (strcmp(l1->u.predicat, l2->u.predicat));
311     break;
312   case 4 : // 1
313     return 0;
314     break;
315   default :
316     return -1;
317     break;
318   }
319 }
320
321 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
322   unsigned int nb_elem = xbt_dynar_length(s1);
323
324   for (unsigned int cursor = 0; cursor < nb_elem; cursor++) {
325     int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
326     int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
327     if(*iptr1 != *iptr2)
328       return 1;
329   }
330
331   return 0;
332 }
333
334 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
335 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
336 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
337
338 void xbt_automaton_state_free(xbt_automaton_state_t s){
339   if (s != NULL) {
340     xbt_free(s->id);
341     xbt_dynar_free(&(s->in));
342     xbt_dynar_free(&(s->out));
343     xbt_free(s);
344   }
345 }
346
347 void xbt_automaton_state_free_voidp(void *s){
348   xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
349 }
350
351 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
352   if(t){
353     xbt_automaton_exp_label_free(t->label);
354     xbt_free(t);
355     t = NULL;
356   }
357 }
358
359 void xbt_automaton_transition_free_voidp(void *t){
360   xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
361 }
362
363 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
364   if(e){
365     switch(e->type){
366     case 0:
367     case 1:
368       xbt_automaton_exp_label_free(e->u.or_and.left_exp);
369       xbt_automaton_exp_label_free(e->u.or_and.right_exp);
370       break;
371     case 2:
372       xbt_automaton_exp_label_free(e->u.exp_not);
373       break;
374     case 3:
375       xbt_free(e->u.predicat);
376       break;
377     default:
378       break;
379     }
380     xbt_free(e);
381     e = NULL;
382   }
383 }
384
385 void xbt_automaton_exp_label_free_voidp(void *e){
386   xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
387 }
388
389 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
390   if(ps){
391     xbt_free(ps->pred);
392     xbt_free(ps);
393     ps = NULL;
394   }
395 }
396
397 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
398   xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
399   if (symbol->free_function)
400     symbol->free_function(symbol->data);
401   xbt_free(symbol->pred);
402   xbt_automaton_propositional_symbol_free(symbol);
403 }
404
405 void xbt_automaton_free(xbt_automaton_t a){
406   if(a){
407     xbt_dynar_free(&(a->propositional_symbols));
408     xbt_dynar_free(&(a->transitions));
409     xbt_dynar_free(&(a->states));
410     xbt_automaton_state_free(a->current_state);
411     xbt_free(a);
412     a = NULL;
413   }
414 }