You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2291 lines
65 KiB

2 months ago
  1. /**CFile**********************************************************************
  2. FileName [testdddmp.c]
  3. PackageName [dddmp]
  4. Synopsis [A simple test function for Dddmp package]
  5. Description [This program constitutes a simple test program
  6. for the dddmp library (version 2.0).
  7. A simple interactive command selection allow the users to perform the
  8. main operation on BDDs, ADDs, and CNF, such as loading and storing.
  9. It can work also as a BDD calculators.
  10. ]
  11. Author [Gianpiero Cabodi and Stefano Quer]
  12. Copyright [
  13. Copyright (c) 2004 by Politecnico di Torino.
  14. All Rights Reserved. This software is for educational purposes only.
  15. Permission is given to academic institutions to use, copy, and modify
  16. this software and its documentation provided that this introductory
  17. message is not removed, that this software and its documentation is
  18. used for the institutions' internal research and educational purposes,
  19. and that no monies are exchanged. No guarantee is expressed or implied
  20. by the distribution of this code.
  21. Send bug-reports and/or questions to:
  22. {gianpiero.cabodi,stefano.quer}@polito.it.
  23. ]
  24. ******************************************************************************/
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include "dddmpInt.h"
  28. /*---------------------------------------------------------------------------*/
  29. /* Constant declarations */
  30. /*---------------------------------------------------------------------------*/
  31. #define DDDMPTEST_MAX_FILENAME_LENGTH 256
  32. #define DDDMPTEST_MAX_STRING_LENGTH 80
  33. #define DDDMPTEST_MAX_OPERAND 20
  34. #define DDDMPTEST_MAX_VARIABLE 50
  35. #define DDDMP_MAX_BDDARRAY_LEN 1000
  36. /**Enum************************************************************************
  37. Synopsis [Message type for output messages]
  38. Description [Type supported by the output function to print-out
  39. the proper message.
  40. ]
  41. ******************************************************************************/
  42. typedef enum {
  43. /* Int Messages */
  44. DDDMP_MESSAGE_MANAGER_VAR,
  45. DDDMP_MESSAGE_BDD,
  46. DDDMP_MESSAGE_BDD_ARRAY,
  47. DDDMP_MESSAGE_SOURCE1,
  48. DDDMP_MESSAGE_SOURCE2,
  49. DDDMP_MESSAGE_DESTINATION,
  50. DDDMP_MESSAGE_CUBE,
  51. DDDMP_MESSAGE_INDEX,
  52. DDDMP_MESSAGE_I_ID,
  53. DDDMP_MESSAGE_EDGE_MAX,
  54. DDDMP_MESSAGE_LENGHT_MAX,
  55. DDDMP_MESSAGE_REORDERING,
  56. /* String Messages */
  57. DDDMP_MESSAGE_PROMPT,
  58. DDDMP_MESSAGE_FILE,
  59. DDDMP_MESSAGE_OP,
  60. DDDMP_MESSAGE_FORMAT
  61. } Dddmp_MessageType;
  62. #if !defined(RAND_MAX) && defined(sun) && defined(sparc)
  63. #define RAND_MAX 2147483647
  64. #endif
  65. /*---------------------------------------------------------------------------*/
  66. /* Stucture declarations */
  67. /*---------------------------------------------------------------------------*/
  68. typedef struct dddmpVarInfo {
  69. /*
  70. * Local Information
  71. */
  72. int nDdVars; /* Local Manager Number of Variables */
  73. char **rootNames; /* Root Names */
  74. /*
  75. * Header File Information
  76. */
  77. Dddmp_DecompType ddType;
  78. int nVars; /* File Manager Number of Variables */
  79. int nSuppVars; /* File Structure Number of Variables */
  80. int varNamesFlagUpdate; /* 0 to NOT Update */
  81. char **suppVarNames;
  82. char **orderedVarNames;
  83. int varIdsFlagUpdate; /* 0 to NOT Update */
  84. int *varIds; /* File ids - nSuppVars size */
  85. int *varIdsAll; /* ALL ids - nVars size */
  86. int varComposeIdsFlagUpdate; /* 0 to NOT Update */
  87. int *varComposeIds; /* File permids - nSuppVars size */
  88. int *varComposeIdsAll; /* ALL permids - nVars size */
  89. int varAuxIdsFlagUpdate; /* 0 to NOT Update */
  90. int *varAuxIds; /* File auxids - nSuppVars size */
  91. int *varAuxIdsAll; /* ALL auxids - nVars size */
  92. int nRoots;
  93. } dddmpVarInfo_t;
  94. /*---------------------------------------------------------------------------*/
  95. /* Type declarations */
  96. /*---------------------------------------------------------------------------*/
  97. /*---------------------------------------------------------------------------*/
  98. /* Variable declarations */
  99. /*---------------------------------------------------------------------------*/
  100. Dddmp_RootMatchType rootmatchmode;
  101. Dddmp_VarMatchType varmatchmode;
  102. Dddmp_VarInfoType varoutinfo;
  103. char varname[DDDMPTEST_MAX_STRING_LENGTH];
  104. /*---------------------------------------------------------------------------*/
  105. /* Macro declarations */
  106. /*---------------------------------------------------------------------------*/
  107. /**AutomaticStart*************************************************************/
  108. /*---------------------------------------------------------------------------*/
  109. /* Static function prototypes */
  110. /*---------------------------------------------------------------------------*/
  111. static DdManager *ManagerInit (dddmpVarInfo_t *varInfo);
  112. static void ManagerQuit (DdManager **ddMgr, dddmpVarInfo_t *varInfo);
  113. static int OneCreate(DdManager *ddMgr, DdNode **operandBdd);
  114. static int BddZeroCreate(DdManager *ddMgr, DdNode **operandBdd);
  115. static int LeafCreate(DdManager *ddMgr, DdNode **operandBdd);
  116. static int BddCreate(DdManager *ddMgr, DdNode **operandBdd);
  117. static int A2B(void);
  118. static int B2A(void);
  119. static int HeaderLoadBdd(dddmpVarInfo_t *varInfo);
  120. static int HeaderLoadCnf(dddmpVarInfo_t *varInfo);
  121. static int HeaderWrite(dddmpVarInfo_t *varInfo);
  122. static int Help(void);
  123. static int OrderNamesLoad(dddmpVarInfo_t *varInfo);
  124. static int IntArrayLoad(dddmpVarInfo_t *varInfo, const char *mode);
  125. static int BddLoad(DdManager *ddMgr, DdNode **operandBdd, dddmpVarInfo_t *varInfo);
  126. static int BddArrayLoad(DdManager *ddMgr, DdNode ***operandBddArray, int *operandBddArraySize, dddmpVarInfo_t *varInfo);
  127. static int AddLoad(DdManager *ddMgr, DdNode **operandBdd, dddmpVarInfo_t *varInfo);
  128. static int AddArrayLoad(DdManager *ddMgr, DdNode ***operandBddArray, int *operandBddArraySize, dddmpVarInfo_t *varInfo);
  129. static int BddLoadCnf(DdManager *ddMgr, DdNode **operandBdd, dddmpVarInfo_t *varInfo);
  130. static int BddArrayLoadCnf(DdManager *ddMgr, DdNode ***operandBddArray, int *operandBddArraySize, dddmpVarInfo_t *varInfo);
  131. static int Operation(DdManager *ddMgr, DdNode **operandBdd);
  132. static int BddStore(DdManager *ddMgr, DdNode **operandBdd, dddmpVarInfo_t *varInfo);
  133. static int BddArrayStore(DdManager *ddMgr, DdNode ***operandBddArray, int *operandBddArraySize, dddmpVarInfo_t *varInfo);
  134. static int AddStore(DdManager *ddMgr, DdNode **operandBdd, dddmpVarInfo_t *varInfo);
  135. static int AddArrayStore(DdManager *ddMgr, DdNode ***operandBddArray, int *operandBddArraySize, dddmpVarInfo_t *varInfo);
  136. static int BddStoreCnf(DdManager *ddMgr, DdNode **operandBdd, dddmpVarInfo_t *varInfo);
  137. static int BddArrayStoreCnf(DdManager *ddMgr, DdNode ***operandBddArray, int *operandBddArraySize, dddmpVarInfo_t *varInfo);
  138. static int DynamicReordering(DdManager *ddMgr);
  139. static int SetLoadMatchmode();
  140. static int CompleteInfoStruct(Dddmp_DecompType ddType, int nVars, int nSuppVars, char **suppVarNames, char **orderedVarNames, int *varIds, int *varComposeIds, int *varAuxIds, int nRoots, dddmpVarInfo_t *varInfo);
  141. static void ReadInt(Dddmp_MessageType message, int *i);
  142. static void ReadString(Dddmp_MessageType message, char string[]);
  143. /**AutomaticEnd***************************************************************/
  144. int
  145. main(
  146. int argc,
  147. char **argv
  148. )
  149. {
  150. DdManager *ddMgr = NULL;
  151. DdNode **operandBdd = NULL;
  152. DdNode ***operandBddArray = NULL;
  153. int *operandBddArraySize = NULL;
  154. char *row = NULL;
  155. dddmpVarInfo_t varInfo;
  156. int i;
  157. /*--------------------- Echo command line and arguments -------------------*/
  158. fprintf (stdout, "#");
  159. for (i=0; i<argc; i++) {
  160. fprintf (stdout, "%s Version 2.0.2 (use command help)", argv[i]);
  161. }
  162. fprintf (stdout, "\n");
  163. if (argc>1) {
  164. Help();
  165. }
  166. /*-------------------------- Init Array of BDDs ---------------------------*/
  167. rootmatchmode = DDDMP_ROOT_MATCHLIST;
  168. #if 1
  169. varmatchmode = DDDMP_VAR_MATCHIDS;
  170. #else
  171. varmatchmode = DDDMP_VAR_MATCHNAMES;
  172. #endif
  173. varoutinfo = DDDMP_VARIDS;
  174. row = DDDMP_ALLOC (char, DDDMPTEST_MAX_STRING_LENGTH);
  175. Dddmp_CheckAndReturn (row==NULL, "Allocation error.");
  176. operandBdd = DDDMP_ALLOC (DdNode *, DDDMPTEST_MAX_OPERAND);
  177. Dddmp_CheckAndReturn (operandBdd==NULL, "Allocation error.");
  178. operandBddArray = DDDMP_ALLOC (DdNode **, DDDMPTEST_MAX_OPERAND);
  179. Dddmp_CheckAndReturn (operandBddArray==NULL, "Allocation error.");
  180. operandBddArraySize = DDDMP_ALLOC (int, DDDMPTEST_MAX_OPERAND);
  181. Dddmp_CheckAndReturn (operandBddArraySize==NULL, "Allocation error.");
  182. for (i=0; i<DDDMPTEST_MAX_OPERAND; i++) {
  183. operandBdd[i] = NULL;
  184. operandBddArray[i] = NULL;
  185. operandBddArraySize[i] = 0;
  186. }
  187. /*--------------------- Manage command line parameters --------------------*/
  188. while (1) {
  189. ReadString (DDDMP_MESSAGE_PROMPT, row);
  190. if (row[0]=='\n') {
  191. continue;
  192. }
  193. if (strncmp (row, "help", 4)==0) {
  194. Help();
  195. } else if (strncmp (row, "mi", 2)==0) {
  196. ddMgr = ManagerInit (&varInfo);
  197. } else if (strncmp (row, "mq", 2)==0) {
  198. ManagerQuit (&ddMgr, &varInfo);
  199. } else if (strncmp (row, "onl", 3)==0) {
  200. OrderNamesLoad (&varInfo);
  201. } else if (strncmp (row, "oil", 3)==0) {
  202. IntArrayLoad (&varInfo, "oil");
  203. } else if (strncmp (row, "cil", 3)==0) {
  204. IntArrayLoad (&varInfo, "cil");
  205. } else if (strncmp (row, "slm", 3)==0) {
  206. SetLoadMatchmode ();
  207. } else if (strncmp (row, "op", 2)==0) {
  208. Operation (ddMgr, operandBdd);
  209. } else if (strncmp (row, "oc", 2)==0) {
  210. OneCreate (ddMgr, operandBdd);
  211. } else if (strncmp (row, "zc", 2)==0) {
  212. BddZeroCreate (ddMgr, operandBdd);
  213. } else if (strncmp (row, "lc", 2)==0) {
  214. LeafCreate (ddMgr, operandBdd);
  215. } else if (strncmp (row, "bc", 2)==0) {
  216. BddCreate (ddMgr, operandBdd);
  217. } else if (strncmp (row, "a2b", 3)==0) {
  218. A2B ();
  219. } else if (strncmp (row, "b2a", 3)==0) {
  220. B2A ();
  221. } else if (strncmp (row, "hlb", 3)==0) {
  222. HeaderLoadBdd (&varInfo);
  223. } else if (strncmp (row, "hlc", 3)==0) {
  224. HeaderLoadCnf (&varInfo);
  225. } else if (strncmp (row, "bl", 3)==0) {
  226. BddLoad (ddMgr, operandBdd, &varInfo);
  227. } else if (strncmp (row, "bal", 3)==0) {
  228. BddArrayLoad (ddMgr, operandBddArray, operandBddArraySize, &varInfo);
  229. } else if (strncmp (row, "al", 2)==0) {
  230. AddLoad (ddMgr, operandBdd, &varInfo);
  231. } else if (strncmp (row, "aal", 3)==0) {
  232. AddArrayLoad (ddMgr, operandBddArray, operandBddArraySize, &varInfo);
  233. } else if (strncmp (row, "cl", 2)==0) {
  234. BddLoadCnf (ddMgr, operandBdd, &varInfo);
  235. } else if (strncmp (row, "cal", 3)==0) {
  236. BddArrayLoadCnf (ddMgr, operandBddArray, operandBddArraySize, &varInfo);
  237. } else if (strncmp (row, "hw", 2)==0) {
  238. HeaderWrite (&varInfo);
  239. } else if (strncmp (row, "bs", 2)==0) {
  240. BddStore (ddMgr, operandBdd, &varInfo);
  241. } else if (strncmp (row, "bas", 3)==0) {
  242. BddArrayStore (ddMgr, operandBddArray, operandBddArraySize, &varInfo);
  243. } else if (strncmp (row, "as", 2)==0) {
  244. AddStore (ddMgr, operandBdd, &varInfo);
  245. } else if (strncmp (row, "aas", 2)==0) {
  246. AddArrayStore (ddMgr, operandBddArray, operandBddArraySize, &varInfo);
  247. } else if (strncmp (row, "cs", 2)==0) {
  248. BddStoreCnf (ddMgr, operandBdd, &varInfo);
  249. } else if (strncmp (row, "cas", 2)==0) {
  250. BddArrayStoreCnf (ddMgr, operandBddArray, operandBddArraySize, &varInfo);
  251. } else if (strncmp (row, "dr", 2)==0) {
  252. DynamicReordering (ddMgr);
  253. } else if (strncmp (row, "quit", 4)==0) {
  254. break;
  255. } else {
  256. fprintf (stderr, "Command not found: %s\n", row);
  257. }
  258. }
  259. /*-------------------------------- Free Memory ----------------------------*/
  260. ManagerQuit (&ddMgr, &varInfo);
  261. DDDMP_FREE (row);
  262. DDDMP_FREE (operandBdd);
  263. for (i=0; i<DDDMPTEST_MAX_OPERAND; i++) {
  264. if (operandBddArray[i] != NULL) {
  265. DDDMP_FREE (operandBddArray[i]);
  266. }
  267. }
  268. DDDMP_FREE (operandBddArray);
  269. DDDMP_FREE (operandBddArraySize);
  270. fprintf (stdout, "End of test.\n");
  271. return (DDDMP_SUCCESS);
  272. }
  273. /*---------------------------------------------------------------------------*/
  274. /* Definition of internal functions */
  275. /*---------------------------------------------------------------------------*/
  276. /*---------------------------------------------------------------------------*/
  277. /* Definition of static functions */
  278. /*---------------------------------------------------------------------------*/
  279. /**Function********************************************************************
  280. Synopsis [Create a CUDD Manager with nVars variables.]
  281. Description [Create a CUDD Manager with nVars variables.]
  282. SideEffects []
  283. SeeAlso []
  284. ******************************************************************************/
  285. static DdManager *
  286. ManagerInit (
  287. dddmpVarInfo_t *varInfo
  288. )
  289. {
  290. DdManager *ddMgr = NULL;
  291. int nVars;
  292. ReadInt (DDDMP_MESSAGE_MANAGER_VAR, &nVars);
  293. /*----------------------- Init Var Information Structure ------------------*/
  294. varInfo->nDdVars = nVars;
  295. varInfo->rootNames = NULL;
  296. varInfo->ddType = DDDMP_NONE;
  297. varInfo->nVars = (-1);
  298. varInfo->nSuppVars = (-1);
  299. varInfo->varNamesFlagUpdate = 1;
  300. varInfo->suppVarNames = NULL;
  301. varInfo->orderedVarNames = NULL;
  302. varInfo->varIdsFlagUpdate = 1;
  303. varInfo->varIds = NULL;
  304. varInfo->varIdsAll = NULL;
  305. varInfo->varComposeIdsFlagUpdate = 1;
  306. varInfo->varComposeIds = NULL;
  307. varInfo->varComposeIdsAll = NULL;
  308. varInfo->varAuxIdsFlagUpdate = 1;
  309. varInfo->varAuxIds = NULL;
  310. varInfo->varAuxIdsAll = NULL;
  311. varInfo->nRoots = (-1);
  312. /*------------------------------ Init DD Manager --------------------------*/
  313. ddMgr = Cudd_Init (nVars, 0, CUDD_UNIQUE_SLOTS,
  314. CUDD_CACHE_SLOTS, 0);
  315. Dddmp_CheckAndReturn (ddMgr==NULL, "DdManager NOT inizializated.");
  316. return (ddMgr);
  317. }
  318. /**Function********************************************************************
  319. Synopsis [Quit a CUDD Manager.]
  320. Description [Quit a CUDD Manager.]
  321. SideEffects []
  322. SeeAlso []
  323. ******************************************************************************/
  324. static void
  325. ManagerQuit (
  326. DdManager **ddMgrPtr /* IN: CUDD Manager */,
  327. dddmpVarInfo_t *varInfo /* IN: Internal Manager */
  328. )
  329. {
  330. if (*ddMgrPtr == NULL) {
  331. return;
  332. }
  333. fprintf (stdout, "Quitting CUDD Manager.\n");
  334. Cudd_Quit (*ddMgrPtr);
  335. *ddMgrPtr = NULL;
  336. DddmpStrArrayFree (varInfo->rootNames, varInfo->nRoots);
  337. DddmpStrArrayFree (varInfo->suppVarNames, varInfo->nSuppVars);
  338. DddmpStrArrayFree (varInfo->orderedVarNames, varInfo->nVars);
  339. DDDMP_FREE (varInfo->varIds);
  340. DDDMP_FREE (varInfo->varIdsAll);
  341. DDDMP_FREE (varInfo->varComposeIds);
  342. DDDMP_FREE (varInfo->varComposeIdsAll);
  343. DDDMP_FREE (varInfo->varAuxIds);
  344. DDDMP_FREE (varInfo->varAuxIdsAll);
  345. varInfo->nDdVars = (-1);
  346. varInfo->rootNames = NULL;
  347. varInfo->ddType = DDDMP_NONE;
  348. varInfo->nVars = (-1);
  349. varInfo->nSuppVars = (-1);
  350. varInfo->varNamesFlagUpdate = 1;
  351. varInfo->suppVarNames = NULL;
  352. varInfo->orderedVarNames = NULL;
  353. varInfo->varIdsFlagUpdate = 1;
  354. varInfo->varIds = NULL;
  355. varInfo->varIdsAll = NULL;
  356. varInfo->varComposeIdsFlagUpdate = 1;
  357. varInfo->varComposeIds = NULL;
  358. varInfo->varComposeIdsAll = NULL;
  359. varInfo->varAuxIdsFlagUpdate = 1;
  360. varInfo->varAuxIds = NULL;
  361. varInfo->varAuxIdsAll = NULL;
  362. varInfo->nRoots = (-1);
  363. return;
  364. }
  365. /**Function********************************************************************
  366. Synopsis [Create a One-BDD Leaf.]
  367. Description [Create a One-BDD Leaf.]
  368. SideEffects []
  369. SeeAlso []
  370. ******************************************************************************/
  371. static int
  372. OneCreate(
  373. DdManager *ddMgr /* IN: CUDD Manager */,
  374. DdNode **operandBdd /* In/OUT: Array of operand */
  375. )
  376. {
  377. int i;
  378. /*------------------------ Read Operation Operands ------------------------*/
  379. ReadInt (DDDMP_MESSAGE_BDD, &i);
  380. operandBdd[i] = Cudd_ReadOne (ddMgr);
  381. return (DDDMP_SUCCESS);
  382. }
  383. /**Function********************************************************************
  384. Synopsis [Create a Zero-BDD Leaf.]
  385. Description [Create a Zero-BDD Leaf.]
  386. SideEffects []
  387. SeeAlso []
  388. ******************************************************************************/
  389. static int
  390. BddZeroCreate(
  391. DdManager *ddMgr /* IN: CUDD Manager */,
  392. DdNode **operandBdd /* IN/OUT: array of operand */
  393. )
  394. {
  395. int i;
  396. DdNode *one = NULL;
  397. /*------------------------ Read Operation Operands ------------------------*/
  398. ReadInt (DDDMP_MESSAGE_BDD, &i);
  399. one = Cudd_ReadOne(ddMgr);
  400. operandBdd[i] = Cudd_Not(one);
  401. return (DDDMP_SUCCESS);
  402. }
  403. /**Function********************************************************************
  404. Synopsis [Create a One-Node BDD.]
  405. Description [Create a One-Node BDD.]
  406. SideEffects []
  407. SeeAlso []
  408. ******************************************************************************/
  409. static int
  410. LeafCreate(
  411. DdManager *ddMgr /* IN: CUDD Manager */,
  412. DdNode **operandBdd /* IN/OUT: Array of operandBdd */
  413. )
  414. {
  415. int i, j;
  416. DdNode *f = NULL;
  417. /*------------------------ Read Operation Operands ------------------------*/
  418. ReadInt (DDDMP_MESSAGE_BDD, &i);
  419. ReadInt (DDDMP_MESSAGE_INDEX, &j);
  420. f = Cudd_bddIthVar (ddMgr, j);
  421. Cudd_Ref(f);
  422. operandBdd[i] = f;
  423. return (DDDMP_SUCCESS);
  424. }
  425. /**Function********************************************************************
  426. Synopsis [Create a BDD.]
  427. Description [Create a BDD: Variable index and number of cubes selection.]
  428. SideEffects []
  429. SeeAlso []
  430. ******************************************************************************/
  431. static int
  432. BddCreate (
  433. DdManager *ddMgr /* IN: CUDD Manager */,
  434. DdNode **operandBdd /* array of operandBdd */
  435. )
  436. {
  437. DdNode **vet, *f, *g, *h;
  438. int nb, nv, vi0, vi1, nc, i, j;
  439. char row[DDDMPTEST_MAX_FILENAME_LENGTH];
  440. char *retval;
  441. /*------------------------ Read Operation Operands ------------------------*/
  442. ReadInt (DDDMP_MESSAGE_BDD, &nb);
  443. fprintf (stdout, "Variables Index [n-m] (m-n = number of variables): ");
  444. retval = fgets (row, DDDMPTEST_MAX_STRING_LENGTH, stdin);
  445. if (!retval)
  446. return (DDDMP_FAILURE);
  447. sscanf (row, "%d-%d", &vi0, &vi1);
  448. nv = vi1-vi0+1;
  449. ReadInt (DDDMP_MESSAGE_CUBE, &nc);
  450. /* Leaf Creation */
  451. vet = DDDMP_ALLOC (DdNode *, nv);
  452. for (i=0; i<nv; i++) {
  453. vet[i] = Cudd_bddIthVar (ddMgr, vi0+i);
  454. }
  455. /* Cubes and BDD creation */
  456. f = Cudd_Not (Cudd_ReadOne (ddMgr));
  457. for (i=0; i<nc; i++)
  458. {
  459. g = Cudd_ReadOne (ddMgr);
  460. for (j=0; j<nv; j++)
  461. {
  462. if ( ((float) rand())/((float) RAND_MAX) > 0.5 ) {
  463. h = Cudd_bddAnd (ddMgr, g, vet[j]);
  464. } else {
  465. h = Cudd_bddAnd (ddMgr, g, Cudd_Not (vet[j]));
  466. }
  467. Cudd_Ref (h);
  468. Cudd_RecursiveDeref (ddMgr, g);
  469. g = h;
  470. }
  471. h = Cudd_bddOr (ddMgr, f, g);
  472. Cudd_Ref (h);
  473. Cudd_RecursiveDeref (ddMgr, f);
  474. Cudd_RecursiveDeref (ddMgr, g);
  475. f = h;
  476. }
  477. operandBdd[nb] = f;
  478. return (DDDMP_SUCCESS);
  479. }
  480. /**Function********************************************************************
  481. Synopsis [Transform a BDD from the ASCII to the Binary format].]
  482. Description [Input and Output file selection.]
  483. SideEffects []
  484. SeeAlso []
  485. ******************************************************************************/
  486. static int
  487. A2B(
  488. void
  489. )
  490. {
  491. fprintf (stderr, "Not yet Implemented!!!\n");
  492. return (DDDMP_FAILURE);
  493. }
  494. /**Function********************************************************************
  495. Synopsis [Transform a BDD from the Binary to the ASCII format].]
  496. Description [Input and Output file selection.]
  497. SideEffects []
  498. SeeAlso []
  499. ******************************************************************************/
  500. static int
  501. B2A(
  502. void
  503. )
  504. {
  505. fprintf (stderr, "Not yet Implemented!!!\n");
  506. return (DDDMP_FAILURE);
  507. }
  508. /**Function********************************************************************
  509. Synopsis [Read the Header of a file containing a BDD.]
  510. Description [File name Selection.]
  511. SideEffects []
  512. SeeAlso []
  513. ******************************************************************************/
  514. static int
  515. HeaderLoadBdd (
  516. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  517. )
  518. {
  519. Dddmp_DecompType ddType;
  520. int retValue, nRoots, nVars, nSuppVars;
  521. int *tmpVarIds = NULL;
  522. int *tmpVarAuxIds = NULL;
  523. int *tmpVarComposeIds = NULL;
  524. char **tmpOrderedVarNames = NULL;
  525. char **tmpSuppVarNames = NULL;
  526. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  527. /*------------------------ Read Operation Operands ------------------------*/
  528. ReadString (DDDMP_MESSAGE_FILE, fileName);
  529. retValue = Dddmp_cuddHeaderLoad (&ddType, &nVars, &nSuppVars,
  530. &tmpSuppVarNames, &tmpOrderedVarNames, &tmpVarIds, &tmpVarComposeIds,
  531. &tmpVarAuxIds, &nRoots, fileName, NULL);
  532. if (retValue == DDDMP_FAILURE) {
  533. return (DDDMP_FAILURE);
  534. }
  535. /*---------------------------- Tail Operations ----------------------------*/
  536. CompleteInfoStruct (ddType, nVars, nSuppVars,
  537. tmpSuppVarNames, tmpOrderedVarNames, tmpVarIds, tmpVarComposeIds,
  538. tmpVarAuxIds, nRoots, varInfo);
  539. return (DDDMP_SUCCESS);
  540. }
  541. /**Function********************************************************************
  542. Synopsis [Read the Header of a file containing a CNF formula.]
  543. Description [File name Selection.]
  544. SideEffects []
  545. SeeAlso []
  546. ******************************************************************************/
  547. static int
  548. HeaderLoadCnf (
  549. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  550. )
  551. {
  552. int retValue, nRoots, nVars, nSuppVars;
  553. int *tmpVarIds = NULL;
  554. int *tmpVarComposeIds = NULL;
  555. int *tmpVarAuxIds = NULL;
  556. char **tmpOrderedVarNames = NULL;
  557. char **tmpSuppVarNames = NULL;
  558. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  559. /*------------------------ Read Operation Operands ------------------------*/
  560. ReadString (DDDMP_MESSAGE_FILE, fileName);
  561. retValue = Dddmp_cuddHeaderLoadCnf (&nVars, &nSuppVars,
  562. &tmpSuppVarNames, &tmpOrderedVarNames, &tmpVarIds, &tmpVarComposeIds,
  563. &tmpVarAuxIds, &nRoots, fileName, NULL);
  564. if (retValue == DDDMP_FAILURE) {
  565. return (DDDMP_FAILURE);
  566. }
  567. /*---------------------------- Tail Operations ----------------------------*/
  568. CompleteInfoStruct (DDDMP_CNF, nVars, nSuppVars,
  569. tmpSuppVarNames, tmpOrderedVarNames, tmpVarIds, tmpVarComposeIds,
  570. tmpVarAuxIds, nRoots, varInfo);
  571. return (DDDMP_SUCCESS);
  572. }
  573. /**Function********************************************************************
  574. Synopsis [Read the Header of a filke containing a BDD.]
  575. Description [File name Selection.]
  576. SideEffects []
  577. SeeAlso []
  578. ******************************************************************************/
  579. static int
  580. HeaderWrite(
  581. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  582. )
  583. {
  584. int i;
  585. switch (varInfo->ddType) {
  586. case DDDMP_BDD:
  587. fprintf (stdout, "DD TYPE: DDDMP_BDD\n");
  588. break;
  589. case DDDMP_ADD:
  590. fprintf (stdout, "DD TYPE: DDDMP_ADD\n");
  591. break;
  592. case DDDMP_CNF:
  593. fprintf (stdout, "DD TYPE: DDDMP_CNF\n");
  594. break;
  595. case DDDMP_NONE:
  596. fprintf (stdout, "DD TYPE: NONE - Error\n");
  597. break;
  598. }
  599. fprintf (stdout, "Number of variables: %d\n", varInfo->nVars);
  600. fprintf (stdout, "Number of support variables: %d\n", varInfo->nSuppVars);
  601. if (varInfo->suppVarNames != NULL) {
  602. fprintf (stdout, "suppVarNames: ");
  603. for (i=0; i<varInfo->nSuppVars; i++) {
  604. if (varInfo->suppVarNames[i] != NULL) {
  605. fprintf (stdout, "%s ", varInfo->suppVarNames[i]);
  606. }
  607. }
  608. fprintf (stdout, "\n");
  609. }
  610. if (varInfo->orderedVarNames != NULL) {
  611. fprintf (stdout, "orderedVarNames: ");
  612. for (i=0; i<varInfo->nVars; i++) {
  613. if (varInfo->orderedVarNames[i] != NULL) {
  614. fprintf (stdout, "%s ", varInfo->orderedVarNames[i]);
  615. }
  616. }
  617. fprintf (stdout, "\n");
  618. }
  619. if (varInfo->varIds != NULL) {
  620. fprintf (stdout, "varIds: ");
  621. for (i=0; i<varInfo->nSuppVars; i++) {
  622. fprintf (stdout, "%d ", varInfo->varIds[i]);
  623. }
  624. fprintf (stdout, "\n");
  625. }
  626. if (varInfo->varIdsAll != NULL) {
  627. fprintf (stdout, "varIds for ALL Manager Variables: ");
  628. for (i=0; i<varInfo->nVars; i++) {
  629. fprintf (stdout, "%d ", varInfo->varIdsAll[i]);
  630. }
  631. fprintf (stdout, "\n");
  632. }
  633. if (varInfo->varComposeIds != NULL) {
  634. fprintf (stdout, "varComposeIds: ");
  635. for (i=0; i<varInfo->nSuppVars; i++) {
  636. fprintf (stdout, "%d ", varInfo->varComposeIds[i]);
  637. }
  638. fprintf (stdout, "\n");
  639. }
  640. if (varInfo->varComposeIdsAll != NULL) {
  641. fprintf (stdout, "varComposeIds for ALL Manager Variables: ");
  642. for (i=0; i<varInfo->nVars; i++) {
  643. fprintf (stdout, "%d ", varInfo->varComposeIdsAll[i]);
  644. }
  645. fprintf (stdout, "\n");
  646. }
  647. if (varInfo->varAuxIds != NULL) {
  648. fprintf (stdout, "varAuxIds: ");
  649. for (i=0; i<varInfo->nSuppVars; i++) {
  650. fprintf (stdout, "%d ", varInfo->varAuxIds[i]);
  651. }
  652. fprintf (stdout, "\n");
  653. }
  654. if (varInfo->varAuxIdsAll != NULL) {
  655. fprintf (stdout, "varAuxIds for ALL Manager Variables: ");
  656. for (i=0; i<varInfo->nVars; i++) {
  657. fprintf (stdout, "%d ", varInfo->varAuxIdsAll[i]);
  658. }
  659. fprintf (stdout, "\n");
  660. }
  661. fprintf (stdout, "Number of roots: %d\n", varInfo->nRoots);
  662. fflush (stdout);
  663. return (DDDMP_SUCCESS);
  664. }
  665. /**Function********************************************************************
  666. Synopsis [Print the Help messages.]
  667. Description [Print the Help messages.]
  668. SideEffects []
  669. SeeAlso []
  670. ******************************************************************************/
  671. static int
  672. Help(
  673. void
  674. )
  675. {
  676. fprintf (stdout, "Commands:\n");
  677. fprintf (stdout, "MAIN\n");
  678. fprintf (stdout, "\thelp : Print this set of messages.\n");
  679. fprintf (stdout, "\tquit : Quit the test program.\n");
  680. fprintf (stdout, "MANAGER OPERATIONs\n");
  681. fprintf (stdout,
  682. "\thmi : Manager Init (To do BEFORE any BDD/ADD operation).\n");
  683. fprintf (stdout, "\thmq : Manager Quit.\n");
  684. fprintf (stdout, "LOAD\n");
  685. fprintf (stdout, "\thlb : Load the header from a BDD/ADD file.\n");
  686. fprintf (stdout, "\thlc : Load the header from a CNF file.\n");
  687. fprintf (stdout, "\tbl : Load a BDD from a file.\n");
  688. fprintf (stdout, "\tbal : Load an Array-BDD from a file.\n");
  689. fprintf (stdout, "\tal : Load an ADD from a file.\n");
  690. fprintf (stdout, "\taal : Load an Array-ADD from a file.\n");
  691. fprintf (stdout, "\tcl : Load a CNF Formula from a file.\n");
  692. fprintf (stdout, "\tcal : Load an Array of CNF Formulas from a file.\n");
  693. fprintf (stdout, "STORE\n");
  694. fprintf (stdout,
  695. "\thw : (Header) Write variable information on stdout.\n");
  696. fprintf (stdout, "\tbs : Store a BDD into a file.\n");
  697. fprintf (stdout, "\tbas : Store an Array-BDD from a file.\n");
  698. fprintf (stdout, "\tas : Store an ADD into a file.\n");
  699. fprintf (stdout, "\taas : Store an Array-ADD into a file.\n");
  700. fprintf (stdout, "\tcs : Store BDD as a CNF formula.\n");
  701. fprintf (stdout, "\tcas : Store and Array of BDDs as a CNF formula.\n");
  702. fprintf (stdout, "MISC\n");
  703. fprintf (stdout, "\tdr : Activate Dynamic Reordering.\n");
  704. fprintf (stdout, "\tonl : Load the order from a file (varNames).\n");
  705. fprintf (stdout, "\toil : Load the order from a file (varAuxIds).\n");
  706. fprintf (stdout, "\tcil : Load compose IDs from a file.\n");
  707. fprintf (stdout, "\tslm : Set Load matchmode for variables.\n");
  708. fprintf (stdout,
  709. "\top : Operation (or, and, xor, not, =) between BDDs.\n");
  710. fprintf (stdout, "\toc : Create a terminal-one BDD.\n");
  711. fprintf (stdout, "\tzc : Create a terminal-zero BDD.\n");
  712. fprintf (stdout, "\tlc : Create a single variable BDD (1 node).\n");
  713. fprintf (stdout, "\tbc : Create a random BDD.\n");
  714. fprintf (stdout, "NOT YET IMPLEMENTED\n");
  715. fprintf (stdout,
  716. "\ta2b : Convert a file from the ASCII format to the binary one.\n");
  717. fprintf (stdout,
  718. "\tb2a : Convert a file from the binary format to the ASCII one.\n");
  719. fprintf (stdout, "HINT\n");
  720. fprintf (stdout,
  721. " Command 'mi' has to be the first instruction to build:\n");
  722. fprintf (stdout, " a) The CUDD manager.\n");
  723. fprintf (stdout,
  724. " b) The internal manager (containing name and variable IDs).\n");
  725. fprintf (stdout,
  726. " After that load an header file with 'hlb' or 'hlc' to have proper\n");
  727. fprintf (stdout,
  728. " names and ids for each subsequent load/store operation.\n");
  729. return (DDDMP_SUCCESS);
  730. }
  731. /**Function********************************************************************
  732. Synopsis [Load the BDD order from a file (varNames).]
  733. Description [Load the BDD order from a file (varNames).
  734. Force the orderedVarNames field of the varInfo structure,
  735. i.e., the local manager, to be stucked to this array of values.
  736. ]
  737. SideEffects []
  738. SeeAlso []
  739. ******************************************************************************/
  740. static int
  741. OrderNamesLoad(
  742. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  743. )
  744. {
  745. FILE *fp = NULL;
  746. int i;
  747. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  748. char tmpBuf[DDDMPTEST_MAX_STRING_LENGTH];
  749. char tmpName[DDDMPTEST_MAX_STRING_LENGTH];
  750. char **tmpOrderedVarNames = NULL;
  751. /*------------------------- Red New Var Names Array ----------------------*/
  752. ReadString (DDDMP_MESSAGE_FILE, fileName);
  753. fp = fopen (fileName, "r");
  754. Dddmp_CheckAndReturn (fp==NULL, "Cannot open file.");
  755. varoutinfo = DDDMP_VARNAMES;
  756. tmpOrderedVarNames = DDDMP_ALLOC (char *, varInfo->nDdVars);
  757. i=0;
  758. while (fgets (tmpBuf, DDDMPTEST_MAX_STRING_LENGTH, fp)!=NULL) {
  759. if (tmpBuf[0]=='#') {
  760. continue;
  761. }
  762. if (i>=varInfo->nDdVars) {
  763. fprintf (stdout,
  764. "Number of variables in files higher than DD manager vars (%d)\n",
  765. varInfo->nDdVars);
  766. fprintf (stdout, "Exceeding variables ignored\n");
  767. fprintf (stdout,
  768. "You might increase the DDDMPTEST_MAX_VARIABLE constant\n");
  769. break;
  770. }
  771. sscanf (tmpBuf, "%s", tmpName);
  772. tmpOrderedVarNames[i] = DDDMP_ALLOC (char, (strlen (tmpName) + 1));
  773. if (tmpOrderedVarNames[i]==NULL) {
  774. fprintf (stdout, "Error allocating memory\n");
  775. } else {
  776. strcpy (tmpOrderedVarNames[i], tmpName);
  777. }
  778. i++;
  779. }
  780. for ( ;i<varInfo->nDdVars; i++) {
  781. tmpOrderedVarNames[i] = NULL;
  782. }
  783. fclose(fp);
  784. /*----------------------- Free and Set Var Names Array --------------------*/
  785. DddmpStrArrayFree (varInfo->orderedVarNames, varInfo->nVars);
  786. varInfo->orderedVarNames = tmpOrderedVarNames;
  787. varInfo->nVars = varInfo->nDdVars;
  788. /* DO NOT ALLOW FURTHER UPDATES */
  789. varInfo->varNamesFlagUpdate = 0;
  790. return (DDDMP_SUCCESS);
  791. }
  792. /**Function********************************************************************
  793. Synopsis [Load the BDD order from a file (varauxids).]
  794. Description [Load the BDD order from a file (varauxids).
  795. Force the
  796. varAuxIds and varAuxIdsAll
  797. or the
  798. varComposeIds and varComposeIdsAll
  799. fields of the varInfo structure, i.e., the local manager, to be
  800. stucked to this array of values.
  801. ]
  802. SideEffects []
  803. SeeAlso []
  804. ******************************************************************************/
  805. static int
  806. IntArrayLoad (
  807. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */,
  808. const char *mode
  809. )
  810. {
  811. FILE *fp = NULL;
  812. int i;
  813. int *tmpArray1 = NULL;
  814. int *tmpArray2 = NULL;
  815. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  816. char buf[DDDMPTEST_MAX_STRING_LENGTH];
  817. ReadString (DDDMP_MESSAGE_FILE, fileName);
  818. fp = fopen(fileName, "r");
  819. Dddmp_CheckAndReturn (fp==NULL, "Cannot open file.");
  820. tmpArray1 = DDDMP_ALLOC (int, varInfo->nDdVars);
  821. tmpArray2 = DDDMP_ALLOC (int, varInfo->nDdVars);
  822. Dddmp_CheckAndReturn (tmpArray1==NULL, "Error allocating memory.");
  823. Dddmp_CheckAndReturn (tmpArray2==NULL, "Error allocating memory.");
  824. i=0;
  825. while (fgets(buf, DDDMPTEST_MAX_STRING_LENGTH, fp)!=NULL) {
  826. if (buf[0]=='#') {
  827. continue;
  828. }
  829. if (i>=varInfo->nDdVars) {
  830. fprintf (stdout,
  831. "Number of variables in files higher than DD manager vars (%d)\n",
  832. varInfo->nDdVars);
  833. fprintf (stdout, "Exceeding variables ignored.\n");
  834. fprintf (stdout, "(Increase the DDDMPTEST_MAX_VARIABLE constant.)\n");
  835. break;
  836. }
  837. sscanf(buf, "%d", &tmpArray1[i]);
  838. sscanf(buf, "%d", &tmpArray2[i++]);
  839. }
  840. for (;i<varInfo->nDdVars;i++) {
  841. tmpArray1[i]= -1;
  842. tmpArray2[i]= -1;
  843. }
  844. fclose(fp);
  845. if (strcmp (mode, "oil") == 0) {
  846. varInfo->varAuxIds = tmpArray1;
  847. varInfo->varAuxIdsAll = tmpArray2;
  848. /* DO NOT ALLOW FURTHER UPDATES */
  849. varInfo->varAuxIdsFlagUpdate = 0;
  850. } else {
  851. if (strcmp (mode, "cil") == 0) {
  852. varInfo->varComposeIds = tmpArray1;
  853. varInfo->varComposeIdsAll = tmpArray2;
  854. /* DO NOT ALLOW FURTHER UPDATES */
  855. varInfo->varComposeIdsFlagUpdate = 0;
  856. }
  857. }
  858. varInfo->nVars = varInfo->nDdVars;
  859. varInfo->nSuppVars = varInfo->nDdVars;
  860. return (DDDMP_SUCCESS);
  861. }
  862. /**Function********************************************************************
  863. Synopsis [Load a BDD from a file.]
  864. Description [Load a BDD from a file.]
  865. SideEffects []
  866. SeeAlso []
  867. ******************************************************************************/
  868. static int
  869. BddLoad (
  870. DdManager *ddMgr /* IN: CUDD Manager */,
  871. DdNode **operandBdd /* IN: Operand BDD */,
  872. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  873. )
  874. {
  875. DdNode *f = NULL;
  876. int i;
  877. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  878. /*------------------------ Read Operation Operands ------------------------*/
  879. ReadString (DDDMP_MESSAGE_FILE, fileName);
  880. ReadInt (DDDMP_MESSAGE_BDD, &i);
  881. /*-------------------------------- Load BDD -------------------------------*/
  882. fprintf (stdout, "Loading %s ...\n", fileName);
  883. f = Dddmp_cuddBddLoad (ddMgr, varmatchmode, varInfo->orderedVarNames,
  884. varInfo->varIdsAll, varInfo->varComposeIdsAll, DDDMP_MODE_DEFAULT,
  885. fileName, NULL);
  886. if (f==NULL) {
  887. fprintf (stderr, "Dddmp Test Error : %s is not loaded from file\n",
  888. fileName);
  889. } else {
  890. operandBdd[i] = f;
  891. }
  892. /*---------------------------- Tail Operations ----------------------------*/
  893. return (DDDMP_SUCCESS);
  894. }
  895. /**Function********************************************************************
  896. Synopsis [Load an array of BDDs from a file.]
  897. Description [Load an array of BDDs from a file.]
  898. SideEffects []
  899. SeeAlso []
  900. ******************************************************************************/
  901. static int
  902. BddArrayLoad(
  903. DdManager *ddMgr /* IN: CUDD Manager */,
  904. DdNode ***operandBddArray /* IN: Array of operand BDD */,
  905. int *operandBddArraySize /* IN: Number of ADD in the Array */,
  906. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  907. )
  908. {
  909. DdNode **bddArray = NULL;
  910. int i, j, nRoots;
  911. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  912. /*------------------------ Read Operation Operands ------------------------*/
  913. ReadString (DDDMP_MESSAGE_FILE, fileName);
  914. ReadInt (DDDMP_MESSAGE_BDD_ARRAY, &i);
  915. /*---------------------------- Load BDDs ----------------------------------*/
  916. nRoots = Dddmp_cuddBddArrayLoad (ddMgr, rootmatchmode,
  917. varInfo->rootNames, varmatchmode,
  918. varInfo->orderedVarNames, varInfo->varIdsAll, varInfo->varComposeIdsAll,
  919. DDDMP_MODE_DEFAULT, fileName, NULL, &bddArray);
  920. Dddmp_CheckAndReturn (nRoots>DDDMP_MAX_BDDARRAY_LEN,
  921. "DDDMP_MAX_BDDARRAY_LEN exceeded by BDD array len (increase it).");
  922. if (nRoots<=0) {
  923. return (DDDMP_FAILURE);
  924. }
  925. varInfo->nRoots = nRoots;
  926. operandBddArray[i] = DDDMP_ALLOC (DdNode *, nRoots);
  927. Dddmp_CheckAndReturn (operandBddArray[i]==NULL, "Allocation error.");
  928. for (j=0; j<nRoots; j++) {
  929. operandBddArray[i][j] = bddArray[j];
  930. }
  931. operandBddArraySize[i] = nRoots;
  932. /*---------------------------- Tail Operations ----------------------------*/
  933. /* free array */
  934. DDDMP_FREE (bddArray);
  935. return (DDDMP_SUCCESS);
  936. }
  937. /**Function********************************************************************
  938. Synopsis [Load an ADD from a file.]
  939. Description [Load an ADD from a file.]
  940. SideEffects []
  941. SeeAlso []
  942. ******************************************************************************/
  943. static int
  944. AddLoad(
  945. DdManager *ddMgr /* IN: CUDD Manager */,
  946. DdNode **operandBdd /* IN: Operand BDD */,
  947. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  948. )
  949. {
  950. DdNode *f = NULL;
  951. int i;
  952. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  953. /*------------------------ Read Operation Operands ------------------------*/
  954. ReadString (DDDMP_MESSAGE_FILE, fileName);
  955. ReadInt (DDDMP_MESSAGE_BDD, &i);
  956. /*-------------------------------- Load ADD -------------------------------*/
  957. fprintf (stdout, "Loading %s ...\n", fileName);
  958. f = Dddmp_cuddAddLoad (ddMgr, varmatchmode, varInfo->orderedVarNames,
  959. varInfo->varIdsAll, varInfo->varComposeIdsAll, DDDMP_MODE_DEFAULT,
  960. fileName, NULL);
  961. if (f==NULL) {
  962. fprintf (stderr, "Dddmp Test Error : %s is not loaded from file\n",
  963. fileName);
  964. } else {
  965. operandBdd[i] = f;
  966. }
  967. /*---------------------------- Tail Operations ----------------------------*/
  968. fprintf (stdout, "Load:\n");
  969. Cudd_PrintMinterm (ddMgr, f);
  970. return (DDDMP_SUCCESS);
  971. }
  972. /**Function********************************************************************
  973. Synopsis [Load an array of ADDs from a file.]
  974. Description [Load an array of ADDs from a file.]
  975. SideEffects []
  976. SeeAlso []
  977. ******************************************************************************/
  978. static int
  979. AddArrayLoad(
  980. DdManager *ddMgr /* IN: CUDD Manager */,
  981. DdNode ***operandBddArray /* IN: Array of operand BDD */,
  982. int *operandBddArraySize /* IN: Number of ADD in the Array */,
  983. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  984. )
  985. {
  986. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  987. int i, j, nRoots;
  988. DdNode **bddArray = NULL;
  989. /*------------------------ Read Operation Operands ------------------------*/
  990. ReadString (DDDMP_MESSAGE_FILE, fileName);
  991. ReadInt (DDDMP_MESSAGE_BDD_ARRAY, &i);
  992. /*------------------------------- Load ADDs -------------------------------*/
  993. nRoots = Dddmp_cuddAddArrayLoad (ddMgr, rootmatchmode,
  994. varInfo->rootNames, varmatchmode,
  995. varInfo->orderedVarNames, varInfo->varIdsAll, varInfo->varComposeIdsAll,
  996. DDDMP_MODE_DEFAULT, fileName, NULL, &bddArray);
  997. Dddmp_CheckAndReturn (nRoots>DDDMP_MAX_BDDARRAY_LEN,
  998. "DDDMP_MAX_BDDARRAY_LEN exceeded by BDD array len (increase it).");
  999. if (nRoots<=0) {
  1000. return (DDDMP_FAILURE);
  1001. }
  1002. varInfo->nRoots = nRoots;
  1003. operandBddArray[i] = DDDMP_ALLOC (DdNode *, nRoots);
  1004. Dddmp_CheckAndReturn (operandBddArray[i]==NULL, "Allocation error.");
  1005. for (j=0; j<nRoots; j++) {
  1006. operandBddArray[i][j] = bddArray[j];
  1007. }
  1008. operandBddArraySize[i] = nRoots;
  1009. /*---------------------------- Tail Operations ----------------------------*/
  1010. /* Free array */
  1011. DDDMP_FREE (bddArray);
  1012. return (DDDMP_SUCCESS);
  1013. }
  1014. /**Function********************************************************************
  1015. Synopsis [Load a CNF formula from a file, and create a BDD.]
  1016. Description [Load a CNF formula from a file, and create a BDD.]
  1017. SideEffects []
  1018. SeeAlso []
  1019. ******************************************************************************/
  1020. static int
  1021. BddLoadCnf (
  1022. DdManager *ddMgr /* IN: CUDD Manager */,
  1023. DdNode **operandBdd /* IN: Operand BDD */,
  1024. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  1025. )
  1026. {
  1027. DdNode **rootsPtr = NULL;
  1028. Dddmp_DecompCnfLoadType loadingMode = DDDMP_CNF_MODE_CONJ_QUANT;
  1029. int i, retValue, nRoots;
  1030. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1031. /*------------------------ Read Operation Operands ------------------------*/
  1032. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1033. ReadInt (DDDMP_MESSAGE_BDD, &i);
  1034. /*------------------------------- Load BDDs -------------------------------*/
  1035. fprintf (stdout, "Loading %s ...\n", fileName);
  1036. retValue = Dddmp_cuddBddLoadCnf (ddMgr, varmatchmode,
  1037. varInfo->orderedVarNames, varInfo->varAuxIdsAll, varInfo->varComposeIdsAll,
  1038. loadingMode, fileName, NULL, &rootsPtr, &nRoots);
  1039. Dddmp_CheckAndGotoLabel (retValue==DDDMP_FAILURE,
  1040. "Dddmp Test: Load From File Error.\n", failure);
  1041. operandBdd[i] = rootsPtr[0];
  1042. /*---------------------------- Tail Operations ----------------------------*/
  1043. /* Free array */
  1044. DDDMP_FREE (rootsPtr);
  1045. return (DDDMP_SUCCESS);
  1046. failure:
  1047. return(DDDMP_FAILURE);
  1048. }
  1049. /**Function********************************************************************
  1050. Synopsis [Load a CNF formula from a file, and create an array of
  1051. BDDs.
  1052. ]
  1053. Description [Load a CNF formula from a file, and create an array of
  1054. BDDs.
  1055. ]
  1056. SideEffects []
  1057. SeeAlso []
  1058. ******************************************************************************/
  1059. static int
  1060. BddArrayLoadCnf (
  1061. DdManager *ddMgr /* IN: CUDD Manager */,
  1062. DdNode ***operandBddArray /* IN: Array of operand BDD */,
  1063. int *operandBddArraySize /* IN: Number of ADD in the Array */,
  1064. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  1065. )
  1066. {
  1067. DdNode **rootsPtr = NULL;
  1068. Dddmp_DecompCnfLoadType loadingMode = DDDMP_CNF_MODE_CONJ_QUANT;
  1069. int i, j, nRoots;
  1070. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1071. /*------------------------ Read Operation Operands ------------------------*/
  1072. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1073. ReadInt (DDDMP_MESSAGE_BDD_ARRAY, &i);
  1074. /*--------------------------- Loading BDDs --------------------------------*/
  1075. (void) Dddmp_cuddBddArrayLoadCnf (ddMgr, rootmatchmode,
  1076. varInfo->rootNames, varmatchmode,
  1077. varInfo->orderedVarNames, varInfo->varIdsAll, varInfo->varComposeIdsAll,
  1078. loadingMode, fileName, NULL, &rootsPtr, &nRoots);
  1079. Dddmp_CheckAndReturn (nRoots>DDDMP_MAX_BDDARRAY_LEN,
  1080. "DDDMP_MAX_BDDARRAY_LEN exceeded by BDD array len (increase it).");
  1081. if (nRoots<=0) {
  1082. return (DDDMP_FAILURE);
  1083. }
  1084. varInfo->nRoots = nRoots;
  1085. operandBddArray[i] = DDDMP_ALLOC (DdNode *, nRoots);
  1086. Dddmp_CheckAndReturn (operandBddArray[i]==NULL, "Allocation error.");
  1087. for (j=0; j<nRoots; j++) {
  1088. operandBddArray[i][j] = rootsPtr[j];
  1089. }
  1090. operandBddArraySize[i] = nRoots;
  1091. /*---------------------------- Tail Operations ----------------------------*/
  1092. /* Free array */
  1093. DDDMP_FREE (rootsPtr);
  1094. return (DDDMP_SUCCESS);
  1095. }
  1096. /**Function********************************************************************
  1097. Synopsis [Perform an Operation among BDDs.]
  1098. Description [Perform an Operation among BDDs.]
  1099. SideEffects []
  1100. SeeAlso []
  1101. ******************************************************************************/
  1102. static int
  1103. Operation(
  1104. DdManager *ddMgr /* IN: CUDD Manager */,
  1105. DdNode **operandBdd /* IN: Array of operandBdd */
  1106. )
  1107. {
  1108. DdNode *f, *g, *h;
  1109. char buf[DDDMPTEST_MAX_STRING_LENGTH];
  1110. int i;
  1111. /*------------------------ Read Operation Operands ------------------------*/
  1112. ReadString (DDDMP_MESSAGE_OP, buf);
  1113. ReadInt (DDDMP_MESSAGE_SOURCE1, &i);
  1114. f = operandBdd[i];
  1115. /*-------------------------- Compute Operation ----------------------------*/
  1116. if ((strcmp(buf, "or")==0)|| (strcmp(buf, "OR")==0)) {
  1117. ReadInt (DDDMP_MESSAGE_SOURCE2, &i);
  1118. g = operandBdd[i];
  1119. h = Cudd_bddOr(ddMgr, f, g);
  1120. Cudd_RecursiveDeref(ddMgr, f);
  1121. Cudd_Ref(h);
  1122. Cudd_RecursiveDeref(ddMgr, g);
  1123. } else if ((strcmp(buf, "and")==0) || (strcmp(buf, "AND")==0)) {
  1124. ReadInt (DDDMP_MESSAGE_SOURCE2, &i);
  1125. g = operandBdd[i];
  1126. h = Cudd_bddAnd(ddMgr, f, g);
  1127. Cudd_Ref(h);
  1128. Cudd_RecursiveDeref(ddMgr, f);
  1129. Cudd_RecursiveDeref(ddMgr, g);
  1130. } else if ((strcmp(buf, "xor")==0) || (strcmp(buf, "XOR")==0)) {
  1131. ReadInt (DDDMP_MESSAGE_SOURCE2, &i);
  1132. g = operandBdd[i];
  1133. h = Cudd_bddXor(ddMgr, f, g);
  1134. Cudd_Ref(h);
  1135. Cudd_RecursiveDeref(ddMgr, f);
  1136. Cudd_RecursiveDeref(ddMgr, g);
  1137. } else if (strcmp(buf, "!")==0) {
  1138. h = Cudd_Not(f);
  1139. Cudd_Ref(h);
  1140. Cudd_RecursiveDeref(ddMgr, f);
  1141. } else if ((strcmp(buf, "buf")==0)|| (strcmp(buf, "BUF")==0)) {
  1142. h = f;
  1143. } else {
  1144. fprintf (stderr, "Dddmp Test Error : Operation %s unknown\n", buf);
  1145. h = NULL;
  1146. }
  1147. ReadInt (DDDMP_MESSAGE_DESTINATION, &i);
  1148. operandBdd[i] = h;
  1149. return (DDDMP_SUCCESS);
  1150. }
  1151. /**Function********************************************************************
  1152. Synopsis [Store a BDD in a file.]
  1153. Description [Store a BDD in a file.]
  1154. SideEffects []
  1155. SeeAlso []
  1156. ******************************************************************************/
  1157. static int
  1158. BddStore (
  1159. DdManager *ddMgr /* IN: CUDD Manager */,
  1160. DdNode **operandBdd /* IN: Operand BDD */,
  1161. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  1162. )
  1163. {
  1164. DdNode *f = NULL;
  1165. int i, retValue;
  1166. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1167. /*------------------------ Read Operation Operands ------------------------*/
  1168. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1169. ReadInt (DDDMP_MESSAGE_BDD, &i);
  1170. fprintf (stdout, "Storing %s ...\n", fileName);
  1171. fflush (stdout);
  1172. f = operandBdd[i];
  1173. /*----------------------------- Store BDDs -------------------------------*/
  1174. retValue = Dddmp_cuddBddStore(ddMgr, NULL, f, varInfo->orderedVarNames,
  1175. varInfo->varAuxIdsAll, DDDMP_MODE_TEXT, varoutinfo, fileName, NULL);
  1176. Dddmp_CheckAndGotoLabel (retValue!=DDDMP_SUCCESS, "BDD NOT stored.",
  1177. failure);
  1178. return (DDDMP_SUCCESS);
  1179. failure:
  1180. return(DDDMP_FAILURE);
  1181. }
  1182. /**Function********************************************************************
  1183. Synopsis [Store an Array of BDD in a file.]
  1184. Description [Store an Array of BDD in a file.]
  1185. SideEffects []
  1186. SeeAlso []
  1187. ******************************************************************************/
  1188. static int
  1189. BddArrayStore (
  1190. DdManager *ddMgr /* IN: CUDD Manager */,
  1191. DdNode ***operandBddArray /* IN: Array of operand BDD */,
  1192. int *operandBddArraySize /* IN: Number of ADD in the Array */,
  1193. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  1194. )
  1195. {
  1196. int i, retValue, nRoots;
  1197. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1198. /*------------------------ Read Operation Operands ------------------------*/
  1199. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1200. ReadInt (DDDMP_MESSAGE_BDD_ARRAY, &i);
  1201. nRoots = operandBddArraySize[i];
  1202. /*----------------------------- Store BDDs -------------------------------*/
  1203. fprintf (stdout, "Storing Array of BDDs in file %s ...\n", fileName);
  1204. fflush (stdout);
  1205. retValue = Dddmp_cuddBddArrayStore (ddMgr, NULL, nRoots, operandBddArray[i],
  1206. NULL, varInfo->orderedVarNames, varInfo->varAuxIdsAll, DDDMP_MODE_TEXT,
  1207. DDDMP_VARIDS, fileName, NULL);
  1208. Dddmp_CheckAndGotoLabel (retValue!=DDDMP_SUCCESS, "BDD NOT stored.",
  1209. failure);
  1210. fprintf (stdout, "done.\n");
  1211. return (DDDMP_SUCCESS);
  1212. failure:
  1213. return(DDDMP_FAILURE);
  1214. }
  1215. /**Function********************************************************************
  1216. Synopsis [Store an ADD in a file.]
  1217. Description [Store an ADD in a file.]
  1218. SideEffects []
  1219. SeeAlso []
  1220. ******************************************************************************/
  1221. static int
  1222. AddStore(
  1223. DdManager *ddMgr /* IN: CUDD Manager */,
  1224. DdNode **operandBdd /* IN: operand Bdd */,
  1225. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  1226. )
  1227. {
  1228. DdNode *f;
  1229. int i, retValue;
  1230. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1231. /*------------------------ Read Operation Operands ------------------------*/
  1232. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1233. ReadInt (DDDMP_MESSAGE_BDD, &i);
  1234. fprintf (stdout, "Storing %s ...\n", fileName);
  1235. fflush (stdout);
  1236. f = operandBdd[i];
  1237. #if 0
  1238. /* StQ Patch - CREATE temporary ADD to Store */
  1239. f = Cudd_addResidue (ddMgr, 4, 3, 1, 1);
  1240. fprintf (stderr, "Store:\n");
  1241. Cudd_PrintMinterm (ddMgr, f);
  1242. /* end ... StQ Patch */
  1243. #endif
  1244. retValue = Dddmp_cuddAddStore (ddMgr, NULL, f, varInfo->orderedVarNames,
  1245. varInfo->varAuxIdsAll, DDDMP_MODE_TEXT, varoutinfo, fileName, NULL);
  1246. Dddmp_CheckAndReturn (retValue!=DDDMP_SUCCESS, "BDD NOT stored.");
  1247. return (DDDMP_SUCCESS);
  1248. }
  1249. /**Function********************************************************************
  1250. Synopsis [Store a BDD in a file.]
  1251. Description [Store a BDD in a file.]
  1252. SideEffects []
  1253. SeeAlso []
  1254. ******************************************************************************/
  1255. static int
  1256. AddArrayStore (
  1257. DdManager *ddMgr /* IN: CUDD Manager */,
  1258. DdNode ***operandBddArray /* IN: Array of operand ADD */,
  1259. int *operandBddArraySize /* IN: Number of ADD in the Array */,
  1260. dddmpVarInfo_t *varInfo
  1261. )
  1262. {
  1263. int i, retValue, nRoots;
  1264. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1265. /*------------------------ Read Operation Operands ------------------------*/
  1266. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1267. ReadInt (DDDMP_MESSAGE_BDD_ARRAY, &i);
  1268. nRoots = operandBddArraySize[i];
  1269. fprintf (stdout, "Storing Array of BDDs in file %s ...\n", fileName);
  1270. fflush (stdout);
  1271. retValue = Dddmp_cuddAddArrayStore (ddMgr, NULL, nRoots, operandBddArray[i],
  1272. NULL, varInfo->orderedVarNames, varInfo->varAuxIdsAll, DDDMP_MODE_TEXT,
  1273. DDDMP_VARIDS, fileName, NULL);
  1274. Dddmp_CheckAndReturn (retValue!=DDDMP_SUCCESS, "BDD NOT stored.");
  1275. fprintf (stdout, "done.\n");
  1276. return (DDDMP_SUCCESS);
  1277. }
  1278. /**Function********************************************************************
  1279. Synopsis [Store a BDD as CNF format in a file.]
  1280. Description [Store a BDD as CNF format in a file.]
  1281. SideEffects []
  1282. SeeAlso []
  1283. ******************************************************************************/
  1284. static int
  1285. BddStoreCnf(
  1286. DdManager *ddMgr /* IN: CUDD Manager */,
  1287. DdNode **operandBdd /* IN: Array of operand ADD */,
  1288. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  1289. )
  1290. {
  1291. DdNode *f = NULL;
  1292. Dddmp_DecompCnfStoreType storingMode = DDDMP_CNF_MODE_BEST;
  1293. int noHeader = 0;
  1294. int i, nVars, retValue, idInitial, varNewN, clauseNewN;
  1295. int edgeInTh = (-1);
  1296. int pathLengthTh = (-1);
  1297. int *tmpBddIds = NULL;
  1298. int *tmpCnfIds = NULL;
  1299. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1300. char row[DDDMPTEST_MAX_FILENAME_LENGTH];
  1301. /*------------------------ Read Operation Operands ------------------------*/
  1302. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1303. ReadInt (DDDMP_MESSAGE_BDD, &i);
  1304. ReadString (DDDMP_MESSAGE_FORMAT, row);
  1305. switch (row[0]) {
  1306. case 'N':
  1307. storingMode = DDDMP_CNF_MODE_NODE;
  1308. break;
  1309. case 'M':
  1310. storingMode = DDDMP_CNF_MODE_MAXTERM;
  1311. break;
  1312. case 'B':
  1313. storingMode = DDDMP_CNF_MODE_BEST;
  1314. ReadInt (DDDMP_MESSAGE_EDGE_MAX, &edgeInTh);
  1315. ReadInt (DDDMP_MESSAGE_LENGHT_MAX, &pathLengthTh);
  1316. break;
  1317. }
  1318. ReadInt (DDDMP_MESSAGE_I_ID, &idInitial);
  1319. fprintf (stdout, "Storing %s ...\n", fileName);
  1320. fflush (stdout);
  1321. f = operandBdd[i];
  1322. nVars = varInfo->nDdVars;
  1323. /*------------ From BDD and CNF ids to Proper Array of ids ----------------*/
  1324. tmpBddIds = DDDMP_ALLOC (int, nVars);
  1325. Dddmp_CheckAndGotoLabel (tmpBddIds==NULL, "Error allocating memory.",
  1326. failure);
  1327. tmpCnfIds = DDDMP_ALLOC (int, nVars);
  1328. Dddmp_CheckAndGotoLabel (tmpBddIds==NULL, "Error allocating memory.",
  1329. failure);
  1330. for (i=0; i<nVars; i++) {
  1331. tmpBddIds[i] = i;
  1332. tmpCnfIds[i] = i+1;
  1333. }
  1334. retValue = Dddmp_cuddBddStoreCnf (ddMgr, f, storingMode, noHeader,
  1335. varInfo->orderedVarNames, tmpBddIds, NULL, tmpCnfIds, idInitial,
  1336. edgeInTh, pathLengthTh, fileName, NULL, &clauseNewN, &varNewN);
  1337. Dddmp_CheckAndGotoLabel (retValue!=DDDMP_SUCCESS, "BDD NOT stored.",
  1338. failure);
  1339. fprintf (stdout, "Number of Clauses Stored = %d\n", clauseNewN);
  1340. fprintf (stdout, "Number of New Variable Created Storing = %d\n",
  1341. varNewN);
  1342. fflush (stdout);
  1343. DDDMP_FREE (tmpBddIds);
  1344. DDDMP_FREE (tmpCnfIds);
  1345. return (DDDMP_SUCCESS);
  1346. failure:
  1347. DDDMP_FREE (tmpBddIds);
  1348. DDDMP_FREE (tmpCnfIds);
  1349. return(DDDMP_FAILURE);
  1350. }
  1351. /**Function********************************************************************
  1352. Synopsis [Store a BDD as CNF format in a file.]
  1353. Description [Store a BDD as CNF format in a file.]
  1354. SideEffects []
  1355. SeeAlso []
  1356. ******************************************************************************/
  1357. static int
  1358. BddArrayStoreCnf(
  1359. DdManager *ddMgr /* IN: CUDD Manager */,
  1360. DdNode ***operandBddArray /* IN: Array of operand ADD */,
  1361. int *operandBddArraySize /* IN: Number of ADD in the Array */,
  1362. dddmpVarInfo_t *varInfo /* IN/OUT: Variable Information */
  1363. )
  1364. {
  1365. Dddmp_DecompCnfStoreType storingMode = DDDMP_CNF_MODE_BEST;
  1366. int noHeader = 0;
  1367. int i, nVars, bddN, retValue, idInitial, varNewN, clauseNewN;
  1368. int edgeInTh = (-1);
  1369. int pathLengthTh = (-1);
  1370. int *tmpBddIds = NULL;
  1371. int *tmpCnfIds = NULL;
  1372. char fileName[DDDMPTEST_MAX_FILENAME_LENGTH];
  1373. char row[DDDMPTEST_MAX_FILENAME_LENGTH];
  1374. /*------------------------ Read Operation Operands ------------------------*/
  1375. ReadString (DDDMP_MESSAGE_FILE, fileName);
  1376. ReadInt (DDDMP_MESSAGE_BDD_ARRAY, &bddN);
  1377. ReadString (DDDMP_MESSAGE_FORMAT, row);
  1378. switch (row[0]) {
  1379. case 'N':
  1380. storingMode = DDDMP_CNF_MODE_NODE;
  1381. break;
  1382. case 'M':
  1383. storingMode = DDDMP_CNF_MODE_MAXTERM;
  1384. break;
  1385. case 'B':
  1386. storingMode = DDDMP_CNF_MODE_BEST;
  1387. ReadInt (DDDMP_MESSAGE_EDGE_MAX, &edgeInTh);
  1388. ReadInt (DDDMP_MESSAGE_LENGHT_MAX, &pathLengthTh);
  1389. break;
  1390. }
  1391. ReadInt (DDDMP_MESSAGE_I_ID, &idInitial);
  1392. nVars = varInfo->nDdVars;
  1393. /*------------ From BDD and CNF ids to Proper Array of ids ----------------*/
  1394. tmpBddIds = DDDMP_ALLOC (int, nVars);
  1395. Dddmp_CheckAndReturn (tmpBddIds==NULL, "Allocation error.");
  1396. tmpCnfIds = DDDMP_ALLOC (int, nVars);
  1397. Dddmp_CheckAndReturn (tmpCnfIds==NULL, "Allocation error.");
  1398. for (i=0; i<nVars; i++) {
  1399. tmpBddIds[i] = i;
  1400. tmpCnfIds[i] = i*10+1;
  1401. }
  1402. fprintf (stdout, "Storing %s ...\n", fileName);
  1403. fflush (stdout);
  1404. retValue = Dddmp_cuddBddArrayStoreCnf (ddMgr, operandBddArray[bddN],
  1405. operandBddArraySize[bddN], storingMode, noHeader, varInfo->orderedVarNames,
  1406. tmpBddIds, NULL, tmpCnfIds, idInitial, edgeInTh, pathLengthTh, fileName,
  1407. NULL, &varNewN, &clauseNewN);
  1408. Dddmp_CheckAndGotoLabel (retValue!=DDDMP_SUCCESS, "BDD NOT stored.",
  1409. failure);
  1410. fprintf (stdout, "Number of Clauses Stored = %d\n", clauseNewN);
  1411. fprintf (stdout, "Number of New Variable Created Storing = %d\n",
  1412. varNewN);
  1413. fflush (stdout);
  1414. DDDMP_FREE (tmpBddIds);
  1415. DDDMP_FREE (tmpCnfIds);
  1416. return (DDDMP_SUCCESS);
  1417. failure:
  1418. DDDMP_FREE (tmpBddIds);
  1419. DDDMP_FREE (tmpCnfIds);
  1420. return(DDDMP_FAILURE);
  1421. }
  1422. /**Function********************************************************************
  1423. Synopsis [Dynamic Reordering BDDs.]
  1424. Description [Dynamic Reordering BDDs using one of the allowed CUDD
  1425. methods.]
  1426. SideEffects []
  1427. SeeAlso []
  1428. ******************************************************************************/
  1429. static int
  1430. DynamicReordering (
  1431. DdManager *ddMgr /* IN: CUDD Manager */
  1432. )
  1433. {
  1434. Cudd_ReorderingType approach = CUDD_REORDER_SIFT;
  1435. int method;
  1436. /*------------------------ Read Operation Operands ------------------------*/
  1437. ReadInt (DDDMP_MESSAGE_REORDERING, &method);
  1438. approach = (Cudd_ReorderingType) method;
  1439. Cudd_ReduceHeap (ddMgr, approach, 5);
  1440. return (DDDMP_SUCCESS);
  1441. }
  1442. /**Function********************************************************************
  1443. Synopsis [Selects variable matching mode.]
  1444. Description [Selects variable matching mode.]
  1445. SideEffects []
  1446. SeeAlso []
  1447. ******************************************************************************/
  1448. static int
  1449. SetLoadMatchmode (
  1450. )
  1451. {
  1452. int sel;
  1453. char row[DDDMPTEST_MAX_FILENAME_LENGTH];
  1454. char *retval;
  1455. fprintf (stdout, "Variable matchmode:\n");
  1456. fprintf (stdout, "Match IDs (1)\n");
  1457. fprintf (stdout, "Match permIDs (2)\n");
  1458. fprintf (stdout, "Match names (must have been loaded) (3)\n");
  1459. fprintf (stdout, "Match auxids (must have been loaded) (4)\n");
  1460. fprintf (stdout, "Match composeids (must have been loaded) (5)\n");
  1461. fprintf (stdout, "Your choice: ");
  1462. fflush (stdout);
  1463. retval = fgets (row, DDDMPTEST_MAX_STRING_LENGTH, stdin);
  1464. if (!retval)
  1465. return (DDDMP_FAILURE);
  1466. sscanf (row, "%d", &sel);
  1467. switch (sel) {
  1468. case 1:
  1469. varmatchmode = DDDMP_VAR_MATCHIDS;
  1470. break;
  1471. case 2:
  1472. varmatchmode = DDDMP_VAR_MATCHPERMIDS;
  1473. break;
  1474. case 3:
  1475. varmatchmode = DDDMP_VAR_MATCHNAMES;
  1476. break;
  1477. case 4:
  1478. varmatchmode = DDDMP_VAR_MATCHAUXIDS;
  1479. break;
  1480. case 5:
  1481. varmatchmode = DDDMP_VAR_COMPOSEIDS;
  1482. break;
  1483. default:
  1484. fprintf (stderr, "Wrong choice!\n");
  1485. break;
  1486. }
  1487. return (DDDMP_SUCCESS);
  1488. }
  1489. /**Function********************************************************************
  1490. Synopsis [Complete the internal manager structure for subsequent
  1491. BDD/ADD/CNF operations.
  1492. ]
  1493. Description [Complete the internal manager structure for subsequent
  1494. BDD/ADD/CNF operations.
  1495. The phylosophy is simple: to have proper names and ids it is necessary
  1496. to load an header before each actual load/store operation.
  1497. An header load should initialize variable names, variable ids,
  1498. variable compose ids, and variable auxiliary ids for all variables
  1499. stored in the file.
  1500. This information has to be extended for all variables in the
  1501. *current* CUDD manager (before any store operation).
  1502. CompleteInfoStruct does this job.
  1503. Arrays varIds, varComposeIds, and varAuxIds contain information for
  1504. all the variable in the BDD/ADD/CNF while arrays varIdsAll,
  1505. varComposeIdsAll, and varAuxIdsAll contain information for *all*
  1506. variable in the current CUDD manager.
  1507. ]
  1508. SideEffects []
  1509. SeeAlso []
  1510. ******************************************************************************/
  1511. static int
  1512. CompleteInfoStruct (
  1513. Dddmp_DecompType ddType /* IN: selects the proper decomp type */,
  1514. int nVars /* IN: number of DD variables */,
  1515. int nSuppVars /* IN: number of support variables */,
  1516. char **suppVarNames /* IN: array of support variable names */,
  1517. char **orderedVarNames /* IN: array of variable names */,
  1518. int *varIds /* IN: array of variable ids */,
  1519. int *varComposeIds /* IN: array of permids ids */,
  1520. int *varAuxIds /* IN: array of variable aux ids */,
  1521. int nRoots /* IN: number of root in the file */,
  1522. dddmpVarInfo_t *varInfo /* IN: Variable Information */
  1523. )
  1524. {
  1525. int i;
  1526. char tmpString[DDDMPTEST_MAX_STRING_LENGTH];
  1527. /*------------------------- Updates Variable Names ------------------------*/
  1528. DddmpStrArrayFree (varInfo->suppVarNames, varInfo->nSuppVars);
  1529. varInfo->suppVarNames = suppVarNames;
  1530. if (varInfo->varNamesFlagUpdate == 1) {
  1531. DddmpStrArrayFree (varInfo->orderedVarNames, varInfo->nVars);
  1532. if (orderedVarNames != NULL) {
  1533. varInfo->orderedVarNames = orderedVarNames;
  1534. } else {
  1535. varInfo->orderedVarNames = DDDMP_ALLOC (char *, nVars);
  1536. Dddmp_CheckAndReturn (varInfo->orderedVarNames==NULL,
  1537. "Allocation error.");
  1538. for (i=0; i<nVars; i++) {
  1539. varInfo->orderedVarNames[i] = NULL;
  1540. }
  1541. if (varInfo->suppVarNames != NULL) {
  1542. for (i=0; i<nSuppVars; i++) {
  1543. varInfo->orderedVarNames[i] = DDDMP_ALLOC (char,
  1544. (strlen (varInfo->suppVarNames[i]) + 1));
  1545. strcpy (varInfo->orderedVarNames[i], varInfo->suppVarNames[i]);
  1546. }
  1547. }
  1548. for (i=0; i<nVars; i++) {
  1549. if (varInfo->orderedVarNames[i] == NULL) {
  1550. sprintf (tmpString, "DUMMY%d", i);
  1551. varInfo->orderedVarNames[i] = DDDMP_ALLOC (char,
  1552. (strlen (tmpString) + 1));
  1553. strcpy (varInfo->orderedVarNames[i], tmpString);
  1554. }
  1555. }
  1556. }
  1557. }
  1558. /*------------------------------ Updates IDs ------------------------------*/
  1559. DDDMP_FREE (varInfo->varIds);
  1560. varInfo->varIds = varIds;
  1561. if (varInfo->varIdsFlagUpdate == 1) {
  1562. /* Free Previously Allocated Memory */
  1563. DDDMP_FREE (varInfo->varIdsAll);
  1564. /* Allocate New Memory and Check */
  1565. varInfo->varIdsAll = DDDMP_ALLOC (int, nVars);
  1566. Dddmp_CheckAndReturn (varInfo->varIdsAll==NULL, "Allocation error.");
  1567. /* Set New Values */
  1568. for (i=0; i<nVars; i++) {
  1569. varInfo->varIdsAll[i] = (-1);
  1570. }
  1571. if (varInfo->varIds != NULL) {
  1572. for (i=0; i<nSuppVars; i++) {
  1573. varInfo->varIdsAll[varInfo->varIds[i]] = varInfo->varIds[i];
  1574. }
  1575. }
  1576. }
  1577. /*-------------------------- Updates Compose IDs --------------------------*/
  1578. DDDMP_FREE (varInfo->varComposeIds);
  1579. varInfo->varComposeIds = varComposeIds;
  1580. if (varInfo->varComposeIdsFlagUpdate == 1) {
  1581. /* Free Previously Allocated Memory */
  1582. DDDMP_FREE (varInfo->varComposeIdsAll);
  1583. /* Allocate New Memory and Check */
  1584. varInfo->varComposeIdsAll = DDDMP_ALLOC (int, nVars);
  1585. Dddmp_CheckAndReturn (varInfo->varComposeIdsAll==NULL,
  1586. "Allocation error.");
  1587. /* Set New Values */
  1588. for (i=0; i<nVars; i++) {
  1589. varInfo->varComposeIdsAll[i] = (-1);
  1590. }
  1591. if (varInfo->varComposeIds != NULL) {
  1592. for (i=0; i<nSuppVars; i++) {
  1593. varInfo->varComposeIdsAll[varInfo->varIds[i]] =
  1594. varInfo->varComposeIds[i];
  1595. }
  1596. }
  1597. }
  1598. /*------------------------- Updates Auxiliary IDs -------------------------*/
  1599. DDDMP_FREE (varInfo->varAuxIds);
  1600. varInfo->varAuxIds = varAuxIds;
  1601. if (varInfo->varAuxIdsFlagUpdate == 1) {
  1602. /* Free Previously Allocated Memory */
  1603. DDDMP_FREE (varInfo->varAuxIdsAll);
  1604. /* Allocate New Memory and Check */
  1605. varInfo->varAuxIdsAll = DDDMP_ALLOC (int, nVars);
  1606. Dddmp_CheckAndReturn (varInfo->varAuxIdsAll==NULL, "Allocation error.");
  1607. /* Set New Values */
  1608. for (i=0; i<nVars; i++) {
  1609. varInfo->varAuxIdsAll[i] = (-1);
  1610. }
  1611. if (varInfo->varAuxIds != NULL) {
  1612. for (i=0; i<nSuppVars; i++) {
  1613. varInfo->varAuxIdsAll[varInfo->varIds[i]] = varInfo->varAuxIds[i];
  1614. }
  1615. }
  1616. }
  1617. /*----------------------------- Updates Sizes -----------------------------*/
  1618. varInfo->ddType = ddType;
  1619. varInfo->nVars = nVars;
  1620. varInfo->nSuppVars = nSuppVars;
  1621. Dddmp_CheckAndReturn (varInfo->nDdVars<varInfo->nVars,
  1622. "Local Manager with Not Enough Variables.");
  1623. varInfo->nRoots = nRoots;
  1624. return (DDDMP_SUCCESS);
  1625. }
  1626. /**Function********************************************************************
  1627. Synopsis [Reads an integer value from standard input.]
  1628. Description [Reads an integer value from standard input.]
  1629. SideEffects []
  1630. SeeAlso []
  1631. ******************************************************************************/
  1632. static void
  1633. ReadInt (
  1634. Dddmp_MessageType message,
  1635. int *i
  1636. )
  1637. {
  1638. char row[DDDMPTEST_MAX_FILENAME_LENGTH];
  1639. char *retval;
  1640. switch (message) {
  1641. case DDDMP_MESSAGE_MANAGER_VAR:
  1642. fprintf (stdout, "Number of Variables: ");
  1643. break;
  1644. case DDDMP_MESSAGE_BDD:
  1645. fprintf (stdout, "Which BDDs [0..%d]: ",
  1646. DDDMPTEST_MAX_OPERAND-1);
  1647. break;
  1648. case DDDMP_MESSAGE_BDD_ARRAY:
  1649. fprintf (stdout, "Which Array of BDDs [0..%d]: ",
  1650. DDDMPTEST_MAX_OPERAND-1);
  1651. break;
  1652. case DDDMP_MESSAGE_CUBE:
  1653. fprintf (stdout, "How many cubes [1..]: ");
  1654. break;
  1655. case DDDMP_MESSAGE_INDEX:
  1656. fprintf (stdout, "Index: ");
  1657. break;
  1658. case DDDMP_MESSAGE_SOURCE1:
  1659. fprintf (stdout, "Source1 [0..%d]: ", DDDMPTEST_MAX_OPERAND-1);
  1660. break;
  1661. case DDDMP_MESSAGE_SOURCE2:
  1662. fprintf (stdout, "Source2 [0..%d]: ", DDDMPTEST_MAX_OPERAND-1);
  1663. break;
  1664. case DDDMP_MESSAGE_DESTINATION:
  1665. fprintf (stdout, "Destination [0..%d]: ", DDDMPTEST_MAX_OPERAND-1);
  1666. break;
  1667. case DDDMP_MESSAGE_I_ID:
  1668. fprintf (stdout, "Initial ID : ");
  1669. break;
  1670. case DDDMP_MESSAGE_EDGE_MAX:
  1671. fprintf (stdout,
  1672. "Max Number of Edges (Insert cut-point from there on) : ");
  1673. break;
  1674. case DDDMP_MESSAGE_LENGHT_MAX:
  1675. fprintf (stdout,
  1676. "Max BDD-Path Length (Insert cut-point from there on) : ");
  1677. break;
  1678. case DDDMP_MESSAGE_REORDERING:
  1679. fprintf (stdout, "Reordering Approach (1..17): ");
  1680. break;
  1681. default:
  1682. fprintf (stdout, "Input Generic Integer: ");
  1683. break;
  1684. }
  1685. fflush (stdout);
  1686. retval = fgets (row, DDDMPTEST_MAX_STRING_LENGTH, stdin);
  1687. if (!retval)
  1688. return;
  1689. sscanf (row, "%d", i);
  1690. return;
  1691. }
  1692. /**Function********************************************************************
  1693. Synopsis [Reads a string from standard input.]
  1694. Description [Reads a string from standard input.]
  1695. SideEffects []
  1696. SeeAlso []
  1697. ******************************************************************************/
  1698. static void
  1699. ReadString (
  1700. Dddmp_MessageType message,
  1701. char string[]
  1702. )
  1703. {
  1704. char localString[DDDMPTEST_MAX_STRING_LENGTH];
  1705. char *retval;
  1706. switch (message) {
  1707. case DDDMP_MESSAGE_PROMPT:
  1708. fprintf (stdout, "TestDddmp> ");
  1709. break;
  1710. case DDDMP_MESSAGE_FILE:
  1711. fprintf (stdout, "File : ");
  1712. break;
  1713. case DDDMP_MESSAGE_OP:
  1714. fprintf (stdout, "Operation [or,and,xor,!,buf(=)] : ");
  1715. break;
  1716. case DDDMP_MESSAGE_FORMAT:
  1717. fprintf (stdout, "Format (Node=N, Maxterm=M, Best=B) : ");
  1718. break;
  1719. default:
  1720. fprintf (stdout, "Input Generic String : ");
  1721. break;
  1722. }
  1723. fflush (stdout);
  1724. string[0] = '\n';
  1725. retval = fgets (localString, DDDMPTEST_MAX_STRING_LENGTH, stdin);
  1726. if (!retval)
  1727. return;
  1728. sscanf (localString, "%s", string);
  1729. return;
  1730. }