Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : new example for liveness properties
[simgrid.git] / examples / msg / mc / automaton.c
1 #include "xbt/automaton.h"
2
3 xbt_automaton_t xbt_automaton_new_automaton(){
4   xbt_automaton_t automaton = NULL;
5   automaton = xbt_new0(struct xbt_automaton, 1);
6   automaton->states = xbt_dynar_new(sizeof(xbt_state_t), NULL);
7   automaton->transitions = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
8   automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_propositional_symbol_t), NULL);
9   return automaton;
10 }
11
12 xbt_state_t xbt_automaton_new_state(xbt_automaton_t a, int type, char* id){
13   xbt_state_t state = NULL;
14   state = xbt_new0(struct xbt_state, 1);
15   state->type = type;
16   state->id = strdup(id);
17   state->in = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
18   state->out = xbt_dynar_new(sizeof(xbt_transition_t), NULL); 
19   xbt_dynar_push(a->states, &state);
20   return state;
21 }
22
23 xbt_transition_t xbt_automaton_new_transition(xbt_automaton_t a, xbt_state_t src, xbt_state_t dst, xbt_exp_label_t label){
24   xbt_transition_t transition = NULL;
25   transition = xbt_new0(struct xbt_transition, 1);
26   if(src != NULL){
27     xbt_dynar_push(src->out, &transition);
28     transition->src = src;
29   }
30   if(dst != NULL){
31     xbt_dynar_push(dst->in, &transition);
32     transition->dst = dst;
33   }
34   transition->label = label;
35   xbt_dynar_push(a->transitions, &transition);
36   return transition;
37 }
38
39 xbt_exp_label_t xbt_automaton_new_label(int type, ...){
40   xbt_exp_label_t label = NULL;
41   label = xbt_new0(struct xbt_exp_label, 1);
42   label->type = type;
43
44   va_list ap;
45   va_start(ap, type);
46   switch(type){
47   case 0 : {
48     xbt_exp_label_t left = va_arg(ap, xbt_exp_label_t);
49     xbt_exp_label_t right = va_arg(ap, xbt_exp_label_t);
50     label->u.or_and.left_exp = left;
51     label->u.or_and.right_exp = right;
52     break;
53   }
54   case 1 : {
55     xbt_exp_label_t left = va_arg(ap, xbt_exp_label_t);
56     xbt_exp_label_t right = va_arg(ap, xbt_exp_label_t);
57     label->u.or_and.left_exp = left;
58     label->u.or_and.right_exp = right;
59     break;
60   }
61   case 2 : {
62     xbt_exp_label_t exp_not = va_arg(ap, xbt_exp_label_t);
63     label->u.exp_not = exp_not;
64     break;
65   }
66   case 3 :{
67     char* p = va_arg(ap, char*);
68     label->u.predicat = strdup(p);
69     break;
70   }
71   }
72   va_end(ap);
73   return label;
74 }
75
76
77 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
78   return a->states;
79 }
80
81 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
82   return a->transitions;
83 }
84
85 xbt_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_state_t src, xbt_state_t dst){
86   xbt_transition_t transition;
87   unsigned int cursor;
88   xbt_dynar_foreach(src->out, cursor, transition){
89     if((transition->src == src) && (transition->dst == dst))
90       return transition;
91   }
92   return NULL;
93 }
94
95 void xbt_automaton_free_automaton(xbt_automaton_t a, void_f_pvoid_t transition_free_function){
96   unsigned int cursor = 0;
97   xbt_state_t state = NULL;
98   xbt_transition_t transition = NULL;
99
100   xbt_dynar_foreach(a->states, cursor, state){
101     xbt_dynar_free(&(state->out));
102     xbt_dynar_free(&(state->in));
103   }
104
105   xbt_dynar_foreach(a->transitions, cursor, transition){
106     if(transition_free_function) 
107       (*transition_free_function) (transition->label);
108   }
109
110   xbt_dynar_foreach(a->states, cursor, state)
111     free(state);
112   xbt_dynar_free(&(a->states));
113
114   xbt_dynar_foreach(a->transitions, cursor, state)
115     free(transition);
116   xbt_dynar_free(&(a->transitions));
117
118   free(a);
119
120   return;
121 }
122
123 void xbt_automaton_free_state(xbt_automaton_t a, xbt_state_t s, void_f_pvoid_t transition_free_function){
124   unsigned long nbr;
125   unsigned long i;
126   unsigned int cursor = 0;
127   xbt_state_t state = NULL;
128   xbt_transition_t transition = NULL;
129
130   nbr = xbt_dynar_length(a->transitions);
131   for(i = 0; i <nbr; i++){
132     xbt_dynar_get_cpy(a->transitions, cursor, &transition);
133     if((transition->src == s) || (transition->dst == s)){
134       xbt_automaton_free_transition(a, transition, transition_free_function);
135     }else{
136       cursor++;
137     }
138   }
139
140   cursor = 0;
141   xbt_dynar_foreach(a->states, cursor, state)
142     if(state == s)
143       xbt_dynar_cursor_rm(a->states, &cursor);
144
145   xbt_dynar_free(&(s->in));
146   xbt_dynar_free(&(s->out));
147
148   free(s);
149
150   return;
151 }
152
153 void xbt_automaton_free_transition(xbt_automaton_t a, xbt_transition_t t, void_f_pvoid_t transition_free_function){
154   int index;
155   unsigned int cursor = 0;
156   xbt_transition_t transition = NULL;
157
158   if((transition_free_function) && (t->label))
159     (*transition_free_function) (t->label);
160
161   xbt_dynar_foreach(a->transitions, cursor, transition) {
162     if(transition == t){
163       index = __xbt_find_in_dynar(transition->dst->in, transition);
164       xbt_dynar_remove_at(transition->dst->in, index, NULL);
165       index = __xbt_find_in_dynar(transition->src->out, transition);
166       xbt_dynar_remove_at(transition->src->out, index, NULL);
167       xbt_dynar_cursor_rm(a->transitions, & cursor);
168       free(transition);
169       break;
170     }  
171   }
172
173
174 xbt_state_t xbt_automaton_transition_get_source(xbt_transition_t t){
175   return t->src;
176 }
177
178 xbt_state_t xbt_automaton_transition_get_destination(xbt_transition_t t){
179   return t->dst;
180 }
181
182 void xbt_automaton_transition_set_source(xbt_transition_t t, xbt_state_t src){
183   t->src = src;
184   xbt_dynar_push(src->out,&t);
185 }
186
187 void xbt_automaton_transition_set_destination(xbt_transition_t t, xbt_state_t dst){
188   t->dst = dst;
189   xbt_dynar_push(dst->in,&t);
190 }
191
192 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_state_t s){
193   return s->out;
194 }
195
196 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_state_t s){
197   return s->in;
198 }
199
200 xbt_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
201   xbt_state_t state = NULL;
202   unsigned int cursor = 0;
203   xbt_dynar_foreach(a->states, cursor, state){
204    if(strcmp(state->id, id)==0)
205      return state;
206   }
207   return NULL;
208 }
209
210 void  xbt_automaton_display(xbt_automaton_t a){
211   unsigned int cursor = 0;
212   xbt_state_t state = NULL;
213
214   printf("\n\nEtat courant : %s\n", a->current_state->id);
215
216   printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
217  
218   
219   xbt_dynar_foreach(a->states, cursor, state){
220     printf("ID : %s, type : %d\n", state->id, state->type);
221   }
222
223   cursor=0;
224   xbt_transition_t transition = NULL;
225   printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
226   
227   xbt_dynar_foreach(a->transitions, cursor, transition){
228     printf("label :");
229     xbt_automaton_display_exp(transition->label);
230     printf(", %s -> %s\n", transition->src->id, transition->dst->id);
231   }
232 }
233
234 void xbt_automaton_display_exp(xbt_exp_label_t label){
235
236   switch(label->type){
237   case 0 :
238     printf("(");
239     xbt_automaton_display_exp(label->u.or_and.left_exp);
240     printf(" || ");
241     xbt_automaton_display_exp(label->u.or_and.right_exp);
242     printf(")");
243     break;
244   case 1 : 
245     printf("(");
246     xbt_automaton_display_exp(label->u.or_and.left_exp);
247     printf(" && ");
248     xbt_automaton_display_exp(label->u.or_and.right_exp);
249     printf(")");
250     break;
251   case 2 : 
252     printf("(!");
253     xbt_automaton_display_exp(label->u.exp_not);
254     printf(")");
255     break;
256   case 3 :
257     printf("(%s)",label->u.predicat);
258     break;
259   case 4 :
260     printf("(1)");
261     break;
262   }
263
264 }
265
266 xbt_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
267   return a->current_state;
268 }
269
270 xbt_propositional_symbol_t xbt_new_propositional_symbol(xbt_automaton_t a, const char* id, void* fct){
271   xbt_propositional_symbol_t prop_symb = NULL;
272   prop_symb = xbt_new0(struct xbt_propositional_symbol, 1);
273   prop_symb->pred = strdup(id);
274   prop_symb->function = fct;
275   xbt_dynar_push(a->propositional_symbols, &prop_symb);
276   return prop_symb;
277 }
278
279 int automaton_state_compare(xbt_state_t s1, xbt_state_t s2){
280
281   /* single id for each state, id and type sufficient for comparison*/
282
283   if(strcmp(s1->id, s2->id))
284     return 1;
285
286   if(s1->type != s2->type)
287     return 1;
288
289   return 0;
290
291 }
292
293 int automaton_transition_compare(const void *t1, const void *t2){
294
295   if(automaton_state_compare(((xbt_transition_t)t1)->src, ((xbt_transition_t)t2)->src))
296     return 1;
297   
298   if(automaton_state_compare(((xbt_transition_t)t1)->dst, ((xbt_transition_t)t2)->dst))
299     return 1;
300
301   if(automaton_label_transition_compare(((xbt_transition_t)t1)->label,((xbt_transition_t)t2)->label))
302     return 1;
303
304   return 0;
305   
306 }
307
308 int automaton_label_transition_compare(xbt_exp_label_t l1, xbt_exp_label_t l2){
309
310   if(l1->type != l2->type)
311     return 1;
312
313   switch(l1->type){
314
315   case 0 : // OR 
316   case 1 : // AND
317     if(automaton_label_transition_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
318       return 1;
319     else
320       return automaton_label_transition_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
321     break;
322
323   case 2 : // NOT
324     return automaton_label_transition_compare(l1->u.exp_not, l2->u.exp_not);
325     break;
326
327   case 3 : // predicat
328     return (strcmp(l1->u.predicat, l2->u.predicat));
329     break;
330
331   case 4 : // 1
332     return 0;
333     break;
334
335   default :
336     return -1;
337     break;
338
339   }
340
341 }
342
343
344 int propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
345
346   int *iptr1, *iptr2;
347   unsigned int cursor;
348   unsigned int nb_elem = xbt_dynar_length(s1);
349
350   for(cursor=0;cursor<nb_elem;cursor++){
351     iptr1 = xbt_dynar_get_ptr(s1, cursor);
352     iptr2 = xbt_dynar_get_ptr(s2, cursor);
353     if(*iptr1 != *iptr2)
354       return 1;
355   } 
356
357   return 0;
358 }