Logo AND Algorithmique Numérique Distribuée

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