1 /* automaton - representation of büchi automaton */
3 /* Copyright (c) 2011-2020. 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 */
10 #include <xbt/sysdep.h>
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 = xbt_new0(struct xbt_automaton, 1);
25 automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
26 automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
27 automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
31 xbt_automaton_state_t xbt_automaton_state_new(const_xbt_automaton_t a, int type, const char* id)
33 xbt_automaton_state_t state = xbt_new0(struct xbt_automaton_state, 1);
35 state->id = xbt_strdup(id);
36 state->in = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
37 state->out = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
38 xbt_dynar_push(a->states, &state);
42 xbt_automaton_transition_t xbt_automaton_transition_new(const_xbt_automaton_t a, xbt_automaton_state_t src,
43 xbt_automaton_state_t dst, xbt_automaton_exp_label_t label)
45 xbt_automaton_transition_t 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_or(xbt_automaton_exp_label_t left,
60 xbt_automaton_exp_label_t right)
62 xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
64 label->u.or_and.left_exp = left;
65 label->u.or_and.right_exp = right;
69 xbt_automaton_exp_label_t xbt_automaton_exp_label_new_and(xbt_automaton_exp_label_t left,
70 xbt_automaton_exp_label_t right)
72 xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
73 label->type = AUT_AND;
74 label->u.or_and.left_exp = left;
75 label->u.or_and.right_exp = right;
79 xbt_automaton_exp_label_t xbt_automaton_exp_label_new_not(xbt_automaton_exp_label_t exp_not)
81 xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
82 label->type = AUT_NOT;
83 label->u.exp_not = exp_not;
87 xbt_automaton_exp_label_t xbt_automaton_exp_label_new_predicat(const char* p)
89 xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
90 label->type = AUT_PREDICAT;
91 label->u.predicat = xbt_strdup(p);
95 xbt_automaton_exp_label_t xbt_automaton_exp_label_new_one(void)
97 xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
98 label->type = AUT_ONE;
102 xbt_dynar_t xbt_automaton_get_states(const_xbt_automaton_t a)
107 xbt_dynar_t xbt_automaton_get_transitions(const_xbt_automaton_t a)
109 return a->transitions;
112 xbt_automaton_transition_t xbt_automaton_get_transition(XBT_ATTRIB_UNUSED const_xbt_automaton_t a,
113 const_xbt_automaton_state_t src,
114 const_xbt_automaton_state_t dst)
116 xbt_automaton_transition_t transition;
118 xbt_dynar_foreach(src->out, cursor, transition){
119 if((transition->src == src) && (transition->dst == dst))
125 xbt_automaton_state_t xbt_automaton_transition_get_source(const_xbt_automaton_transition_t t)
130 xbt_automaton_state_t xbt_automaton_transition_get_destination(const_xbt_automaton_transition_t t)
135 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
137 xbt_dynar_push(src->out,&t);
140 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
142 xbt_dynar_push(dst->in,&t);
145 xbt_dynar_t xbt_automaton_state_get_out_transitions(const_xbt_automaton_state_t s)
150 xbt_dynar_t xbt_automaton_state_get_in_transitions(const_xbt_automaton_state_t s)
155 xbt_automaton_state_t xbt_automaton_state_exists(const_xbt_automaton_t a, const char* id)
157 xbt_automaton_state_t state = NULL;
158 unsigned int cursor = 0;
159 xbt_dynar_foreach(a->states, cursor, state){
160 if(strcmp(state->id, id)==0)
166 void xbt_automaton_display(const_xbt_automaton_t a)
169 xbt_automaton_state_t state = NULL;
171 printf("\n\nCurrent state: %s\n", a->current_state->id);
173 printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
175 xbt_dynar_foreach(a->states, cursor, state)
176 printf("ID: %s, type: %d\n", state->id, state->type);
178 xbt_automaton_transition_t transition;
179 printf("\nTransitions: %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(const_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);
198 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
200 xbt_automaton_exp_label_display(label->u.or_and.right_exp);
204 xbt_automaton_exp_label_display(label->u.exp_not);
207 printf("%s", label->u.predicat);
218 xbt_automaton_state_t xbt_automaton_get_current_state(const_xbt_automaton_t a)
220 return a->current_state;
223 static int call_simple_function(int function(void) )
228 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(const_xbt_automaton_t a, const char* id,
231 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
232 prop_symb->pred = xbt_strdup(id);
233 prop_symb->callback = ((int (*)(void *))&call_simple_function);
234 prop_symb->data = (void*)&fct;
235 prop_symb->free_function = NULL;
236 xbt_dynar_push(a->propositional_symbols, &prop_symb);
240 XBT_PUBLIC xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new_pointer(const_xbt_automaton_t a,
244 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
245 prop_symb->pred = xbt_strdup(id);
246 prop_symb->callback = NULL;
247 prop_symb->data = value;
248 prop_symb->free_function = NULL;
249 xbt_dynar_push(a->propositional_symbols, &prop_symb);
253 XBT_PUBLIC xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new_callback(
254 const_xbt_automaton_t a, const char* id, xbt_automaton_propositional_symbol_callback_type callback, void* data,
255 xbt_automaton_propositional_symbol_free_function_type free_function)
257 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
258 prop_symb->pred = xbt_strdup(id);
259 prop_symb->callback = callback;
260 prop_symb->data = data;
261 prop_symb->free_function = free_function;
262 xbt_dynar_push(a->propositional_symbols, &prop_symb);
266 XBT_PUBLIC int xbt_automaton_propositional_symbol_evaluate(const_xbt_automaton_propositional_symbol_t symbol)
268 if (symbol->callback)
269 return (symbol->callback)(symbol->data);
271 return *(int*) symbol->data;
274 XBT_PUBLIC xbt_automaton_propositional_symbol_callback_type
275 xbt_automaton_propositional_symbol_get_callback(const_xbt_automaton_propositional_symbol_t symbol)
277 return symbol->callback;
280 XBT_PUBLIC void* xbt_automaton_propositional_symbol_get_data(const_xbt_automaton_propositional_symbol_t symbol)
285 XBT_PUBLIC const char* xbt_automaton_propositional_symbol_get_name(const_xbt_automaton_propositional_symbol_t symbol)
290 int xbt_automaton_state_compare(const_xbt_automaton_state_t s1, const_xbt_automaton_state_t s2)
292 /* single id for each state, id and type sufficient for comparison*/
293 return (strcmp(s1->id, s2->id) != 0) || (s1->type != s2->type);
296 int xbt_automaton_transition_compare(const_xbt_automaton_transition_t t1, const_xbt_automaton_transition_t t2)
298 return xbt_automaton_state_compare(t1->src, t2->src) || xbt_automaton_state_compare(t1->dst, t2->dst) ||
299 xbt_automaton_exp_label_compare(t1->label, t2->label);
302 int xbt_automaton_exp_label_compare(const_xbt_automaton_exp_label_t l1, const_xbt_automaton_exp_label_t l2)
304 if(l1->type != l2->type)
311 res = xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp) ||
312 xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
315 res = xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
318 res = strcmp(l1->u.predicat, l2->u.predicat) != 0;
330 int xbt_automaton_propositional_symbols_compare_value(const_xbt_dynar_t s1, const_xbt_dynar_t s2)
332 unsigned long nb_elem = xbt_dynar_length(s1);
334 for (unsigned long cursor = 0; cursor < nb_elem; cursor++) {
335 const int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
336 const int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
344 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
345 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
346 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
348 void xbt_automaton_state_free(xbt_automaton_state_t s){
351 xbt_dynar_free(&(s->in));
352 xbt_dynar_free(&(s->out));
357 void xbt_automaton_state_free_voidp(void *s){
358 xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
361 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
363 xbt_automaton_exp_label_free(t->label);
369 void xbt_automaton_transition_free_voidp(void *t){
370 xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
373 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
378 xbt_automaton_exp_label_free(e->u.or_and.left_exp);
379 xbt_automaton_exp_label_free(e->u.or_and.right_exp);
382 xbt_automaton_exp_label_free(e->u.exp_not);
385 xbt_free(e->u.predicat);
395 void xbt_automaton_exp_label_free_voidp(void *e){
396 xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
399 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
407 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
408 xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
409 if (symbol->free_function)
410 symbol->free_function(symbol->data);
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);