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);
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);
17 state->id = strdup(id);
18 state->in = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
19 state->out = xbt_dynar_new(sizeof(xbt_transition_t), NULL);
20 xbt_dynar_push(a->states, &state);
24 xbt_transition_t xbt_automaton_new_transition(xbt_automaton_t a, xbt_state_t src, xbt_state_t dst, xbt_exp_label_t label){
25 xbt_transition_t transition = NULL;
26 transition = xbt_new0(struct xbt_transition, 1);
28 xbt_dynar_push(src->out, &transition);
29 transition->src = src;
32 xbt_dynar_push(dst->in, &transition);
33 transition->dst = dst;
35 transition->label = label;
36 xbt_dynar_push(a->transitions, &transition);
40 xbt_exp_label_t xbt_automaton_new_label(int type, ...){
41 xbt_exp_label_t label = NULL;
42 label = xbt_new0(struct xbt_exp_label, 1);
49 xbt_exp_label_t left = va_arg(ap, xbt_exp_label_t);
50 xbt_exp_label_t right = va_arg(ap, xbt_exp_label_t);
51 label->u.or_and.left_exp = left;
52 label->u.or_and.right_exp = right;
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;
63 xbt_exp_label_t exp_not = va_arg(ap, xbt_exp_label_t);
64 label->u.exp_not = exp_not;
68 char* p = va_arg(ap, char*);
69 label->u.predicat = strdup(p);
78 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
82 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
83 return a->transitions;
86 xbt_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_state_t src, xbt_state_t dst){
87 xbt_transition_t transition;
89 xbt_dynar_foreach(src->out, cursor, transition){
90 if((transition->src == src) && (transition->dst == dst))
96 void xbt_automaton_free_automaton(xbt_automaton_t a, void_f_pvoid_t transition_free_function){
97 unsigned int cursor = 0;
98 xbt_state_t state = NULL;
99 xbt_transition_t transition = NULL;
101 xbt_dynar_foreach(a->states, cursor, state){
102 xbt_dynar_free(&(state->out));
103 xbt_dynar_free(&(state->in));
106 xbt_dynar_foreach(a->transitions, cursor, transition){
107 if(transition_free_function)
108 (*transition_free_function) (transition->label);
111 xbt_dynar_foreach(a->states, cursor, state)
113 xbt_dynar_free(&(a->states));
115 xbt_dynar_foreach(a->transitions, cursor, state)
117 xbt_dynar_free(&(a->transitions));
124 void xbt_automaton_free_state(xbt_automaton_t a, xbt_state_t s, void_f_pvoid_t transition_free_function){
127 unsigned int cursor = 0;
128 xbt_state_t state = NULL;
129 xbt_transition_t transition = NULL;
131 nbr = xbt_dynar_length(a->transitions);
132 for(i = 0; i <nbr; i++){
133 xbt_dynar_get_cpy(a->transitions, cursor, &transition);
134 if((transition->src == s) || (transition->dst == s)){
135 xbt_automaton_free_transition(a, transition, transition_free_function);
142 xbt_dynar_foreach(a->states, cursor, state)
144 xbt_dynar_cursor_rm(a->states, &cursor);
146 xbt_dynar_free(&(s->in));
147 xbt_dynar_free(&(s->out));
154 void xbt_automaton_free_transition(xbt_automaton_t a, xbt_transition_t t, void_f_pvoid_t transition_free_function){
156 unsigned int cursor = 0;
157 xbt_transition_t transition = NULL;
159 if((transition_free_function) && (t->label))
160 (*transition_free_function) (t->label);
162 xbt_dynar_foreach(a->transitions, cursor, transition) {
164 index = __xbt_find_in_dynar(transition->dst->in, transition);
165 xbt_dynar_remove_at(transition->dst->in, index, NULL);
166 index = __xbt_find_in_dynar(transition->src->out, transition);
167 xbt_dynar_remove_at(transition->src->out, index, NULL);
168 xbt_dynar_cursor_rm(a->transitions, & cursor);
175 xbt_state_t xbt_automaton_transition_get_source(xbt_transition_t t){
179 xbt_state_t xbt_automaton_transition_get_destination(xbt_transition_t t){
183 void xbt_automaton_transition_set_source(xbt_transition_t t, xbt_state_t src){
185 xbt_dynar_push(src->out,&t);
188 void xbt_automaton_transition_set_destination(xbt_transition_t t, xbt_state_t dst){
190 xbt_dynar_push(dst->in,&t);
193 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_state_t s){
197 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_state_t s){
201 xbt_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
202 xbt_state_t state = NULL;
203 unsigned int cursor = 0;
204 xbt_dynar_foreach(a->states, cursor, state){
205 if(strcmp(state->id, id)==0)
211 void xbt_automaton_display(xbt_automaton_t a){
212 unsigned int cursor = 0;
213 xbt_state_t state = NULL;
215 printf("\n\nEtat courant : %s\n", a->current_state->id);
217 printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
220 xbt_dynar_foreach(a->states, cursor, state){
221 printf("ID : %s, type : %d\n", state->id, state->type);
225 xbt_transition_t transition = NULL;
226 printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
228 xbt_dynar_foreach(a->transitions, cursor, transition){
230 xbt_automaton_display_exp(transition->label);
231 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
235 void xbt_automaton_display_exp(xbt_exp_label_t label){
240 xbt_automaton_display_exp(label->u.or_and.left_exp);
242 xbt_automaton_display_exp(label->u.or_and.right_exp);
247 xbt_automaton_display_exp(label->u.or_and.left_exp);
249 xbt_automaton_display_exp(label->u.or_and.right_exp);
254 xbt_automaton_display_exp(label->u.exp_not);
258 printf("(%s)",label->u.predicat);
267 xbt_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
268 return a->current_state;
271 xbt_propositional_symbol_t xbt_new_propositional_symbol(xbt_automaton_t a, const char* id, void* fct){
272 xbt_propositional_symbol_t prop_symb = NULL;
273 prop_symb = xbt_new0(struct xbt_propositional_symbol, 1);
274 prop_symb->pred = strdup(id);
275 prop_symb->function = fct;
276 xbt_dynar_push(a->propositional_symbols, &prop_symb);