cnf::Formula Class Reference

An instance implements a complete CNF formula using several Clause instances. More...

#include <formula.h>

+ Collaboration diagram for cnf::Formula:

List of all members.

Public Member Functions

 Formula (VariableSet *, std::ostream *, std::string)
 Creates an instance of Formula which will use the VariableSet of the input. Creates also the DIMACS formated file corresponding to this formula.
 ~Formula ()
 When this instance is destroyed, prints the clauses still in the cnfs attribute.
void newClause ()
 Reinitialise the bufferClause attribute to an emtpy one.
void addLit (unsigned int, bool, unsigned int, unsigned int)
 Adds a new variable to the bufferClause attribute, negated if sign==false.
void addLit (unsigned int, bool, unsigned int, unsigned int, unsigned int)
 Adds a new variable to the bufferClause attribute, negated if sign==false.
void addLitCode (int)
 Adds a new variable to the bufferClause attribute from its code.
void pushClause ()
 Use this method to put the current clause in the formula modeled by this instance.
void flushCNF ()
 Prints the content of the cnfs attribute to the output and reinitialises it.

Private Attributes

VariableSetvarSet
 The set of the variables.
std::vector< Clausecnfs
 Stores the CNF clauses of this Formula as Clause instances.
Clause bufferClause
 The clause currently being modified. It is modified by the newClause(), addLit() and pushClause().
std::ostream * output
 The stream to which the instance must be printed.

Detailed Description

An instance implements a complete CNF formula using several Clause instances.

A CNF formula is the conjunction of an arbitrary number of disjunctive clauses. These are represented by Clause instances stored in a std::vector. Note that the codes of the literals of the clauses are given by a VariableSet instance connected to this one.

The CNF is printed in the ostream specified at the construction as it is build. For efficiency however, it is printed by bursts of at most CLAUSE_BUFFER_SIZE clauses.

Definition at line 35 of file formula.h.


Constructor & Destructor Documentation

Formula::Formula ( VariableSet S,
std::ostream *  out,
std::string  comment 
)

Creates an instance of Formula which will use the VariableSet of the input. Creates also the DIMACS formated file corresponding to this formula.

For more info on the DIMACS encoding, see this. A commentary will be added in the beginning of the file.

Parameters:
outA pointer to the stream where the file must be written.
commentThe commentary to add on the first line.
SA pointer to the VariableSet to link to this Formula.

Definition at line 17 of file formula.cpp.

{
        varSet = S;
        // initializing output
        output = out;
        (*output) << "c " << comment << std::endl
               << "p cnf " << varSet->getCard()
               << " 1 " << std::endl;
}

When this instance is destroyed, prints the clauses still in the cnfs attribute.

Definition at line 29 of file formula.cpp.

{
        flushCNF();
}

Member Function Documentation

void Formula::addLit ( unsigned int  s,
bool  sign,
unsigned int  i,
unsigned int  j 
)

Adds a new variable to the bufferClause attribute, negated if sign==false.

Parameters:
sThe subset in which to take the variable
signFalse if the literal is negated, true otherwise.
iFirst coordinate.
jSecond coordinate.

Definition at line 41 of file formula.cpp.

{
        if (sign)
                bufferClause.addLit((int)varSet->getVarCode(s,i,j));
        else
                bufferClause.addLit((-1)*(int)varSet->getVarCode(s,i,j));
}
void Formula::addLit ( unsigned int  s,
bool  sign,
unsigned int  i,
unsigned int  j,
unsigned int  k 
)

Adds a new variable to the bufferClause attribute, negated if sign==false.

Parameters:
sThe subset in which to take the variable
signFalse if the literal is negated, true otherwise.
iFirst coordinate.
jSecond coordinate.
kThird coordinate.

Definition at line 50 of file formula.cpp.

{
        if (sign)
                bufferClause.addLit((int)varSet->getVarCode(s,i,j,k));
        else
                bufferClause.addLit((-1)*(int)varSet->getVarCode(s,i,j,k));
}
void Formula::addLitCode ( int  c)

Adds a new variable to the bufferClause attribute from its code.

Parameters:
cThe code of the variable to add to the clause.

Definition at line 59 of file formula.cpp.

{
        if (
                ((c < 0)  &&  ((-1)*c > (int)varSet->getCard()))
                || ((c >= 0)  &&  (c > (int)varSet->getCard()))
                )
        {
                std::cout<<"ERROR in Formula::addLitCode(), the code is"
                        " too big: c="<<c<<" is greater than the varSet"
                        " size, "<<varSet->getCard()<<std::endl;
                exit(1);
        }
        else
                bufferClause.addLit(c);
}

Prints the content of the cnfs attribute to the output and reinitialises it.

Definition at line 84 of file formula.cpp.

{
        for (unsigned int i=0; i<cnfs.size(); i++)
                (*output) << cnfs[i].toString() << "\n";
        output->flush();
        cnfs.clear();
}

Reinitialise the bufferClause attribute to an emtpy one.

Definition at line 35 of file formula.cpp.

Use this method to put the current clause in the formula modeled by this instance.

Definition at line 76 of file formula.cpp.

{
        cnfs.push_back(bufferClause);
        if (cnfs.size() > CLAUSE_BUFFER_SIZE)
                flushCNF();
}

Member Data Documentation

The clause currently being modified. It is modified by the newClause(), addLit() and pushClause().

Definition at line 57 of file formula.h.

std::vector<Clause> cnf::Formula::cnfs [private]

Stores the CNF clauses of this Formula as Clause instances.

Definition at line 50 of file formula.h.

std::ostream* cnf::Formula::output [private]

The stream to which the instance must be printed.

Definition at line 63 of file formula.h.

The set of the variables.

It "lives" independantly of the formula so it is a pointer to it.

Definition at line 44 of file formula.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Defines