Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
correction compile warnings
[simgrid.git] / src / xbt / automaton.c
1 #include "xbt/automaton.h"
2 #include "xbt/dynar.h"
3
4 xbt_automaton_t xbt_automaton_new_automaton(){
5   xbt_automaton_t automaton = NULL;
6   automaton = xbt_new0(struct xbt_automaton, 1);
7   automaton->states = xbt_dynar_new(sizeof(xbt_state_t), NULL);
8   automaton->transitions = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
9   automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_propositional_symbol_t), NULL);
10   return automaton;
11 }
12
13 xbt_state_t xbt_automaton_new_state(xbt_automaton_t a, int type, char* id){
14   xbt_state_t state = NULL;
15   state = xbt_new0(struct xbt_state, 1);
16   state->visited = 0;
17   state->type = type;
18   state->id = strdup(id);
19   state->in = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
20   state->out = xbt_dynar_new(sizeof(xbt_transition_t), NULL); 
21   xbt_dynar_push(a->states, &state);
22   return state;
23 }
24
25 xbt_transition_t xbt_automaton_new_transition(xbt_automaton_t a, xbt_state_t src, xbt_state_t dst, xbt_exp_label_t label){
26   xbt_transition_t transition = NULL;
27   transition = xbt_new0(struct xbt_transition, 1);
28   if(src != NULL){
29     xbt_dynar_push(src->out, &transition);
30     transition->src = src;
31   }
32   if(dst != NULL){
33     xbt_dynar_push(dst->in, &transition);
34     transition->dst = dst;
35   }
36   transition->label = label;
37   xbt_dynar_push(a->transitions, &transition);
38   return transition;
39 }
40
41 xbt_exp_label_t xbt_automaton_new_label(int type, ...){
42   xbt_exp_label_t label = NULL;
43   label = xbt_new0(struct xbt_exp_label, 1);
44   label->type = type;
45
46   va_list ap;
47   va_start(ap, type);
48   switch(type){
49   case 0 : {
50     xbt_exp_label_t left = va_arg(ap, xbt_exp_label_t);
51     xbt_exp_label_t right = va_arg(ap, xbt_exp_label_t);
52     label->u.or_and.left_exp = left;
53     label->u.or_and.right_exp = right;
54     break;
55   }
56   case 1 : {
57     xbt_exp_label_t left = va_arg(ap, xbt_exp_label_t);
58     xbt_exp_label_t right = va_arg(ap, xbt_exp_label_t);
59     label->u.or_and.left_exp = left;
60     label->u.or_and.right_exp = right;
61     break;
62   }
63   case 2 : {
64     xbt_exp_label_t exp_not = va_arg(ap, xbt_exp_label_t);
65     label->u.exp_not = exp_not;
66     break;
67   }
68   case 3 :{
69     char* p = va_arg(ap, char*);
70     label->u.predicat = strdup(p);
71     break;
72   }
73   }
74   va_end(ap);
75   return label;
76 }
77
78
79 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
80   return a->states;
81 }
82
83 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
84   return a->transitions;
85 }
86
87 xbt_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_state_t src, xbt_state_t dst){
88   xbt_transition_t transition;
89   unsigned int cursor;
90   xbt_dynar_foreach(src->out, cursor, transition){
91     if((transition->src == src) && (transition->dst == dst))
92       return transition;
93   }
94   return NULL;
95 }
96
97 void xbt_automaton_free_automaton(xbt_automaton_t a, void_f_pvoid_t transition_free_function){
98   unsigned int cursor = 0;
99   xbt_state_t state = NULL;
100   xbt_transition_t transition = NULL;
101
102   xbt_dynar_foreach(a->states, cursor, state){
103     xbt_dynar_free(&(state->out));
104     xbt_dynar_free(&(state->in));
105   }
106
107   xbt_dynar_foreach(a->transitions, cursor, transition){
108     if(transition_free_function) 
109       (*transition_free_function) (transition->label);
110   }
111
112   xbt_dynar_foreach(a->states, cursor, state)
113     free(state);
114   xbt_dynar_free(&(a->states));
115
116   xbt_dynar_foreach(a->transitions, cursor, state)
117     free(transition);
118   xbt_dynar_free(&(a->transitions));
119
120   free(a);
121
122   return;
123 }
124
125 void xbt_automaton_free_state(xbt_automaton_t a, xbt_state_t s, void_f_pvoid_t transition_free_function){
126   unsigned long nbr;
127   unsigned long i;
128   unsigned int cursor = 0;
129   xbt_state_t state = NULL;
130   xbt_transition_t transition = NULL;
131
132   nbr = xbt_dynar_length(a->transitions);
133   for(i = 0; i <nbr; i++){
134     xbt_dynar_get_cpy(a->transitions, cursor, &transition);
135     if((transition->src == s) || (transition->dst == s)){
136       xbt_automaton_free_transition(a, transition, transition_free_function);
137     }else{
138       cursor++;
139     }
140   }
141
142   cursor = 0;
143   xbt_dynar_foreach(a->states, cursor, state)
144     if(state == s)
145       xbt_dynar_cursor_rm(a->states, &cursor);
146
147   xbt_dynar_free(&(s->in));
148   xbt_dynar_free(&(s->out));
149
150   free(s);
151
152   return;
153 }
154
155 void xbt_automaton_free_transition(xbt_automaton_t a, xbt_transition_t t, void_f_pvoid_t transition_free_function){
156   int index;
157   unsigned int cursor = 0;
158   xbt_transition_t transition = NULL;
159
160   if((transition_free_function) && (t->label))
161     (*transition_free_function) (t->label);
162
163   xbt_dynar_foreach(a->transitions, cursor, transition) {
164     if(transition == t){
165       index = __xbt_find_in_dynar(transition->dst->in, transition);
166       xbt_dynar_remove_at(transition->dst->in, index, NULL);
167       index = __xbt_find_in_dynar(transition->src->out, transition);
168       xbt_dynar_remove_at(transition->src->out, index, NULL);
169       xbt_dynar_cursor_rm(a->transitions, & cursor);
170       free(transition);
171       break;
172     }  
173   }
174
175
176 xbt_state_t xbt_automaton_transition_get_source(xbt_transition_t t){
177   return t->src;
178 }
179
180 xbt_state_t xbt_automaton_transition_get_destination(xbt_transition_t t){
181   return t->dst;
182 }
183
184 void xbt_automaton_transition_set_source(xbt_transition_t t, xbt_state_t src){
185   t->src = src;
186   xbt_dynar_push(src->out,&t);
187 }
188
189 void xbt_automaton_transition_set_destination(xbt_transition_t t, xbt_state_t dst){
190   t->dst = dst;
191   xbt_dynar_push(dst->in,&t);
192 }
193
194 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_state_t s){
195   return s->out;
196 }
197
198 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_state_t s){
199   return s->in;
200 }
201
202 xbt_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
203   xbt_state_t state = NULL;
204   unsigned int cursor = 0;
205   xbt_dynar_foreach(a->states, cursor, state){
206    if(strcmp(state->id, id)==0)
207      return state;
208   }
209   return NULL;
210 }
211
212 void  xbt_automaton_display(xbt_automaton_t a){
213   unsigned int cursor = 0;
214   xbt_state_t state = NULL;
215
216   printf("\n\nEtat courant : %s\n", a->current_state->id);
217
218   printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
219  
220   
221   xbt_dynar_foreach(a->states, cursor, state){
222     printf("ID : %s, type : %d\n", state->id, state->type);
223   }
224
225   cursor=0;
226   xbt_transition_t transition = NULL;
227   printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
228   
229   xbt_dynar_foreach(a->transitions, cursor, transition){
230     printf("label :");
231     xbt_automaton_display_exp(transition->label);
232     printf(", %s -> %s\n", transition->src->id, transition->dst->id);
233   }
234 }
235
236 void xbt_automaton_display_exp(xbt_exp_label_t label){
237
238   switch(label->type){
239   case 0 :
240     printf("(");
241     xbt_automaton_display_exp(label->u.or_and.left_exp);
242     printf(" || ");
243     xbt_automaton_display_exp(label->u.or_and.right_exp);
244     printf(")");
245     break;
246   case 1 : 
247     printf("(");
248     xbt_automaton_display_exp(label->u.or_and.left_exp);
249     printf(" && ");
250     xbt_automaton_display_exp(label->u.or_and.right_exp);
251     printf(")");
252     break;
253   case 2 : 
254     printf("(!");
255     xbt_automaton_display_exp(label->u.exp_not);
256     printf(")");
257     break;
258   case 3 :
259     printf("(%s)",label->u.predicat);
260     break;
261   case 4 :
262     printf("(1)");
263     break;
264   }
265
266 }
267
268 xbt_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
269   return a->current_state;
270 }
271
272 xbt_propositional_symbol_t xbt_new_propositional_symbol(xbt_automaton_t a, const char* id, void* fct){
273   xbt_propositional_symbol_t prop_symb = NULL;
274   prop_symb = xbt_new0(struct xbt_propositional_symbol, 1);
275   prop_symb->pred = strdup(id);
276   prop_symb->function = fct;
277   xbt_dynar_push(a->propositional_symbols, &prop_symb);
278   return prop_symb;
279 }