aqhome-react: extended reading of network files.

network files can now extend a template network making it much easier
to create new networks based on existing one.
This commit is contained in:
Martin Preuss
2024-04-13 00:03:08 +02:00
parent a94ebeca29
commit a7267c061a

View File

@@ -37,6 +37,8 @@ static AQHREACT_UNIT_NET_LIST *_readUnitNetFiles(AQHOME_REACT *aqh, const GWEN_S
static int _readAllNetworksFromFileIntoList(AQHOME_REACT *aqh, const char *sFilename, AQHREACT_UNIT_NET_LIST *unitNetList); static int _readAllNetworksFromFileIntoList(AQHOME_REACT *aqh, const char *sFilename, AQHREACT_UNIT_NET_LIST *unitNetList);
static GWEN_XMLNODE *_readNetworkFromDatadirIntoXml(AQHOME_REACT *aqh, const char *networkName); static GWEN_XMLNODE *_readNetworkFromDatadirIntoXml(AQHOME_REACT *aqh, const char *networkName);
static AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode); static AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode);
static AQHREACT_UNIT_NET *_readUnitNetWithTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode);
static AQHREACT_UNIT_NET *_readUnitNetNoTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode);
static GWEN_XMLNODE *_readUnitNetFileToXml(AQHOME_REACT *aqh, const char *sFilename); static GWEN_XMLNODE *_readUnitNetFileToXml(AQHOME_REACT *aqh, const char *sFilename);
static void _readNetParamDefsWithList(AQHREACT_PARAM_LIST *paramList, GWEN_XMLNODE *unitNetNode); static void _readNetParamDefsWithList(AQHREACT_PARAM_LIST *paramList, GWEN_XMLNODE *unitNetNode);
static int _readUnits(AQHOME_REACT *aqh, AQHREACT_UNIT_NET *unitNet, GWEN_XMLNODE *unitNetNode, GWEN_DB_NODE *dbNetParams); static int _readUnits(AQHOME_REACT *aqh, AQHREACT_UNIT_NET *unitNet, GWEN_XMLNODE *unitNetNode, GWEN_DB_NODE *dbNetParams);
@@ -236,6 +238,114 @@ int _readAllNetworksFromFileIntoList(AQHOME_REACT *aqh, const char *sFilename, A
AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode) AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode)
{
AQHREACT_UNIT_NET *unitNet;
const char *s;
s=GWEN_XMLNode_GetProperty(unitNetNode, "type", NULL);
if (s && *s)
unitNet=_readUnitNetWithTemplate(aqh, unitNetNode);
else
unitNet=_readUnitNetNoTemplate(aqh, unitNetNode);
if (unitNet==NULL) {
DBG_INFO(NULL, "Error reading unit");
return NULL;
}
return unitNet;
}
AQHREACT_UNIT_NET *_readUnitNetWithTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetXml)
{
AQHREACT_UNIT_NET *unitNet;
GWEN_XMLNODE *templateNetXml;
const char *s;
int rv;
GWEN_DB_NODE *dbNetParams=NULL;
unitNet=AQHREACT_UnitNet_new();
s=GWEN_XMLNode_GetProperty(unitNetXml, "id", NULL);
AQHREACT_UnitNet_SetName(unitNet, s);
s=GWEN_XMLNode_GetProperty(unitNetXml, "type", NULL);
DBG_INFO(NULL, "Loading base network \"%s\"", s);
templateNetXml=_readNetworkFromDatadirIntoXml(aqh, s);
if (templateNetXml==NULL) {
DBG_ERROR(NULL, "Base network \"%s\" not available (error or missing)", s);
AQHREACT_UnitNet_free(unitNet);
return NULL;
}
_readNetParamDefsWithList(AQHREACT_UnitNet_GetParamList(unitNet), templateNetXml);
/* also read netParams from this file (after reading from template) */
rv=_readParamValuesForList(AQHREACT_UnitNet_GetParamList(unitNet), unitNetXml, NULL);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
dbNetParams=GWEN_DB_Group_new("params");
rv=AQHREACT_Param_List_WriteIntoDb(AQHREACT_UnitNet_GetParamList(unitNet), dbNetParams, GWEN_DB_FLAGS_OVERWRITE_VARS);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
/* first read units from this file (if any) */
rv=_readUnits(aqh, unitNet, unitNetXml, dbNetParams);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
/* then read units from template file */
rv=_readUnits(aqh, unitNet, templateNetXml, dbNetParams);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
/* read links from template file */
rv=_readLinks(aqh, unitNet, templateNetXml);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
/* read additional links from this file (if any) */
rv=_readLinks(aqh, unitNet, unitNetXml);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(templateNetXml);
return NULL;
}
GWEN_DB_Group_free(dbNetParams);
GWEN_XMLNode_free(templateNetXml);
return unitNet;
}
AQHREACT_UNIT_NET *_readUnitNetNoTemplate(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetNode)
{ {
AQHREACT_UNIT_NET *unitNet; AQHREACT_UNIT_NET *unitNet;
const char *s; const char *s;
@@ -246,90 +356,35 @@ AQHREACT_UNIT_NET *_readUnitNetFromXml(AQHOME_REACT *aqh, GWEN_XMLNODE *unitNetN
s=GWEN_XMLNode_GetProperty(unitNetNode, "id", NULL); s=GWEN_XMLNode_GetProperty(unitNetNode, "id", NULL);
AQHREACT_UnitNet_SetName(unitNet, s); AQHREACT_UnitNet_SetName(unitNet, s);
s=GWEN_XMLNode_GetProperty(unitNetNode, "type", NULL); /* just directly read network from given XML node (no need to load a template file) */
if (s && *s) { _readNetParamDefsWithList(AQHREACT_UnitNet_GetParamList(unitNet), unitNetNode);
GWEN_XMLNODE *baseNetXml;
/* uses a template file, load that and only set params from non-template file */ dbNetParams=GWEN_DB_Group_new("params");
DBG_INFO(NULL, "Loading base network \"%s\"", s); rv=AQHREACT_Param_List_WriteIntoDb(AQHREACT_UnitNet_GetParamList(unitNet), dbNetParams, GWEN_DB_FLAGS_OVERWRITE_VARS);
baseNetXml=_readNetworkFromDatadirIntoXml(aqh, s); if (rv<0) {
if (baseNetXml==NULL) { DBG_INFO(NULL, "here (%d)", rv);
DBG_ERROR(NULL, "Base network \"%s\" not available (error or missing)", s);
AQHREACT_UnitNet_free(unitNet);
return NULL;
}
_readNetParamDefsWithList(AQHREACT_UnitNet_GetParamList(unitNet), baseNetXml);
/* also read netParams from this file (after reading from template) */
rv=_readParamValuesForList(AQHREACT_UnitNet_GetParamList(unitNet), unitNetNode, NULL);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
return NULL;
}
dbNetParams=GWEN_DB_Group_new("params");
rv=AQHREACT_Param_List_WriteIntoDb(AQHREACT_UnitNet_GetParamList(unitNet), dbNetParams, GWEN_DB_FLAGS_OVERWRITE_VARS);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
return NULL;
}
rv=_readUnits(aqh, unitNet, baseNetXml, dbNetParams);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
return NULL;
}
rv=_readLinks(aqh, unitNet, baseNetXml);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
GWEN_XMLNode_free(baseNetXml);
return NULL;
}
GWEN_DB_Group_free(dbNetParams); GWEN_DB_Group_free(dbNetParams);
GWEN_XMLNode_free(baseNetXml); AQHREACT_UnitNet_free(unitNet);
return NULL;
} }
else {
/* just directly read network from given XML node (no need to load a template file) */
_readNetParamDefsWithList(AQHREACT_UnitNet_GetParamList(unitNet), unitNetNode);
dbNetParams=GWEN_DB_Group_new("params"); rv=_readUnits(aqh, unitNet, unitNetNode, dbNetParams);
rv=AQHREACT_Param_List_WriteIntoDb(AQHREACT_UnitNet_GetParamList(unitNet), dbNetParams, GWEN_DB_FLAGS_OVERWRITE_VARS); if (rv<0) {
if (rv<0) { DBG_INFO(NULL, "here (%d)", rv);
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
return NULL;
}
rv=_readUnits(aqh, unitNet, unitNetNode, dbNetParams);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
return NULL;
}
rv=_readLinks(aqh, unitNet, unitNetNode);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
return NULL;
}
GWEN_DB_Group_free(dbNetParams); GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
return NULL;
} }
rv=_readLinks(aqh, unitNet, unitNetNode);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_DB_Group_free(dbNetParams);
AQHREACT_UnitNet_free(unitNet);
return NULL;
}
GWEN_DB_Group_free(dbNetParams);
return unitNet; return unitNet;
} }