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), xbt_state_free_voidp);
15 automaton->transitions = xbt_dynar_new(sizeof(xbt_transition_t), xbt_transition_free_voidp);
16 automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_propositional_symbol_t), xbt_propositional_symbol_free_voidp);
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), xbt_transition_free_voidp);
26 state->out = xbt_dynar_new(sizeof(xbt_transition_t), xbt_transition_free_voidp);
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 xbt_state_t xbt_automaton_transition_get_source(xbt_transition_t t){
107 xbt_state_t xbt_automaton_transition_get_destination(xbt_transition_t t){
111 void xbt_automaton_transition_set_source(xbt_transition_t t, xbt_state_t src){
113 xbt_dynar_push(src->out,&t);
116 void xbt_automaton_transition_set_destination(xbt_transition_t t, xbt_state_t dst){
118 xbt_dynar_push(dst->in,&t);
121 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_state_t s){
125 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_state_t s){
129 xbt_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
130 xbt_state_t state = NULL;
131 unsigned int cursor = 0;
132 xbt_dynar_foreach(a->states, cursor, state){
133 if(strcmp(state->id, id)==0)
139 void xbt_automaton_display(xbt_automaton_t a){
140 unsigned int cursor = 0;
141 xbt_state_t state = NULL;
143 printf("\n\nEtat courant : %s\n", a->current_state->id);
145 printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
148 xbt_dynar_foreach(a->states, cursor, state){
149 printf("ID : %s, type : %d\n", state->id, state->type);
153 xbt_transition_t transition = NULL;
154 printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
156 xbt_dynar_foreach(a->transitions, cursor, transition){
158 xbt_automaton_display_exp(transition->label);
159 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
163 void xbt_automaton_display_exp(xbt_exp_label_t label){
168 xbt_automaton_display_exp(label->u.or_and.left_exp);
170 xbt_automaton_display_exp(label->u.or_and.right_exp);
175 xbt_automaton_display_exp(label->u.or_and.left_exp);
177 xbt_automaton_display_exp(label->u.or_and.right_exp);
182 xbt_automaton_display_exp(label->u.exp_not);
186 printf("(%s)",label->u.predicat);
195 xbt_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
196 return a->current_state;
199 xbt_propositional_symbol_t xbt_new_propositional_symbol(xbt_automaton_t a, const char* id, void* fct){
200 xbt_propositional_symbol_t prop_symb = NULL;
201 prop_symb = xbt_new0(struct xbt_propositional_symbol, 1);
202 prop_symb->pred = strdup(id);
203 prop_symb->function = fct;
204 xbt_dynar_push(a->propositional_symbols, &prop_symb);
208 int automaton_state_compare(xbt_state_t s1, xbt_state_t s2){
210 /* single id for each state, id and type sufficient for comparison*/
212 if(strcmp(s1->id, s2->id))
215 if(s1->type != s2->type)
222 int automaton_transition_compare(const void *t1, const void *t2){
224 if(automaton_state_compare(((xbt_transition_t)t1)->src, ((xbt_transition_t)t2)->src))
227 if(automaton_state_compare(((xbt_transition_t)t1)->dst, ((xbt_transition_t)t2)->dst))
230 if(automaton_label_transition_compare(((xbt_transition_t)t1)->label,((xbt_transition_t)t2)->label))
237 int automaton_label_transition_compare(xbt_exp_label_t l1, xbt_exp_label_t l2){
239 if(l1->type != l2->type)
246 if(automaton_label_transition_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
249 return automaton_label_transition_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
253 return automaton_label_transition_compare(l1->u.exp_not, l2->u.exp_not);
257 return (strcmp(l1->u.predicat, l2->u.predicat));
273 int propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
277 unsigned int nb_elem = xbt_dynar_length(s1);
279 for(cursor=0;cursor<nb_elem;cursor++){
280 iptr1 = xbt_dynar_get_ptr(s1, cursor);
281 iptr2 = xbt_dynar_get_ptr(s2, cursor);
289 /************ Free functions ****************/
291 static void xbt_transition_free(xbt_transition_t t);
292 static void xbt_exp_label_free(xbt_exp_label_t e);
293 static void xbt_propositional_symbol_free(xbt_propositional_symbol_t ps);
295 void xbt_state_free(xbt_state_t s){
298 xbt_dynar_free(&(s->in));
299 xbt_dynar_free(&(s->out));
305 void xbt_state_free_voidp(void *s){
306 xbt_state_free((xbt_state_t) * (void **) s);
309 static void xbt_transition_free(xbt_transition_t t){
311 xbt_exp_label_free(t->label);
317 void xbt_transition_free_voidp(void *t){
318 xbt_transition_free((xbt_transition_t) * (void **) t);
321 static void xbt_exp_label_free(xbt_exp_label_t e){
326 xbt_exp_label_free(e->u.or_and.left_exp);
327 xbt_exp_label_free(e->u.or_and.right_exp);
330 xbt_exp_label_free(e->u.exp_not);
333 xbt_free(e->u.predicat);
343 void xbt_exp_label_free_voidp(void *e){
344 xbt_exp_label_free((xbt_exp_label_t) * (void **) e);
347 static void xbt_propositional_symbol_free(xbt_propositional_symbol_t ps){
355 void xbt_propositional_symbol_free_voidp(void *ps){
356 xbt_propositional_symbol_free((xbt_propositional_symbol_t) * (void **) ps);
359 void xbt_automaton_free(xbt_automaton_t a){
361 xbt_dynar_free(&(a->propositional_symbols));
362 xbt_dynar_free(&(a->transitions));
363 xbt_dynar_free(&(a->states));
364 xbt_state_free(a->current_state);