1 /**CFile**********************************************************************
3 FileName [dddmpLoadCnf.c]
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]
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 ******************************************************************************/
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
80 Dddmp_cuddBddArrayLoadCnf is used through a dummy array.
81 The results is returned in different formats depending on the
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 ******************************************************************************/
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 */
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
);
120 "Warning: %d BDD roots found in file. Only first retrieved.\n",
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
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 */
166 retValueBis
= Cudd_DebugCheck (ddMgr
);
167 if (retValueBis
== 1) {
168 fprintf (stderr
, "Inconsistency Found During CNF Load.\n");
171 if (retValueBis
== CUDD_OUT_OF_MEM
) {
172 fprintf (stderr
, "Out of Memory During CNF Load.\n");
179 retValue
= DddmpCuddDdArrayLoadCnf (ddMgr
, rootmatchmode
,
180 rootmatchnames
, varmatchmode
, varmatchnames
, varmatchauxids
,
181 varcomposeids
, mode
, file
, fp
, rootsPtrPtr
, nRoots
);
185 retValueBis
= Cudd_DebugCheck (ddMgr
);
186 if (retValueBis
== 1) {
187 fprintf (stderr
, "Inconsistency Found During CNF Load.\n");
190 if (retValueBis
== CUDD_OUT_OF_MEM
) {
191 fprintf (stderr
, "Out of Memory During CNF Load.\n");
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.
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 */
233 char **tmpOrderedVarNames
= NULL
;
234 char **tmpSuppVarNames
= NULL
;
235 int *tmpVarIds
= NULL
;
236 int *tmpVarComposeIds
= NULL
;
237 int *tmpVarAuxIds
= NULL
;
241 fp
= fopen (file
, "r");
242 Dddmp_CheckAndGotoLabel (fp
==NULL
, "Error opening file.",
247 Hdr
= DddmpBddReadHeaderCnf (NULL
, fp
);
249 Dddmp_CheckAndGotoLabel (Hdr
->nnodes
==0, "Zero number of nodes.",
253 * Number of variables (tot and support)
257 *nsuppvars
= Hdr
->nsuppvars
;
263 if (Hdr
->suppVarNames
!= NULL
) {
264 tmpSuppVarNames
= DDDMP_ALLOC (char *, *nsuppvars
);
265 Dddmp_CheckAndGotoLabel (tmpSuppVarNames
==NULL
, "Error allocating memory.",
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
;
278 *suppVarNames
= NULL
;
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
;
300 *orderedVarNames
= NULL
;
307 if (Hdr
->ids
!= NULL
) {
308 tmpVarIds
= DDDMP_ALLOC (int, *nsuppvars
);
309 Dddmp_CheckAndGotoLabel (tmpVarIds
==NULL
, "Error allocating memory.",
311 for (i
=0; i
<*nsuppvars
; i
++) {
312 tmpVarIds
[i
] = Hdr
->ids
[i
];
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
;
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
;
358 *nRoots
= Hdr
->nRoots
;
364 if (fileToClose
== 1) {
368 DddmpFreeHeaderCnf (Hdr
);
370 return (DDDMP_SUCCESS
);
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
390 Description [Reads a dump file representing the argument BDDs in CNF
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 ******************************************************************************/
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
;
428 fp
= fopen (file
, "r");
429 Dddmp_CheckAndGotoLabel (fp
==NULL
, "Error opening file.",
434 /*--------------------------- Read the Header -----------------------------*/
436 Hdr
= DddmpBddReadHeaderCnf (NULL
, fp
);
438 Dddmp_CheckAndGotoLabel (Hdr
->nnodes
==0, "Zero number of nodes.",
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
;
461 for (i
=0; i
<Hdr
->nClausesCnf
; i
++) {
462 DDDMP_FREE (cnfTable
[i
]);
464 DDDMP_FREE (cnfTable
);
466 DddmpFreeHeaderCnf (Hdr
);
468 return (DDDMP_SUCCESS
);
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 ! */
490 return (DDDMP_FAILURE
);
493 /**Function********************************************************************
495 Synopsis [Reads a the header of a dump file representing the argument
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.
507 ******************************************************************************/
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;
520 fp
= fopen (file
, "r");
521 Dddmp_CheckAndGotoLabel (fp
==NULL
, "Error opening file.",
527 Hdr
= DDDMP_ALLOC (Dddmp_Hdr_t
, 1);
534 Hdr
->ddType
= DDDMP_CNF
;
535 Hdr
->varinfo
= DDDMP_VARIDS
;
540 Hdr
->orderedVarNames
= NULL
;
541 Hdr
->suppVarNames
= NULL
;
548 Hdr
->rootnames
= NULL
;
549 Hdr
->nAddedCnfVar
= 0;
551 Hdr
->nClausesCnf
= 0;
553 while (fscanf (fp
, "%s", buf
) != EOF
) {
555 /* Init Problem Line */
557 fscanf (fp
, "%*s %d %d", &nv
, &nc
);
559 Hdr
->nClausesCnf
= nc
;
563 /* CNF Comment Line */
565 if (fscanf (fp
, "%s", buf
) == EOF
) {
572 fgets (buf
, DDDMP_MAXSTRLEN
, fp
);
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.",
582 Hdr
->ver
=DddmpStrDup(buf
);
583 Dddmp_CheckAndGotoLabel (Hdr
->ver
==NULL
,
584 "Error allocating memory.", failure
);
589 if (matchkeywd (buf
, ".dd")) {
590 retValue
= fscanf (fp
, "%s", buf
);
591 Dddmp_CheckAndGotoLabel (retValue
==EOF
, "Error reading file.",
594 Hdr
->dd
= DddmpStrDup (buf
);
595 Dddmp_CheckAndGotoLabel (Hdr
->dd
==NULL
, "Error allocating memory.",
601 if (matchkeywd (buf
, ".nnodes")) {
602 retValue
= fscanf (fp
, "%d", &(Hdr
->nnodes
));
603 Dddmp_CheckAndGotoLabel (retValue
==EOF
, "Error reading file.",
609 if (matchkeywd (buf
, ".nvars")) {
610 retValue
= fscanf (fp
, "%d", &(Hdr
->nVars
));
611 Dddmp_CheckAndGotoLabel (retValue
==EOF
, "Error reading file.",
617 if (matchkeywd (buf
, ".nsuppvars")) {
618 retValue
= fscanf (fp
, "%d", &(Hdr
->nsuppvars
));
619 Dddmp_CheckAndGotoLabel (retValue
==EOF
, "Error reading file.",
625 if (matchkeywd (buf
, ".orderedvarnames")) {
626 Hdr
->orderedVarNames
= DddmpStrArrayRead (fp
, Hdr
->nVars
);
627 Dddmp_CheckAndGotoLabel (Hdr
->orderedVarNames
==NULL
,
628 "Error allocating memory.", failure
);
633 if (matchkeywd (buf
, ".suppvarnames")) {
634 Hdr
->suppVarNames
= DddmpStrArrayRead (fp
, Hdr
->nsuppvars
);
635 Dddmp_CheckAndGotoLabel (Hdr
->suppVarNames
==NULL
,
636 "Error allocating memory.", failure
);
641 if matchkeywd (buf
, ".ids") {
642 Hdr
->ids
= DddmpIntArrayRead(fp
,Hdr
->nsuppvars
);
643 Dddmp_CheckAndGotoLabel (Hdr
->ids
==NULL
,
644 "Error allocating memory.", failure
);
649 if (matchkeywd (buf
, ".permids")) {
650 Hdr
->permids
= DddmpIntArrayRead(fp
,Hdr
->nsuppvars
);
651 Dddmp_CheckAndGotoLabel (Hdr
->permids
==NULL
,
652 "Error allocating memory.", failure
);
657 if (matchkeywd (buf
, ".auxids")) {
658 Hdr
->auxids
= DddmpIntArrayRead(fp
,Hdr
->nsuppvars
);
659 Dddmp_CheckAndGotoLabel (Hdr
->auxids
==NULL
,
660 "Error allocating memory.", failure
);
665 if (matchkeywd (buf
, ".cnfids")) {
666 Hdr
->cnfids
= DddmpIntArrayRead (fp
, Hdr
->nsuppvars
);
667 Dddmp_CheckAndGotoLabel (Hdr
->cnfids
==NULL
,
668 "Error allocating memory.", failure
);
673 if (matchkeywd (buf
, ".nroots")) {
674 retValue
= fscanf (fp
, "%d", &(Hdr
->nRoots
));
675 Dddmp_CheckAndGotoLabel (retValue
==EOF
, "Error reading file.",
681 if (matchkeywd (buf
, ".rootids")) {
682 Hdr
->rootids
= DddmpIntArrayRead(fp
,Hdr
->nRoots
);
683 Dddmp_CheckAndGotoLabel (Hdr
->rootids
==NULL
,
684 "Error allocating memory.", failure
);
689 if (matchkeywd (buf
, ".rootnames")) {
690 Hdr
->rootnames
= DddmpStrArrayRead(fp
,Hdr
->nRoots
);
691 Dddmp_CheckAndGotoLabel (Hdr
->rootnames
==NULL
,
692 "Error allocating memory.", failure
);
698 if (matchkeywd (buf
, ".nAddedCnfVar")) {
699 retValue
= fscanf (fp
, "%d", &(Hdr
->nAddedCnfVar
));
700 Dddmp_CheckAndGotoLabel (retValue
==EOF
, "Error reading file.",
712 if (fileToClose
== 1) {
716 DddmpFreeHeaderCnf (Hdr
);
722 /**Function********************************************************************
724 Synopsis [Frees the internal header structure.]
726 Description [Frees the internal header structure by freeing all internal
734 ******************************************************************************/
738 Dddmp_Hdr_t
*Hdr
/* IN: pointer to header */
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
);
761 /**Function********************************************************************
763 Synopsis [Read the CNF clauses from the file in the standard DIMACS
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.
776 ******************************************************************************/
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
];
787 int **cnfTableLocal
= 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
++) {
803 if (fscanf(fp
, "%s", word
)==EOF
) {
805 /* force last zero */
811 /* Check for Comment */
812 if (word
[0] == 'c') {
813 /* Comment Found: Skip line */
814 fgets (word
, DDDMP_MAX_CNF_ROW_LENGTH
-1, fp
);
819 Dddmp_Assert ((var
>=(-Hdr
->nVarsCnf
))&&(var
<=Hdr
->nVarsCnf
),
823 cnfTableLocal
[i
] = DDDMP_ALLOC (int, j
);
825 cnfTableLocal
[i
][j
] = clause
[j
];
833 Dddmp_Assert (i
==Hdr
->nClausesCnf
,
834 "Wrong number of clauses in file");
837 for (i
=0; i
<Hdr
->nClausesCnf
; i
++) {
838 fprintf (stdout
, "[%4d] ", i
);
840 while ((var
= cnfTableLocal
[i
][j
++]) != 0) {
841 fprintf (stdout
, "%d ", var
);
843 fprintf (stdout
, "0\n");
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
870 ******************************************************************************/
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) */
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
;
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
));
909 while ((var1
= cnfTable
[i
][j
++]) != 0) {
911 /* Deal with the Literal */
914 for (k
=0; k
<Hdr
->nsuppvars
; k
++) {
915 if (Hdr
->cnfids
[k
] == var2
) {
922 lit
= Cudd_bddIthVar (ddMgr
, var2
);
924 /* Create the cubes of CNF Variables */
925 tmp1
= Cudd_bddAnd (ddMgr
, cubeCnfVar
, lit
);
927 Cudd_RecursiveDeref (ddMgr
, cubeCnfVar
);
931 lit
= Cudd_bddIthVar (ddMgr
, Hdr
->ids
[n
]);
933 /* Create the cubes of BDD Variables */
934 tmp1
= Cudd_bddAnd (ddMgr
, cubeBddVar
, lit
);
936 Cudd_RecursiveDeref (ddMgr
, cubeBddVar
);
940 /* Create the cubes of ALL Variables */
941 tmp1
= Cudd_bddAnd (ddMgr
, cubeAllVar
, lit
);
943 Cudd_RecursiveDeref (ddMgr
, cubeAllVar
);
946 /* Deal with Relations */
948 lit
= Cudd_Not (lit
);
950 tmp1
= Cudd_bddOr (ddMgr
, rel
[i
], lit
);
952 Cudd_RecursiveDeref (ddMgr
, rel
[i
]);
958 * Mode == 0 Return the Clauses without Conjunction
962 return (DDDMP_SUCCESS
);
965 rootsPtr
= DDDMP_ALLOC (DdNode
*, Hdr
->nRoots
);
966 Dddmp_CheckAndGotoLabel (rootsPtr
==NULL
, "Error allocating memory.",
969 for (i
=0; i
<Hdr
->nRoots
; i
++) {
970 if (i
== (Hdr
->nRoots
-1)) {
971 fromLine
= Hdr
->rootids
[i
] - 1;
972 toLine
= Hdr
->nClausesCnf
;
974 fromLine
= Hdr
->rootids
[i
] - 1;
975 toLine
= Hdr
->rootids
[i
+1];
978 tmp1
= Cudd_ReadOne (ddMgr
);
980 for (j
=fromLine
; j
<toLine
; j
++) {
981 tmp2
= Cudd_bddAnd (ddMgr
, rel
[j
], tmp1
);
983 Cudd_RecursiveDeref (ddMgr
, tmp1
);
984 Cudd_RecursiveDeref (ddMgr
, rel
[j
]);
993 * Mode == 1 Return the sets of BDDs without Quantification
997 *rootsPtrPtr
= rootsPtr
;
998 return (DDDMP_SUCCESS
);
1002 * Mode == 2 Return the sets of BDDs AFTER Existential Quantification
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
);
1012 Cudd_RecursiveDeref (ddMgr
, cubeBddVar
);
1016 cubeCnfVar
= Cudd_bddExistAbstract (ddMgr
, cubeAllVar
, cubeBddVar
);
1019 for (i
=0; i
<Hdr
->nRoots
; i
++) {
1021 fprintf (stdout
, "rootsPtr Before Exist:\n");
1022 Cudd_PrintDebug (ddMgr
, rootsPtr
[i
], 0, 3);
1025 tmp1
= Cudd_bddExistAbstract (ddMgr
, rootsPtr
[i
], cubeCnfVar
);
1026 Cudd_RecursiveDeref (ddMgr
, rootsPtr
[i
]);
1030 fprintf (stdout
, "rootsPtr After Exist:\n");
1031 Cudd_PrintDebug (ddMgr
, rootsPtr
[i
], 0, 3);
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);
1044 Cudd_RecursiveDeref (ddMgr
, cubeAllVar
);
1045 Cudd_RecursiveDeref (ddMgr
, cubeBddVar
);
1046 Cudd_RecursiveDeref (ddMgr
, cubeCnfVar
);
1047 *rootsPtrPtr
= rootsPtr
;
1049 return (DDDMP_SUCCESS
);
1058 DDDMP_FREE (rootsPtrPtr
);
1060 return (DDDMP_FAILURE
);