ChangingRuleBased_v2

The code has the following modifications:
- RULE section is written just after controls
- SYST is now written SYSTEM (also PRESSURE, DEMAND, POWER, SETTING,
FILLTIME and DRAINTIME have been changed)
- some description to the function writeRuleinInp has been added
- Actions are now written in the correct order
This commit is contained in:
AngelaMarchi
2016-11-08 18:37:54 +10:30
parent 4e349cd3c1
commit 83dd6c7b7c
4 changed files with 79 additions and 28 deletions

View File

@@ -421,6 +421,17 @@ int saveinpfile(char *fname)
}
}
/*moved after control section */
fprintf(f, "\n\n[RULES]");
for (i=1; i<=Nrules; i++)
{
fprintf(f, "\nRULE %s",Rule[i].label);
errcode = writeRuleinInp(f, i);
fprintf(f, "\n");
}
//fprintf(f, "\n\n");
/* end move after control section */
/* Write [QUALITY] section */
/* (Skip nodes with default quality of 0) */
@@ -624,6 +635,7 @@ int saveinpfile(char *fname)
}
fprintf(f, "\n\n");
/*moved after control section
fprintf(f, "\n\n[RULES]");
for (i=1; i<=Nrules; i++)
{
@@ -632,6 +644,7 @@ int saveinpfile(char *fname)
fprintf(f, "\n");
}
fprintf(f, "\n\n");
end move after control section */
/* Write [COORDINATES] section */

View File

