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 \
-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
@bin/gem-graph-server -C debian/etc -M debian/var/models \
-U debian/var/users & sleep 1 && bin/gem-graph-ctl

View File

@ -7,8 +7,8 @@
<!-- Model identity informations -->
<identity>
<name>Modèle de test</name>
<author>Adrien Bourmault, Jean Sirmai</author>
<author_id>1,2</author_id>
<owner>Jean Sirmai</owner>
<owner_id>2</owner_id>
<date>1629830000</date> <!--Warning: it's an arbitrary unchecked date...-->
<version>1.0</version>
@ -91,26 +91,6 @@
</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>
<!-- Non context-specific tags -->

View File

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

View File

@ -23,24 +23,11 @@
#include "../include/parsing.h"
#include "../include/arrows.h"
#include "../include/scheduler.h"
#include "../include/model.h"
#include <dirent.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 int loadedModelSize; // begins to 1
@ -59,12 +46,12 @@ void printModels(char *buf)
sprintf(buf + strlen(buf),"Known models\n");
for (int i = 0; i <= knownModelSize-1; i++) {
sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s, date: %lu, "
"author: %s\n",
"owner: %s\n",
knownModel[i]->id,
knownModel[i],
knownModel[i]->name,
knownModel[i]->date,
knownModel[i]->author
knownModel[i]->owner
);
}
@ -95,7 +82,7 @@ int ModelLoad(int id)
return loadedModelSize;
}
void ModelUnload(int id)
int ModelUnload(int id)
{
// Destroy 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 =
(char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE);
knownModel[knownModelSize-1]->author =
(char *) calloc(1, sizeof(char) * MAX_AUTHOR_NAME_SIZE);
knownModel[knownModelSize-1]->owner =
(char *) calloc(1, sizeof(char) * MAX_OWNER_NAME_SIZE);
knownModel[knownModelSize-1]->version =
(char *) calloc(1, sizeof(char) * MAX_VERSION_SIZE);
@ -219,8 +206,8 @@ void ModelDelete(int id) //XXX
free(knownModel[id-1]->filename);
knownModel[id-1]->filename = NULL;
free(knownModel[id-1]->author);
knownModel[id-1]->author = NULL;
free(knownModel[id-1]->owner);
knownModel[id-1]->owner = NULL;
free(knownModel[id-1]->version);
knownModel[id-1]->version = NULL;
@ -293,7 +280,7 @@ void ModelSystemInit(Parameters_t *parameters)
// Ask to parse the new model
if (ParseModelXML(newModel) != 0) {
ModelDelete(newModel->id);
continue;XMAX
continue;
};
// Check model is valid and/or parsed

View File

@ -20,60 +20,102 @@
//=-------------------------------------------------------------------------=//
#include "../include/base.h"
#include "../include/model.h"
#include <string.h>
#include <stdlib.h>
#include <libxml2/libxml/xmlmemory.h>
#include <libxml2/libxml/parser.h>
#include <libxml/parser.h>
/* -------------------------------------------------------------------------- */
static int parseIdentityXML(Model_t*, xmlDocPtr, xmlNodePtr);
static int parseIdentityNameXML(Model_t*, xmlDocPtr, xmlNodePtr);
#define NO_DESTINATION NULL
// Table of all parsers
struct parserTableXML_t {
/* -------------------------------------------------------------------------- */
struct ModelParserTableXML_t;
struct ParserTableXML_t
{
const xmlChar *tag;
int (*execute) (Model_t*, xmlDocPtr, xmlNodePtr);
int (*parse) (xmlDoc*,
struct ModelParserTableXML_t*,
int currentParser,
xmlNode*);
void *destination;
} typedef ParserTableXML_t;
static ParserTableXML_t toplevelParserTableXML[] =
struct ModelParserTableXML_t
{
{(const xmlChar *)"identity", parseIdentityXML},
};
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},
};
size_t len;
ParserTableXML_t *table;
} typedef ModelParserTableXML_t;
/* -------------------------------------------------------------------------- */
static int parseIdentityNameXML (Model_t *model, xmlDocPtr doc,
int parseParentFieldXML(xmlDocPtr,
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;
key = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1);
printLog("key: %s\n", key);
xmlFree(key);
xmlChar *content;
content = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1);
printLog("%s (stub): %s\n",
ModelTable->table[currentParser].tag,
content);
xmlFree(content);
return 0;
}
static int parseIdentityXML (Model_t *model, xmlDocPtr doc,
// -------------------------------------------------------------------------- //
// Parsing a text field //
// -------------------------------------------------------------------------- //
int parseTextFieldXML (xmlDocPtr doc,
ModelParserTableXML_t *ModelTable,
int currentParser,
xmlNodePtr currentNode)
{
xmlChar *content;
content = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1);
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)
{
xmlChar *key;
currentNode = currentNode->xmlChildrenNode;
while (currentNode != NULL) {
for (int i = 0; i < LEN(identityParserTableXML); i++) {
for (int i = 0; i < ModelTable->len; i++) {
if ((!xmlStrcmp(currentNode->name,
identityParserTableXML[i].tag))) {
identityParserTableXML[i].execute(model,
doc,
ModelTable->table[i].tag))) {
ModelTable->table[i].parse(doc,
ModelTable,
i,
currentNode);
}
}
@ -84,6 +126,28 @@ static int parseIdentityXML (Model_t *model, xmlDocPtr doc,
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);
xmlDocPtr xmlDocument;
xmlNodePtr currentNode;
@ -114,11 +178,12 @@ int ParseModelXML(Model_t *model)
currentNode = currentNode->xmlChildrenNode;
while (currentNode != NULL) {
for (int i = 0; i < LEN(toplevelParserTableXML); i++) {
for (int i = 0; i < modelParserTable.len; i++) {
if ((!xmlStrcmp(currentNode->name,
toplevelParserTableXML[i].tag))) {
toplevelParserTableXML[i].execute(model,
xmlDocument,
modelParserTable.table[i].tag))) {
modelParserTable.table[i].parse(xmlDocument,
&modelParserTable,
i,
currentNode);
}
}