1
#include "xbt/automaton.h"
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);
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);
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);
27
xbt_dynar_push(src->out, &transition);
28
transition->src = src;
31
xbt_dynar_push(dst->in, &transition);
32
transition->dst = dst;
34
transition->label = label;
35
xbt_dynar_push(a->transitions, &transition);
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);
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;
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;
62
xbt_exp_label_t exp_not = va_arg(ap, xbt_exp_label_t);
63
label->u.exp_not = exp_not;
67
char* p = va_arg(ap, char*);
68
label->u.predicat = strdup(p);
77
xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
81
xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
82
return a->transitions;
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;
88
xbt_dynar_foreach(src->out, cursor, transition){
89
if((transition->src == src) && (transition->dst == dst))
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;
100
xbt_dynar_foreach(a->states, cursor, state){
101
xbt_dynar_free(&(state->out));
102
xbt_dynar_free(&(state->in));
105
xbt_dynar_foreach(a->transitions, cursor, transition){
106
if(transition_free_function)
107
(*transition_free_function) (transition->label);
110
xbt_dynar_foreach(a->states, cursor, state)
112
xbt_dynar_free(&(a->states));
114
xbt_dynar_foreach(a->transitions, cursor, state)
116
xbt_dynar_free(&(a->transitions));
123
void xbt_automaton_free_state(xbt_automaton_t a, xbt_state_t s, void_f_pvoid_t transition_free_function){
126
unsigned int cursor = 0;
127
xbt_state_t state = NULL;
128
xbt_transition_t transition = NULL;
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);
141
xbt_dynar_foreach(a->states, cursor, state)
143
xbt_dynar_cursor_rm(a->states, &cursor);
145
xbt_dynar_free(&(s->in));
146
xbt_dynar_free(&(s->out));
153
void xbt_automaton_free_transition(xbt_automaton_t a, xbt_transition_t t, void_f_pvoid_t transition_free_function){
155
unsigned int cursor = 0;
156
xbt_transition_t transition = NULL;
158
if((transition_free_function) && (t->label))
159
(*transition_free_function) (t->label);
161
xbt_dynar_foreach(a->transitions, cursor, transition) {
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);
174
xbt_state_t xbt_automaton_transition_get_source(xbt_transition_t t){
178
xbt_state_t xbt_automaton_transition_get_destination(xbt_transition_t t){
182
void xbt_automaton_transition_set_source(xbt_transition_t t, xbt_state_t src){
184
xbt_dynar_push(src->out,&t);
187
void xbt_automaton_transition_set_destination(xbt_transition_t t, xbt_state_t dst){
189
xbt_dynar_push(dst->in,&t);
192
xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_state_t s){
196
xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_state_t s){
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)
210
void xbt_automaton_display(xbt_automaton_t a){
211
unsigned int cursor = 0;
212
xbt_state_t state = NULL;
214
printf("\n\nEtat courant : %s\n", a->current_state->id);
216
printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
219
xbt_dynar_foreach(a->states, cursor, state){
220
printf("ID : %s, type : %d\n", state->id, state->type);
224
xbt_transition_t transition = NULL;
225
printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
227
xbt_dynar_foreach(a->transitions, cursor, transition){
229
xbt_automaton_display_exp(transition->label);
230
printf(", %s -> %s\n", transition->src->id, transition->dst->id);
234
void xbt_automaton_display_exp(xbt_exp_label_t label){
239
xbt_automaton_display_exp(label->u.or_and.left_exp);
241
xbt_automaton_display_exp(label->u.or_and.right_exp);
246
xbt_automaton_display_exp(label->u.or_and.left_exp);
248
xbt_automaton_display_exp(label->u.or_and.right_exp);
253
xbt_automaton_display_exp(label->u.exp_not);
257
printf("(%s)",label->u.predicat);
266
xbt_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
267
return a->current_state;
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);
279
int automaton_state_compare(xbt_state_t s1, xbt_state_t s2){
281
/* single id for each state, id and type sufficient for comparison*/
283
if(strcmp(s1->id, s2->id))
286
if(s1->type != s2->type)
293
int automaton_transition_compare(const void *t1, const void *t2){
295
if(automaton_state_compare(((xbt_transition_t)t1)->src, ((xbt_transition_t)t2)->src))
298
if(automaton_state_compare(((xbt_transition_t)t1)->dst, ((xbt_transition_t)t2)->dst))
301
if(automaton_label_transition_compare(((xbt_transition_t)t1)->label,((xbt_transition_t)t2)->label))
308
int automaton_label_transition_compare(xbt_exp_label_t l1, xbt_exp_label_t l2){
310
if(l1->type != l2->type)
317
if(automaton_label_transition_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
320
return automaton_label_transition_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
324
return automaton_label_transition_compare(l1->u.exp_not, l2->u.exp_not);
328
return (strcmp(l1->u.predicat, l2->u.predicat));
344
int propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
348
unsigned int nb_elem = xbt_dynar_length(s1);
350
for(cursor=0;cursor<nb_elem;cursor++){
351
iptr1 = xbt_dynar_get_ptr(s1, cursor);
352
iptr2 = xbt_dynar_get_ptr(s2, cursor);