@@ -326,6 +326,8 @@ void newrule()
Rule[Nrules].Fchain = NULL;
Rule[Nrules].priority = 0.0;
Plast = NULL;
Tlast = NULL;
Flast = NULL;
}
@@ -521,13 +523,21 @@ int newaction()
/* Add action to current rule's action list */
if (RuleState == r_THEN)
{
a->next = Rule[Nrules].Tchain;
Rule[Nrules].Tchain = a;
a->next = NULL;
if (Tlast == NULL) Rule[Nrules].Tchain = a;
else Tlast->next = a;
Tlast = a;
//a->next = Rule[Nrules].Tchain;
//Rule[Nrules].Tchain = a;
}
else
{
a->next = Rule[Nrules].Fchain;
Rule[Nrules].Fchain = a;
a->next = NULL;
if (Flast == NULL) Rule[Nrules].Fchain = a;
else Flast->next = a;
Flast = a;
//a->next = Rule[Nrules].Fchain;
//Rule[Nrules].Fchain = a;
}
return(0);
}
@@ -929,20 +939,34 @@ void ruleerrmsg(int err)
int writeRuleinInp(FILE *f, int RuleIdx){
/*
**-----------------------------------------------------------
** This function writes a specific rule (rule ID,
** premises, true and false actions and prioriry in the
** text input file.
** INPUT:
** - FILE *f : pointer to the .inp file to be written
** - int RuleIdx : index of the rule that needs to be written
** OUTPUT: error code
**-----------------------------------------------------------
*/
//int i,j;
struct Premise *p;
struct Action *a;
int hours = 0, minutes = 0, seconds = 0;
//the first condition/premises is different from the others because it starts with IF (but it is kept in memory as AND)
p = Rule[RuleIdx].Pchain;
if (p->value==MISSING)
{
fprintf(f, "\nIF ");
if ((strncmp(Object[p->object], "NODE", 4)==0) || (strncmp(Object[p->object], "Junc", 4)==0) || (strncmp(Object[p->object], "Reser", 5)==0) || (strncmp(Object[p->object], "Tank", 4)==0) )
{
if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
//if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
//else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
if (p->index <= Njuncs) fprintf(f,"JUNCTION %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERVOIR %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
else fprintf(f,"TANK %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
}
else
@@ -974,14 +998,16 @@ int writeRuleinInp(FILE *f, int RuleIdx){
}
else
{
if (p->variable == r_FILLTIME || p->variable == r_DRAINTIME) fprintf(f, "\nIF %s %s %s %s %.4lf", Object[p->object], Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value/3600.0);
if (p->variable == r_FILLTIME || p->variable == r_DRAINTIME) fprintf(f, "\nIF TANK %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value/3600.0);
else
{
fprintf(f, "\nIF ");
if ((strncmp(Object[p->object], "NODE", 4)==0) || (strncmp(Object[p->object], "Junc", 4)==0) || (strncmp(Object[p->object], "Reser", 5)==0) || (strncmp(Object[p->object], "Tank", 4)==0))
{
if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
//if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
//else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
if (p->index <= Njuncs) fprintf(f,"JUNCTION %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERVOIR %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
else fprintf(f,"TANK %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
}
else
@@ -996,15 +1022,17 @@ int writeRuleinInp(FILE *f, int RuleIdx){
}
p = p->next;
while (p != NULL)
while (p != NULL) //for all other premises/conditions write the corresponding logicOperator
{
if (p->value==MISSING)
{
fprintf(f, "\n%s ", Ruleword[p->logop]);
if ((strncmp(Object[p->object], "NODE", 4)==0) || (strncmp(Object[p->object], "Junc", 4)==0) || (strncmp(Object[p->object], "Reser", 5)==0) || (strncmp(Object[p->object], "Tank", 4)==0))
{
if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
//if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
//else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
if (p->index <= Njuncs) fprintf(f,"JUNCTION %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERVOIR %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
else fprintf(f,"TANK %s %s %s %s", Node[p->index].ID, Varword[p->variable], Operator[p->relop], Value[p->status]);
}
else
@@ -1036,13 +1064,15 @@ int writeRuleinInp(FILE *f, int RuleIdx){
}
else
{
if (p->variable == r_FILLTIME || p->variable == r_DRAINTIME) fprintf(f, "\nIF %s %s %s %s %.4lf", Object[p->object], Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value/3600.0);
if (p->variable == r_FILLTIME || p->variable == r_DRAINTIME) fprintf(f, "\n%s TANK %s %s %s %.4lf", Ruleword[p->logop], Object[p->object], Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value/3600.0);
else
{
fprintf(f, "\n%s ", Ruleword[p->logop]);
if ((strncmp(Object[p->object], "NODE", 4)==0) || (strncmp(Object[p->object], "Junc", 4)==0) || (strncmp(Object[p->object], "Reser", 5)==0) || (strncmp(Object[p->object], "Tank", 4)==0)) {
if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
//if (p->index <= Njuncs) fprintf(f,"JUNC %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
//else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERV %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
if (p->index <= Njuncs) fprintf(f,"JUNCTION %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
else if (Tank[p->index-Njuncs].A == 0.0) fprintf(f,"RESERVOIR %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
else fprintf(f,"TANK %s %s %s %.4lf", Node[p->index].ID, Varword[p->variable], Operator[p->relop], p->value);
}
else
@@ -1058,7 +1088,7 @@ int writeRuleinInp(FILE *f, int RuleIdx){
p = p->next;
}
a = Rule[RuleIdx].Tchain;
a = Rule[RuleIdx].Tchain; //The first action in hte list of true actions starts with THEN
if (a->setting==MISSING)
{
if (Link[a->link].Type == PIPE || Link[a->link].Type == CV) fprintf(f, "\nTHEN PIPE %s STATUS IS %s", Link[a->link].ID, Value[a->status]);
@@ -1072,7 +1102,7 @@ int writeRuleinInp(FILE *f, int RuleIdx){
else fprintf(f, "\nTHEN VALVE %s SETTING IS %.4f", Link[a->link].ID, a->setting);
}
a = a->next;
a = a->next; //The other actions in the list of true actions start with AND
while (a != NULL)
{
if (a->setting==MISSING)
@@ -1092,7 +1122,7 @@ int writeRuleinInp(FILE *f, int RuleIdx){
}
a = Rule[RuleIdx].Fchain;
a = Rule[RuleIdx].Fchain; //The first action in the list of false actions starts with ELSE
if (a != NULL)
{
if (a->setting==MISSING)
@@ -1108,7 +1138,7 @@ int writeRuleinInp(FILE *f, int RuleIdx){
else fprintf(f, "\nELSE VALVE %s SETTING IS %.4f", Link[a->link].ID, a->setting);
}
a = a->next;
a = a->next; //The other actions in the list of false actions start with AND
while (a != NULL)
{
if (a->setting==MISSING)

View File

@@ -27,7 +27,8 @@ AUTHOR: L. Rossman
#define w_AGE "AGE"
#define w_TRACE "TRACE"
#define w_SYSTEM "SYST"
//#define w_SYSTEM "SYST"
#define w_SYSTEM "SYSTEM"
#define w_JUNC "Junc"
#define w_RESERV "Reser"
#define w_TANK "Tank"
@@ -94,9 +95,11 @@ AUTHOR: L. Rossman
#define w_METERS "METERS"
#define w_ELEV "ELEV"
#define w_DEMAND "DEMA"
//#define w_DEMAND "DEMA"
#define w_DEMAND "DEMAND"
#define w_HEAD "HEAD"
#define w_PRESSURE "PRES"
//#define w_PRESSURE "PRES"
#define w_PRESSURE "PRESSURE"
#define w_QUALITY "QUAL"
#define w_DIAM "DIAM"
@@ -104,12 +107,16 @@ AUTHOR: L. Rossman
#define w_ROUGHNESS "ROUG"
#define w_VELOCITY "VELO"
#define w_HEADLOSS "HEADL"
#define w_SETTING "SETT"
#define w_POWER "POWE"
//#define w_SETTING "SETT"
#define w_SETTING "SETTING"
//#define w_POWER "POWE"
#define w_POWER "POWER"
#define w_VOLUME "VOLU"
#define w_CLOCKTIME "CLOCKTIME"
#define w_FILLTIME "FILL"
#define w_DRAINTIME "DRAI"
//#define w_FILLTIME "FILL"
#define w_FILLTIME "FILLTIME"
//#define w_DRAINTIME "DRAI"
#define w_DRAINTIME "DRAINTIME"
#define w_GRADE "GRADE"
#define w_LEVEL "LEVEL"

View File

@@ -506,6 +506,7 @@ struct ActItem *ActList; /* Linked list of action items */
int RuleState; /* State of rule interpreter */
long Time1; /* Start of rule evaluation time interval (sec) */
struct Premise *Plast; /* Previous premise clause */
struct Action *Tlast; /*Previous true action */
struct Action *Flast; /*Previous false action */
#endif