VIS
|
00001 00017 #include "ctlspInt.h" 00018 00021 /*---------------------------------------------------------------------------*/ 00022 /* Static function prototypes */ 00023 /*---------------------------------------------------------------------------*/ 00024 00025 static int CommandCtlspTest(Hrc_Manager_t ** hmgr, int argc, char ** argv); 00026 static int CommandLtlToSNF(Hrc_Manager_t ** hmgr, int argc, char ** argv); 00027 static int FormulaArrayCountSubformulae(array_t *formulaArray); 00028 static void FormulaVisitUnvisitedSubformulae(Ctlsp_Formula_t *formula, int *ptr); 00029 00033 /*---------------------------------------------------------------------------*/ 00034 /* Definition of exported functions */ 00035 /*---------------------------------------------------------------------------*/ 00045 void 00046 Ctlsp_Init(void) 00047 { 00048 Cmd_CommandAdd("_ctlsp_test", CommandCtlspTest, 0); 00049 Cmd_CommandAdd("ltl2snf", CommandLtlToSNF, 0); 00050 } 00051 00052 00062 void 00063 Ctlsp_End(void) 00064 { 00065 } 00066 00067 /*---------------------------------------------------------------------------*/ 00068 /* Definition of internal functions */ 00069 /*---------------------------------------------------------------------------*/ 00070 00071 00083 void 00084 CtlspFormulaSetStatesToNULL( 00085 Ctlsp_Formula_t *formula) 00086 { 00087 if(formula!=NIL(Ctlsp_Formula_t)) { 00088 formula->states = NIL(mdd_t); 00089 if(formula->type != Ctlsp_ID_c) { 00090 CtlspFormulaSetStatesToNULL(formula->left); 00091 CtlspFormulaSetStatesToNULL(formula->right); 00092 } 00093 } 00094 } 00095 00096 00097 /*---------------------------------------------------------------------------*/ 00098 /* Definition of static functions */ 00099 /*---------------------------------------------------------------------------*/ 00100 00134 static int 00135 CommandCtlspTest( 00136 Hrc_Manager_t ** hmgr, 00137 int argc, 00138 char ** argv) 00139 { 00140 int c; 00141 int i; 00142 FILE *fp; 00143 array_t *formulaArray; 00144 array_t *CtlformulaArray; 00145 array_t *LTLformulaArray; 00146 00147 array_t *convertedArray; /* in DAG format */ 00148 00149 /* 00150 * Parse command line options. 00151 */ 00152 util_getopt_reset(); 00153 while ((c = util_getopt(argc, argv, "h")) != EOF) { 00154 switch(c) { 00155 case 'h': 00156 goto usage; 00157 default: 00158 goto usage; 00159 } 00160 } 00161 00162 /* 00163 * Open the ctl file. 00164 */ 00165 if (argc - util_optind == 0) { 00166 (void) fprintf(vis_stderr, "** CTL* error: ctl* file not provided\n"); 00167 goto usage; 00168 } 00169 else if (argc - util_optind > 1) { 00170 (void) fprintf(vis_stderr, "** CTL* error: too many arguments\n"); 00171 goto usage; 00172 } 00173 00174 fp = Cmd_FileOpen(argv[util_optind], "r", NIL(char *), 0); 00175 if (fp == NIL(FILE)) { 00176 (void)fprintf(vis_stderr, "** CTL* error: Cannot open the file %s\n", argv[util_optind]); 00177 return 1; 00178 } 00179 00180 /* 00181 * Parse the CTL* formulas in the file. 00182 */ 00183 formulaArray = Ctlsp_FileParseFormulaArray(fp); 00184 (void) fclose(fp); 00185 00186 if (formulaArray == NIL(array_t)) { 00187 fflush(vis_stdout); 00188 fflush(vis_stderr); 00189 return 1; 00190 } 00191 00192 00193 convertedArray = Ctlsp_FormulaArrayConvertToDAG(formulaArray); 00194 /* array_free(formulaArray); */ 00195 00196 /* 00197 * Print each original formula and its corresponding converted formula. 00198 */ 00199 for (i = 0; i < array_n(convertedArray); i++) { 00200 Ctlsp_Formula_t *formula; 00201 00202 formula = array_fetch(Ctlsp_Formula_t *, convertedArray, i); 00203 (void) fprintf(vis_stdout, "original formula: "); 00204 Ctlsp_FormulaPrint(vis_stdout, formula); 00205 00206 (void) fprintf(vis_stdout, " [CTL* "); 00207 00208 if (Ctlsp_isCtlFormula(formula)){ 00209 (void) fprintf(vis_stdout, ",CTL "); 00210 } 00211 00212 if (Ctlsp_isLtlFormula(formula)){ 00213 (void) fprintf(vis_stdout, ",LTL "); 00214 } 00215 00216 (void) fprintf(vis_stdout, "]\n"); 00217 00218 } 00219 00220 (void)fprintf(vis_stdout, "No. of subformulae (including formulae) = %d\n", 00221 FormulaArrayCountSubformulae(convertedArray)); 00222 00223 /* Converts array of CTL* formula to CTL */ 00224 CtlformulaArray = Ctlsp_FormulaArrayConvertToCTL(formulaArray); 00225 if (CtlformulaArray != NIL(array_t)){ 00226 for (i = 0; i < array_n(CtlformulaArray); i++) { 00227 Ctlp_Formula_t *newFormula; 00228 00229 newFormula = array_fetch(Ctlp_Formula_t *, CtlformulaArray, i); 00230 (void) fprintf(vis_stdout, "CTL formula: "); 00231 Ctlp_FormulaPrint(vis_stdout, newFormula); 00232 (void) fprintf(vis_stdout, "\n"); 00233 00234 } 00235 array_free(CtlformulaArray); 00236 } 00237 00238 LTLformulaArray = Ctlsp_FormulaArrayConvertToLTL(formulaArray); 00239 if (LTLformulaArray != NIL(array_t)){ 00240 for (i = 0; i < array_n(LTLformulaArray); i++) { 00241 Ctlsp_Formula_t *newFormula; 00242 00243 newFormula = array_fetch(Ctlsp_Formula_t *, LTLformulaArray, i); 00244 (void) fprintf(vis_stdout, "LTL formula: "); 00245 Ctlsp_FormulaPrint(vis_stdout, newFormula); 00246 (void) fprintf(vis_stdout, "\n"); 00247 00248 } 00249 array_free(LTLformulaArray); 00250 } else { 00251 (void) fprintf(vis_stdout, "No LTL \n"); 00252 } 00253 00254 array_free(formulaArray); 00255 Ctlsp_FormulaArrayFree(convertedArray); 00256 00257 fflush(vis_stdout); 00258 fflush(vis_stderr); 00259 00260 return 0; /* normal exit */ 00261 00262 usage: 00263 (void) fprintf(vis_stderr, "usage: _ctlsp_test file [-h]\n"); 00264 (void) fprintf(vis_stderr, " -h print the command usage\n"); 00265 return 1; /* error exit */ 00266 } 00267 00268 00302 static int 00303 CommandLtlToSNF( 00304 Hrc_Manager_t ** hmgr, 00305 int argc, 00306 char ** argv) 00307 { 00308 int c, i; 00309 FILE *fp; 00310 array_t *formulaArray; 00311 array_t *LTLformulaArray; 00312 char *ltlFileName = (char *)0; 00313 int negateFormula = 0; 00314 00315 /* 00316 * Parse command line options. 00317 */ 00318 util_getopt_reset(); 00319 while ((c = util_getopt(argc, argv, "hf:n")) != EOF) { 00320 switch(c) { 00321 case 'h': 00322 goto usage; 00323 case 'f': 00324 ltlFileName = util_strsav(util_optarg); 00325 break; 00326 case 'n': 00327 negateFormula = 1; 00328 break; 00329 default: 00330 goto usage; 00331 } 00332 } 00333 if (!ltlFileName){ 00334 (void) fprintf(vis_stderr, "** ltl2snf error: ltl file not provided\n"); 00335 goto usage; 00336 } 00337 fp = Cmd_FileOpen(ltlFileName, "r", NIL(char *), 0); 00338 FREE(ltlFileName); 00339 if (fp == NIL(FILE)) { 00340 (void) fprintf(vis_stdout, "** ltl2snf error: error in opening file %s\n", ltlFileName); 00341 return 1; 00342 } 00343 /* 00344 * Parse the CTL* formulas in the file. 00345 */ 00346 formulaArray = Ctlsp_FileParseFormulaArray(fp); 00347 (void) fclose(fp); 00348 00349 if (formulaArray == NIL(array_t)) { 00350 fflush(vis_stdout); 00351 fflush(vis_stderr); 00352 return 1; 00353 } 00354 00355 LTLformulaArray = Ctlsp_FormulaArrayConvertToLTL(formulaArray); 00356 if (LTLformulaArray != NIL(array_t)){ 00357 for (i = 0; i < array_n(LTLformulaArray); i++) { 00358 Ctlsp_Formula_t *newFormula; 00359 00360 newFormula = array_fetch(Ctlsp_Formula_t *, LTLformulaArray, i); 00361 (void) fprintf(vis_stdout, "LTL formula: "); 00362 Ctlsp_FormulaPrint(vis_stdout, newFormula); 00363 (void) fprintf(vis_stdout, "\n"); 00364 00365 if(negateFormula){ 00366 newFormula = Ctlsp_LtllFormulaNegate(newFormula); 00367 00368 (void) fprintf(vis_stdout, "Negated LTL formula: "); 00369 Ctlsp_FormulaPrint(vis_stdout, newFormula); 00370 (void) fprintf(vis_stdout, "\n"); 00371 } 00372 00373 printf("SNF \n"); 00374 Ctlsp_LtlTranslateIntoSNF(newFormula); 00375 } 00376 array_free(LTLformulaArray); 00377 } else { 00378 (void) fprintf(vis_stdout, "No LTL \n"); 00379 } 00380 00381 array_free(formulaArray); 00382 00383 fflush(vis_stdout); 00384 fflush(vis_stderr); 00385 00386 return 0; /* normal exit */ 00387 00388 usage: 00389 (void) fprintf(vis_stderr, "usage: ltl2snf <-f ltl_file> [-h]\n"); 00390 (void) fprintf(vis_stderr, " -f <ltl_file>\n"); 00391 (void) fprintf(vis_stderr, " -n negate formula\n"); 00392 (void) fprintf(vis_stderr, " -h print the command usage\n"); 00393 return 1; /* error exit */ 00394 } 00395 00396 00408 static int 00409 FormulaArrayCountSubformulae( 00410 array_t *formulaArray) 00411 { 00412 int num, i; 00413 Ctlsp_Formula_t *formula; 00414 int count = 0; 00415 00416 num = array_n(formulaArray); 00417 for(i=0; i<num; i++){ 00418 formula = array_fetch(Ctlsp_Formula_t *, formulaArray, i); 00419 FormulaVisitUnvisitedSubformulae(formula, &count); 00420 } 00421 /* Set the field states to NULL */ 00422 for(i=0; i<num; i++){ 00423 formula = array_fetch(Ctlsp_Formula_t *, formulaArray, i); 00424 CtlspFormulaSetStatesToNULL(formula); 00425 } 00426 return count; 00427 } 00428 00439 static void 00440 FormulaVisitUnvisitedSubformulae( 00441 Ctlsp_Formula_t *formula, 00442 int *ptr) 00443 { 00444 if(formula!=NIL(Ctlsp_Formula_t)) { 00445 if(formula->states == NIL(mdd_t)) { 00446 (*ptr)++; 00447 formula->states = (mdd_t *) 1; 00448 if(formula->type != Ctlsp_ID_c) { 00449 FormulaVisitUnvisitedSubformulae(formula->left, ptr); 00450 FormulaVisitUnvisitedSubformulae(formula->right, ptr); 00451 } 00452 } 00453 } 00454 } 00455 00456 00457 00458 00459 00460 00461 00462 00463 00464 00465 00466 00467 00468 00469 00470 00471 00472 00473 00474