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 */
11 struct xbt_automaton_propositional_symbol{
13 /** Callback used to evaluate the value of the symbol */
14 int (*callback)(void*);
15 /** Additional data for the callback.
16 Alternatively it can be used as a pointer to the data. */
18 /** Optional callback used to free the data field */
19 void (*free_function)(void*);
22 xbt_automaton_t xbt_automaton_new(void){
23 xbt_automaton_t automaton = xbt_new0(struct xbt_automaton, 1);
24 automaton->states = xbt_dynar_new(sizeof(xbt_automaton_state_t), xbt_automaton_state_free_voidp);
25 automaton->transitions = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
26 automaton->propositional_symbols = xbt_dynar_new(sizeof(xbt_automaton_propositional_symbol_t), xbt_automaton_propositional_symbol_free_voidp);
30 xbt_automaton_state_t xbt_automaton_state_new(xbt_automaton_t a, int type, char* id){
31 xbt_automaton_state_t state = xbt_new0(struct xbt_automaton_state, 1);
33 state->id = xbt_strdup(id);
34 state->in = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
35 state->out = xbt_dynar_new(sizeof(xbt_automaton_transition_t), xbt_automaton_transition_free_voidp);
36 xbt_dynar_push(a->states, &state);
40 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){
41 xbt_automaton_transition_t transition = xbt_new0(struct xbt_automaton_transition, 1);
43 xbt_dynar_push(src->out, &transition);
44 transition->src = src;
47 xbt_dynar_push(dst->in, &transition);
48 transition->dst = dst;
50 transition->label = label;
51 xbt_dynar_push(a->transitions, &transition);
55 xbt_automaton_exp_label_t xbt_automaton_exp_label_new(int type, ...){
56 xbt_automaton_exp_label_t label = xbt_new0(struct xbt_automaton_exp_label, 1);
58 xbt_automaton_exp_label_t left;
59 xbt_automaton_exp_label_t right;
60 xbt_automaton_exp_label_t exp_not;
66 left = va_arg(ap, xbt_automaton_exp_label_t);
67 right = va_arg(ap, xbt_automaton_exp_label_t);
68 label->u.or_and.left_exp = left;
69 label->u.or_and.right_exp = right;
72 left = va_arg(ap, xbt_automaton_exp_label_t);
73 right = va_arg(ap, xbt_automaton_exp_label_t);
74 label->u.or_and.left_exp = left;
75 label->u.or_and.right_exp = right;
78 exp_not = va_arg(ap, xbt_automaton_exp_label_t);
79 label->u.exp_not = exp_not;
82 p = va_arg(ap, char*);
83 label->u.predicat = xbt_strdup(p);
90 xbt_dynar_t xbt_automaton_get_states(xbt_automaton_t a){
94 xbt_dynar_t xbt_automaton_get_transitions(xbt_automaton_t a){
95 return a->transitions;
98 xbt_automaton_transition_t xbt_automaton_get_transition(xbt_automaton_t XBT_ATTRIB_UNUSED a, xbt_automaton_state_t src,
99 xbt_automaton_state_t dst)
101 xbt_automaton_transition_t transition;
103 xbt_dynar_foreach(src->out, cursor, transition){
104 if((transition->src == src) && (transition->dst == dst))
110 xbt_automaton_state_t xbt_automaton_transition_get_source(xbt_automaton_transition_t t){
114 xbt_automaton_state_t xbt_automaton_transition_get_destination(xbt_automaton_transition_t t){
118 void xbt_automaton_transition_set_source(xbt_automaton_transition_t t, xbt_automaton_state_t src){
120 xbt_dynar_push(src->out,&t);
123 void xbt_automaton_transition_set_destination(xbt_automaton_transition_t t, xbt_automaton_state_t dst){
125 xbt_dynar_push(dst->in,&t);
128 xbt_dynar_t xbt_automaton_state_get_out_transitions(xbt_automaton_state_t s){
132 xbt_dynar_t xbt_automaton_state_get_in_transitions(xbt_automaton_state_t s){
136 xbt_automaton_state_t xbt_automaton_state_exists(xbt_automaton_t a, char *id){
137 xbt_automaton_state_t state = NULL;
138 unsigned int cursor = 0;
139 xbt_dynar_foreach(a->states, cursor, state){
140 if(strcmp(state->id, id)==0)
146 void xbt_automaton_display(xbt_automaton_t a){
148 xbt_automaton_state_t state = NULL;
150 printf("\n\nCurrent state: %s\n", a->current_state->id);
152 printf("\nStates' List: %lu\n\n", xbt_dynar_length(a->states));
154 xbt_dynar_foreach(a->states, cursor, state)
155 printf("ID: %s, type: %d\n", state->id, state->type);
157 xbt_automaton_transition_t transition;
158 printf("\nTransitions: %lu\n\n", xbt_dynar_length(a->transitions));
160 xbt_dynar_foreach(a->transitions, cursor, transition){
162 xbt_automaton_exp_label_display(transition->label);
163 printf(", %s -> %s\n", transition->src->id, transition->dst->id);
167 void xbt_automaton_exp_label_display(xbt_automaton_exp_label_t label){
171 xbt_automaton_exp_label_display(label->u.or_and.left_exp);
173 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);
182 xbt_automaton_exp_label_display(label->u.exp_not);
185 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 static int call_simple_function(void* function)
202 return ((int (*)(void)) function)();
205 xbt_automaton_propositional_symbol_t xbt_automaton_propositional_symbol_new(xbt_automaton_t a, const char* id, int(*fct)(void)){
206 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
207 prop_symb->pred = xbt_strdup(id);
208 prop_symb->callback = &call_simple_function;
209 prop_symb->data = fct;
210 prop_symb->free_function = NULL;
211 xbt_dynar_push(a->propositional_symbols, &prop_symb);
215 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_pointer(xbt_automaton_t a, const char* id, int* value)
217 xbt_automaton_propositional_symbol_t prop_symb = xbt_new0(struct xbt_automaton_propositional_symbol, 1);
218 prop_symb->pred = xbt_strdup(id);
219 prop_symb->callback = NULL;
220 prop_symb->data = value;
221 prop_symb->free_function = NULL;
222 xbt_dynar_push(a->propositional_symbols, &prop_symb);
226 XBT_PUBLIC(xbt_automaton_propositional_symbol_t) xbt_automaton_propositional_symbol_new_callback(
227 xbt_automaton_t a, const char* id,
228 xbt_automaton_propositional_symbol_callback_type callback,
229 void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
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 = callback;
234 prop_symb->data = data;
235 prop_symb->free_function = free_function;
236 xbt_dynar_push(a->propositional_symbols, &prop_symb);
240 XBT_PUBLIC(int) xbt_automaton_propositional_symbol_evaluate(xbt_automaton_propositional_symbol_t symbol)
242 if (symbol->callback)
243 return (symbol->callback)(symbol->data);
245 return *(int*) symbol->data;
248 XBT_PUBLIC(xbt_automaton_propositional_symbol_callback_type) xbt_automaton_propositional_symbol_get_callback(xbt_automaton_propositional_symbol_t symbol)
250 return symbol->callback;
253 XBT_PUBLIC(void*) xbt_automaton_propositional_symbol_get_data(xbt_automaton_propositional_symbol_t symbol)
258 XBT_PUBLIC(const char*) xbt_automaton_propositional_symbol_get_name(xbt_automaton_propositional_symbol_t symbol)
263 int xbt_automaton_state_compare(xbt_automaton_state_t s1, xbt_automaton_state_t s2){
265 /* single id for each state, id and type sufficient for comparison*/
267 if(strcmp(s1->id, s2->id))
270 if(s1->type != s2->type)
277 int xbt_automaton_transition_compare(const void *t1, const void *t2){
279 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->src, ((xbt_automaton_transition_t)t2)->src))
282 if(xbt_automaton_state_compare(((xbt_automaton_transition_t)t1)->dst, ((xbt_automaton_transition_t)t2)->dst))
285 if(xbt_automaton_exp_label_compare(((xbt_automaton_transition_t)t1)->label,((xbt_automaton_transition_t)t2)->label))
292 int xbt_automaton_exp_label_compare(xbt_automaton_exp_label_t l1, xbt_automaton_exp_label_t l2){
294 if(l1->type != l2->type)
300 if(xbt_automaton_exp_label_compare(l1->u.or_and.left_exp, l2->u.or_and.left_exp))
303 return xbt_automaton_exp_label_compare(l1->u.or_and.right_exp, l2->u.or_and.right_exp);
306 return xbt_automaton_exp_label_compare(l1->u.exp_not, l2->u.exp_not);
309 return (strcmp(l1->u.predicat, l2->u.predicat));
320 int xbt_automaton_propositional_symbols_compare_value(xbt_dynar_t s1, xbt_dynar_t s2){
321 unsigned int nb_elem = xbt_dynar_length(s1);
323 for (unsigned int cursor = 0; cursor < nb_elem; cursor++) {
324 int* iptr1 = xbt_dynar_get_ptr(s1, cursor);
325 int* iptr2 = xbt_dynar_get_ptr(s2, cursor);
333 static void xbt_automaton_transition_free(xbt_automaton_transition_t t);
334 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e);
335 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps);
337 void xbt_automaton_state_free(xbt_automaton_state_t s){
340 xbt_dynar_free(&(s->in));
341 xbt_dynar_free(&(s->out));
346 void xbt_automaton_state_free_voidp(void *s){
347 xbt_automaton_state_free((xbt_automaton_state_t) * (void **) s);
350 static void xbt_automaton_transition_free(xbt_automaton_transition_t t){
352 xbt_automaton_exp_label_free(t->label);
358 void xbt_automaton_transition_free_voidp(void *t){
359 xbt_automaton_transition_free((xbt_automaton_transition_t) * (void **) t);
362 static void xbt_automaton_exp_label_free(xbt_automaton_exp_label_t e){
367 xbt_automaton_exp_label_free(e->u.or_and.left_exp);
368 xbt_automaton_exp_label_free(e->u.or_and.right_exp);
371 xbt_automaton_exp_label_free(e->u.exp_not);
374 xbt_free(e->u.predicat);
384 void xbt_automaton_exp_label_free_voidp(void *e){
385 xbt_automaton_exp_label_free((xbt_automaton_exp_label_t) * (void **) e);
388 static void xbt_automaton_propositional_symbol_free(xbt_automaton_propositional_symbol_t ps){
396 void xbt_automaton_propositional_symbol_free_voidp(void *ps){
397 xbt_automaton_propositional_symbol_t symbol = (xbt_automaton_propositional_symbol_t) * (void **) ps;
398 if (symbol->free_function)
399 symbol->free_function(symbol->data);
400 xbt_automaton_propositional_symbol_free(symbol);
403 void xbt_automaton_free(xbt_automaton_t a){
405 xbt_dynar_free(&(a->propositional_symbols));
406 xbt_dynar_free(&(a->transitions));
407 xbt_dynar_free(&(a->states));
408 xbt_automaton_state_free(a->current_state);