1 /* automaton - representation of büchi automaton */
3 /* Copyright (c) 2011-2015. 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 (*callback)(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 xbt_automaton_t xbt_automaton_new(void){
24 xbt_automaton_t automaton = NULL;
25 automaton = xbt_new0(struct xbt_automaton, 1);
26 automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
27 automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
28 automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
32 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
33 xbt_automaton_state_t state = NULL;
34 state = xbt_new0(struct xbt_automaton_state, 1);
36 state->id = xbt_strdup(id);
37 state->in = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
38 state->out = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
39 xbt_dynar_push(a->states, &state);
43 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){
44 xbt_automaton_transition_t transition = NULL;
45 transition = xbt_new0(struct xbt_automaton_transition, 1);
47 xbt_dynar_push(src->out, &transition);
48 transition->src = src;
51 xbt_dynar_push(dst->in, &transition);
52 transition->dst = dst;
54 transition->label = label;
55 xbt_dynar_push(a->transitions, &transition);
59 xbt_automaton_exp_label_t xbt_automaton_exp_label_new(int type, ...){
60 xbt_automaton_exp_label_t label = NULL;
61 label = xbt_new0(struct xbt_automaton_exp_label, 1);
63 xbt_automaton_exp_label_t left;
64 xbt_automaton_exp_label_t right;
65 xbt_automaton_exp_label_t exp_not;
71 left = va_arg(ap, xbt_automaton_exp_label_t);
72 right = va_arg(ap, xbt_automaton_exp_label_t);
73 label->u.or_and.left_exp = left;
74 label->u.or_and.right_exp = right;
77 left = va_arg(ap, xbt_automaton_exp_label_t);
78 right = va_arg(ap, xbt_automaton_exp_label_t);
79 label->u.or_and.left_exp = left;
80 label->u.or_and.right_exp = right;
83 exp_not = va_arg(ap, xbt_automaton_exp_label_t);
84 label->u.exp_not = exp_not;
87 p = va_arg(ap, char*);
88 label->u.predicat = xbt_strdup(p);
95 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
99 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
100 return a->transitions;
103 xbt_automaton_transition_t xbt_automaton_get_transition(xbt_automaton_t a, xbt_automaton_state_t src, xbt_automaton_state_t dst){
104 xbt_automaton_transition_t transition;
106 xbt_dynar_foreach(src->out, cursor, transition){
107 if((transition->src == src) && (transition->dst == dst))
113 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
117 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
121 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
123 xbt_dynar_push(src->out,&t);
126 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
128 xbt_dynar_push(dst->in,&t);
131 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
135 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
139 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
140 xbt_automaton_state_t state = NULL;
141 unsigned int cursor = 0;
142 xbt_dynar_foreach(a->states, cursor, state){
143 if(strcmp(state->id, id)==0)
149 void xbt_automaton_display(xbt_automaton_t a){
151 xbt_automaton_state_t state = NULL;
153 printf("\n\nCurrent state: %s\n", a->current_state->id);
155 printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
157 xbt_dynar_foreach(a->states, cursor, state)
158 printf("ID: %s, type: %d\n", state->id, state->type);
160 xbt_automaton_transition_t transition;
161 printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
163 xbt_dynar_foreach(a->transitions, cursor, transition){
165 xbt_automaton_exp_label_display(transition->label);
166 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
170 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
174 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
176 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
181 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
183 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
188 xbt_automaton_exp_label_display(label->u.exp_not);
192 printf("(%s)",label->u.predicat);
200 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
201 return a->current_state;
204 static int call_simple_function(void* function)
206 return ((int (*)(void)) function)();
209 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
210 xbt_automaton_propositional_symbol_t prop_symb = NULL;
211 prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
212 prop_symb->pred = xbt_strdup(id);
213 prop_symb->callback = call_simple_function;
214 prop_symb->data = fct;
215 prop_symb->free_function = NULL;
216 xbt_dynar_push(a->propositional_symbols, &prop_symb);
220 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a, const char* id, int* value)
222 xbt_automaton_propositional_symbol_t prop_symb = NULL;
223 prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
224 prop_symb->pred = xbt_strdup(id);
225 prop_symb->callback = NULL;
226 prop_symb->data = value;
227 prop_symb->free_function = NULL;
228 xbt_dynar_push(a->propositional_symbols, &prop_symb);
232 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_callback(
233 xbt_automaton_t a, const char* id,
234 xbt_automaton_propositional_symbol_callback_type callback,
235 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
237 xbt_automaton_propositional_symbol_t prop_symb = NULL;
238 prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
239 prop_symb->pred = xbt_strdup(id);
240 prop_symb->callback = callback;
241 prop_symb->data = data;
242 prop_symb->free_function = free_function;
243 xbt_dynar_push(a->propositional_symbols, &prop_symb);
247 XBT_PUBLIC(int) xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
249 if (symbol->callback)
250 return (symbol->callback)(symbol->data);
252 return *(int*) symbol->data;
255 XBT_PUBLIC(xbt_automaton_propositional_symbol_callback_type) xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
257 return symbol->callback;
260 XBT_PUBLIC(void*) xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
265 XBT_PUBLIC(const char*) xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
270 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
272 /* single id for each state, id and type sufficient for comparison*/
274 if(strcmp(s1->id, s2->id))
277 if(s1->type != s2->type)
284 int xbt_automaton_transition_compare(const void *t1, const void *t2){
286 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
289 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
292 if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
299 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
301 if(l1->type != l2->type)
307 if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
310 return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
313 return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
316 return (strcmp(l1->u.predicat, l2->u.predicat));
327 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
330 unsigned int nb_elem = xbt_dynar_length(s1);
332 for(cursor=0;cursor<nb_elem;cursor++){
333 iptr1 = xbt_dynar_get_ptr(s1, cursor);
334 iptr2 = xbt_dynar_get_ptr(s2, cursor);
342 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
343 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
344 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
346 void xbt_automaton_state_free(xbt_automaton_state_t s){
349 xbt_dynar_free(&(s->in));
350 xbt_dynar_free(&(s->out));
356 void xbt_automaton_state_free_voidp(void *s){
357 xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
360 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
362 xbt_automaton_exp_label_free(t->label);
368 void xbt_automaton_transition_free_voidp(void *t){
369 xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
372 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
377 xbt_automaton_exp_label_free(e->u.or_and.left_exp);
378 xbt_automaton_exp_label_free(e->u.or_and.right_exp);
381 xbt_automaton_exp_label_free(e->u.exp_not);
384 xbt_free(e->u.predicat);
394 void xbt_automaton_exp_label_free_voidp(void *e){
395 xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
398 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
406 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
407 xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
408 if (symbol->free_function)
409 symbol->free_function(symbol->data);
410 xbt_free(symbol->pred);
411 xbt_automaton_propositional_symbol_free(symbol);
414 void xbt_automaton_free(xbt_automaton_t a){
416 xbt_dynar_free(&(a->propositional_symbols));
417 xbt_dynar_free(&(a->transitions));
418 xbt_dynar_free(&(a->states));
419 xbt_automaton_state_free(a->current_state);