IOHandler.cpp
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "stdinc.h"
#include "IOHandler.h"
#include "Scanner.h"
#include "VarNames.h"
#include "NameFactory.h"
#include "FrobbyStringStream.h"
#include "DataType.h"
#include "BigTermConsumer.h"
#include "CoefBigTermConsumer.h"
#include "CountingIOHandler.h"
#include "NewMonosIOHandler.h"
#include "MonosIOHandler.h"
#include "Macaulay2IOHandler.h"
#include "Fourti2IOHandler.h"
#include "NullIOHandler.h"
#include "CoCoA4IOHandler.h"
#include "SingularIOHandler.h"
#include "error.h"
#include "BigTermRecorder.h"
#include "InputConsumer.h"
IOHandler::~IOHandler() {
}
void IOHandler::readIdeal(Scanner& in, InputConsumer& consumer) {
doReadIdeal(in, consumer);
}
void IOHandler::readIdeals(Scanner& in, InputConsumer& consumer) {
doReadIdeals(in, consumer);
}
void IOHandler::readTerm
(Scanner& in, const VarNames& names, vector<mpz_class>& term) {
InputConsumer consumer;
consumer.consumeRing(names);
consumer.beginIdeal();
doReadTerm(in, consumer);
consumer.endIdeal();
ASSERT(!consumer.empty());
unique_ptr<BigIdeal> ideal = consumer.releaseBigIdeal();
ASSERT(consumer.empty());
ASSERT(ideal->getGeneratorCount() == 1);
term = (*ideal)[0];
}
void IOHandler::readPolynomial(Scanner& in, CoefBigTermConsumer& consumer) {
doReadPolynomial(in, consumer);
}
void IOHandler::readSatBinomIdeal(Scanner& in, SatBinomConsumer& consumer) {
doReadSatBinomIdeal(in, consumer);
}
void IOHandler::writeTerm(const vector<mpz_class>& term,
const VarNames& names, FILE* out) {
doWriteTerm(term, names, out);
}
bool IOHandler::hasMoreInput(Scanner& in) const {
return doHasMoreInput(in);
}
const char* IOHandler::getName() const {
return doGetName();
}
const char* IOHandler::getDescription() const {
return doGetDescription();
}
unique_ptr<BigTermConsumer> IOHandler::createIdealWriter(FILE* out) {
if (!supportsOutput(DataType::getMonomialIdealType())) {
throwError<UnsupportedException>
("The " + string(getName()) +
" format does not support output of a monomial ideal.");
}
return unique_ptr<BigTermConsumer>(doCreateIdealWriter(out));
}
unique_ptr<BigTermConsumer> IOHandler::createIdealListWriter(FILE* out) {
if (!supportsOutput(DataType::getMonomialIdealListType())) {
throwError<UnsupportedException>
("The " + string(getName()) +
" format does not support output of a list of monomial ideals.");
}
// This is the same kind of object as for a non-list ideal
// writer. The only difference is that we checked for support for
// output of lists above.
return unique_ptr<BigTermConsumer>(doCreateIdealWriter(out));
}
unique_ptr<CoefBigTermConsumer> IOHandler::createPolynomialWriter(FILE* out) {
if (!supportsOutput(DataType::getPolynomialType())) {
throwError<UnsupportedException>
("The " + string(getName()) +
" format does not support output of a polynomial.");
}
return unique_ptr<CoefBigTermConsumer>(doCreatePolynomialWriter(out));
}
bool IOHandler::supportsInput(const DataType& type) const {
return doSupportsInput(type);
}
bool IOHandler::supportsOutput(const DataType& type) const {
return doSupportsOutput(type);
}
namespace {
typedef NameFactory<IOHandler> IOHandlerFactory;
IOHandlerFactory getIOHandlerFactory() {
IOHandlerFactory factory("format");
nameFactoryRegister<IO::Macaulay2IOHandler>(factory);
nameFactoryRegister<IO::CoCoA4IOHandler>(factory);
nameFactoryRegister<IO::SingularIOHandler>(factory);
nameFactoryRegister<IO::MonosIOHandler>(factory);
nameFactoryRegister<IO::NewMonosIOHandler>(factory);
nameFactoryRegister<IO::Fourti2IOHandler>(factory);
nameFactoryRegister<IO::NullIOHandler>(factory);
nameFactoryRegister<IO::CountingIOHandler>(factory);
return factory;
}
}
unique_ptr<IOHandler> createIOHandler(const string& prefix) {
return createWithPrefix(getIOHandlerFactory(), prefix);
}
unique_ptr<IOHandler> createOHandler(const string& input, const string& output) {
if (output == getFormatNameIndicatingToUseInputFormatAsOutputFormat())
return createIOHandler(input);
else
return createIOHandler(output);
}
void getIOHandlerNames(vector<string>& names) {
getIOHandlerFactory().getNamesWithPrefix("", names);
}
void readFrobeniusInstance(Scanner& in, vector<mpz_class>& numbers) {
numbers.clear();
string number;
mpz_class n;
while (!in.matchEOF()) {
in.readInteger(n);
if (n <= 1) {
FrobbyStringStream errorMsg;
errorMsg << "Read the number " << n
<< " while reading Frobenius instance. "
<< "Only integers strictly larger than 1 are valid.";
reportSyntaxError(in, errorMsg);
}
numbers.push_back(n);
}
if (numbers.empty())
reportSyntaxError
(in, "Read empty Frobenius instance, which is not allowed.");
mpz_class gcd = numbers[0];
for (size_t i = 1; i < numbers.size(); ++i)
mpz_gcd(gcd.get_mpz_t(), gcd.get_mpz_t(), numbers[i].get_mpz_t());
if (gcd != 1) {
// Maybe not strictly speaking a syntax error, but that category
// of errors still fits best.
FrobbyStringStream errorMsg;
errorMsg << "The numbers in the Frobenius instance are not "
<< "relatively prime. They are all divisible by "
<< gcd << '.';
reportSyntaxError(in, errorMsg);
}
}
string autoDetectFormat(Scanner& in) {
// We guess based on the initial non-whitespace character. We detect
// more than the correct initial character to try to guess the
// intended format in the face of mistakes.
in.eatWhite();
switch (in.peek()) {
case 'U': // correct
case 'u': // incorrect
return IO::CoCoA4IOHandler::staticGetName();
case 'r': // correct
return IO::SingularIOHandler::staticGetName();
case '(': // correct
case 'l': // incorrect
case ')': // incorrect
return IO::NewMonosIOHandler::staticGetName();
case '0': case '1': case '2': case '3': case '4': // correct
case '5': case '6': case '7': case '8': case '9': // correct
case '+': case '-': // incorrect
return IO::Fourti2IOHandler::staticGetName();
case 'v': // correct
return IO::MonosIOHandler::staticGetName();
case 'R': // correct
default: // incorrect
return IO::Macaulay2IOHandler::staticGetName();
}
}
string getFormatNameIndicatingToUseInputFormatAsOutputFormat() {
return "input";
}
string getFormatNameIndicatingToGuessTheInputFormat() {
return "autodetect";
}
BigTermConsumer.h
BigTermRecorder.h
CoCoA4IOHandler.h
CoefBigTermConsumer.h
CountingIOHandler.h
DataType.h
Fourti2IOHandler.h
FrobbyStringStream.h
readFrobeniusInstance
void readFrobeniusInstance(Scanner &in, vector< mpz_class > &numbers)
Definition IOHandler.cpp:160
getIOHandlerNames
void getIOHandlerNames(vector< string > &names)
Add the name of each fomat to names.
Definition IOHandler.cpp:156
getFormatNameIndicatingToUseInputFormatAsOutputFormat
string getFormatNameIndicatingToUseInputFormatAsOutputFormat()
Using the returned string in place of an (output) format name indicates to use the input format as th...
Definition IOHandler.cpp:230
getFormatNameIndicatingToGuessTheInputFormat
string getFormatNameIndicatingToGuessTheInputFormat()
Using the returned string in place of an (input) format name indicates to guess the format based on w...
Definition IOHandler.cpp:234
createOHandler
unique_ptr< IOHandler > createOHandler(const string &input, const string &output)
Returns an IOHandler for the output format.
Definition IOHandler.cpp:149
createIOHandler
unique_ptr< IOHandler > createIOHandler(const string &prefix)
Returns an IOHandler for the format whose name has the given prefix.
Definition IOHandler.cpp:145
autoDetectFormat
string autoDetectFormat(Scanner &in)
Return the format of what in is reading based on the first non-whitespace character.
Definition IOHandler.cpp:198
IOHandler.h
InputConsumer.h
Macaulay2IOHandler.h
MonosIOHandler.h
NameFactory.h
nameFactoryRegister
void nameFactoryRegister(NameFactory< AbstractProduct > &factory)
Registers the string returned by ConcreteProduct::getStaticName() to a function that default-construc...
Definition NameFactory.h:142
createWithPrefix
unique_ptr< AbstractProduct > createWithPrefix(const NameFactory< AbstractProduct > &factory, const string &prefix)
Creates the unique product that has the indicated prefix, or create the actual product that has name ...
Definition NameFactory.h:154
NewMonosIOHandler.h
NullIOHandler.h
Scanner.h
SingularIOHandler.h
VarNames.h
CoefBigTermConsumer
Definition CoefBigTermConsumer.h:29
DataType
The intention of this class is to describe the different kinds of mathematical structures that Frobby...
Definition DataType.h:29
DataType::getMonomialIdealListType
static const DataType & getMonomialIdealListType()
Returns the one and only instance for monomial ideal lists.
Definition DataType.cpp:54
DataType::getMonomialIdealType
static const DataType & getMonomialIdealType()
Returns the one and only instance for monomial ideals.
Definition DataType.cpp:45
DataType::getPolynomialType
static const DataType & getPolynomialType()
Returns the one and only instance for polynomials.
Definition DataType.cpp:50
FrobbyStringStream
A replacement for stringstream.
Definition FrobbyStringStream.h:26
IOHandler::doWriteTerm
virtual void doWriteTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)=0
IOHandler::doReadSatBinomIdeal
virtual void doReadSatBinomIdeal(Scanner &in, SatBinomConsumer &consumer)=0
IOHandler::readSatBinomIdeal
void readSatBinomIdeal(Scanner &in, SatBinomConsumer &consumer)
Definition IOHandler.cpp:68
IOHandler::readPolynomial
void readPolynomial(Scanner &in, CoefBigTermConsumer &consumer)
Definition IOHandler.cpp:64
IOHandler::doSupportsInput
virtual bool doSupportsInput(const DataType &type) const =0
IOHandler::doReadIdeals
virtual void doReadIdeals(Scanner &in, InputConsumer &consumer)=0
IOHandler::doCreatePolynomialWriter
virtual CoefBigTermConsumer * doCreatePolynomialWriter(FILE *out)=0
IOHandler::writeTerm
void writeTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
Definition IOHandler.cpp:72
IOHandler::doReadPolynomial
virtual void doReadPolynomial(Scanner &in, CoefBigTermConsumer &consumer)=0
IOHandler::doGetDescription
virtual const char * doGetDescription() const =0
IOHandler::doCreateIdealWriter
virtual BigTermConsumer * doCreateIdealWriter(FILE *out)=0
IOHandler::readIdeal
void readIdeal(Scanner &in, InputConsumer &consumer)
Read an ideal and feed it to the consumer.
Definition IOHandler.cpp:42
IOHandler::supportsInput
bool supportsInput(const DataType &type) const
Definition IOHandler.cpp:119
IOHandler::getName
const char * getName() const
Definition IOHandler.cpp:81
IOHandler::createIdealWriter
unique_ptr< BigTermConsumer > createIdealWriter(FILE *out)
Definition IOHandler.cpp:89
IOHandler::doSupportsOutput
virtual bool doSupportsOutput(const DataType &type) const =0
IOHandler::doReadIdeal
virtual void doReadIdeal(Scanner &in, InputConsumer &consumer)=0
IOHandler::createIdealListWriter
unique_ptr< BigTermConsumer > createIdealListWriter(FILE *out)
Definition IOHandler.cpp:98
IOHandler::doGetName
virtual const char * doGetName() const =0
IOHandler::getDescription
const char * getDescription() const
Definition IOHandler.cpp:85
IOHandler::doHasMoreInput
virtual bool doHasMoreInput(Scanner &in) const =0
IOHandler::supportsOutput
bool supportsOutput(const DataType &type) const
Definition IOHandler.cpp:123
IOHandler::createPolynomialWriter
unique_ptr< CoefBigTermConsumer > createPolynomialWriter(FILE *out)
Definition IOHandler.cpp:110
IOHandler::doReadTerm
virtual void doReadTerm(Scanner &in, InputConsumer &consumer)=0
IOHandler::readTerm
void readTerm(Scanner &in, const VarNames &names, vector< mpz_class > &term)
Definition IOHandler.cpp:51
IOHandler::~IOHandler
virtual ~IOHandler()
Definition IOHandler.cpp:39
IOHandler::hasMoreInput
bool hasMoreInput(Scanner &in) const
Definition IOHandler.cpp:77
IOHandler::readIdeals
void readIdeals(Scanner &in, InputConsumer &consumer)
Read a number of ideals and feed them to the consumer.
Definition IOHandler.cpp:46
IO::CoCoA4IOHandler::staticGetName
static const char * staticGetName()
Definition CoCoA4IOHandler.cpp:160
IO::Fourti2IOHandler::staticGetName
static const char * staticGetName()
Definition Fourti2IOHandler.cpp:162
IO::Macaulay2IOHandler::staticGetName
static const char * staticGetName()
Definition Macaulay2IOHandler.cpp:145
IO::MonosIOHandler::staticGetName
static const char * staticGetName()
Definition MonosIOHandler.cpp:77
IO::NewMonosIOHandler::staticGetName
static const char * staticGetName()
Definition NewMonosIOHandler.cpp:78
IO::SingularIOHandler::staticGetName
static const char * staticGetName()
Definition SingularIOHandler.cpp:116
InputConsumer
Definition InputConsumer.h:30
InputConsumer::empty
bool empty() const
Returns true if there are ideals stored.
Definition InputConsumer.h:74
InputConsumer::releaseBigIdeal
unique_ptr< BigIdeal > releaseBigIdeal()
Returns the least recently read ideal that has not been released.
Definition InputConsumer.cpp:209
InputConsumer::beginIdeal
void beginIdeal()
Start consuming an ideal.
Definition InputConsumer.cpp:50
InputConsumer::consumeRing
void consumeRing(const VarNames &names)
Definition InputConsumer.cpp:33
InputConsumer::endIdeal
void endIdeal()
Done reading an ideal.
Definition InputConsumer.cpp:191
NameFactory
A NameFactory takes a name and then creates an instance of a class that has been previously registere...
Definition NameFactory.h:33
SatBinomConsumer
Definition SatBinomConsumer.h:25
Scanner
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition Scanner.h:50
Scanner::peek
bool peek(char character)
Skips whitespace and returns true if the next character is equal to the parameter(s).
Definition Scanner.h:262
Scanner::eatWhite
void eatWhite()
Reads past any whitespace, where whitespace is defined by the standard function isspace().
Definition Scanner.h:267
Scanner::matchEOF
bool matchEOF()
Return true if no more input.
Definition Scanner.h:210
Scanner::readInteger
void readInteger(mpz_class &integer)
Read an arbitrary-precision integer.
Definition Scanner.h:238
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
reportSyntaxError
void reportSyntaxError(const Scanner &scanner, const string &errorMsg)
Definition error.cpp:44
error.h
throwError
void throwError(const string &errorMsg)
Definition error.h:58
stdinc.h
This header file includes common definitions and is included as the first line of code in every imple...
ASSERT
#define ASSERT(X)
Definition stdinc.h:86