1 #include "xbt/automaton.h"
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);
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);
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);
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);
29 xbt_dynar_push(src->out, &transition);
30 transition->src = src;
33 xbt_dynar_push(dst->in, &transition);
34 transition->dst = dst;
36 transition->label = label;
37 xbt_dynar_push(a->transitions, &transition);
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);
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;
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;
64 xbt_exp_label_t exp_not = va_arg(ap, xbt_exp_label_t);
65 label->u.exp_not = exp_not;
69 char* p = va_arg(ap, char*);
70 label->u.predicat = strdup(p);
79 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
83 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
84 return a->transitions;
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;
90 xbt_dynar_foreach(src->out, cursor, transition){
91 if((transition->src == src) && (transition->dst == dst))
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;
102 xbt_dynar_foreach(a->states, cursor, state){
103 xbt_dynar_free(&(state->out));
104 xbt_dynar_free(&(state->in));
107 xbt_dynar_foreach(a->transitions, cursor, transition){
108 if(transition_free_function)
109 (*transition_free_function) (transition->label);
112 xbt_dynar_foreach(a->states, cursor, state)
114 xbt_dynar_free(&(a->states));
116 xbt_dynar_foreach(a->transitions, cursor, state)
118 xbt_dynar_free(&(a->transitions));
125 void xbt_automaton_free_state(xbt_automaton_t a, xbt_state_t s, void_f_pvoid_t transition_free_function){
128 unsigned int cursor = 0;
129 xbt_state_t state = NULL;
130 xbt_transition_t transition = NULL;
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);
143 xbt_dynar_foreach(a->states, cursor, state)
145 xbt_dynar_cursor_rm(a->states, &cursor);
147 xbt_dynar_free(&(s->in));
148 xbt_dynar_free(&(s->out));
155 void xbt_automaton_free_transition(xbt_automaton_t a, xbt_transition_t t, void_f_pvoid_t transition_free_function){
157 unsigned int cursor = 0;
158 xbt_transition_t transition = NULL;
160 if((transition_free_function) && (t->label))
161 (*transition_free_function) (t->label);
163 xbt_dynar_foreach(a->transitions, cursor, transition) {
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);
176 xbt_state_t xbt_automaton_transition_get_source(xbt_transition_t t){
180 xbt_state_t xbt_automaton_transition_get_destination(xbt_transition_t t){
184 void xbt_automaton_transition_set_source(xbt_transition_t t, xbt_state_t src){
186 xbt_dynar_push(src->out,&t);
189 void xbt_automaton_transition_set_destination(xbt_transition_t t, xbt_state_t dst){
191 xbt_dynar_push(dst->in,&t);
194 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_state_t s){
198 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_state_t s){
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)
212 void xbt_automaton_display(xbt_automaton_t a){
213 unsigned int cursor = 0;
214 xbt_state_t state = NULL;
216 printf("\n\nEtat courant : %s\n", a->current_state->id);
218 printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
221 xbt_dynar_foreach(a->states, cursor, state){
222 printf("ID : %s, type : %d\n", state->id, state->type);
226 xbt_transition_t transition = NULL;
227 printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
229 xbt_dynar_foreach(a->transitions, cursor, transition){
231 xbt_automaton_display_exp(transition->label);
232 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
236 void xbt_automaton_display_exp(xbt_exp_label_t label){
241 xbt_automaton_display_exp(label->u.or_and.left_exp);
243 xbt_automaton_display_exp(label->u.or_and.right_exp);
248 xbt_automaton_display_exp(label->u.or_and.left_exp);
250 xbt_automaton_display_exp(label->u.or_and.right_exp);
255 xbt_automaton_display_exp(label->u.exp_not);
259 printf("(%s)",label->u.predicat);
268 xbt_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
269 return a->current_state;
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);