Added parsing/, currently parses states
This commit is contained in:
parent
abea32cd88
commit
4a46e62cbc
|
@ -0,0 +1,402 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <libxml/tree.h>
|
||||
#include <libxml/parser.h>
|
||||
#include <libxml/xmlreader.h> // http://xmlsoft.org/examples/#parse1.c
|
||||
#define VRB0 0
|
||||
#define VRB1 0
|
||||
#define VRB2 0
|
||||
#define VRB3 0
|
||||
#define VRB4 1
|
||||
|
||||
// https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/general.html
|
||||
// make && bin/learning ./data/dimers\ random\ walk.xml
|
||||
|
||||
/****** WARNING ! VARIABLES GLOBALES !! ÇA PAS BON !!! > TODO < **********/
|
||||
|
||||
xmlDocPtr doc; // ! WARNING ! I TAKE FOR GRANTED THAT xmlDocPtr doc != NULL
|
||||
xmlHashTable *my_table;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
bool model_init(char *docname)
|
||||
{
|
||||
xmlNodePtr node;
|
||||
|
||||
doc = xmlParseFile(docname);
|
||||
|
||||
if (doc == NULL ) {
|
||||
fprintf(stderr,"Document not parsed successfully.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
node = xmlDocGetRootElement(doc);
|
||||
|
||||
if (node == NULL) {
|
||||
fprintf(stderr,"empty document\n");
|
||||
xmlFreeDoc(doc);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) {
|
||||
fprintf(stderr,"document of the wrong type, root node != gem-graph-model\n");
|
||||
xmlFreeDoc(doc);
|
||||
return false;
|
||||
}
|
||||
|
||||
my_table = xmlHashCreate(10);
|
||||
printf("-------------------- model_init(%s) -----------------------\n", docname);
|
||||
|
||||
/* xmlDocPtr my_tree = xmlReadIO, */
|
||||
/* xmlInputReadCallback ioread, */
|
||||
/* xmlInputCloseCallback ioclose, */
|
||||
/* void * ioctx, */
|
||||
/* const char * URL, */
|
||||
/* const char * encoding, */
|
||||
/* int options); */
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
bool model_shutdown(char *docname){
|
||||
|
||||
xmlFreeDoc(doc);
|
||||
|
||||
printf("-------------------- model_shutdown(%s) -------------------\n", docname);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
static xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last){
|
||||
while (node != NULL && xmlStrcmp(node->name, last)) {
|
||||
// printf(" <>--- line n°%lu <%s>\n", xmlGetLineNo(node), node->name);
|
||||
node = node->next;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
static xmlChar* splitStrAtSlash(xmlChar *toSplit){
|
||||
toSplit = (xmlChar *)xmlStrchr(toSplit, '/');
|
||||
toSplit = xmlStrsub (toSplit, 1, xmlStrlen(toSplit));
|
||||
return toSplit;
|
||||
}
|
||||
|
||||
static xmlChar* getFirstTag(xmlChar *path){
|
||||
xmlChar *preop = path;
|
||||
path = (xmlChar *)xmlStrchr(path, '/');
|
||||
path = xmlStrsub (path, 1, xmlStrlen(path));
|
||||
/* printf("%s = %s + / + %s\n", preop,\ */
|
||||
/* xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1), path); */
|
||||
return xmlStrsub (preop, 0, xmlStrlen(preop) - xmlStrlen(path) - 1);
|
||||
}
|
||||
|
||||
static xmlChar* getLastTag(xmlChar *path){
|
||||
while ((ulong)xmlStrchr (path, '/')) path = splitStrAtSlash((xmlChar *)path);
|
||||
// printf("last tag in the path = <%s>\n", path);
|
||||
return path; // which is no more the given path but only its last tag !
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
static xmlNodePtr model_get_node(xmlChar *path)
|
||||
{
|
||||
xmlNodePtr node;
|
||||
|
||||
// Lookup for node from path in hash table
|
||||
node = xmlHashLookup(my_table, path);
|
||||
|
||||
// Found a node in hash table
|
||||
if(node){
|
||||
if (VRB0) printf("xmlHashLookup (my_table, path: <%s>, tag: <%s> @>%p)\n",
|
||||
path, node->name, node);
|
||||
|
||||
assert(node > (xmlNodePtr)2048);
|
||||
|
||||
return node;
|
||||
|
||||
// no node found in hash table
|
||||
} else {
|
||||
xmlChar *reste = path, *last = reste, *affich = reste;
|
||||
last = getLastTag(last);
|
||||
xmlNodePtr node = xmlDocGetRootElement(doc);
|
||||
|
||||
if (VRB1) printf("line n° %lu : tag <%s> reste = %s\n",\
|
||||
xmlGetLineNo(node), (xmlChar *)node->name, (xmlChar *)affich);
|
||||
|
||||
xmlChar *extrait;
|
||||
|
||||
while ((ulong)xmlStrchr (reste, '/')){
|
||||
extrait = getFirstTag(reste); //printf("extrait = %s\n", extrait);
|
||||
reste = splitStrAtSlash((xmlChar *)reste);//printf("reste = %s\n", reste);
|
||||
node = node->xmlChildrenNode;
|
||||
node = getNextChild(node, extrait);
|
||||
|
||||
if (VRB1) printf("line n°%lu : tag <%s> reste = %s\n",\
|
||||
xmlGetLineNo(node), (xmlChar *)extrait, (xmlChar *)reste);
|
||||
}
|
||||
|
||||
if(node != NULL && xmlStrcmp(node->name, last)) {
|
||||
node = node->xmlChildrenNode;
|
||||
|
||||
while (node != NULL && xmlStrcmp(node->name, last)) {
|
||||
node = node->next;
|
||||
}
|
||||
|
||||
if (VRB1) printf("line n°%lu : tag <%s>\n",\
|
||||
xmlGetLineNo(node), (xmlChar *)node->name);
|
||||
|
||||
xmlHashAddEntry (my_table, path, node);
|
||||
}
|
||||
|
||||
if (VRB0) printf("VRB0 xmlHashAddEntry (my_table, path: <%s>, tag: <%s> @>%p)\n",\
|
||||
path, node->name, node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
return NULL; // ?
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
static long model_get_node_attrib(xmlNodePtr node)
|
||||
{
|
||||
long value = 0;
|
||||
if (node && node->properties) {
|
||||
xmlAttr* attribute = node->properties;
|
||||
xmlChar* value_txt = NULL;
|
||||
char *ptr;
|
||||
while(attribute && attribute->name && attribute->children)
|
||||
{
|
||||
value_txt = xmlNodeListGetString(node->doc, attribute->children, 1);
|
||||
value = strtol((char *)value_txt, &ptr, 10);
|
||||
if (VRB3) printf ("model_get_node_attrib <%s> = %s = %ld\n",\
|
||||
attribute->name, value_txt, value);
|
||||
attribute = attribute->next;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static long model_get_state_arrows_nb(xmlNodePtr node)
|
||||
{
|
||||
long arrows_nb= 0;
|
||||
if (VRB0) printf ("VRB0 model_get_state_arrows_nb <%s> start at line %ld\n",\
|
||||
node->name, xmlGetLineNo(node));
|
||||
while (node && node->next){
|
||||
if (VRB2) printf ("%s %ld >", (xmlChar *)node->name, arrows_nb);
|
||||
if (!xmlStrcmp(node->name, (xmlChar *)"arrow")) ++arrows_nb;
|
||||
if (VRB2) printf (" %ld ", arrows_nb);
|
||||
node = node->next;
|
||||
}
|
||||
if (VRB2) printf ("\n");
|
||||
if (VRB0) printf ("VRB0 model_get_state_arrows_nb = %ld end at line %ld\n",\
|
||||
arrows_nb, xmlGetLineNo(node));
|
||||
return arrows_nb;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
char model_get_dim(xmlChar *path){
|
||||
|
||||
xmlNodePtr node = model_get_node(path); if (VRB2) printf("@node = %p ", node);
|
||||
|
||||
if (node && node->properties) {
|
||||
xmlAttr* attribute = node->properties;
|
||||
while(attribute && attribute->name && attribute->children)
|
||||
{
|
||||
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
|
||||
// printf ("model_get_dim %s = %s\n", attribute->name, value);
|
||||
xmlFree(value);
|
||||
attribute = attribute->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (xmlHasProp (node, (xmlChar *) "z")) return 3;
|
||||
if (xmlHasProp (node, (xmlChar *) "y")) return 2;
|
||||
if (xmlHasProp (node, (xmlChar *) "x")) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_dim_value(xmlChar *path, xmlChar *axis){
|
||||
xmlNodePtr node = model_get_node(path);
|
||||
//xmlNodePtr node= xmlTextReadernoderentNode(reader);
|
||||
if (node && node->properties) {
|
||||
xmlAttr* attribute = node->properties;
|
||||
while(attribute && attribute->name && attribute->children)
|
||||
{
|
||||
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
|
||||
if (!xmlStrcmp(attribute->name, axis))
|
||||
return (int)strtol((char *)value, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char model_get_cond_tree (xmlChar *path){
|
||||
xmlNodePtr node = model_get_node(path);
|
||||
node = node->xmlChildrenNode;
|
||||
node = getNextChild(node, (xmlChar *)"condition");
|
||||
node = xmlCopyNodeList (node->xmlChildrenNode);
|
||||
printf("model_get_cond_tree(\"%s\") > line n°%ld\n", path, xmlGetLineNo(node));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_cond (xmlChar *path, xmlChar *node_id){
|
||||
xmlNodePtr node = model_get_node(path);
|
||||
printf("model_get_cond(...) {...node->name = %s...}\n", node->name);
|
||||
node = getNextChild(node->next, (xmlChar *)"condition");
|
||||
node = getNextChild(node->next, (xmlChar *)"condition");
|
||||
if (xmlHasProp (node, (xmlChar *) "site")) {
|
||||
printf(" node %s has attribute site\n", node->name);
|
||||
}
|
||||
if (xmlHasProp (node, (xmlChar *) "node_id")) {
|
||||
printf(" node %s has attribute %s with value %hhn %s %s\n",\
|
||||
node->name,\
|
||||
xmlHasProp (node, (xmlChar *) "node_id")->name,\
|
||||
xmlGetProp (node, (xmlChar *) "node_id"),\
|
||||
(xmlChar *)"?! xmlGetProp is deprecated !",\
|
||||
(xmlChar *)"");
|
||||
}
|
||||
node = getNextChild(node->next, (xmlChar *)"condition");
|
||||
return 0;
|
||||
}
|
||||
|
||||
long model_get_dim_X (xmlChar *path){
|
||||
xmlNodePtr node = xmlHashLookup(my_table, (xmlChar *)"get_X");
|
||||
if(node){
|
||||
//if (VRB1){
|
||||
printf("get_X > tag <%s> found in hash table\n",\
|
||||
node->name); // node->name = "x"
|
||||
//}
|
||||
return (long)xmlHashLookup(my_table, (xmlChar *)"get_X"); //TODO
|
||||
|
||||
}else{
|
||||
xmlNodePtr node = model_get_node(path); // node = dimension
|
||||
if (node && node->properties) {
|
||||
xmlAttr* attribute = node->properties;
|
||||
while(attribute && attribute->name && attribute->children)
|
||||
{
|
||||
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
|
||||
if (!xmlStrcmp(attribute->name, (xmlChar *)"x")) {
|
||||
xmlHashAddEntry (my_table, (xmlChar *)"get_X", attribute);
|
||||
return (int)strtol((char *)value, NULL, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_dim_Y (xmlChar *path){
|
||||
printf("model_get_dim_Y(%s)\n", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_dim_Z (xmlChar *path){
|
||||
printf("model_get_dim_Z(%s)\n", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_multiplicity (xmlChar *path){
|
||||
xmlNodePtr node = model_get_node(path);
|
||||
if(node) return (int)strtol((char *)xmlNodeGetContent(node), NULL, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_objects_list(xmlChar *path){
|
||||
return xmlGetLineNo(model_get_node(path));
|
||||
}
|
||||
|
||||
xmlChar *model_get_states_list(xmlChar *path){
|
||||
xmlNodePtr node = model_get_node(path);
|
||||
return (xmlChar *)xmlNodeGetContent(node);
|
||||
}
|
||||
|
||||
long model_get_state_arrows_count(xmlChar *path, long n){
|
||||
// Ici, je sais que c'est le 'PREMIER' état qui est demandé car n == 0
|
||||
char compl[] = "/state/arrow";
|
||||
char *new_p = calloc(1, (sizeof(compl) + strlen((char *)path)) * sizeof(char));
|
||||
strcat(new_p, (char *)path);
|
||||
strcat(new_p + strlen((char *)path), compl); // printf("%s\n", new_p);
|
||||
|
||||
xmlNodePtr node = model_get_node((xmlChar *)new_p);
|
||||
long size = model_get_state_arrows_nb(node);
|
||||
int dim = model_get_dim((xmlChar *)"parameters/space-param/dimension");
|
||||
if (VRB0) printf("VRB0 model_get_saved_state(size = %ld)->%p<-attrib dim = %d size = %ld\n",\
|
||||
size, (xmlNodePtr)model_get_node_attrib(node), dim, size * ( 2 + dim ) * sizeof(int));
|
||||
|
||||
int c = 0;
|
||||
int *t = calloc(1, (size * ( 2 + dim ) * sizeof(int)));
|
||||
// int rk = 0;
|
||||
// int *s = calloc(1, ( 2 + dim ) * sizeof(int));
|
||||
|
||||
if(1)
|
||||
{
|
||||
long arrows_nb= 0;
|
||||
if (VRB4) printf ("VRB4 model_get_state_arrows_nb <%s> start at line %ld\n",\
|
||||
node->name, xmlGetLineNo(node));
|
||||
while (node && node->next){
|
||||
if (!xmlStrcmp(node->name, (xmlChar *)"arrow")) ++arrows_nb;
|
||||
if (VRB4 && !xmlStrcmp(node->name, (xmlChar *)"arrow")) printf ("VRB4 %s-%ld ",\
|
||||
(xmlChar *)node->name, arrows_nb);
|
||||
if (node && node->properties) {
|
||||
xmlAttr* attribute = node->properties;
|
||||
while(attribute)
|
||||
{
|
||||
xmlChar* value = xmlNodeListGetString(node->doc, attribute->children, 1);
|
||||
if (VRB4) printf("v=%s ", value);
|
||||
*(t + c) = strtol((char*)value, NULL, 0);
|
||||
++c;
|
||||
xmlFree(value);
|
||||
attribute = attribute->next;
|
||||
}
|
||||
if (VRB4) printf("\n");
|
||||
}
|
||||
node = node->next;
|
||||
}
|
||||
if (VRB4) printf ("VRB4 model_get_state_arrows_nb = %ld end at line %ld\n",\
|
||||
arrows_nb, xmlGetLineNo(node));
|
||||
if (VRB4) for(int k = 0; k < c; k++) printf("%d ", *(t + k)); printf("\n");
|
||||
return arrows_nb;
|
||||
}
|
||||
|
||||
|
||||
return (long) size * ( 2 + dim ) * sizeof(int);
|
||||
}
|
||||
|
||||
int model_get_state(xmlChar *path, xmlChar *state_id){
|
||||
printf("model_get_state (%s -> %s state)\n", path, state_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_conditions_list(xmlChar *path){
|
||||
printf("model_get_conditions_list(%s)\n", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int model_get_transitions_list(xmlChar *path){
|
||||
printf("model_get_transitions_list(%s)\n", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue