/* print out given rule */
void show_rule_without_status(int num)
{
	int i;

	for (i = 0; i < MAX_COND; i++)
		{
		if (!its_empty(get_rb_cond_id(num, i)))
			{
			if (i == 0)
				printf("\t\tIf ");
			else
				printf("\t\t And ");
			printf("%s %s\n", get_rb_cond_id(num, i), get_rb_cond_val(num, i));
			}
		}
	for (i = 0; i < MAX_CONS; i++)
		{
		if (!its_empty(get_rb_cons_id(num, i)))
			{
			if (i == 0)
				printf("\t\tThen ");
			else
				printf("\t\t And ");
			printf("%s %s\n", get_rb_cons_id(num, i), get_rb_cons_val(num, i));
			}
		}
	printf("\n");
}
/* do list of questions for forward chaining */
void do_quest_menu(void)
{
	int choice, q, ctr;

	do
		{
		ctr = 0;
		_clearscreen(_GCLEARSCREEN);
		printf("\tQuestion Menu\n\n\n");
		for (q = 0; q < MAX_QUESTIONS; q++)
			{
			if (!its_empty(get_qb_id(q)))
				{
				printf("\t\t%d) %s   current value is %s\n",
					  q+1, get_qb_id(q), get_value(get_qb_id(q)));
				ctr++;
				}
			}
		printf("\t\t%d) Return to main menu\n\n", ctr+1);
		printf("\tSelect a question : ");
		scanf("%d", &choice);
		}
	while (choice <= 0 || choice > ctr+1);
	if (choice == ctr+1)
		return; /* return to main question menu */
	else
		do_question_forward(--choice); /* otherwise ask the question */
}
/* Print consequences of single rule */
void print_cons(int i)
{
	register int z;

	for (z = 0; z < MAX_CONS; z++)
		if (!its_empty(get_rb_cons_id(i, z)))
			printf("%s %s\n", get_rb_cons_id(i, z), get_rb_cons_val(i, z));
}
/* return the number of items in the data base, used in forward chaining */
int find_db_num(void)
{
	int number = 0, z;

	for (z = 0; z < SIZE_OF_DB; z++)
		if (!its_empty(get_db_id(z)))
			number++;
	return number;
}
/* print out the data base */
void examine_d_base(void)
{
	register int i;
	char c;

	_clearscreen(_GCLEARSCREEN);
	for (i = 0; i < SIZE_OF_DB; i++)
		{
		if (!its_empty(get_db_id(i)))
			{
			printf("data base item %d ", i+1);
			printf("%s %s", get_db_id(i), get_db_val(i));
			if (get_db_question_flag(i))
				printf("     User supplied\n");
			else
				printf("     Inferred data\n");
			}
		}
	printf("\n\t============Press a key to continue================");
	c = getch();
}
Exemple #6
0
int main(int argc, char *argv[])
{
    int free_parking_lots = PARKING_LOT_SIZE;
    char *entrance_log,*exit_log; // Strings para log files
    char msg_buffer[30];
    char time_string[30];
    
    /* Auxiliares */
    int status;       // Estatus de procedimientos
    int i;            // Iteraciones
    int last_ticket;  // Último ticket
    struct msg *m;    // Auxiliar para lectura de mensajes
    pthread_t tid;    // Thread para socket
    

    if (argc != 7) {
        printf ("Uso: sem_svr -l <puerto_sem_svr> -i <bitácora_entrada> -o <bitácora_salida> \n");
        exit(0);
    }

    /* Port get for listening */
    if      (strcmp("-l",argv[1]) == 0 ) listen_port = atoi(argv[2]);
    else if (strcmp("-l",argv[3]) == 0 ) listen_port = atoi(argv[4]);
    else if (strcmp("-l",argv[5]) == 0 ) listen_port = atoi(argv[6]);
    else {
        perror("Error: No se ha especificado del puerto");
        exit(0);
    }

    if      (strcmp("-i",argv[1]) == 0 ) entrance_log = argv[2];
    else if (strcmp("-i",argv[3]) == 0 ) entrance_log = argv[4];
    else if (strcmp("-i",argv[5]) == 0 ) entrance_log = argv[6];
    else {
        perror("Error: No se ha especificado la bitacora de entrada");
        exit(0);
    }

    if      (strcmp("-o",argv[1]) == 0 ) exit_log = argv[2];
    else if (strcmp("-o",argv[3]) == 0 ) exit_log = argv[4];
    else if (strcmp("-o",argv[5]) == 0 ) exit_log = argv[6];
    else {
        perror("Error: No se ha especificado la bitacora de salida");
        exit(0);
    }
    /* Initialize parking lot */
    for (i = 0; i < PARKING_LOT_SIZE; ++i)
        parking_space[i] = NULL;
    /* initialize circular buffer for messages*/
    init_buffer(&cb,(sizeof(struct msg)));


    /* Iniciando hilo de socket de lectura */ 
    pthread_create(&tid, NULL, read_messages, NULL);
    pthread_detach(tid);

    /* Ciclo indefinido de espera de mensajes, procesamiento y respuesta*/
    while (1){
        // Entrega de tickets y aceptar tickets de salida
        while(free_parking_lots > 0 ) {

            /* Esperar a el socket haya recibido alg'un mensaje */
            while (its_empty(cb))
                sleep(0.5); 
            
            /* Consumir mensaje */
            m = read_cb(&cb);

            if (m->in_out=='e')
            {   
                last_ticket = new_ticket();
                --free_parking_lots;

                strftime(time_string
                        ,30
                        ,"%d%m%Y%H%M"
                        ,parking_space[last_ticket]);

                // formato BDDMMYYYYHHMMSSS con SSS como ticket serial
                sprintf(msg_buffer,"1%s%03d",time_string,last_ticket);

                write_action(entrance_log,WENT_IN,last_ticket);
                answerClient(m->client.sin_addr,msg_buffer); 

            }
            else if (m->in_out=='s' && parking_space[m->car_id] != NULL)
            {
                ++free_parking_lots;

                last_payment = ticket_price(m->car_id);

                sprintf(msg_buffer,"X%d",last_payment);
                answerClient(m->client.sin_addr,msg_buffer);

                write_action(exit_log,WENT_OUT,m->car_id);

                
            }
            else if (m->in_out=='s' && parking_space[m->car_id] == NULL)
                printf("Ese ticket no est'a en circulaci'on, intente de nuevo\n");
            else printf("Accion inesperada\n");
            
        }
        

        /* Indicar que no hay puestos libres o aceptar ticket de salida */
        while(free_parking_lots == 0)
        {   
            while (its_empty(cb)) 
                sleep(0.5); 

            m = read_cb(&cb);

            if (m->in_out=='e') {
                strftime(time_string
                        ,20
                        ,"%d%m%Y%H%M"
                        ,parking_space[last_ticket]);

                sprintf(msg_buffer,"0%sXXX",time_string);

                answerClient(m->client.sin_addr,msg_buffer);  
                write_action(entrance_log,FULL,last_ticket);
            }
            else if (m->in_out=='s' && parking_space[m->car_id] != NULL){
                ++free_parking_lots;

                last_payment = ticket_price(m->car_id);

                sprintf(msg_buffer,"X%d",last_payment);
                answerClient(m->client.sin_addr,msg_buffer); 

                write_action(exit_log,WENT_OUT,m->car_id);
                
            }
            else if (m->in_out=='s' && parking_space[m->car_id] == NULL)
                printf("Ese ticket no est'a en circulaci'on, intente de nuevo\n");
            else printf("Accion inesperada\n");
        }   
    }

}