1 /* automaton - representation of büchi automaton */
3 /* Copyright (c) 2011. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #include "xbt/automaton.h"
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);
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);
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);
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);
35 xbt_dynar_push(src->out, &transition);
36 transition->src = src;
39 xbt_dynar_push(dst->in, &transition);
40 transition->dst = dst;
42 transition->label = label;
43 xbt_dynar_push(a->transitions, &transition);
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);
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 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;
70 xbt_exp_label_t exp_not = va_arg(ap, xbt_exp_label_t);
71 label->u.exp_not = exp_not;
75 char* p = va_arg(ap, char*);
76 label->u.predicat = strdup(p);
85 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
89 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
90 return a->transitions;
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;
96 xbt_dynar_foreach(src->out, cursor, transition){
97 if((transition->src == src) && (transition->dst == dst))
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;
108 xbt_dynar_foreach(a->states, cursor, state){
109 xbt_dynar_free(&(state->out));
110 xbt_dynar_free(&(state->in));
113 xbt_dynar_foreach(a->transitions, cursor, transition){
114 if(transition_free_function)
115 (*transition_free_function) (transition->label);
118 xbt_dynar_foreach(a->states, cursor, state)
120 xbt_dynar_free(&(a->states));
122 xbt_dynar_foreach(a->transitions, cursor, state)
124 xbt_dynar_free(&(a->transitions));
131 void xbt_automaton_free_state(xbt_automaton_t a, xbt_state_t s, void_f_pvoid_t transition_free_function){
134 unsigned int cursor = 0;
135 xbt_state_t state = NULL;
136 xbt_transition_t transition = NULL;
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);
149 xbt_dynar_foreach(a->states, cursor, state)
151 xbt_dynar_cursor_rm(a->states, &cursor);
153 xbt_dynar_free(&(s->in));
154 xbt_dynar_free(&(s->out));
161 void xbt_automaton_free_transition(xbt_automaton_t a, xbt_transition_t t, void_f_pvoid_t transition_free_function){
163 unsigned int cursor = 0;
164 xbt_transition_t transition = NULL;
166 if((transition_free_function) && (t->label))
167 (*transition_free_function) (t->label);
169 xbt_dynar_foreach(a->transitions, cursor, transition) {
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);
182 xbt_state_t xbt_automaton_transition_get_source(xbt_transition_t t){
186 xbt_state_t xbt_automaton_transition_get_destination(xbt_transition_t t){
190 void xbt_automaton_transition_set_source(xbt_transition_t t, xbt_state_t src){
192 xbt_dynar_push(src->out,&t);
195 void xbt_automaton_transition_set_destination(xbt_transition_t t, xbt_state_t dst){
197 xbt_dynar_push(dst->in,&t);
200 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_state_t s){
204 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_state_t s){
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)
218 void xbt_automaton_display(xbt_automaton_t a){
219 unsigned int cursor = 0;
220 xbt_state_t state = NULL;
222 printf("\n\nEtat courant : %s\n", a->current_state->id);
224 printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
227 xbt_dynar_foreach(a->states, cursor, state){
228 printf("ID : %s, type : %d\n", state->id, state->type);
232 xbt_transition_t transition = NULL;
233 printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
235 xbt_dynar_foreach(a->transitions, cursor, transition){
237 xbt_automaton_display_exp(transition->label);
238 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
242 void xbt_automaton_display_exp(xbt_exp_label_t label){
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.or_and.left_exp);
256 xbt_automaton_display_exp(label->u.or_and.right_exp);
261 xbt_automaton_display_exp(label->u.exp_not);
265 printf("(%s)",label->u.predicat);
274 xbt_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
275 return a->current_state;
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);
287 int automaton_state_compare(xbt_state_t s1, xbt_state_t s2){
289 /* single id for each state, id and type sufficient for comparison*/
291 if(strcmp(s1->id, s2->id))
294 if(s1->type != s2->type)
301 int automaton_transition_compare(const void *t1, const void *t2){
303 if(automaton_state_compare(((xbt_transition_t)t1)->src, ((xbt_transition_t)t2)->src))
306 if(automaton_state_compare(((xbt_transition_t)t1)->dst, ((xbt_transition_t)t2)->dst))
309 if(automaton_label_transition_compare(((xbt_transition_t)t1)->label,((xbt_transition_t)t2)->label))
316 int automaton_label_transition_compare(xbt_exp_label_t l1, xbt_exp_label_t l2){
318 if(l1->type != l2->type)
325 if(automaton_label_transition_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
328 return automaton_label_transition_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
332 return automaton_label_transition_compare(l1->u.exp_not, l2->u.exp_not);
336 return (strcmp(l1->u.predicat, l2->u.predicat));
352 int propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
356 unsigned int nb_elem = xbt_dynar_length(s1);
358 for(cursor=0;cursor<nb_elem;cursor++){
359 iptr1 = xbt_dynar_get_ptr(s1, cursor);
360 iptr2 = xbt_dynar_get_ptr(s2, cursor);