struct fsm_read_handle *fsm_read_init(struct fsm *net) { struct fsm_read_handle *handle; struct fsm_state *fsm; int i, j, k, num_states, num_initials, num_finals, sno, *finals_head, *initials_head, *states_head; unsigned char *lookuptable; if (net == NULL) {return (NULL);} num_states = net->statecount; lookuptable = xxcalloc(num_states, sizeof(unsigned char)); num_initials = num_finals = 0; for (i=0, fsm=net->states; (fsm+i)->state_no != -1; i++) { sno = (fsm+i)->state_no; if ((fsm+i)->start_state) { if (!(*(lookuptable+sno) & 1)) { *(lookuptable+sno) |= 1; num_initials++; } } if ((fsm+i)->final_state) { if (!(*(lookuptable+sno) & 2)) { *(lookuptable+sno) |= 2; num_finals++; } } } finals_head = xxcalloc(num_finals+1,sizeof(int)); initials_head = xxcalloc(num_initials+1,sizeof(int)); states_head = xxcalloc(num_states+1,sizeof(int)); for (i=j=k=0; i < num_states; i++) { if (*(lookuptable+i) & 1) { *(initials_head+j) = i; j++; } if (*(lookuptable+i) & 2) { *(finals_head+k) = i; k++; } *(states_head+i) = i; } *(initials_head+j) = -1; *(finals_head+k) = -1; *(states_head+i) = -1; xxfree(lookuptable); handle = xxcalloc(1,sizeof(struct fsm_read_handle)); handle->finals_head = finals_head; handle->initials_head = initials_head; handle->states_head = states_head; handle->fsm_sigma_list = sigma_to_list(net->sigma); handle->sigma_list_size = sigma_max(net->sigma)+1; handle->arcs_head = fsm; return(handle); }
void sigma_cleanup (struct fsm *net, int force) { int i,j,first,maxsigma,*attested; struct fsm_state *fsm; struct sigma *sig, *sig_prev, *sign; if (force == 0) { if (sigma_find_number(IDENTITY, net->sigma) != -1) return; if (sigma_find_number(UNKNOWN, net->sigma) != -1) return; } maxsigma = sigma_max(net->sigma); if (maxsigma < 0) { return; } attested = xxmalloc(sizeof(int)*(maxsigma+1)); for (i=0; i<=maxsigma; i++) *(attested+i) = 0; fsm = net->states; for (i=0; (fsm+i)->state_no != -1; i++) { if ((fsm+i)->in >=0) *(attested+(fsm+i)->in) = 1; if ((fsm+i)->out >=0) *(attested+(fsm+i)->out) = 1; } for (i=3,j=3; i<=maxsigma;i++ ) { if (*(attested+i)) { *(attested+i) = j; j++; } } for (i=0; (fsm+i)->state_no != -1; i++) { if ((fsm+i)->in > 2) (fsm+i)->in = *(attested+(fsm+i)->in); if ((fsm+i)->out > 2) (fsm+i)->out = *(attested+(fsm+i)->out); } sig_prev = NULL; for (sig = net->sigma; sig != NULL && sig->number != -1; sig = sign) { first = 1; sign = sig->next; if (!*(attested+(sig->number))) { xxfree(sig->symbol); xxfree(sig); if (sig_prev != NULL) { sig_prev->next = sign; first = 0; } else { first = 0; net->sigma = sign; } } else { sig->number = sig->number >= 3 ? *(attested+(sig->number)) : sig->number; } if (first) sig_prev = sig; } xxfree(attested); return; }
int foma_net_print(struct fsm *net, gzFile outfile) { struct sigma *sigma; struct fsm_state *fsm; int i, maxsigma, laststate, *cm, extras; /* Header */ gzprintf(outfile, "%s","##foma-net 1.0##\n"); /* Properties */ gzprintf(outfile, "%s","##props##\n"); extras = (net->is_completed) | (net->arcs_sorted_in << 2) | (net->arcs_sorted_out << 4); gzprintf(outfile, "%i %i %i %i %i %lld %i %i %i %i %i %i %s\n", net->arity, net->arccount, net->statecount, net->linecount, net->finalcount, net->pathcount, net->is_deterministic, net->is_pruned, net->is_minimized, net->is_epsilon_free, net->is_loop_free, extras, net->name); /* Sigma */ gzprintf(outfile, "%s","##sigma##\n"); for (sigma = net->sigma; sigma != NULL && sigma->number != -1; sigma = sigma->next) { gzprintf(outfile, "%i %s\n",sigma->number, sigma->symbol); } /* State array */ laststate = -1; gzprintf(outfile, "%s","##states##\n"); for (fsm = net->states; fsm->state_no !=-1; fsm++) { if (fsm->state_no != laststate) { if (fsm->in != fsm->out) { gzprintf(outfile, "%i %i %i %i %i\n",fsm->state_no, fsm->in, fsm->out, fsm->target, fsm->final_state); } else { gzprintf(outfile, "%i %i %i %i\n",fsm->state_no, fsm->in, fsm->target, fsm->final_state); } } else { if (fsm->in != fsm->out) { gzprintf(outfile, "%i %i %i\n", fsm->in, fsm->out, fsm->target); } else { gzprintf(outfile, "%i %i\n", fsm->in, fsm->target); } } laststate = fsm->state_no; } /* Sentinel for states */ gzprintf(outfile, "-1 -1 -1 -1 -1\n"); /* Store confusion matrix */ if (net->medlookup != NULL && net->medlookup->confusion_matrix != NULL) { gzprintf(outfile, "%s","##cmatrix##\n"); cm = net->medlookup->confusion_matrix; maxsigma = sigma_max(net->sigma)+1; for (i=0; i < maxsigma*maxsigma; i++) { gzprintf(outfile, "%i\n", *(cm+i)); } } /* End */ gzprintf(outfile, "%s","##end##\n"); return(1); }
static void sigma_to_pairs(struct fsm *net) { int i, j, x, y, z, next_x = 0; struct fsm_state *fsm; fsm = net->states; epsilon_symbol = -1; maxsigma = sigma_max(net->sigma); maxsigma++; single_sigma_array = xxmalloc(2*maxsigma*maxsigma*sizeof(int)); double_sigma_array = xxmalloc(maxsigma*maxsigma*sizeof(int)); for (i=0; i < maxsigma; i++) { for (j=0; j< maxsigma; j++) { *(double_sigma_array+maxsigma*i+j) = -1; } } /* f(x) -> y,z sigma pair */ /* f(y,z) -> x simple entry */ /* if exists f(n) <-> EPSILON, EPSILON, save n */ /* symbol(x) x>=1 */ /* Forward mapping: */ /* *(double_sigma_array+maxsigma*in+out) */ /* Backmapping: */ /* *(single_sigma_array+(symbol*2) = in(symbol) */ /* *(single_sigma_array+(symbol*2+1) = out(symbol) */ /* Table for checking whether a state is final */ x = 0; net->arity = 1; for (i=0; (fsm+i)->state_no != -1; i++) { y = (fsm+i)->in; z = (fsm+i)->out; if ((y == -1) || (z == -1)) continue; if (y != z || y == UNKNOWN || z == UNKNOWN) net->arity = 2; if (*(double_sigma_array+maxsigma*y+z) == -1) { *(double_sigma_array+maxsigma*y+z) = x; *(single_sigma_array+next_x) = y; next_x++; *(single_sigma_array+next_x) = z; next_x++; if (y == EPSILON && z == EPSILON) { epsilon_symbol = x; } x++; } } num_symbols = x; }
struct fsm_sigma_list *sigma_to_list(struct sigma *sigma) { struct fsm_sigma_list *sl; struct sigma *s; sl = xxcalloc(sigma_max(sigma)+1,sizeof(struct fsm_sigma_list)); for (s = sigma; s != NULL && s->number != -1; s = s->next) { (sl+(s->number))->symbol = s->symbol; } return sl; }
void apply_create_sigarray(struct apply_handle *h, struct fsm *net) { struct sigma *sig; struct fsm_state *fsm; int i, maxsigma; fsm = net->states; maxsigma = sigma_max(net->sigma); // Default size created at init, resized later if necessary h->sigmatch_array = xxcalloc(1024,sizeof(struct sigmatch_array)); h->sigmatch_array_size = 1024; h->sigs = xxmalloc(sizeof(char **)*(maxsigma+1)); h->has_flags = 0; h->flag_list = NULL; /* Malloc first array of trie and store trie ptrs to free later */ h->sigma_trie = xxcalloc(256,sizeof(struct sigma_trie)); h->sigma_trie_arrays = xxmalloc(sizeof(struct sigma_trie_arrays)); h->sigma_trie_arrays->arr = h->sigma_trie; h->sigma_trie_arrays->next = NULL; for (i=0;i<256;i++) (h->sigma_trie+i)->next = NULL; for (sig = h->gsigma; sig != NULL && sig->number != -1; sig = sig->next) { if (flag_check(sig->symbol)) { h->has_flags = 1; apply_add_flag(h, flag_get_name(sig->symbol)); } *(h->sigs+(sig->number)) = sig->symbol; /* Add sigma entry to trie */ if (sig->number > IDENTITY) { apply_add_sigma_trie(h, sig->number, sig->symbol); } } if (h->has_flags) { h->flag_lookup = xxmalloc(sizeof(struct flag_lookup)*(maxsigma+1)); for (i=0; i <= maxsigma; i++) { (h->flag_lookup+i)->type = 0; (h->flag_lookup+i)->name = NULL; (h->flag_lookup+i)->value = NULL; } for (sig = h->gsigma; sig != NULL ; sig = sig->next) { if (flag_check(sig->symbol)) { (h->flag_lookup+sig->number)->type = flag_get_type(sig->symbol); (h->flag_lookup+sig->number)->name = flag_get_name(sig->symbol); (h->flag_lookup+sig->number)->value = flag_get_value(sig->symbol); } } } }
int sigma_sort(struct fsm *net) { #ifdef ORIGINAL int(*comp)() = ssortcmp; #else int(*comp)(const void*, const void*) = ssortcmp; #endif int size, i, max, *replacearray; struct ssort *ssort; struct sigma *sigma; struct fsm_state *fsm_state; size = sigma_max(net->sigma); if (size < 0) { return 1; } ssort = xxmalloc(sizeof(struct ssort)*size); for (i=0, sigma=net->sigma; sigma != NULL; sigma=sigma->next) { if (sigma->number > IDENTITY) { ssort[i].symbol = (char *)sigma->symbol; ssort[i].number = sigma->number; i++; } } max = i; qsort(ssort, max, sizeof(struct ssort), comp); replacearray = xxmalloc(sizeof(int)*(size+3)); for (i=0; i<max; i++) replacearray[(ssort+i)->number] = i+3; /* Replace arcs */ for(i=0, fsm_state = net->states; (fsm_state+i)->state_no != -1; i++) { if ((fsm_state+i)->in > IDENTITY) (fsm_state+i)->in = replacearray[(fsm_state+i)->in]; if ((fsm_state+i)->out > IDENTITY) (fsm_state+i)->out = replacearray[(fsm_state+i)->out]; } /* Replace sigma */ for (i=0, sigma=net->sigma; sigma != NULL; sigma=sigma->next) { if (sigma->number > IDENTITY) { sigma->number = i+3; sigma->symbol = (ssort+i)->symbol; i++; } } xxfree(replacearray); xxfree(ssort); return(1); }
void flag_purge (struct fsm *net, char *name) { struct fsm_state *fsm; struct sigma *sigma; int i, *ftable, sigmasize; char *csym; sigmasize = sigma_max(net->sigma)+1; ftable = xxmalloc(sizeof(int) * sigmasize); fsm = net->states; for (i=0; i<sigmasize; i++) *(ftable+i)=0; for (sigma = net->sigma; sigma != NULL && sigma->number != -1; sigma = sigma->next) { if (flag_check(sigma->symbol)) { if (name == NULL) { *(ftable+(sigma->number)) = 1; } else { csym = (sigma->symbol) + 3; if (strncmp(csym,name,strlen(name)) == 0 && (strlen(csym)>strlen(name)) && (strncmp(csym+strlen(name),".",1) == 0 || strncmp(csym+strlen(name),"@",1) == 0)) { *(ftable+(sigma->number)) = 1; } } } } for (i = 0; i < sigmasize; i++) { if (*(ftable+i)) { net->sigma = sigma_remove_num(i, net->sigma); } } for (i=0; (fsm+i)->state_no != -1; i++) { if ((fsm+i)->in >= 0 && (fsm+i)->out >= 0) { if (*(ftable+(fsm+i)->in)) (fsm+i)->in = EPSILON; if (*(ftable+(fsm+i)->out)) (fsm+i)->out = EPSILON; } } xxfree(ftable); net->is_deterministic = net->is_minimized = net->is_epsilon_free = NO; return; }
int net_print_att(struct fsm *net, FILE *outfile) { struct fsm_state *fsm; struct fsm_sigma_list *sl; int i, prev; fsm = net->states; sl = sigma_to_list(net->sigma); if (sigma_max(net->sigma) >= 0) { (sl+0)->symbol = g_att_epsilon; } for (i=0; (fsm+i)->state_no != -1; i++) { if ((fsm+i)->target != -1) { fprintf(outfile, "%i\t%i\t%s\t%s\n",(fsm+i)->state_no,(fsm+i)->target, (sl+(fsm+i)->in)->symbol, (sl+(fsm+i)->out)->symbol); } } prev = -1; for (i=0; (fsm+i)->state_no != -1; prev = (fsm+i)->state_no, i++) { if ((fsm+i)->state_no != prev && (fsm+i)->final_state == 1) { fprintf(outfile, "%i\n",(fsm+i)->state_no); } } xxfree(sl); return(1); }
void apply_create_sigarray(struct apply_handle *h, struct fsm *net) { struct sigma *sig; int i, maxsigma; maxsigma = sigma_max(net->sigma); h->sigma_size = maxsigma+1; // Default size created at init, resized later if necessary h->sigmatch_array = xxcalloc(1024,sizeof(struct sigmatch_array)); h->sigmatch_array_size = 1024; h->sigs = xxmalloc(sizeof(struct sigs)*(maxsigma+1)); h->has_flags = 0; h->flag_list = NULL; /* Malloc first array of trie and store trie ptrs to be able to free later */ /* when apply_clear() is called. */ h->sigma_trie = xxcalloc(256,sizeof(struct sigma_trie)); h->sigma_trie_arrays = xxmalloc(sizeof(struct sigma_trie_arrays)); h->sigma_trie_arrays->arr = h->sigma_trie; h->sigma_trie_arrays->next = NULL; for (i=0;i<256;i++) (h->sigma_trie+i)->next = NULL; for (sig = h->gsigma; sig != NULL && sig->number != -1; sig = sig->next) { if (flag_check(sig->symbol)) { h->has_flags = 1; apply_add_flag(h, flag_get_name(sig->symbol)); } (h->sigs+(sig->number))->symbol = sig->symbol; (h->sigs+(sig->number))->length = strlen(sig->symbol); /* Add sigma entry to trie */ if (sig->number > IDENTITY) { apply_add_sigma_trie(h, sig->number, sig->symbol, (h->sigs+(sig->number))->length); } } if (maxsigma >= IDENTITY) { (h->sigs+EPSILON)->symbol = "0"; (h->sigs+EPSILON)->length = 1; (h->sigs+UNKNOWN)->symbol = "?"; (h->sigs+UNKNOWN)->length = 1; (h->sigs+IDENTITY)->symbol = "@"; (h->sigs+IDENTITY)->length = 1; } if (h->has_flags) { h->flag_lookup = xxmalloc(sizeof(struct flag_lookup)*(maxsigma+1)); for (i=0; i <= maxsigma; i++) { (h->flag_lookup+i)->type = 0; (h->flag_lookup+i)->name = NULL; (h->flag_lookup+i)->value = NULL; } for (sig = h->gsigma; sig != NULL ; sig = sig->next) { if (flag_check(sig->symbol)) { (h->flag_lookup+sig->number)->type = flag_get_type(sig->symbol); (h->flag_lookup+sig->number)->name = flag_get_name(sig->symbol); (h->flag_lookup+sig->number)->value = flag_get_value(sig->symbol); } } apply_mark_flagstates(h); } }
static struct fsm *fsm_subset(struct fsm *net, int operation) { int T, U; if (net->is_deterministic == YES && operation != SUBSET_TEST_STAR_FREE) { return(net); } /* Export this var */ op = operation; fsm_count(net); num_states = net->statecount; deterministic = 1; init(net); nhash_init((num_states < 12) ? 6 : num_states/2); T = initial_e_closure(net); int_stack_clear(); if (deterministic == 1 && epsilon_symbol == -1 && num_start_states == 1 && numss == 0) { net->is_deterministic = YES; net->is_epsilon_free = YES; nhash_free(table, nhash_tablesize); xxfree(T_ptr); xxfree(e_table); xxfree(trans_list); xxfree(trans_array); xxfree(double_sigma_array); xxfree(single_sigma_array); xxfree(finals); xxfree(temp_move); xxfree(set_table); return(net); } if (operation == SUBSET_EPSILON_REMOVE && epsilon_symbol == -1) { net->is_epsilon_free = YES; nhash_free(table, nhash_tablesize); xxfree(T_ptr); xxfree(e_table); xxfree(trans_list); xxfree(trans_array); xxfree(double_sigma_array); xxfree(single_sigma_array); xxfree(finals); xxfree(temp_move); xxfree(set_table); return(net); } if (operation == SUBSET_TEST_STAR_FREE) { fsm_state_init(sigma_max(net->sigma)+1); star_free_mark = 0; } else { fsm_state_init(sigma_max(net->sigma)); xxfree(net->states); } /* init */ do { int i, j, tail, setsize, *theset, stateno, has_trans, minsym, next_minsym, trgt, symbol_in, symbol_out; struct trans_list *transitions; struct trans_array *tptr; fsm_state_set_current_state(T, (T_ptr+T)->finalstart, T == 0 ? 1 : 0); /* Prepare set */ setsize = (T_ptr+T)->size; theset = set_table+(T_ptr+T)->set_offset; minsym = INT_MAX; has_trans = 0; for (i = 0; i < setsize; i++) { stateno = *(theset+i); tptr = trans_array+stateno; tptr->tail = 0; if (tptr->size == 0) continue; if ((tptr->transitions)->inout < minsym) { minsym = (tptr->transitions)->inout; has_trans = 1; } } if (!has_trans) { /* close state */ fsm_state_end_state(); continue; } /* While set not empty */ for (next_minsym = INT_MAX; minsym != INT_MAX ; minsym = next_minsym, next_minsym = INT_MAX) { theset = set_table+(T_ptr+T)->set_offset; for (i = 0, j = 0 ; i < setsize; i++) { stateno = *(theset+i); tptr = trans_array+stateno; tail = tptr->tail; transitions = (tptr->transitions)+tail; while (tail < tptr->size && transitions->inout == minsym) { trgt = transitions->target; if (*(e_table+(trgt)) != mainloop) { *(e_table+(trgt)) = mainloop; *(temp_move+j) = trgt; j++; if (operation == SUBSET_EPSILON_REMOVE) { mainloop++; if ((U = e_closure(j)) != -1) { single_symbol_to_symbol_pair(minsym, &symbol_in, &symbol_out); fsm_state_add_arc(T, symbol_in, symbol_out, U, (T_ptr+T)->finalstart, T == 0 ? 1 : 0); j = 0; } } } tail++; transitions++; } tptr->tail = tail; if (tail == tptr->size) continue; /* Check next minsym */ if (transitions->inout < next_minsym) { next_minsym = transitions->inout; } } if (operation == SUBSET_DETERMINIZE) { mainloop++; if ((U = e_closure(j)) != -1) { single_symbol_to_symbol_pair(minsym, &symbol_in, &symbol_out); fsm_state_add_arc(T, symbol_in, symbol_out, U, (T_ptr+T)->finalstart, T == 0 ? 1 : 0); } } if (operation == SUBSET_TEST_STAR_FREE) { mainloop++; if ((U = e_closure(j)) != -1) { single_symbol_to_symbol_pair(minsym, &symbol_in, &symbol_out); fsm_state_add_arc(T, symbol_in, symbol_out, U, (T_ptr+T)->finalstart, T == 0 ? 1 : 0); if (star_free_mark == 1) { //fsm_state_add_arc(T, maxsigma, maxsigma, U, (T_ptr+T)->finalstart, T == 0 ? 1 : 0); star_free_mark = 0; } } } } /* end state */ fsm_state_end_state(); } while ((T = next_unmarked()) != -1); /* wrapup() */ nhash_free(table, nhash_tablesize); xxfree(set_table); xxfree(T_ptr); xxfree(temp_move); xxfree(e_table); xxfree(trans_list); xxfree(trans_array); if (epsilon_symbol != -1) e_closure_free(); xxfree(double_sigma_array); xxfree(single_sigma_array); xxfree(finals); fsm_state_close(net); return(net); }
struct fsm *flag_twosided(struct fsm *net) { struct fsm_state *fsm; struct sigma *sigma; int i, j, tail, *isflag, maxsigma, maxstate, newarcs, change; /* Enforces twosided flag diacritics */ /* Mark flag symbols */ maxsigma = sigma_max(net->sigma); isflag = xxcalloc(maxsigma+1, sizeof(int)); fsm = net->states; for (sigma = net->sigma ; sigma != NULL; sigma = sigma->next) { if (flag_check(sigma->symbol)) { *(isflag+sigma->number) = 1; } else { *(isflag+sigma->number) = 0; } } maxstate = 0; change = 0; for (i = 0, newarcs = 0; (fsm+i)->state_no != -1 ; i++) { maxstate = (fsm+i)->state_no > maxstate ? (fsm+i)->state_no : maxstate; if ((fsm+i)->target == -1) continue; if (*(isflag+(fsm+i)->in) && (fsm+i)->out == EPSILON) { change = 1; (fsm+i)->out = (fsm+i)->in; } else if (*(isflag+(fsm+i)->out) && (fsm+i)->in == EPSILON) { change = 1; (fsm+i)->in = (fsm+i)->out; } if ((*(isflag+(fsm+i)->in) || *(isflag+(fsm+i)->out)) && (fsm+i)->in != (fsm+i)->out) { newarcs++; } } if (newarcs == 0) { if (change == 1) { net->is_deterministic = UNK; net->is_minimized = UNK; net->is_pruned = UNK; return fsm_topsort(fsm_minimize(net)); } return net; } net->states = xxrealloc(net->states, sizeof(struct fsm)*(i+newarcs)); fsm = net->states; tail = j = i; maxstate++; for (i = 0; i < tail; i++) { if ((fsm+i)->target == -1) continue; if ((*(isflag+(fsm+i)->in) || *(isflag+(fsm+i)->out)) && (fsm+i)->in != (fsm+i)->out) { if (*(isflag+(fsm+i)->in) && !*(isflag+(fsm+i)->out)) { j = add_fsm_arc(fsm, j, maxstate, EPSILON, (fsm+i)->out, (fsm+i)->target, 0, 0); (fsm+i)->out = (fsm+i)->in; (fsm+i)->target = maxstate; maxstate++; } else if (*(isflag+(fsm+i)->out) && !*(isflag+(fsm+i)->in)) { j = add_fsm_arc(fsm, j, maxstate, (fsm+i)->out, (fsm+i)->out, (fsm+i)->target, 0, 0); (fsm+i)->out = EPSILON; (fsm+i)->target = maxstate; maxstate++; } else if (*(isflag+(fsm+i)->in) && *(isflag+(fsm+i)->out)) { j = add_fsm_arc(fsm, j, maxstate, (fsm+i)->out, (fsm+i)->out, (fsm+i)->target, 0, 0); (fsm+i)->out = (fsm+i)->in; (fsm+i)->target = maxstate; maxstate++; } } } /* Add sentinel */ add_fsm_arc(fsm, j, -1, -1, -1, -1, -1, -1); net->is_deterministic = UNK; net->is_minimized = UNK; return fsm_topsort(fsm_minimize(net)); }
struct fsm_read_handle *fsm_read_init(struct fsm *net) { struct fsm_read_handle *handle; struct fsm_state *fsm, **states_head; int i, j, k, num_states, num_initials, num_finals, sno, *finals_head, *initials_head, laststate; unsigned char *lookuptable; if (net == NULL) {return (NULL);} num_states = net->statecount; lookuptable = xxcalloc(num_states, sizeof(unsigned char)); num_initials = num_finals = 0; handle = xxcalloc(1,sizeof(struct fsm_read_handle)); states_head = xxcalloc(num_states+1,sizeof(struct fsm **)); laststate = -1; for (i=0, fsm=net->states; (fsm+i)->state_no != -1; i++) { sno = (fsm+i)->state_no; if ((fsm+i)->start_state) { if (!(*(lookuptable+sno) & 1)) { *(lookuptable+sno) |= 1; num_initials++; } } if ((fsm+i)->final_state) { if (!(*(lookuptable+sno) & 2)) { *(lookuptable+sno) |= 2; num_finals++; } } if ((fsm+i)->in == UNKNOWN || (fsm+i)->out == UNKNOWN || (fsm+i)->in == IDENTITY || (fsm+i)->out == IDENTITY) { handle->has_unknowns = 1; } if ((fsm+i)->state_no != laststate) { *(states_head+(fsm+i)->state_no) = fsm+i; } laststate = (fsm+i)->state_no; } finals_head = xxcalloc(num_finals+1,sizeof(int)); initials_head = xxcalloc(num_initials+1,sizeof(int)); for (i=j=k=0; i < num_states; i++) { if (*(lookuptable+i) & 1) { *(initials_head+j) = i; j++; } if (*(lookuptable+i) & 2) { *(finals_head+k) = i; k++; } } *(initials_head+j) = -1; *(finals_head+k) = -1; handle->finals_head = finals_head; handle->initials_head = initials_head; handle->states_head = states_head; handle->fsm_sigma_list = sigma_to_list(net->sigma); handle->sigma_list_size = sigma_max(net->sigma)+1; handle->arcs_head = fsm; handle->lookuptable = lookuptable; handle->net = net; return(handle); }
int write_prolog (struct fsm *net, char *filename) { struct fsm_state *stateptr; int i, *finals, *used_symbols, maxsigma; FILE *out; char *outstring, *instring, identifier[100]; if (filename == NULL) { out = stdout; } else { if ((out = fopen(filename, "w")) == NULL) { printf("Error writing to file '%s'. Using stdout.\n", filename); out = stdout; } printf("Writing prolog to file '%s'.\n", filename); } fsm_count(net); maxsigma = sigma_max(net->sigma); used_symbols = xxcalloc(maxsigma+1,sizeof(int)); finals = xxmalloc(sizeof(int)*(net->statecount)); stateptr = net->states; identifier[0] = '\0'; strcpy(identifier, net->name); /* Print identifier */ fprintf(out, "%s%s%s", "network(",identifier,").\n"); for (i=0; (stateptr+i)->state_no != -1; i++) { if ((stateptr+i)->final_state == 1) { *(finals+((stateptr+i)->state_no)) = 1; } else { *(finals+((stateptr+i)->state_no)) = 0; } if ((stateptr+i)->in != -1) { *(used_symbols+((stateptr+i)->in)) = 1; } if ((stateptr+i)->out != -1) { *(used_symbols+((stateptr+i)->out)) = 1; } } for (i = 3; i <= maxsigma; i++) { if (*(used_symbols+i) == 0) { instring = sigma_string(i, net->sigma); if (strcmp(instring,"0") == 0) { instring = "%0"; } fprintf(out, "symbol(%s, \"", identifier); escape_print(out, instring); fprintf(out, "\").\n"); } } for (; stateptr->state_no != -1; stateptr++) { if (stateptr->target == -1) continue; fprintf(out, "arc(%s, %i, %i, ", identifier, stateptr->state_no, stateptr->target); if (stateptr->in == 0) instring = "0"; else if (stateptr->in == 1) instring = "?"; else if (stateptr->in == 2) instring = "?"; else instring = sigma_string(stateptr->in, net->sigma); if (stateptr->out == 0) outstring = "0"; else if (stateptr->out == 1) outstring = "?"; else if (stateptr->out == 2) outstring = "?"; else outstring = sigma_string(stateptr->out, net->sigma); if (strcmp(instring,"0") == 0 && stateptr->in != 0) instring = "%0"; if (strcmp(outstring,"0") == 0 && stateptr->out != 0) outstring = "%0"; if (strcmp(instring,"?") == 0 && stateptr->in > 2) instring = "%?"; if (strcmp(outstring,"?") == 0 && stateptr->in > 2) outstring = "%?"; /* Escape quotes */ if (net->arity == 2 && stateptr->in == IDENTITY && stateptr->out == IDENTITY) { fprintf(out, "\"?\").\n"); } else if (net->arity == 2 && stateptr->in == stateptr->out && stateptr->in != UNKNOWN) { fprintf(out, "\""); escape_print(out, instring); fprintf(out, "\").\n"); } else if (net->arity == 2) { fprintf(out, "\""); escape_print(out, instring); fprintf(out, "\":\""); escape_print(out, outstring); fprintf(out, "\").\n"); } else if (net->arity == 1) { fprintf(out, "\""); escape_print(out, instring); fprintf(out, "\").\n"); } } for (i = 0; i < net->statecount; i++) { if (*(finals+i)) { fprintf(out, "final(%s, %i).\n", identifier, i); } } if (filename != NULL) { fclose(out); } xxfree(finals); xxfree(used_symbols); return 1; }
void indice_vecnos_prox() { int s1,s2; int s1min,s1max,s2min,s2max; int dir; int s1_dir,s2_dir,s1_old,s2_old; int i; int vec_new,vec_old; int ord; for(s1_dir=1,s2_dir=0,dir=0; dir<6; dir++) { basev1[dir].s1=s1_dir; basev1[dir].s2=s2_dir; s1_old=s1_dir; s2_old=s2_dir; s1_dir=-s2_old; s2_dir= s1_old + s2_old; } for(dir=0; dir<6; dir++) { rombov1[dir].min.s1=sigma_min(basev1[dir].s1); rombov1[dir].max.s1=sigma_max(basev1[dir].s1); rombov1[dir].min.s2=sigma_min(basev1[dir].s2); rombov1[dir].max.s2=sigma_max(basev1[dir].s2); } // Cálculo de v1[N][6],zv1[N] for(s2=0; s2<L; s2++) { for(s1=0; s1<L; s1++) { i=s1+L*s2; vec_old=NO; zv1[i]=0; dirv1_ini[i]=0; dirv1_fin[i]=0; for(dir=0; dir<6; dir++) { if(s1>=rombov1[dir].min.s1 && s1<rombov1[dir].max.s1 && s2>=rombov1[dir].min.s2 && s2<rombov1[dir].max.s2) { v1[i][dir] = i + basev1[dir].s1 + L * basev1[dir].s2; zv1[i]+=1; vec_new=SI; } else { v1[i][dir]=-1; vec_new=NO; } if(vec_old-vec_new<0) dirv1_ini[i]=dir; if(vec_old-vec_new>0) dirv1_fin[i]=dir-1; vec_old=vec_new; } if(vec_old==SI && v1[i][0]==-1)//Comparamos dir 0 con dir 5 dirv1_fin[i]=5; } } }