Progressing into parsing and model file syntax

This commit is contained in:
Adrien Bourmault 2021-09-17 16:14:04 +02:00
parent 1342b445af
commit 5ee70f6c44
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
6 changed files with 144 additions and 89 deletions

View File

@ -121,6 +121,10 @@ run-gdb: all
@gdb bin/gem-graph-server \ @gdb bin/gem-graph-server \
-ex "set args -C debian/etc -M debian/var/models -U debian/var/users" -ex "set args -C debian/etc -M debian/var/models -U debian/var/users"
run-valgrind: all
@valgrind bin/gem-graph-server \
-C debian/etc -M debian/var/models -U debian/var/users
run-both: all run-both: all
@bin/gem-graph-server -C debian/etc -M debian/var/models \ @bin/gem-graph-server -C debian/etc -M debian/var/models \
-U debian/var/users & sleep 1 && bin/gem-graph-ctl -U debian/var/users & sleep 1 && bin/gem-graph-ctl

View File

@ -7,9 +7,9 @@
<!-- Model identity informations --> <!-- Model identity informations -->
<identity> <identity>
<name>Modèle de test</name> <name>Modèle de test</name>
<author>Adrien Bourmault, Jean Sirmai</author> <owner>Jean Sirmai</owner>
<author_id>1,2</author_id> <owner_id>2</owner_id>
<date>1629830000</date> <!--Warning: it's an arbitrary unchecked date...--> <date>1629830000</date> <!--Warning: it's an arbitrary unchecked date...-->
<version>1.0</version> <version>1.0</version>
</identity> </identity>
@ -91,26 +91,6 @@
</transition> </transition>
<transition id="0" date="" author="" probability="1"> <!-- moves a dimer to west-->
<if>
<arrow x="0" y="0" z="0" site="1" weight="1"/>
<arrow x="1" y="0" z="0" site="0" weight="1"/> <!-- a dimer is identified-->
<!-- the local space origin is its "east" particule-->
<arrow x="-1" y="0" z="0" site="0" weight="0"/> <!-- the west location next this dimer is empty-->
<arrow x="-2" y="0" z="0" site="1" weight="0"/> <!-- In this very simple model, I do not need to check this condition-->
</if>
<then> <!-- Therefore I can (and I want to) move it to its next west location-->
<arrow x="0" y="0" z="0" site="1" weight="0"/>
<arrow x="1" y="0" z="0" site="0" weight="0"/> <!-- The identified dimer no more exists ( :-(( -->
<arrow x="0" y="0" z="0" site="0" weight="1"/>
<arrow x="-1" y="0" z="0" site="1" weight="1"/> <!-- Happily, it is now rebuilt at its west location ( ;-))-->
</then>
</transition>
</transitions> </transitions>
<!-- Non context-specific tags --> <!-- Non context-specific tags -->

View File

@ -160,7 +160,8 @@ struct Model_t {
int id; int id;
bool validated; // true if model has been correctly parsed bool validated; // true if model has been correctly parsed
char *name; char *name;
char *author; char *owner;
char *owner_id;
time_t date; time_t date;
char *version; char *version;
char *filename; char *filename;

View File

@ -26,6 +26,24 @@
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
#define MODEL_STRING_SIZE 48
#define MAX_MODEL_NUMBER 1
#define MAX_MODEL_NAME_SIZE MODEL_STRING_SIZE
#define MAX_MODEL_FILENAME_SIZE MODEL_STRING_SIZE
#define MAX_OWNER_NAME_SIZE MODEL_STRING_SIZE
#define MAX_VERSION_SIZE MODEL_STRING_SIZE
#define ARROW_NUMBER 6
#define SITE_NUMBER 2
#define MAX_CYCLES 10
#define MAX_THREAD 0
#define XMAX 39
#define YMAX 0
#define ZMAX 0
#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1)
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Model init function (and model discovery) // // Model init function (and model discovery) //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //

View File

@ -23,24 +23,11 @@
#include "../include/parsing.h" #include "../include/parsing.h"
#include "../include/arrows.h" #include "../include/arrows.h"
#include "../include/scheduler.h" #include "../include/scheduler.h"
#include "../include/model.h"
#include <dirent.h> #include <dirent.h>
#include <signal.h> #include <signal.h>
#define MAX_MODEL_NUMBER 1
#define MAX_MODEL_NAME_SIZE 48
#define MAX_MODEL_FILENAME_SIZE 48
#define MAX_AUTHOR_NAME_SIZE 48
#define MAX_VERSION_SIZE 16
#define ARROW_NUMBER 6
#define SITE_NUMBER 2
#define MAX_CYCLES 10
#define MAX_THREAD 0
#define XMAX 39
#define YMAX 0
#define ZMAX 0
#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1)
static Model_t **loadedModel; static Model_t **loadedModel;
static int loadedModelSize; // begins to 1 static int loadedModelSize; // begins to 1
@ -59,12 +46,12 @@ void printModels(char *buf)
sprintf(buf + strlen(buf),"Known models\n"); sprintf(buf + strlen(buf),"Known models\n");
for (int i = 0; i <= knownModelSize-1; i++) { for (int i = 0; i <= knownModelSize-1; i++) {
sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s, date: %lu, " sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s, date: %lu, "
"author: %s\n", "owner: %s\n",
knownModel[i]->id, knownModel[i]->id,
knownModel[i], knownModel[i],
knownModel[i]->name, knownModel[i]->name,
knownModel[i]->date, knownModel[i]->date,
knownModel[i]->author knownModel[i]->owner
); );
} }
@ -95,7 +82,7 @@ int ModelLoad(int id)
return loadedModelSize; return loadedModelSize;
} }
void ModelUnload(int id) int ModelUnload(int id)
{ {
// Destroy scheduler // Destroy scheduler
SchedDestroy(loadedModel[id-1]->scheduler); SchedDestroy(loadedModel[id-1]->scheduler);
@ -196,8 +183,8 @@ void ModelAddToKnown(Model_t **newModel) // TODO manage deletion and empty slots
knownModel[knownModelSize-1]->filename = knownModel[knownModelSize-1]->filename =
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
knownModel[knownModelSize-1]->author = knownModel[knownModelSize-1]->owner =
(char *) calloc(1, sizeof(char) * MAX_AUTHOR_NAME_SIZE); (char *) calloc(1, sizeof(char) * MAX_OWNER_NAME_SIZE);
knownModel[knownModelSize-1]->version = knownModel[knownModelSize-1]->version =
(char *) calloc(1, sizeof(char) * MAX_VERSION_SIZE); (char *) calloc(1, sizeof(char) * MAX_VERSION_SIZE);
@ -219,8 +206,8 @@ void ModelDelete(int id) //XXX
free(knownModel[id-1]->filename); free(knownModel[id-1]->filename);
knownModel[id-1]->filename = NULL; knownModel[id-1]->filename = NULL;
free(knownModel[id-1]->author); free(knownModel[id-1]->owner);
knownModel[id-1]->author = NULL; knownModel[id-1]->owner = NULL;
free(knownModel[id-1]->version); free(knownModel[id-1]->version);
knownModel[id-1]->version = NULL; knownModel[id-1]->version = NULL;
@ -293,7 +280,7 @@ void ModelSystemInit(Parameters_t *parameters)
// Ask to parse the new model // Ask to parse the new model
if (ParseModelXML(newModel) != 0) { if (ParseModelXML(newModel) != 0) {
ModelDelete(newModel->id); ModelDelete(newModel->id);
continue;XMAX continue;
}; };
// Check model is valid and/or parsed // Check model is valid and/or parsed

View File

@ -20,70 +20,134 @@
//=-------------------------------------------------------------------------=// //=-------------------------------------------------------------------------=//
#include "../include/base.h" #include "../include/base.h"
#include "../include/model.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <libxml2/libxml/xmlmemory.h> #include <libxml/parser.h>
#include <libxml2/libxml/parser.h>
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static int parseIdentityXML(Model_t*, xmlDocPtr, xmlNodePtr); #define NO_DESTINATION NULL
static int parseIdentityNameXML(Model_t*, xmlDocPtr, xmlNodePtr);
// Table of all parsers /* -------------------------------------------------------------------------- */
struct parserTableXML_t {
struct ModelParserTableXML_t;
struct ParserTableXML_t
{
const xmlChar *tag; const xmlChar *tag;
int (*execute) (Model_t*, xmlDocPtr, xmlNodePtr); int (*parse) (xmlDoc*,
struct ModelParserTableXML_t*,
int currentParser,
xmlNode*);
void *destination;
} typedef ParserTableXML_t; } typedef ParserTableXML_t;
static ParserTableXML_t toplevelParserTableXML[] = struct ModelParserTableXML_t
{ {
{(const xmlChar *)"identity", parseIdentityXML}, size_t len;
}; ParserTableXML_t *table;
} typedef ModelParserTableXML_t;
static ParserTableXML_t identityParserTableXML[] =
{
{(const xmlChar *)"name", parseIdentityChildren},
{(const xmlChar *)"author", parseIdentityChildren},
{(const xmlChar *)"author_id", parseIdentityChildren},
{(const xmlChar *)"date", parseIdentityChildren},
{(const xmlChar *)"version", parseIdentityChildren},
};
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
static int parseIdentityNameXML (Model_t *model, xmlDocPtr doc, int parseParentFieldXML(xmlDocPtr,
xmlNodePtr currentNode) ModelParserTableXML_t*,
int,
xmlNodePtr);
int parseTextField(xmlDocPtr,
ModelParserTableXML_t*,
int,
xmlNodePtr);
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- //
// Parsing NOTHING //
// -------------------------------------------------------------------------- //
int parseStubFieldXML (xmlDocPtr doc,
ModelParserTableXML_t *ModelTable,
int currentParser,
xmlNodePtr currentNode)
{ {
xmlChar *key; xmlChar *content;
key = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1); content = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1);
printLog("key: %s\n", key);
xmlFree(key); printLog("%s (stub): %s\n",
ModelTable->table[currentParser].tag,
content);
xmlFree(content);
return 0; return 0;
} }
static int parseIdentityXML (Model_t *model, xmlDocPtr doc, // -------------------------------------------------------------------------- //
xmlNodePtr currentNode) // Parsing a text field //
// -------------------------------------------------------------------------- //
int parseTextFieldXML (xmlDocPtr doc,
ModelParserTableXML_t *ModelTable,
int currentParser,
xmlNodePtr currentNode)
{ {
xmlChar *key; xmlChar *content;
currentNode = currentNode->xmlChildrenNode; content = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1);
while (currentNode != NULL) {
for (int i = 0; i < LEN(identityParserTableXML); i++) { printLog("%s: %s\n", ModelTable->table[currentParser].tag,
content);
strncpy((char *)ModelTable->table[currentParser].destination,
(char *)content, MODEL_STRING_SIZE);
xmlFree(content);
return 0;
}
int parseParentFieldXML (xmlDocPtr doc,
ModelParserTableXML_t *ModelTable,
int currentParser,
xmlNodePtr currentNode)
{
currentNode = currentNode->xmlChildrenNode;
while (currentNode != NULL) {
for (int i = 0; i < ModelTable->len; i++) {
if ((!xmlStrcmp(currentNode->name, if ((!xmlStrcmp(currentNode->name,
identityParserTableXML[i].tag))) { ModelTable->table[i].tag))) {
identityParserTableXML[i].execute(model, ModelTable->table[i].parse(doc,
doc, ModelTable,
currentNode); i,
currentNode);
} }
} }
currentNode = currentNode->next; currentNode = currentNode->next;
} }
return 0; return 0;
} }
int ParseModelXML(Model_t *model) int ParseModelXML(Model_t *model)
{ {
ParserTableXML_t table[] =
{
{(const xmlChar *)"identity", parseParentFieldXML,
model},
{(const xmlChar *)"name", parseTextFieldXML,
model->name},
{(const xmlChar *)"owner", parseTextFieldXML,
model->owner},
{(const xmlChar *)"owner_id", parseStubFieldXML,
model->owner_id},
{(const xmlChar *)"date", parseStubFieldXML,
model->date},
{(const xmlChar *)"version", parseTextFieldXML,
model->version},
};
ModelParserTableXML_t modelParserTable =
{
LEN(table),
&table[0]
};
printLog("Parsing model %s\n", model->name); printLog("Parsing model %s\n", model->name);
xmlDocPtr xmlDocument; xmlDocPtr xmlDocument;
xmlNodePtr currentNode; xmlNodePtr currentNode;
@ -114,12 +178,13 @@ int ParseModelXML(Model_t *model)
currentNode = currentNode->xmlChildrenNode; currentNode = currentNode->xmlChildrenNode;
while (currentNode != NULL) { while (currentNode != NULL) {
for (int i = 0; i < LEN(toplevelParserTableXML); i++) { for (int i = 0; i < modelParserTable.len; i++) {
if ((!xmlStrcmp(currentNode->name, if ((!xmlStrcmp(currentNode->name,
toplevelParserTableXML[i].tag))) { modelParserTable.table[i].tag))) {
toplevelParserTableXML[i].execute(model, modelParserTable.table[i].parse(xmlDocument,
xmlDocument, &modelParserTable,
currentNode); i,
currentNode);
} }
} }