VIS

src/ctlp/ctlpCmd.c

Go to the documentation of this file.
00001 
00032 #include "ctlpInt.h"
00033 
00034 static char rcsid[] UNUSED = "$Id: ctlpCmd.c,v 1.14 2005/05/19 02:35:25 awedh Exp $";
00035 
00038 /*---------------------------------------------------------------------------*/
00039 /* Static function prototypes                                                */
00040 /*---------------------------------------------------------------------------*/
00041 
00042 static int CommandCtlpTest(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00043 static int FormulaArrayCountSubformulae(array_t *formulaArray);
00044 static void FormulaVisitUnvisitedSubformulae(Ctlp_Formula_t *formula, int *ptr);
00045 
00049 /*---------------------------------------------------------------------------*/
00050 /* Definition of exported functions                                          */
00051 /*---------------------------------------------------------------------------*/
00061 void
00062 Ctlp_Init(void)
00063 {
00064   Cmd_CommandAdd("_ctlp_test",   CommandCtlpTest,   0);
00065 }
00066 
00067 
00077 void
00078 Ctlp_End(void)
00079 {
00080 }
00081 
00082 
00083 /*---------------------------------------------------------------------------*/
00084 /* Definition of internal functions                                          */
00085 /*---------------------------------------------------------------------------*/
00086 
00098 void
00099 CtlpFormulaSetStatesToNULL(
00100   Ctlp_Formula_t *formula)
00101 {
00102   if(formula!=NIL(Ctlp_Formula_t)) {
00103     formula->states = NIL(mdd_t);
00104     if(formula->type != Ctlp_ID_c) {
00105       CtlpFormulaSetStatesToNULL(formula->left);
00106       CtlpFormulaSetStatesToNULL(formula->right);
00107     }
00108   }
00109 }
00110 
00111 /*---------------------------------------------------------------------------*/
00112 /* Definition of static functions                                            */
00113 /*---------------------------------------------------------------------------*/
00114 
00147 static int
00148 CommandCtlpTest(
00149   Hrc_Manager_t ** hmgr,
00150   int  argc,
00151   char ** argv)
00152 {
00153   int     c;
00154   int      i;
00155   FILE    *fp;
00156   array_t *formulaArray;
00157   array_t *convertedArray;
00158   array_t *existentialConvertedArray;
00159   array_t *forwardExistentialArray = NIL(array_t);
00160   int     forwardTraversal = 0;
00161   
00162   /*
00163    * Parse command line options.
00164    */
00165   util_getopt_reset();
00166   while ((c = util_getopt(argc, argv, "Fh")) != EOF) {
00167     switch(c) {
00168       case 'F':
00169         forwardTraversal = 1;
00170         break;
00171       case 'h':
00172         goto usage;
00173       default:
00174         goto usage;
00175     }
00176   }
00177 
00178   /*
00179    * Open the ctl file.
00180    */
00181   if (argc - util_optind == 0) {
00182     (void) fprintf(vis_stderr, "** ctl error: ctl file not provided\n");
00183     goto usage;
00184   }
00185   else if (argc - util_optind > 1) {
00186     (void) fprintf(vis_stderr, "** ctl error: too many arguments\n");
00187     goto usage;
00188   }
00189 
00190   fp = Cmd_FileOpen(argv[util_optind], "r", NIL(char *), 0);
00191   if (fp == NIL(FILE)) {
00192     (void)fprintf(vis_stderr, "** ctl error: Cannot open the file %s\n", argv[util_optind]);
00193     return 1;
00194   }
00195 
00196   /*
00197    * Parse the formulas in the file.
00198    */
00199   formulaArray = Ctlp_FileParseFormulaArray(fp);
00200   (void) fclose(fp);
00201 
00202   if (formulaArray == NIL(array_t)) {
00203     fflush(vis_stdout);
00204     fflush(vis_stderr);
00205     return 1;
00206   }
00207 
00208   if (forwardTraversal)
00209     forwardExistentialArray =
00210       Ctlp_FormulaArrayConvertToForward(formulaArray, 1, FALSE);
00211 
00212   convertedArray = Ctlp_FormulaArrayConvertToDAG(formulaArray);
00213   array_free(formulaArray);
00214   existentialConvertedArray =
00215       Ctlp_FormulaDAGConvertToExistentialFormDAG(convertedArray);
00216   
00217   /*
00218    * Print each original formula and its corresponding converted formula.
00219    */
00220   for (i = 0; i < array_n(convertedArray); i++) {
00221     Ctlp_Formula_t *formula;
00222 
00223     formula = array_fetch(Ctlp_Formula_t *, convertedArray, i);
00224     (void) fprintf(vis_stdout, "original formula: ");
00225     Ctlp_FormulaPrint(vis_stdout, formula);
00226     (void) fprintf(vis_stdout, "\n");
00227 
00228     formula = array_fetch(Ctlp_Formula_t *, existentialConvertedArray, i);
00229     (void) fprintf(vis_stdout, "=> equivalent existential formula: ");
00230     Ctlp_FormulaPrint(vis_stdout, formula);
00231     (void) fprintf(vis_stdout, "\n");
00232 
00233     if (forwardTraversal) {
00234       formula = array_fetch(Ctlp_Formula_t *, forwardExistentialArray, i);
00235       (void) fprintf(vis_stdout, "=> equivalent forward existential formula: ");
00236       Ctlp_FormulaPrint(vis_stdout, formula);
00237       (void) fprintf(vis_stdout, "\n");
00238     }
00239   }
00240 
00241   (void)fprintf(vis_stdout, "No. of subformulae (including formulae) = %d\n",
00242                 FormulaArrayCountSubformulae(existentialConvertedArray));
00243   if (forwardTraversal) {
00244     (void)fprintf(vis_stdout,
00245                   "No. of forward subformulae (including formulae) = %d\n",
00246                   FormulaArrayCountSubformulae(forwardExistentialArray));
00247   }
00248 
00249 
00250   Ctlp_FormulaArrayFree(convertedArray);
00251   Ctlp_FormulaArrayFree(existentialConvertedArray);
00252   if (forwardTraversal)
00253     Ctlp_FormulaArrayFree(forwardExistentialArray);
00254   
00255   fflush(vis_stdout);
00256   fflush(vis_stderr);
00257 
00258   return 0;             /* normal exit */
00259 
00260 usage:
00261   (void) fprintf(vis_stderr, "usage: _ctlp_test file [-h]\n");
00262   (void) fprintf(vis_stderr, "   -h  print the command usage\n");
00263   return 1;             /* error exit */
00264 }
00265 
00266 
00278 static int
00279 FormulaArrayCountSubformulae(
00280   array_t *formulaArray)
00281 {
00282   int num, i;
00283   Ctlp_Formula_t *formula;
00284   int count = 0;
00285   
00286   num = array_n(formulaArray);
00287   for(i=0; i<num; i++){
00288     formula = array_fetch(Ctlp_Formula_t *, formulaArray, i);
00289     FormulaVisitUnvisitedSubformulae(formula, &count);
00290   }
00291   /* Set the field states to NULL */
00292   for(i=0; i<num; i++){
00293     formula = array_fetch(Ctlp_Formula_t *, formulaArray, i);
00294     CtlpFormulaSetStatesToNULL(formula);
00295   }
00296   return count;
00297 }
00298 
00309 static void
00310 FormulaVisitUnvisitedSubformulae(
00311   Ctlp_Formula_t *formula,
00312   int *ptr)
00313 {
00314   if(formula!=NIL(Ctlp_Formula_t)) {
00315     if(formula->states == NIL(mdd_t)) {
00316       (*ptr)++;
00317       formula->states = (mdd_t *) 1;
00318       if(formula->type != Ctlp_ID_c) {
00319         FormulaVisitUnvisitedSubformulae(formula->left, ptr);
00320         FormulaVisitUnvisitedSubformulae(formula->right, ptr);
00321       }
00322     }
00323   }
00324 }
00325 
00326 
00327   
00328   
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336