/* * create_automatons_from_xml * * Once this functions is complete all the structure will be in place in the * automaton_set to fill in the names of the automatons and transitions. * It goes through the file twice. The first time counting the number of * automatons and then allocating the memory required for that number. * The second time it creates each of the automatons and puts them into the * automaton set. * * They get entered into the array in the order that they appear in the file. * * Parameters: xml_file - A reference to the structure that contains the entire * xml. * automaton_set - The collection of automatons to be filled in. * state_callback - Callback function to generate the states for * each of the automatons. * event_callback - Callback function to generate the events for * each of the automatons. */ void create_automatons_from_xml(ezxml_t *xml_file, AUTOMATON_SET *automaton_set, int (*state_callback)(AUTOMATON_STATE ***,int), int (*event_callback)(AUTOMATON_EVENT ***)) { /* * Local Variables. */ int ii; int num_automatons; ezxml_t xml_automaton; /* * Count the number of automaton objects in this file. We need to know this * so that we allocate the memory for the automaton array. */ num_automatons = 0; for (xml_automaton = ezxml_child(*xml_file, "automaton"); xml_automaton; xml_automaton = xml_automaton->next) { num_automatons++; } automaton_set->num_automatons = num_automatons; /* * Attempt to allocate the memory for the automaton array. */ automaton_set->automaton_array = (AUTOMATON **) DT_MALLOC(sizeof(AUTOMATON *) * num_automatons); /* * Create each of the automatons individually. */ ii = 0; for (xml_automaton = ezxml_child(*xml_file, "automaton"); xml_automaton; xml_automaton = xml_automaton->next) { /* * This allocated the memory and sets up the state, event tables. */ automaton_set->automaton_array[ii] = create_automaton(state_callback, event_callback); ii++; } }
int main () { Automaton *a = create_automaton(); int e0 = add_state(a); int e1 = add_state(a); int e2 = add_state(a); set_initial(a, e1); set_final(a, e2); add_arc(a, e0, e1, 1); add_arc(a, e1, e1, 2); add_arc(a, e1, e2, 3); print_automaton(a); return 0; }