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 struct xbt_automaton_propositional_symbol{
14 /** Callback used to evaluate the value of the symbol */
15 int (*function)(void*);
16 /** Additional data for the callback.
17 Alternatively it can be used as a pointer to the data. */
19 //** Optional callback used to free the data field */
20 void (*free_function)(void*);
23 int xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
26 return (symbol->function)(symbol->data);
28 return *(int*) symbol->data;
31 char* xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
36 xbt_automaton_t xbt_automaton_new(){
37 xbt_automaton_t automaton = NULL;
38 automaton = xbt_new0(struct xbt_automaton, 1);
39 automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
40 automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
41 automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
45 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
46 xbt_automaton_state_t state = NULL;
47 state = xbt_new0(struct xbt_automaton_state, 1);
49 state->id = strdup(id);
50 state->in = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
51 state->out = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
52 xbt_dynar_push(a->states, &state);
56 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){
57 xbt_automaton_transition_t transition = NULL;
58 transition = xbt_new0(struct xbt_automaton_transition, 1);
60 xbt_dynar_push(src->out, &transition);
61 transition->src = src;
64 xbt_dynar_push(dst->in, &transition);
65 transition->dst = dst;
67 transition->label = label;
68 xbt_dynar_push(a->transitions, &transition);
72 xbt_automaton_exp_label_t xbt_automaton_exp_label_new(int type, ...){
73 xbt_automaton_exp_label_t label = NULL;
74 label = xbt_new0(struct xbt_automaton_exp_label, 1);
81 xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
82 xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
83 label->u.or_and.left_exp = left;
84 label->u.or_and.right_exp = right;
88 xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
89 xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
90 label->u.or_and.left_exp = left;
91 label->u.or_and.right_exp = right;
95 xbt_automaton_exp_label_t exp_not = va_arg(ap, xbt_automaton_exp_label_t);
96 label->u.exp_not = exp_not;
100 char* p = va_arg(ap, char*);
101 label->u.predicat = strdup(p);
110 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
114 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
115 return a->transitions;
118 xbt_automaton_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_automaton_state_t src, xbt_automaton_state_t dst){
119 xbt_automaton_transition_t transition;
121 xbt_dynar_foreach(src->out, cursor, transition){
122 if((transition->src == src) && (transition->dst == dst))
128 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
132 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
136 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
138 xbt_dynar_push(src->out,&t);
141 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
143 xbt_dynar_push(dst->in,&t);
146 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
150 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
154 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
155 xbt_automaton_state_t state = NULL;
156 unsigned int cursor = 0;
157 xbt_dynar_foreach(a->states, cursor, state){
158 if(strcmp(state->id, id)==0)
164 void xbt_automaton_display(xbt_automaton_t a){
165 unsigned int cursor = 0;
166 xbt_automaton_state_t state = NULL;
168 printf("\n\nEtat courant : %s\n", a->current_state->id);
170 printf("\nListe des états : %lu\n\n", xbt_dynar_length(a->states));
173 xbt_dynar_foreach(a->states, cursor, state){
174 printf("ID : %s, type : %d\n", state->id, state->type);
178 xbt_automaton_transition_t transition = NULL;
179 printf("\nListe des transitions : %lu\n\n", xbt_dynar_length(a->transitions));
181 xbt_dynar_foreach(a->transitions, cursor, transition){
183 xbt_automaton_exp_label_display(transition->label);
184 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
188 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
193 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
195 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
200 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
202 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
207 xbt_automaton_exp_label_display(label->u.exp_not);
211 printf("(%s)",label->u.predicat);
220 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
221 return a->current_state;
224 static int call_simple_function(void* function)
226 return ((int (*)(void)) function)();
229 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
230 xbt_automaton_propositional_symbol_t prop_symb = NULL;
231 prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
232 prop_symb->pred = strdup(id);
233 prop_symb->function = call_simple_function;
234 prop_symb->data = fct;
235 prop_symb->free_function = NULL;
236 xbt_dynar_push(a->propositional_symbols, &prop_symb);
240 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
242 /* single id for each state, id and type sufficient for comparison*/
244 if(strcmp(s1->id, s2->id))
247 if(s1->type != s2->type)
254 int xbt_automaton_transition_compare(const void *t1, const void *t2){
256 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
259 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
262 if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
269 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
271 if(l1->type != l2->type)
278 if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
281 return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
285 return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
289 return (strcmp(l1->u.predicat, l2->u.predicat));
305 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
309 unsigned int nb_elem = xbt_dynar_length(s1);
311 for(cursor=0;cursor<nb_elem;cursor++){
312 iptr1 = xbt_dynar_get_ptr(s1, cursor);
313 iptr2 = xbt_dynar_get_ptr(s2, cursor);
321 /************ Free functions ****************/
323 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
324 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
325 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
327 void xbt_automaton_state_free(xbt_automaton_state_t s){
330 xbt_dynar_free(&(s->in));
331 xbt_dynar_free(&(s->out));
337 void xbt_automaton_state_free_voidp(void *s){
338 xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
341 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
343 xbt_automaton_exp_label_free(t->label);
349 void xbt_automaton_transition_free_voidp(void *t){
350 xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
353 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
358 xbt_automaton_exp_label_free(e->u.or_and.left_exp);
359 xbt_automaton_exp_label_free(e->u.or_and.right_exp);
362 xbt_automaton_exp_label_free(e->u.exp_not);
365 xbt_free(e->u.predicat);
375 void xbt_automaton_exp_label_free_voidp(void *e){
376 xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
379 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
387 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
388 xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
389 if (symbol->free_function)
390 symbol->free_function(symbol->data);
391 xbt_free(symbol->pred);
392 xbt_automaton_propositional_symbol_free(symbol);
395 void xbt_automaton_free(xbt_automaton_t a){
397 xbt_dynar_free(&(a->propositional_symbols));
398 xbt_dynar_free(&(a->transitions));
399 xbt_dynar_free(&(a->states));
400 xbt_automaton_state_free(a->current_state);