2024-09-10 12:58:18 +02:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* Gem-graph client *
|
|
|
|
* Finite State Machine - Journal *
|
|
|
|
* *
|
|
|
|
* Copyright © 2024 Libre en Communs <contact@a-lec.org> *
|
|
|
|
* Copyright © 2024 Jean Sirmai <jean@a-lec.org> *
|
|
|
|
* *
|
|
|
|
* This file is part of Gem-graph. *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Affero General Public License *
|
|
|
|
* as published by the Free Software Foundation, *
|
|
|
|
* either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; *
|
|
|
|
* without even the implied warranty of MERCHANTABILITY *
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU Affero General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2024-08-29 08:21:22 +02:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "../../include/fsm.h"
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2024-09-10 22:37:31 +02:00
|
|
|
/* A journal stores chronologically the fsm events */
|
|
|
|
/* during a session run (rules exec, mainly) */
|
2024-08-29 08:21:22 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-10 22:37:31 +02:00
|
|
|
void fsm_journal_init (journal *jj)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
jj->first = NULL;
|
|
|
|
jj->last = NULL;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:37:31 +02:00
|
|
|
void fsm_journal_clear (journal *jj)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *tmp;
|
|
|
|
unit *a_unit = jj->first;
|
|
|
|
while(a_unit)
|
|
|
|
{
|
|
|
|
tmp = a_unit;
|
|
|
|
a_unit = a_unit->next;
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
jj->first = NULL;
|
|
|
|
jj->last = NULL;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:37:31 +02:00
|
|
|
int fsm_journal_push_back (journal *jj, int event)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *new_unit = malloc (sizeof(unit));
|
|
|
|
if (! new_unit) exit (EXIT_FAILURE);
|
|
|
|
new_unit->event = event;
|
|
|
|
new_unit->prev = jj->last;
|
|
|
|
new_unit->next = NULL;
|
|
|
|
if (jj->last) jj->last->next = new_unit;
|
|
|
|
else jj->first = new_unit;
|
|
|
|
jj->last = new_unit;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fsm_journal_push_front (journal *jj, int event)
|
|
|
|
{
|
|
|
|
unit *new_unit = malloc(sizeof(unit));
|
|
|
|
if (! new_unit) exit (EXIT_FAILURE);
|
|
|
|
new_unit->event = event;
|
|
|
|
new_unit->next = jj->first;
|
|
|
|
new_unit->prev = NULL;
|
|
|
|
if (jj->first) jj->first->prev = new_unit;
|
|
|
|
else jj->last = new_unit;
|
|
|
|
jj->first = new_unit;
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fsm_journal_pop_back (journal *jj)
|
|
|
|
{
|
|
|
|
int event;
|
|
|
|
unit *tmp = jj->last;
|
|
|
|
if (! tmp) return -1;
|
|
|
|
event = tmp->event;
|
|
|
|
jj->last = tmp->prev;
|
|
|
|
if (jj->last) jj->last->next = NULL;
|
|
|
|
else jj->first = NULL;
|
|
|
|
free (tmp);
|
|
|
|
return event; // retourne l'évènement retiré du journal.
|
|
|
|
}
|
|
|
|
|
|
|
|
int fsm_journal_pop_front (journal *jj)
|
|
|
|
{
|
|
|
|
int event;
|
|
|
|
unit *tmp = jj->first;
|
|
|
|
if (! tmp) return -1;
|
|
|
|
event = tmp->event;
|
|
|
|
jj->first = tmp->next;
|
|
|
|
if (jj->first) jj->first->prev = NULL;
|
|
|
|
else jj->last = NULL;
|
|
|
|
free (tmp);
|
|
|
|
return event; // retourne l'évènement retiré du journal.
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:37:31 +02:00
|
|
|
int fsm_journal_length (journal jj)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *a_unit = jj.first;
|
|
|
|
int nb = 0;
|
|
|
|
while (a_unit)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
nb ++;
|
|
|
|
a_unit = a_unit->next;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
2024-09-10 22:37:31 +02:00
|
|
|
return nb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fsm_journal_seek (journal *jj, int event)
|
|
|
|
{
|
|
|
|
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:37:31 +02:00
|
|
|
void fsm_journal_view (journal jj)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
|
|
|
printf ("view journal (n = %d)\n", fsm_journal_length (jj));
|
2024-09-10 22:37:31 +02:00
|
|
|
unit *a_unit = jj.first;
|
|
|
|
while (a_unit)
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
printf ("> %d <\n", a_unit->event);
|
|
|
|
a_unit = a_unit->next;
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
|
|
|
puts ("------");
|
|
|
|
}
|
|
|
|
|
2024-09-10 22:37:31 +02:00
|
|
|
void fsm_journal_test()
|
2024-08-29 08:21:22 +02:00
|
|
|
{
|
2024-09-10 22:37:31 +02:00
|
|
|
journal exemple;
|
|
|
|
|
|
|
|
fsm_journal_init (&exemple);
|
|
|
|
|
|
|
|
printf ("push front > %d\n", fsm_journal_push_front (&exemple, 10));
|
|
|
|
printf ("push back > %d\n", fsm_journal_push_back (&exemple, 100));
|
|
|
|
printf ("push back > %d\n", fsm_journal_push_back (&exemple, 1000));
|
|
|
|
printf ("push front > %d\n", fsm_journal_push_front (&exemple, 1));
|
|
|
|
|
|
|
|
fsm_journal_view (exemple);
|
2024-08-29 08:21:22 +02:00
|
|
|
|
2024-09-10 22:37:31 +02:00
|
|
|
printf ("pop back > %d\n", fsm_journal_pop_back (&exemple));
|
|
|
|
printf ("pop back > %d\n", fsm_journal_pop_back (&exemple));
|
|
|
|
printf ("pop back > %d\n", fsm_journal_pop_back (&exemple));
|
|
|
|
printf ("push back > %d\n", fsm_journal_push_back (&exemple, 2));
|
|
|
|
|
|
|
|
fsm_journal_view (exemple);
|
|
|
|
|
|
|
|
printf ("push back > %d\n", fsm_journal_push_back (&exemple, 3));
|
|
|
|
printf ("push front > %d\n", fsm_journal_push_front (&exemple, 0));
|
|
|
|
|
|
|
|
fsm_journal_view (exemple);
|
|
|
|
|
|
|
|
printf ("clear list\n"); fsm_journal_clear (&exemple);
|
|
|
|
|
|
|
|
fsm_journal_view (exemple);
|
2024-08-29 08:21:22 +02:00
|
|
|
}
|
2024-09-10 22:37:31 +02:00
|
|
|
|