1 /* automaton - representation of büchi automaton */
3 /* Copyright (c) 2011-2014. 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"
10 #include <stdio.h> /* printf */
12 xbt_automaton_t xbt_automaton_new(){
13 xbt_automaton_t automaton = NULL;
14 automaton = xbt_new0(struct xbt_automaton, 1);
15 automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
16 automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
17 automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
21 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
22 xbt_automaton_state_t state = NULL;
23 state = xbt_new0(struct xbt_automaton_state, 1);
25 state->id = strdup(id);
26 state->in = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
27 state->out = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
28 xbt_dynar_push(a->states, &state);
32 xbt_automaton_transition_t xbt_automaton_transition_new(xbt_automaton_t a, xbt_automaton_state_t src, xbt_automaton_state_t dst, xbt_automaton_exp_label_t label){
33 xbt_automaton_transition_t transition = NULL;
34 transition = xbt_new0(struct xbt_automaton_transition, 1);
36 xbt_dynar_push(src->out, &transition);
37 transition->src = src;
40 xbt_dynar_push(dst->in, &transition);
41 transition->dst = dst;
43 transition->label = label;
44 xbt_dynar_push(a->transitions, &transition);
48 xbt_automaton_exp_label_t xbt_automaton_exp_label_new(int type, ...){
49 xbt_automaton_exp_label_t label = NULL;
50 label = xbt_new0(struct xbt_automaton_exp_label, 1);
57 xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
58 xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
59 label->u.or_and.left_exp = left;
60 label->u.or_and.right_exp = right;
64 xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
65 xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
66 label->u.or_and.left_exp = left;
67 label->u.or_and.right_exp = right;
71 xbt_automaton_exp_label_t exp_not = va_arg(ap, xbt_automaton_exp_label_t);
72 label->u.exp_not = exp_not;
76 char* p = va_arg(ap, char*);
77 label->u.predicat = strdup(p);
86 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
90 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
91 return a->transitions;
94 xbt_automaton_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_automaton_state_t src, xbt_automaton_state_t dst){
95 xbt_automaton_transition_t transition;
97 xbt_dynar_foreach(src->out, cursor, transition){
98 if((transition->src == src) && (transition->dst == dst))
104 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
108 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
112 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
114 xbt_dynar_push(src->out,&t);
117 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
119 xbt_dynar_push(dst->in,&t);
122 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
126 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
130 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
131 xbt_automaton_state_t state = NULL;
132 unsigned int cursor = 0;
133 xbt_dynar_foreach(a->states, cursor, state){
134 if(strcmp(state->id, id)==0)
140 void xbt_automaton_display(xbt_automaton_t a){
141 unsigned int cursor = 0;
142 xbt_automaton_state_t state = NULL;
144 printf("\n\nEtat courant : %s\n", a->current_state->id);
146 printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
149 xbt_dynar_foreach(a->states, cursor, state){
150 printf("ID : %s, type : %d\n", state->id, state->type);
154 xbt_automaton_transition_t transition = NULL;
155 printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
157 xbt_dynar_foreach(a->transitions, cursor, transition){
159 xbt_automaton_exp_label_display(transition->label);
160 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
164 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
169 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
171 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
176 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
178 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
183 xbt_automaton_exp_label_display(label->u.exp_not);
187 printf("(%s)",label->u.predicat);
196 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
197 return a->current_state;
200 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, void* fct){
201 xbt_automaton_propositional_symbol_t prop_symb = NULL;
202 prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
203 prop_symb->pred = strdup(id);
204 prop_symb->function = fct;
205 xbt_dynar_push(a->propositional_symbols, &prop_symb);
209 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
211 /* single id for each state, id and type sufficient for comparison*/
213 if(strcmp(s1->id, s2->id))
216 if(s1->type != s2->type)
223 int xbt_automaton_transition_compare(const void *t1, const void *t2){
225 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
228 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
231 if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
238 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
240 if(l1->type != l2->type)
247 if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
250 return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
254 return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
258 return (strcmp(l1->u.predicat, l2->u.predicat));
274 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
278 unsigned int nb_elem = xbt_dynar_length(s1);
280 for(cursor=0;cursor<nb_elem;cursor++){
281 iptr1 = xbt_dynar_get_ptr(s1, cursor);
282 iptr2 = xbt_dynar_get_ptr(s2, cursor);
290 /************ Free functions ****************/
292 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
293 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
294 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
296 void xbt_automaton_state_free(xbt_automaton_state_t s){
299 xbt_dynar_free(&(s->in));
300 xbt_dynar_free(&(s->out));
306 void xbt_automaton_state_free_voidp(void *s){
307 xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
310 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
312 xbt_automaton_exp_label_free(t->label);
318 void xbt_automaton_transition_free_voidp(void *t){
319 xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
322 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
327 xbt_automaton_exp_label_free(e->u.or_and.left_exp);
328 xbt_automaton_exp_label_free(e->u.or_and.right_exp);
331 xbt_automaton_exp_label_free(e->u.exp_not);
334 xbt_free(e->u.predicat);
344 void xbt_automaton_exp_label_free_voidp(void *e){
345 xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
348 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
356 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
357 xbt_automaton_propositional_symbol_free((xbt_automaton_propositional_symbol_t) * (void **) ps);
360 void xbt_automaton_free(xbt_automaton_t a){
362 xbt_dynar_free(&(a->propositional_symbols));
363 xbt_dynar_free(&(a->transitions));
364 xbt_dynar_free(&(a->states));
365 xbt_automaton_state_free(a->current_state);