emergency commit
[cl-cudd.git] / distr / dddmp / dddmpLoadCnf.c
blob35bec273b12f97f7c268c7d258ae0575db3c945a
1 /**CFile**********************************************************************
3 FileName [dddmpLoadCnf.c]
5 PackageName [dddmp]
7 Synopsis [Functions to read in CNF from file as BDDs.]
9 Description [Functions to read in CNF from file as BDDs.
12 Author [Gianpiero Cabodi and Stefano Quer]
14 Copyright [
15 Copyright (c) 2004 by Politecnico di Torino.
16 All Rights Reserved. This software is for educational purposes only.
17 Permission is given to academic institutions to use, copy, and modify
18 this software and its documentation provided that this introductory
19 message is not removed, that this software and its documentation is
20 used for the institutions' internal research and educational purposes,
21 and that no monies are exchanged. No guarantee is expressed or implied
22 by the distribution of this code.
23 Send bug-reports and/or questions to:
24 {gianpiero.cabodi,stefano.quer}@polito.it.
27 ******************************************************************************/
29 #include "dddmpInt.h"
31 /*---------------------------------------------------------------------------*/
32 /* Constant declarations */
33 /*---------------------------------------------------------------------------*/
35 #define DDDMP_MAX_CNF_ROW_LENGTH 1000
36 #define DDDMP_DEBUG_CNF 0
38 /*---------------------------------------------------------------------------*/
39 /* Stucture declarations */
40 /*---------------------------------------------------------------------------*/
42 /*---------------------------------------------------------------------------*/
43 /* Type declarations */
44 /*---------------------------------------------------------------------------*/
46 /*---------------------------------------------------------------------------*/
47 /* Variable declarations */
48 /*---------------------------------------------------------------------------*/
50 /*---------------------------------------------------------------------------*/
51 /* Macro declarations */
52 /*---------------------------------------------------------------------------*/
54 #define matchkeywd(str,key) (strncmp(str,key,strlen(key))==0)
56 /**AutomaticStart*************************************************************/
58 /*---------------------------------------------------------------------------*/
59 /* Static function prototypes */
60 /*---------------------------------------------------------------------------*/
62 static int DddmpCuddDdArrayLoadCnf(DdManager *ddMgr, Dddmp_RootMatchType rootmatchmode, char **rootmatchnames, Dddmp_VarMatchType varmatchmode, char **varmatchnames, int *varmatchauxids, int *varcomposeids, int mode, char *file, FILE *fp, DdNode ***rootsPtrPtr, int *nRoots);
63 static Dddmp_Hdr_t * DddmpBddReadHeaderCnf(char *file, FILE *fp);
64 static void DddmpFreeHeaderCnf(Dddmp_Hdr_t *Hdr);
65 static int DddmpReadCnfClauses(Dddmp_Hdr_t *Hdr, int ***cnfTable, FILE *fp);
66 static int DddmpCnfClauses2Bdd(Dddmp_Hdr_t *Hdr, DdManager *ddMgr, int **cnfTable, int mode, DdNode ***rootsPtrPtr);
68 /**AutomaticEnd***************************************************************/
70 /*---------------------------------------------------------------------------*/
71 /* Definition of exported functions */
72 /*---------------------------------------------------------------------------*/
74 /**Function********************************************************************
76 Synopsis [Reads a dump file in a CNF format.]
78 Description [Reads a dump file representing the argument BDD in a
79 CNF formula.
80 Dddmp_cuddBddArrayLoadCnf is used through a dummy array.
81 The results is returned in different formats depending on the
82 mode selection:
83 IFF mode == 0 Return the Clauses without Conjunction
84 IFF mode == 1 Return the sets of BDDs without Quantification
85 IFF mode == 2 Return the sets of BDDs AFTER Existential Quantification
88 SideEffects [A vector of pointers to DD nodes is allocated and freed.]
90 SeeAlso [Dddmp_cuddBddLoad, Dddmp_cuddBddArrayLoad]
92 ******************************************************************************/
94 int
95 Dddmp_cuddBddLoadCnf (
96 DdManager *ddMgr /* IN: DD Manager */,
97 Dddmp_VarMatchType varmatchmode /* IN: storing mode selector */,
98 char **varmatchnames /* IN: array of variable names, by IDs */,
99 int *varmatchauxids /* IN: array of variable auxids, by IDs */,
100 int *varcomposeids /* IN: array of new ids accessed, by IDs */,
101 int mode /* IN: computation mode */,
102 char *file /* IN: file name */,
103 FILE *fp /* IN: file pointer */,
104 DdNode ***rootsPtrPtr /* OUT: array of returned BDD roots */,
105 int *nRoots /* OUT: number of BDDs returned */
108 int i, retValue;
110 retValue = Dddmp_cuddBddArrayLoadCnf (ddMgr, DDDMP_ROOT_MATCHLIST, NULL,
111 varmatchmode, varmatchnames, varmatchauxids, varcomposeids, mode,
112 file, fp, rootsPtrPtr, nRoots);
114 if (retValue == DDDMP_FAILURE) {
115 return (DDDMP_FAILURE);
118 if (*nRoots > 1) {
119 fprintf (stderr,
120 "Warning: %d BDD roots found in file. Only first retrieved.\n",
121 *nRoots);
122 for (i=1; i<*nRoots; i++) {
123 Cudd_RecursiveDeref (ddMgr, *rootsPtrPtr[i]);
127 return (DDDMP_SUCCESS);
130 /**Function********************************************************************
132 Synopsis [Reads a dump file in a CNF format.]
134 Description [Reads a dump file representing the argument BDD in a
135 CNF formula.
138 SideEffects [A vector of pointers to DD nodes is allocated and freed.]
140 SeeAlso [Dddmp_cuddBddArrayLoad]
142 ******************************************************************************/
145 Dddmp_cuddBddArrayLoadCnf (
146 DdManager *ddMgr /* IN: DD Manager */,
147 Dddmp_RootMatchType rootmatchmode/* IN: storing mode selector */,
148 char **rootmatchnames /* IN: sorted names for loaded roots */,
149 Dddmp_VarMatchType varmatchmode /* IN: storing mode selector */,
150 char **varmatchnames /* IN: array of variable names, by IDs */,
151 int *varmatchauxids /* IN: array of variable auxids, by IDs */,
152 int *varcomposeids /* IN: array of new ids, by IDs */,
153 int mode /* IN: computation Mode */,
154 char *file /* IN: file name */,
155 FILE *fp /* IN: file pointer */,
156 DdNode ***rootsPtrPtr /* OUT: array of returned BDD roots */,
157 int *nRoots /* OUT: number of BDDs returned */
160 int retValue;
162 #ifdef DDDMP_DEBUG
163 #ifndef __alpha__
164 int retValueBis;
166 retValueBis = Cudd_DebugCheck (ddMgr);
167 if (retValueBis == 1) {
168 fprintf (stderr, "Inconsistency Found During CNF Load.\n");
169 fflush (stderr);
170 } else {
171 if (retValueBis == CUDD_OUT_OF_MEM) {
172 fprintf (stderr, "Out of Memory During CNF Load.\n");
173 fflush (stderr);
176 #endif
177 #endif
179 retValue = DddmpCuddDdArrayLoadCnf (ddMgr, rootmatchmode,
180 rootmatchnames, varmatchmode, varmatchnames, varmatchauxids,
181 varcomposeids, mode, file, fp, rootsPtrPtr, nRoots);
183 #ifdef DDDMP_DEBUG
184 #ifndef __alpha__
185 retValueBis = Cudd_DebugCheck (ddMgr);
186 if (retValueBis == 1) {
187 fprintf (stderr, "Inconsistency Found During CNF Load.\n");
188 fflush (stderr);
189 } else {
190 if (retValueBis == CUDD_OUT_OF_MEM) {
191 fprintf (stderr, "Out of Memory During CNF Load.\n");
192 fflush (stderr);
195 #endif
196 #endif
198 return (retValue);
201 /**Function********************************************************************
203 Synopsis [Reads the header of a dump file representing the argument BDDs]
205 Description [Reads the header of a dump file representing the argument BDDs.
206 Returns main information regarding DD type stored in the file,
207 the variable ordering used, the number of variables, etc.
208 It reads only the header of the file NOT the BDD/ADD section.
211 SideEffects []
213 SeeAlso [Dddmp_cuddBddArrayLoad]
215 ******************************************************************************/
218 Dddmp_cuddHeaderLoadCnf (
219 int *nVars /* OUT: number of DD variables */,
220 int *nsuppvars /* OUT: number of support variables */,
221 char ***suppVarNames /* OUT: array of support variable names */,
222 char ***orderedVarNames /* OUT: array of variable names */,
223 int **varIds /* OUT: array of variable ids */,
224 int **varComposeIds /* OUT: array of permids ids */,
225 int **varAuxIds /* OUT: array of variable aux ids */,
226 int *nRoots /* OUT: number of root in the file */,
227 char *file /* IN: file name */,
228 FILE *fp /* IN: file pointer */
231 Dddmp_Hdr_t *Hdr;
232 int i, fileToClose;
233 char **tmpOrderedVarNames = NULL;
234 char **tmpSuppVarNames = NULL;
235 int *tmpVarIds = NULL;
236 int *tmpVarComposeIds = NULL;
237 int *tmpVarAuxIds = NULL;
239 fileToClose = 0;
240 if (fp == NULL) {
241 fp = fopen (file, "r");
242 Dddmp_CheckAndGotoLabel (fp==NULL, "Error opening file.",
243 failure);
244 fileToClose = 1;
247 Hdr = DddmpBddReadHeaderCnf (NULL, fp);
249 Dddmp_CheckAndGotoLabel (Hdr->nnodes==0, "Zero number of nodes.",
250 failure);
253 * Number of variables (tot and support)
256 *nVars = Hdr->nVars;
257 *nsuppvars = Hdr->nsuppvars;
260 * Support Varnames
263 if (Hdr->suppVarNames != NULL) {
264 tmpSuppVarNames = DDDMP_ALLOC (char *, *nsuppvars);
265 Dddmp_CheckAndGotoLabel (tmpSuppVarNames==NULL, "Error allocating memory.",
266 failure);
268 for (i=0; i<*nsuppvars; i++) {
269 tmpSuppVarNames[i] = DDDMP_ALLOC (char,
270 (strlen (Hdr->suppVarNames[i]) + 1));
271 Dddmp_CheckAndGotoLabel (Hdr->suppVarNames[i]==NULL,
272 "Support Variable Name Missing in File.", failure);
273 strcpy (tmpSuppVarNames[i], Hdr->suppVarNames[i]);
276 *suppVarNames = tmpSuppVarNames;
277 } else {
278 *suppVarNames = NULL;
282 * Ordered Varnames
285 if (Hdr->orderedVarNames != NULL) {
286 tmpOrderedVarNames = DDDMP_ALLOC (char *, *nVars);
287 Dddmp_CheckAndGotoLabel (tmpOrderedVarNames==NULL,
288 "Error allocating memory.", failure);
290 for (i=0; i<*nVars; i++) {
291 tmpOrderedVarNames[i] = DDDMP_ALLOC (char,
292 (strlen (Hdr->orderedVarNames[i]) + 1));
293 Dddmp_CheckAndGotoLabel (Hdr->orderedVarNames[i]==NULL,
294 "Support Variable Name Missing in File.", failure);
295 strcpy (tmpOrderedVarNames[i], Hdr->orderedVarNames[i]);
298 *orderedVarNames = tmpOrderedVarNames;
299 } else {
300 *orderedVarNames = NULL;
304 * Variable Ids
307 if (Hdr->ids != NULL) {
308 tmpVarIds = DDDMP_ALLOC (int, *nsuppvars);
309 Dddmp_CheckAndGotoLabel (tmpVarIds==NULL, "Error allocating memory.",
310 failure);
311 for (i=0; i<*nsuppvars; i++) {
312 tmpVarIds[i] = Hdr->ids[i];
315 *varIds = tmpVarIds;
316 } else {
317 *varIds = NULL;
321 * Variable Compose Ids
324 if (Hdr->permids != NULL) {
325 tmpVarComposeIds = DDDMP_ALLOC (int, *nsuppvars);
326 Dddmp_CheckAndGotoLabel (tmpVarComposeIds==NULL,
327 "Error allocating memory.", failure);
328 for (i=0; i<*nsuppvars; i++) {
329 tmpVarComposeIds[i] = Hdr->permids[i];
332 *varComposeIds = tmpVarComposeIds;
333 } else {
334 *varComposeIds = NULL;
338 * Variable Auxiliary Ids
341 if (Hdr->auxids != NULL) {
342 tmpVarAuxIds = DDDMP_ALLOC (int, *nsuppvars);
343 Dddmp_CheckAndGotoLabel (tmpVarAuxIds==NULL,
344 "Error allocating memory.", failure);
345 for (i=0; i<*nsuppvars; i++) {
346 tmpVarAuxIds[i] = Hdr->auxids[i];
349 *varAuxIds = tmpVarAuxIds;
350 } else {
351 *varAuxIds = NULL;
355 * Number of roots
358 *nRoots = Hdr->nRoots;
361 * Free and Return
364 if (fileToClose == 1) {
365 fclose (fp);
368 DddmpFreeHeaderCnf (Hdr);
370 return (DDDMP_SUCCESS);
372 failure:
373 return (DDDMP_FAILURE);
376 /*---------------------------------------------------------------------------*/
377 /* Definition of internal functions */
378 /*---------------------------------------------------------------------------*/
380 /*---------------------------------------------------------------------------*/
381 /* Definition of static functions */
382 /*---------------------------------------------------------------------------*/
384 /**Function********************************************************************
386 Synopsis [Reads a dump file representing the argument BDDs in CNF
387 format.
390 Description [Reads a dump file representing the argument BDDs in CNF
391 format.
392 IFF mode == 0 Return the Clauses without Conjunction
393 IFF mode == 1 Return the sets of BDDs without Quantification
394 IFF mode == 2 Return the sets of BDDs AFTER Existential Quantification
397 SideEffects [A vector of pointers to DD nodes is allocated and freed.]
399 SeeAlso [Dddmp_cuddBddArrayLoad]
401 ******************************************************************************/
403 static int
404 DddmpCuddDdArrayLoadCnf (
405 DdManager *ddMgr /* IN: DD Manager */,
406 Dddmp_RootMatchType rootmatchmode/* IN: storing mode selector */,
407 char **rootmatchnames /* IN: sorted names for loaded roots */,
408 Dddmp_VarMatchType varmatchmode /* IN: storing mode selector */,
409 char **varmatchnames /* IN: array of variable names, by ids */,
410 int *varmatchauxids /* IN: array of variable auxids, by ids */,
411 int *varcomposeids /* IN: array of new ids, by ids */,
412 int mode /* IN: computation mode */,
413 char *file /* IN: file name */,
414 FILE *fp /* IN: file pointer */,
415 DdNode ***rootsPtrPtr /* OUT: array of BDD roots */,
416 int *nRoots /* OUT: number of BDDs returned */
419 Dddmp_Hdr_t *Hdr = NULL;
420 int **cnfTable = NULL;
421 int fileToClose = 0;
422 int retValue, i;
424 fileToClose = 0;
425 *rootsPtrPtr = NULL;
427 if (fp == NULL) {
428 fp = fopen (file, "r");
429 Dddmp_CheckAndGotoLabel (fp==NULL, "Error opening file.",
430 failure);
431 fileToClose = 1;
434 /*--------------------------- Read the Header -----------------------------*/
436 Hdr = DddmpBddReadHeaderCnf (NULL, fp);
438 Dddmp_CheckAndGotoLabel (Hdr->nnodes==0, "Zero number of nodes.",
439 failure);
441 /*------------------------ Read the CNF Clauses ---------------------------*/
443 retValue = DddmpReadCnfClauses (Hdr, &cnfTable, fp);
445 Dddmp_CheckAndGotoLabel (retValue==DDDMP_FAILURE,
446 "Read CNF Clauses Failure.", failure);
448 /*------------------------- From Clauses to BDDs --------------------------*/
450 retValue = DddmpCnfClauses2Bdd (Hdr, ddMgr, cnfTable, mode, rootsPtrPtr);
452 Dddmp_CheckAndGotoLabel (retValue==DDDMP_FAILURE,
453 "CNF Clauses To BDDs Failure.", failure);
455 *nRoots = Hdr->nRoots;
457 if (fileToClose) {
458 fclose (fp);
461 for (i=0; i<Hdr->nClausesCnf; i++) {
462 DDDMP_FREE (cnfTable[i]);
464 DDDMP_FREE (cnfTable);
466 DddmpFreeHeaderCnf (Hdr);
468 return (DDDMP_SUCCESS);
471 * Failure Condition
474 failure:
476 if (fileToClose) {
477 fclose (fp);
480 for (i=0; i<Hdr->nClausesCnf; i++) {
481 DDDMP_FREE (cnfTable[i]);
483 DDDMP_FREE (cnfTable);
485 DddmpFreeHeaderCnf (Hdr);
487 /* return 0 on error ! */
488 nRoots = 0;
490 return (DDDMP_FAILURE);
493 /**Function********************************************************************
495 Synopsis [Reads a the header of a dump file representing the argument
496 BDDs.
499 Description [Reads the header of a dump file. Builds a Dddmp_Hdr_t struct
500 containing all infos in the header, for next manipulations.
503 SideEffects [none]
505 SeeAlso []
507 ******************************************************************************/
509 static Dddmp_Hdr_t *
510 DddmpBddReadHeaderCnf (
511 char *file /* IN: file name */,
512 FILE *fp /* IN: file pointer */
515 Dddmp_Hdr_t *Hdr = NULL;
516 char buf[DDDMP_MAXSTRLEN];
517 int nv, nc, retValue, fileToClose = 0;
519 if (fp == NULL) {
520 fp = fopen (file, "r");
521 Dddmp_CheckAndGotoLabel (fp==NULL, "Error opening file.",
522 failure);
523 fileToClose = 1;
526 /* Start Header */
527 Hdr = DDDMP_ALLOC (Dddmp_Hdr_t, 1);
528 if (Hdr == NULL) {
529 return NULL;
532 Hdr->ver = NULL;
533 Hdr->mode = 0;
534 Hdr->ddType = DDDMP_CNF;
535 Hdr->varinfo = DDDMP_VARIDS;
536 Hdr->dd = NULL;
537 Hdr->nnodes = 0;
538 Hdr->nVars = 0;
539 Hdr->nsuppvars = 0;
540 Hdr->orderedVarNames = NULL;
541 Hdr->suppVarNames = NULL;
542 Hdr->ids = NULL;
543 Hdr->permids = NULL;
544 Hdr->auxids = NULL;
545 Hdr->cnfids = NULL;
546 Hdr->nRoots = 0;
547 Hdr->rootids = NULL;
548 Hdr->rootnames = NULL;
549 Hdr->nAddedCnfVar = 0;
550 Hdr->nVarsCnf = 0;
551 Hdr->nClausesCnf = 0;
553 while (fscanf (fp, "%s", buf) != EOF) {
555 /* Init Problem Line */
556 if (buf[0] == 'p') {
557 fscanf (fp, "%*s %d %d", &nv, &nc);
558 Hdr->nVarsCnf = nv;
559 Hdr->nClausesCnf = nc;
560 break;
563 /* CNF Comment Line */
564 if (buf[0] == 'c') {
565 if (fscanf (fp, "%s", buf) == EOF) {
566 break;
570 /* Skip Comment? */
571 if (buf[0] != '.') {
572 fgets (buf, DDDMP_MAXSTRLEN, fp);
573 continue;
576 if (matchkeywd (buf, ".ver")) {
577 /* this not checked so far: only read */
578 retValue = fscanf (fp, "%s", buf);
579 Dddmp_CheckAndGotoLabel (retValue==EOF, "Error reading from file.",
580 failure);
582 Hdr->ver=DddmpStrDup(buf);
583 Dddmp_CheckAndGotoLabel (Hdr->ver==NULL,
584 "Error allocating memory.", failure);
586 continue;
589 if (matchkeywd (buf, ".dd")) {
590 retValue = fscanf (fp, "%s", buf);
591 Dddmp_CheckAndGotoLabel (retValue==EOF, "Error reading file.",
592 failure);
594 Hdr->dd = DddmpStrDup (buf);
595 Dddmp_CheckAndGotoLabel (Hdr->dd==NULL, "Error allocating memory.",
596 failure);
598 continue;
601 if (matchkeywd (buf, ".nnodes")) {
602 retValue = fscanf (fp, "%d", &(Hdr->nnodes));
603 Dddmp_CheckAndGotoLabel (retValue==EOF, "Error reading file.",
604 failure);
606 continue;
609 if (matchkeywd (buf, ".nvars")) {
610 retValue = fscanf (fp, "%d", &(Hdr->nVars));
611 Dddmp_CheckAndGotoLabel (retValue==EOF, "Error reading file.",
612 failure);
614 continue;
617 if (matchkeywd (buf, ".nsuppvars")) {
618 retValue = fscanf (fp, "%d", &(Hdr->nsuppvars));
619 Dddmp_CheckAndGotoLabel (retValue==EOF, "Error reading file.",
620 failure);
622 continue;
625 if (matchkeywd (buf, ".orderedvarnames")) {
626 Hdr->orderedVarNames = DddmpStrArrayRead (fp, Hdr->nVars);
627 Dddmp_CheckAndGotoLabel (Hdr->orderedVarNames==NULL,
628 "Error allocating memory.", failure);
630 continue;
633 if (matchkeywd (buf, ".suppvarnames")) {
634 Hdr->suppVarNames = DddmpStrArrayRead (fp, Hdr->nsuppvars);
635 Dddmp_CheckAndGotoLabel (Hdr->suppVarNames==NULL,
636 "Error allocating memory.", failure);
638 continue;
641 if matchkeywd (buf, ".ids") {
642 Hdr->ids = DddmpIntArrayRead(fp,Hdr->nsuppvars);
643 Dddmp_CheckAndGotoLabel (Hdr->ids==NULL,
644 "Error allocating memory.", failure);
646 continue;
649 if (matchkeywd (buf, ".permids")) {
650 Hdr->permids = DddmpIntArrayRead(fp,Hdr->nsuppvars);
651 Dddmp_CheckAndGotoLabel (Hdr->permids==NULL,
652 "Error allocating memory.", failure);
654 continue;
657 if (matchkeywd (buf, ".auxids")) {
658 Hdr->auxids = DddmpIntArrayRead(fp,Hdr->nsuppvars);
659 Dddmp_CheckAndGotoLabel (Hdr->auxids==NULL,
660 "Error allocating memory.", failure);
662 continue;
665 if (matchkeywd (buf, ".cnfids")) {
666 Hdr->cnfids = DddmpIntArrayRead (fp, Hdr->nsuppvars);
667 Dddmp_CheckAndGotoLabel (Hdr->cnfids==NULL,
668 "Error allocating memory.", failure);
670 continue;
673 if (matchkeywd (buf, ".nroots")) {
674 retValue = fscanf (fp, "%d", &(Hdr->nRoots));
675 Dddmp_CheckAndGotoLabel (retValue==EOF, "Error reading file.",
676 failure);
678 continue;
681 if (matchkeywd (buf, ".rootids")) {
682 Hdr->rootids = DddmpIntArrayRead(fp,Hdr->nRoots);
683 Dddmp_CheckAndGotoLabel (Hdr->rootids==NULL,
684 "Error allocating memory.", failure);
686 continue;
689 if (matchkeywd (buf, ".rootnames")) {
690 Hdr->rootnames = DddmpStrArrayRead(fp,Hdr->nRoots);
691 Dddmp_CheckAndGotoLabel (Hdr->rootnames==NULL,
692 "Error allocating memory.", failure);
694 continue;
698 if (matchkeywd (buf, ".nAddedCnfVar")) {
699 retValue = fscanf (fp, "%d", &(Hdr->nAddedCnfVar));
700 Dddmp_CheckAndGotoLabel (retValue==EOF, "Error reading file.",
701 failure);
703 continue;
707 /* END HEADER */
708 return (Hdr);
710 failure:
712 if (fileToClose == 1) {
713 fclose (fp);
716 DddmpFreeHeaderCnf (Hdr);
718 return (NULL);
722 /**Function********************************************************************
724 Synopsis [Frees the internal header structure.]
726 Description [Frees the internal header structure by freeing all internal
727 fields first.
730 SideEffects []
732 SeeAlso []
734 ******************************************************************************/
736 static void
737 DddmpFreeHeaderCnf (
738 Dddmp_Hdr_t *Hdr /* IN: pointer to header */
741 if (Hdr==NULL) {
742 return;
745 DDDMP_FREE (Hdr->ver);
746 DDDMP_FREE (Hdr->dd);
747 DddmpStrArrayFree (Hdr->orderedVarNames, Hdr->nVars);
748 DddmpStrArrayFree (Hdr->suppVarNames, Hdr->nsuppvars);
749 DDDMP_FREE (Hdr->ids);
750 DDDMP_FREE (Hdr->permids);
751 DDDMP_FREE (Hdr->auxids);
752 DDDMP_FREE (Hdr->cnfids);
753 DDDMP_FREE (Hdr->rootids);
754 DddmpStrArrayFree (Hdr->rootnames, Hdr->nRoots);
756 DDDMP_FREE (Hdr);
758 return;
761 /**Function********************************************************************
763 Synopsis [Read the CNF clauses from the file in the standard DIMACS
764 format.
767 Description [Read the CNF clauses from the file in the standard DIMACS
768 format. Store all the clauses in an internal structure for
769 future transformation into BDDs.
772 SideEffects []
774 SeeAlso []
776 ******************************************************************************/
778 static int
779 DddmpReadCnfClauses (
780 Dddmp_Hdr_t *Hdr /* IN: file header */,
781 int ***cnfTable /* OUT: CNF table for clauses */,
782 FILE *fp /* IN: source file */
785 char word[DDDMP_MAX_CNF_ROW_LENGTH];
786 int i, j, var;
787 int **cnfTableLocal = NULL;
788 int *clause = NULL;
790 cnfTableLocal = DDDMP_ALLOC (int *, Hdr->nClausesCnf);
791 clause = DDDMP_ALLOC (int, 2*Hdr->nVarsCnf+1);
793 for (i=0; i<Hdr->nClausesCnf; i++) {
794 cnfTableLocal[i] = NULL;
797 for (i=0; i<=2*Hdr->nVarsCnf; i++) {
798 clause[i] = 0;
801 i = j = 0;
802 do {
803 if (fscanf(fp, "%s", word)==EOF) {
804 if (j>0) {
805 /* force last zero */
806 strcpy(word,"0");
808 else break;
811 /* Check for Comment */
812 if (word[0] == 'c') {
813 /* Comment Found: Skip line */
814 fgets (word, DDDMP_MAX_CNF_ROW_LENGTH-1, fp);
815 break;
818 var = atoi (word);
819 Dddmp_Assert ((var>=(-Hdr->nVarsCnf))&&(var<=Hdr->nVarsCnf),
820 "Wrong num found");
821 clause[j++] = var;
822 if (var == 0) {
823 cnfTableLocal[i] = DDDMP_ALLOC (int, j);
824 while (--j >=0) {
825 cnfTableLocal[i][j] = clause[j];
827 i++;
828 j=0;
831 } while (!feof(fp));
833 Dddmp_Assert (i==Hdr->nClausesCnf,
834 "Wrong number of clauses in file");
836 #if DDDMP_DEBUG_CNF
837 for (i=0; i<Hdr->nClausesCnf; i++) {
838 fprintf (stdout, "[%4d] ", i);
839 j=0;
840 while ((var = cnfTableLocal[i][j++]) != 0) {
841 fprintf (stdout, "%d ", var);
843 fprintf (stdout, "0\n");
845 #endif
847 DDDMP_FREE (clause);
849 *cnfTable = cnfTableLocal;
851 return (DDDMP_SUCCESS);
854 /**Function********************************************************************
856 Synopsis [Transforms CNF clauses into BDDs.]
858 Description [Transforms CNF clauses into BDDs. Clauses are stored in an
859 internal structure previously read. The results can be given in
860 different format according to the mode selection:
861 IFF mode == 0 Return the Clauses without Conjunction
862 IFF mode == 1 Return the sets of BDDs without Quantification
863 IFF mode == 2 Return the sets of BDDs AFTER Existential Quantification
866 SideEffects []
868 SeeAlso []
870 ******************************************************************************/
872 static int
873 DddmpCnfClauses2Bdd (
874 Dddmp_Hdr_t *Hdr /* IN: file header */,
875 DdManager *ddMgr /* IN: DD Manager */,
876 int **cnfTable /* IN: CNF table for clauses */,
877 int mode /* IN: computation mode */,
878 DdNode ***rootsPtrPtr /* OUT: array of returned BDD roots (by reference) */
881 DdNode **rel = NULL;
882 DdNode *lit = NULL;
883 DdNode *tmp1 = NULL;
884 DdNode *tmp2 = NULL;
885 DdNode **rootsPtr = NULL;
886 DdNode *cubeAllVar = NULL;
887 DdNode *cubeBddVar = NULL;
888 DdNode *cubeCnfVar = NULL;
889 int i, j, k, n, var1, var2, fromLine, toLine;
891 rootsPtr = NULL;
892 *rootsPtrPtr = NULL;
894 /*-------------------------- Read The Clauses -----------------------------*/
896 rel = DDDMP_ALLOC (DdNode *, Hdr->nClausesCnf);
898 cubeBddVar = Cudd_ReadOne (ddMgr);
899 cubeCnfVar = Cudd_ReadOne (ddMgr);
900 cubeAllVar = Cudd_ReadOne (ddMgr);
901 Cudd_Ref (cubeBddVar);
902 Cudd_Ref (cubeCnfVar);
903 Cudd_Ref (cubeAllVar);
905 for (i=0; i<Hdr->nClausesCnf; i++) {
906 rel[i] = Cudd_Not (Cudd_ReadOne (ddMgr));
907 Cudd_Ref (rel[i]);
908 j=0;
909 while ((var1 = cnfTable[i][j++]) != 0) {
911 /* Deal with the Literal */
912 var2 = abs (var1);
913 n = (-1);
914 for (k=0; k<Hdr->nsuppvars; k++) {
915 if (Hdr->cnfids[k] == var2) {
916 n = k;
917 break;
921 if (n == (-1)) {
922 lit = Cudd_bddIthVar (ddMgr, var2);
924 /* Create the cubes of CNF Variables */
925 tmp1 = Cudd_bddAnd (ddMgr, cubeCnfVar, lit);
926 Cudd_Ref (tmp1);
927 Cudd_RecursiveDeref (ddMgr, cubeCnfVar);
928 cubeCnfVar = tmp1;
930 } else {
931 lit = Cudd_bddIthVar (ddMgr, Hdr->ids[n]);
933 /* Create the cubes of BDD Variables */
934 tmp1 = Cudd_bddAnd (ddMgr, cubeBddVar, lit);
935 Cudd_Ref (tmp1);
936 Cudd_RecursiveDeref (ddMgr, cubeBddVar);
937 cubeBddVar = tmp1;
940 /* Create the cubes of ALL Variables */
941 tmp1 = Cudd_bddAnd (ddMgr, cubeAllVar, lit);
942 Cudd_Ref (tmp1);
943 Cudd_RecursiveDeref (ddMgr, cubeAllVar);
944 cubeAllVar = tmp1;
946 /* Deal with Relations */
947 if (var1<0) {
948 lit = Cudd_Not (lit);
950 tmp1 = Cudd_bddOr (ddMgr, rel[i], lit);
951 Cudd_Ref (tmp1);
952 Cudd_RecursiveDeref (ddMgr, rel[i]);
953 rel[i] = tmp1;
958 * Mode == 0 Return the Clauses without Conjunction
961 if (mode == 0) {
962 return (DDDMP_SUCCESS);
965 rootsPtr = DDDMP_ALLOC (DdNode *, Hdr->nRoots);
966 Dddmp_CheckAndGotoLabel (rootsPtr==NULL, "Error allocating memory.",
967 failure);
969 for (i=0; i<Hdr->nRoots; i++) {
970 if (i == (Hdr->nRoots-1)) {
971 fromLine = Hdr->rootids[i] - 1;
972 toLine = Hdr->nClausesCnf;
973 } else {
974 fromLine = Hdr->rootids[i] - 1;
975 toLine = Hdr->rootids[i+1];
978 tmp1 = Cudd_ReadOne (ddMgr);
979 Cudd_Ref (tmp1);
980 for (j=fromLine; j<toLine; j++) {
981 tmp2 = Cudd_bddAnd (ddMgr, rel[j], tmp1);
982 Cudd_Ref (tmp2);
983 Cudd_RecursiveDeref (ddMgr, tmp1);
984 Cudd_RecursiveDeref (ddMgr, rel[j]);
985 tmp1 = tmp2;
987 rootsPtr[i] = tmp1;
990 DDDMP_FREE (rel);
993 * Mode == 1 Return the sets of BDDs without Quantification
996 if (mode == 1) {
997 *rootsPtrPtr = rootsPtr;
998 return (DDDMP_SUCCESS);
1002 * Mode == 2 Return the sets of BDDs AFTER Existential Quantification
1005 #if DDDMP_DEBUG_CNF
1006 cubeBddVar = Cudd_ReadOne (ddMgr);
1007 Cudd_Ref (cubeBddVar);
1008 for (i=0; i<Hdr->nsuppvars; i++) {
1009 lit = Cudd_bddIthVar (ddMgr, Hdr->ids[i]);
1010 tmp1 = Cudd_bddAnd (ddMgr, cubeBddVar, lit);
1011 Cudd_Ref (tmp1);
1012 Cudd_RecursiveDeref (ddMgr, cubeBddVar);
1013 cubeBddVar = tmp1;
1016 cubeCnfVar = Cudd_bddExistAbstract (ddMgr, cubeAllVar, cubeBddVar);
1017 #endif
1019 for (i=0; i<Hdr->nRoots; i++) {
1020 #if DDDMP_DEBUG_CNF
1021 fprintf (stdout, "rootsPtr Before Exist:\n");
1022 Cudd_PrintDebug (ddMgr, rootsPtr[i], 0, 3);
1023 #endif
1025 tmp1 = Cudd_bddExistAbstract (ddMgr, rootsPtr[i], cubeCnfVar);
1026 Cudd_RecursiveDeref (ddMgr, rootsPtr[i]);
1027 rootsPtr[i] = tmp1;
1029 #if DDDMP_DEBUG_CNF
1030 fprintf (stdout, "rootsPtr After Exist:\n");
1031 Cudd_PrintDebug (ddMgr, rootsPtr[i], 0, 3);
1032 #endif
1035 #if DDDMP_DEBUG_CNF
1036 fprintf (stdout, "cubeAllVar:\n");
1037 Cudd_PrintDebug (ddMgr, cubeAllVar, 0, 3);
1038 fprintf (stdout, "cubeBddVar:\n");
1039 Cudd_PrintDebug (ddMgr, cubeBddVar, 0, 3);
1040 fprintf (stdout, "cubeCnfVar:\n");
1041 Cudd_PrintDebug (ddMgr, cubeCnfVar, 0, 3);
1042 #endif
1044 Cudd_RecursiveDeref (ddMgr, cubeAllVar);
1045 Cudd_RecursiveDeref (ddMgr, cubeBddVar);
1046 Cudd_RecursiveDeref (ddMgr, cubeCnfVar);
1047 *rootsPtrPtr = rootsPtr;
1049 return (DDDMP_SUCCESS);
1052 * Failure Condition
1055 failure:
1057 DDDMP_FREE (rel);
1058 DDDMP_FREE (rootsPtrPtr);
1060 return (DDDMP_FAILURE);