From 83dd6c7b7cb30247ae27a92a273820100c0a621d Mon Sep 17 00:00:00 2001 From: AngelaMarchi Date: Tue, 8 Nov 2016 18:37:54 +1030 Subject: [PATCH] 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 --- src/inpfile.c | 13 ++++++++++ src/rules.c | 70 ++++++++++++++++++++++++++++++++++++--------------- src/text.h | 21 ++++++++++------ src/types.h | 3 ++- 4 files changed, 79 insertions(+), 28 deletions(-) diff --git a/src/inpfile.c b/src/inpfile.c index 364e4e6..8f42c14 100755 --- a/src/inpfile.c +++ b/src/inpfile.c @@ -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 */ diff --git a/src/rules.c b/src/rules.c index cb316f3..bfff91f 100755 --- a/src/rules.c +++ b/src/rules.c @@ -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) diff --git a/src/text.h b/src/text.h index f449321..9a64798 100755 --- a/src/text.h +++ b/src/text.h @@ -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" diff --git a/src/types.h b/src/types.h index 7796150..d04b4f9 100755 --- a/src/types.h +++ b/src/types.h @@ -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