1 /* automaton - representation of büchi automaton */
3 /* Copyright (c) 2011-2017. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "xbt/automaton.h"
9 #include <stdio.h> /* printf */
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_automaton, xbt, "Automaton");
14 struct xbt_automaton_propositional_symbol{
16 /** Callback used to evaluate the value of the symbol */
17 int (*callback)(void*);
18 /** Additional data for the callback.
19 Alternatively it can be used as a pointer to the data. */
21 /** Optional callback used to free the data field */
22 void (*free_function)(void*);
25 xbt_automaton_t xbt_automaton_new(void){
26 xbt_automaton_t automaton = xbt_new0(struct xbt_automaton, 1);
27 automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
28 automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
29 automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
33 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
34 xbt_automaton_state_t 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 = xbt_new0(struct xbt_automaton_transition, 1);
46 xbt_dynar_push(src->out, &transition);
47 transition->src = src;
50 xbt_dynar_push(dst->in, &transition);
51 transition->dst = dst;
53 transition->label = label;
54 xbt_dynar_push(a->transitions, &transition);
58 xbt_automaton_exp_label_t xbt_automaton_exp_label_new(int type, ...){
59 xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
61 xbt_automaton_exp_label_t left;
62 xbt_automaton_exp_label_t right;
63 xbt_automaton_exp_label_t exp_not;
69 left = va_arg(ap, xbt_automaton_exp_label_t);
70 right = va_arg(ap, xbt_automaton_exp_label_t);
71 label->u.or_and.left_exp = left;
72 label->u.or_and.right_exp = right;
75 left = va_arg(ap, xbt_automaton_exp_label_t);
76 right = va_arg(ap, xbt_automaton_exp_label_t);
77 label->u.or_and.left_exp = left;
78 label->u.or_and.right_exp = right;
81 exp_not = va_arg(ap, xbt_automaton_exp_label_t);
82 label->u.exp_not = exp_not;
85 p = va_arg(ap, char*);
86 label->u.predicat = xbt_strdup(p);
91 XBT_DEBUG("Invalid type: %d", type);
100 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
104 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
105 return a->transitions;
108 xbt_automaton_transition_t xbt_automaton_get_transition(XBT_ATTRIB_UNUSED xbt_automaton_t a, xbt_automaton_state_t src,
109 xbt_automaton_state_t dst)
111 xbt_automaton_transition_t transition;
113 xbt_dynar_foreach(src->out, cursor, transition){
114 if((transition->src == src) && (transition->dst == dst))
120 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
124 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
128 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
130 xbt_dynar_push(src->out,&t);
133 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
135 xbt_dynar_push(dst->in,&t);
138 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
142 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
146 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
147 xbt_automaton_state_t state = NULL;
148 unsigned int cursor = 0;
149 xbt_dynar_foreach(a->states, cursor, state){
150 if(strcmp(state->id, id)==0)
156 void xbt_automaton_display(xbt_automaton_t a){
158 xbt_automaton_state_t state = NULL;
160 printf("\n\nCurrent state: %s\n", a->current_state->id);
162 printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
164 xbt_dynar_foreach(a->states, cursor, state)
165 printf("ID: %s, type: %d\n", state->id, state->type);
167 xbt_automaton_transition_t transition;
168 printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
170 xbt_dynar_foreach(a->transitions, cursor, transition){
172 xbt_automaton_exp_label_display(transition->label);
173 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
177 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
181 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
183 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
186 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
188 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
192 xbt_automaton_exp_label_display(label->u.exp_not);
195 printf("%s", label->u.predicat);
206 xbt_automaton_state_t xbt_automaton_get_current_state(xbt_automaton_t a){
207 return a->current_state;
210 static int call_simple_function(void* function)
212 return ((int (*)(void)) function)();
215 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
216 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
217 prop_symb->pred = xbt_strdup(id);
218 prop_symb->callback = &call_simple_function;
219 prop_symb->data = fct;
220 prop_symb->free_function = NULL;
221 xbt_dynar_push(a->propositional_symbols, &prop_symb);
225 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a, const char* id, int* value)
227 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
228 prop_symb->pred = xbt_strdup(id);
229 prop_symb->callback = NULL;
230 prop_symb->data = value;
231 prop_symb->free_function = NULL;
232 xbt_dynar_push(a->propositional_symbols, &prop_symb);
236 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_callback(
237 xbt_automaton_t a, const char* id,
238 xbt_automaton_propositional_symbol_callback_type callback,
239 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
241 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
242 prop_symb->pred = xbt_strdup(id);
243 prop_symb->callback = callback;
244 prop_symb->data = data;
245 prop_symb->free_function = free_function;
246 xbt_dynar_push(a->propositional_symbols, &prop_symb);
250 XBT_PUBLIC(int) xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
252 if (symbol->callback)
253 return (symbol->callback)(symbol->data);
255 return *(int*) symbol->data;
258 XBT_PUBLIC(xbt_automaton_propositional_symbol_callback_type) xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
260 return symbol->callback;
263 XBT_PUBLIC(void*) xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
268 XBT_PUBLIC(const char*) xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
273 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
275 /* single id for each state, id and type sufficient for comparison*/
277 if(strcmp(s1->id, s2->id))
280 if(s1->type != s2->type)
287 int xbt_automaton_transition_compare(const void *t1, const void *t2){
289 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
292 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
295 if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
302 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
304 if(l1->type != l2->type)
310 if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
313 return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
316 return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
319 return (strcmp(l1->u.predicat, l2->u.predicat));
330 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
331 unsigned int nb_elem = xbt_dynar_length(s1);
333 for (unsigned int cursor = 0; cursor < nb_elem; cursor++) {
334 int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
335 int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
343 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
344 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
345 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
347 void xbt_automaton_state_free(xbt_automaton_state_t s){
350 xbt_dynar_free(&(s->in));
351 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_automaton_propositional_symbol_free(symbol);
413 void xbt_automaton_free(xbt_automaton_t a){
415 xbt_dynar_free(&(a->propositional_symbols));
416 xbt_dynar_free(&(a->transitions));
417 xbt_dynar_free(&(a->states));
418 xbt_automaton_state_free(a->current_state);