VIS

src/amc/amcCmd.c

Go to the documentation of this file.
00001 
00017 #include "amcInt.h"
00018 
00019 static char rcsid[] UNUSED = "$Id: amcCmd.c,v 1.44 2005/05/19 02:36:36 awedh Exp $";
00020 
00021 
00022 /*---------------------------------------------------------------------------*/
00023 /* Constant declarations                                                     */
00024 /*---------------------------------------------------------------------------*/
00025 
00026 /*---------------------------------------------------------------------------*/
00027 /* Structure declarations                                                    */
00028 /*---------------------------------------------------------------------------*/
00029 
00030 /*---------------------------------------------------------------------------*/
00031 /* Type declarations                                                         */
00032 /*---------------------------------------------------------------------------*/
00033 
00034 /*---------------------------------------------------------------------------*/
00035 /* Variable declarations                                                     */
00036 /*---------------------------------------------------------------------------*/
00037 
00038 static jmp_buf timeOutEnv;
00039 
00040 /*---------------------------------------------------------------------------*/
00041 /* Macro declarations                                                        */
00042 /*---------------------------------------------------------------------------*/
00043 
00046 /*---------------------------------------------------------------------------*/
00047 /* Static function prototypes                                                */
00048 /*---------------------------------------------------------------------------*/
00049 
00050 static int CommandAmc(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00051 static void ApproximateModelCheckUsage(void);
00052 static void TimeOutHandle(void);
00053 
00057 /*---------------------------------------------------------------------------*/
00058 /* Definition of exported functions                                          */
00059 /*---------------------------------------------------------------------------*/
00060 
00070 void
00071 Amc_Init(void)
00072 {
00073   Cmd_CommandAdd("approximate_model_check", CommandAmc, 0);
00074 /*  Cmd_CommandAdd("approximate_compute_reach", CommandApproxReach, 0); */
00075 }
00076 
00077 
00087 void
00088 Amc_End(void)
00089 {
00090 }
00091 
00092 
00093 /*---------------------------------------------------------------------------*/
00094 /* Definition of internal functions                                          */
00095 /*---------------------------------------------------------------------------*/
00096 
00097 
00098 /*---------------------------------------------------------------------------*/
00099 /* Definition of static functions                                            */
00100 /*---------------------------------------------------------------------------*/
00101 
00316 static int
00317 CommandAmc(
00318   Hrc_Manager_t ** hmgr,
00319   int  argc,
00320   char ** argv)
00321 {
00322   static int            timeOutPeriod;    /* CPU seconds allowed */
00323   char                  *ctlFileName;     /* Name of file containing formulas */
00324   FILE                  *ctlFile;         /* File to read the formulas from */
00325   Ntk_Network_t         *network;         /* Pointer to the current network */
00326   array_t               *ctlFormulaArray; /* Array of read ctl formulas */
00327   array_t               *ctlNormalForm;   /* Array of normal ctl formulas */
00328   Ctlp_Formula_t        *currentFormula;  /* Formula being verified */
00329   int                   c, i;
00330   Amc_VerbosityLevel    verbosity;        /* Verbosity level */
00331 
00332   long initialTime;
00333   long finalTime;
00334 
00335 
00336   /* Initialize Default Values */
00337   timeOutPeriod   = 0;
00338   ctlFileName     = NIL(char);
00339   verbosity       = Amc_VerbosityNone_c;
00340 
00341   /*
00342    * Parse command line options.
00343    */
00344   util_getopt_reset();
00345   while ((c = util_getopt(argc, argv, "ht:v:")) != EOF) {
00346     switch(c) {
00347       case 'h':
00348         ApproximateModelCheckUsage();
00349         return 1;
00350       case 't':
00351         timeOutPeriod = atoi(util_optarg);
00352         break;
00353       case 'v':
00354         verbosity = (Amc_VerbosityLevel) atoi(util_optarg);
00355         break;
00356       default:
00357         ApproximateModelCheckUsage();
00358         return 1;
00359     }
00360   }
00361 
00362   /* Make sure the CTL file has been provided */
00363   if (argc == util_optind) {
00364     ApproximateModelCheckUsage();
00365     return 1;
00366   }
00367   else {
00368     ctlFileName = argv[util_optind];
00369   }
00370 
00371   /* 
00372    * Obtain the network from the hierarchy manager 
00373    */
00374   network = Ntk_HrcManagerReadCurrentNetwork(*hmgr);
00375   if (network == NIL(Ntk_Network_t)) {
00376     return 1;
00377   }
00378   /*
00379    * Check whether the fairness was read.
00380    */
00381   {
00382     Fsm_Fsm_t      *exactFsm = Fsm_NetworkReadOrCreateFsm(network);
00383     Fsm_Fairness_t *fairCons =  Fsm_FsmReadFairnessConstraint(exactFsm);
00384     int numOfDisjuncts = Fsm_FairnessReadNumDisjuncts(fairCons);
00385     if (numOfDisjuncts > 1) {
00386       /* non-Buchi */
00387       fprintf(vis_stdout, "** amc warning: Fairness Constraint is not supported.\n");
00388     }
00389     else {
00390       int numOfConj     = Fsm_FairnessReadNumConjunctsOfDisjunct(fairCons, 0);
00391       if (numOfConj > 1) {
00392         /* Buchi */
00393         fprintf(vis_stdout, "** amc warning: Fairness Constraint is not supported.\n");
00394       }
00395     }
00396   } 
00397 
00398   /* 
00399    * Read in the array of CTL formulas provided in ctlFileName 
00400    */
00401   ctlFile = Cmd_FileOpen(ctlFileName, "r", NIL(char *), 0);
00402   if (ctlFile == NIL(FILE)) {
00403     return 1;
00404   }
00405   ctlFormulaArray = Ctlp_FileParseFormulaArray(ctlFile);
00406   fclose(ctlFile);
00407 
00408   if (ctlFormulaArray == NIL(array_t)) {
00409     (void) fprintf(vis_stderr, "** amc error: Error while parsing ctl formulas in file.\n");
00410     return 1;
00411   } 
00412 
00413   ctlNormalForm = Ctlp_FormulaArrayConvertToExistentialFormTree(ctlFormulaArray);
00414 
00415   /* 
00416    * Start the timer. 
00417    */
00418   if (timeOutPeriod > 0) {
00419     (void) signal(SIGALRM, (void (*)(int))TimeOutHandle);
00420     (void) alarm(timeOutPeriod);
00421 
00422     /* The second time setjmp is called, it returns here !!*/
00423     if (setjmp(timeOutEnv) > 0) {
00424       (void) fprintf(vis_stdout, "# AMC: timeout ocurred after %d seconds.\n",
00425                      timeOutPeriod);
00426       alarm(0);
00427 
00428       /* Note that there is a huge memory leak here. */
00429       Ctlp_FormulaArrayFree(ctlFormulaArray);
00430       Ctlp_FormulaArrayFree(ctlNormalForm);
00431       return 1;
00432     } 
00433   }
00434 
00435 
00436   /* 
00437    * Loop over every CTL formula in the array 
00438    */
00439   arrayForEachItem(Ctlp_Formula_t *, ctlNormalForm, i, currentFormula) {
00440     boolean validFormula;
00441 
00442     validFormula = FormulaTestIsForallQuantifier(currentFormula); 
00443     if (!validFormula) {
00444       fprintf(vis_stderr, "Formula ");
00445       Ctlp_FormulaPrint(vis_stderr, currentFormula);
00446       fprintf(vis_stderr, " is not a valid ACTL formula. Skipping it\n");
00447     }
00448     else if (!Mc_FormulaStaticSemanticCheckOnNetwork(currentFormula, network,
00449                                                      FALSE)) {
00450       (void) fprintf(vis_stdout, "\n** amc error: Error in parsing Atomic Formula:\n%s\n", error_string());
00451       error_init();
00452       Ctlp_FormulaFree( currentFormula );
00453       continue;
00454     }
00455     else {
00456       error_init();
00457       initialTime = util_cpu_time();
00458       Amc_AmcEvaluateFormula(network, currentFormula, verbosity);
00459       finalTime = util_cpu_time();
00460       if(verbosity == Amc_VerbosityVomit_c) {
00461         (void) fprintf(vis_stdout, "\n%-20s%10ld\n", "analysis time =",
00462                        (finalTime - initialTime) / 1000);
00463       }
00464       fprintf(vis_stdout, "\n");
00465     }
00466 
00467   } /* End of arrayForEachItem in ctlFormulaArray */
00468 
00469 
00470 
00471   /* Deactivate the alarm */
00472   alarm(0);
00473 
00474 
00475   /* Clean up */
00476   Ctlp_FormulaArrayFree(ctlFormulaArray);
00477   Ctlp_FormulaArrayFree(ctlNormalForm);
00478   error_cleanup();
00479 
00480 
00481   /* normal exit */
00482   return 0;
00483 }
00484 
00494 static void
00495 ApproximateModelCheckUsage(void)
00496 {
00497   (void) fprintf(vis_stderr, "usage: approximate_model_check [-h] ");
00498   (void) fprintf(vis_stderr, "[-t secs] [-v] ctlfile\n");
00499   (void) fprintf(vis_stderr, "   -h        print the command usage\n");
00500   (void) fprintf(vis_stderr, "   -t secs   Seconds allowed for computation\n");
00501   (void) fprintf(vis_stderr, "   -v number verbosity level\n");
00502   (void) fprintf(vis_stderr, "   ctlfile   File containing the ctl formulas\n");
00503 
00504   return;
00505 } /* End of ApproximateModelCheckUsage */
00506 
00507 
00508 
00521 boolean
00522 FormulaTestIsForallQuantifier(
00523   Ctlp_Formula_t *formula)
00524 {
00525   Ctlp_Formula_t    *newFormula;
00526   boolean           converted; 
00527   Ctlp_FormulaClass result;
00528 
00529   if (!Ctlp_FormulaTestIsConverted(formula)) {
00530     newFormula = Ctlp_FormulaConvertToExistentialForm(formula);
00531     converted = TRUE;
00532   }
00533   else {
00534     newFormula = formula;
00535     converted = FALSE;
00536   }
00537 
00538   result = Ctlp_CheckClassOfExistentialFormula(newFormula);
00539 
00540   if (converted) {
00541     Ctlp_FormulaFree(newFormula);
00542   } /* End of if */
00543 
00544   return result == Ctlp_ACTL_c || result == Ctlp_QuantifierFree_c;
00545 
00546 } /* End of FormulaTestIsForallQuantifier */
00547 
00548 
00561 static void
00562 TimeOutHandle(void)
00563 {
00564   longjmp(timeOutEnv, 1);
00565 } /* End of TimeOutHandle */