Frobby  0.9.5
IOFacade.cpp
Go to the documentation of this file.
1 /* Frobby: Software for monomial ideal computations.
2  Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see http://www.gnu.org/licenses/.
16 */
17 #include "stdinc.h"
18 #include "IOFacade.h"
19 
20 #include "IOHandler.h"
21 #include "fourti2.h"
22 #include "BigIdeal.h"
23 #include "fplllIO.h"
24 #include "Scanner.h"
25 #include "ElementDeleter.h"
26 #include "error.h"
27 #include "FrobbyStringStream.h"
28 #include "BigTermRecorder.h"
29 #include "CoefBigTermConsumer.h"
30 #include "CoefBigTermRecorder.h"
31 #include "SatBinomIdeal.h"
32 #include "SatBinomRecorder.h"
33 #include "InputConsumer.h"
34 #include "SquareFreeIdeal.h"
35 #include "RawSquareFreeIdeal.h"
36 #include <iterator>
37 
38 IOFacade::IOFacade(bool printActions):
39  Facade(printActions) {
40 }
41 
42 bool IOFacade::isValidMonomialIdealFormat(const string& format) {
43  beginAction("Validating monomial ideal format name.");
44 
45  bool valid = true;
46  try {
47  createIOHandler(format).get();
48  } catch (const UnknownNameException&) {
49  valid = false;
50  }
51 
52  endAction();
53 
54  return valid;
55 }
56 
58  beginAction("Reading saturated binomial ideal.");
59 
60  auto_ptr<IOHandler> handler(in.createIOHandler());
61  ASSERT(handler.get() != 0);
62 
63  handler->readSatBinomIdeal(in, consumer);
64 
65  endAction();
66 }
67 
69  beginAction("Reading saturated binomial ideal.");
70 
71  auto_ptr<IOHandler> handler(in.createIOHandler());
72  ASSERT(handler.get() != 0);
73 
74  ideal.clear();
75  SatBinomRecorder recorder(ideal);
76  handler->readSatBinomIdeal(in, recorder);
77 
78  endAction();
79 }
80 
82  beginAction("Reading monomial ideal.");
83 
84  auto_ptr<IOHandler> handler(in.createIOHandler());
85  ASSERT(handler.get() != 0);
86 
87  InputConsumer middleman;
88  handler->readIdeal(in, middleman);
89  // todo: find a way to generate the input as it comes in rather than
90  // storing it and only then letting it go on.
91  ASSERT(!middleman.empty());
92  consumer.consume(middleman.releaseBigIdeal());
93  ASSERT(middleman.empty());
94 
95  endAction();
96 }
97 
99  beginAction("Reading monomial ideal.");
100 
101  auto_ptr<IOHandler> handler(in.createIOHandler());
102  ASSERT(handler.get() != 0);
103 
104  InputConsumer recorder;
105  handler->readIdeal(in, recorder);
106 
107  ASSERT(!recorder.empty());
108  ideal.swap(*(recorder.releaseBigIdeal()));
109  ASSERT(recorder.empty());
110 
111  endAction();
112 }
113 
117  beginAction("Reading square free ideal.");
118 
119  auto_ptr<IOHandler> handler(in.createIOHandler());
120  ASSERT(handler.get() != 0);
121 
122  InputConsumer consumer;
123  consumer.requireSquareFree();
124  handler->readIdeal(in, consumer);
125  ASSERT(!consumer.empty());
126  ideal.swap(*consumer.releaseSquareFreeIdeal());
127  ASSERT(consumer.empty());
128 
129  endAction();
130 }
131 
133  vector<BigIdeal*>& ideals,
134  VarNames& names) {
135  beginAction("Reading monomial ideals.");
136 
137  // To make it clear what needs to be deleted in case of an exception.
138  ASSERT(ideals.empty());
139  ElementDeleter<vector<BigIdeal*> > idealsDeleter(ideals);
140 
141  auto_ptr<IOHandler> handler(in.createIOHandler());
142 
143  //BigTermRecorder recorder;
144  InputConsumer recorder;
145  handler->readIdeals(in, recorder);
146 
147  names = recorder.getRing();
148  while (!recorder.empty())
149  exceptionSafePushBack(ideals, recorder.releaseBigIdeal());
150 
151  idealsDeleter.release();
152 
153  endAction();
154 }
155 
156 void IOFacade::writeIdeal(const BigIdeal& ideal,
157  IOHandler* handler,
158  FILE* out) {
159  ASSERT(handler != 0);
160 
161  beginAction("Writing monomial ideal.");
162 
163  handler->createIdealWriter(out)->consume(ideal);
164 
165  endAction();
166 }
167 
168 void IOFacade::writeIdeals(const vector<BigIdeal*>& ideals,
169  const VarNames& names,
170  IOHandler* handler,
171  FILE* out) {
172  ASSERT(handler != 0);
173 
174  beginAction("Writing monomial ideals.");
175 
176  {
177  auto_ptr<BigTermConsumer> consumer = handler->createIdealWriter(out);
178 
179  consumer->beginConsumingList();
180  consumer->consumeRing(names);
181 
182  for (vector<BigIdeal*>::const_iterator it = ideals.begin();
183  it != ideals.end(); ++it)
184  consumer->consume(**it);
185 
186  consumer->doneConsumingList();
187  }
188 
189  endAction();
190 }
191 
193 
194  beginAction("Reading polynomial.");
195 
196  auto_ptr<IOHandler> handler(in.createIOHandler());
197  ASSERT(handler.get() != 0);
198 
199  CoefBigTermRecorder recorder(&polynomial);
200  handler->readPolynomial(in, recorder);
201 
202  endAction();
203 }
204 
206  IOHandler* handler,
207  FILE* out) {
208  ASSERT(handler != 0);
209  ASSERT(out != 0);
210 
211  beginAction("Writing polynomial.");
212 
213  handler->createPolynomialWriter(out)->consume(polynomial);
214 
215  endAction();
216 }
217 
218 void IOFacade::writeTerm(const vector<mpz_class>& term,
219  const VarNames& names,
220  IOHandler* handler,
221  FILE* out) {
222  beginAction("Writing monomial.");
223 
224  handler->writeTerm(term, names, out);
225 
226  endAction();
227 }
228 
230 (Scanner& in, BigIdeal& ideal, vector<mpz_class>& term) {
231  beginAction("Reading Alexander dual input.");
232 
233  auto_ptr<IOHandler> handler(in.createIOHandler());
234  ASSERT(handler.get() != 0);
235 
236  InputConsumer recorder;
237  handler->readIdeal(in, recorder);
238 
239  // TODO: return value instead of this copy.
240  ASSERT(!recorder.empty());
241  ideal = *(recorder.releaseBigIdeal());
242  ASSERT(recorder.empty());
243 
244  bool pointSpecified = false;
245  if (handler->hasMoreInput(in)) {
246  handler->readTerm(in, ideal.getNames(), term);
247  pointSpecified = true;
248  }
249 
250  endAction();
251 
252  return pointSpecified;
253 }
254 
256 (Scanner& in, vector<mpz_class>& v, size_t integerCount) {
257  beginAction("Reading vector.");
258 
259  v.resize(integerCount);
260  for (size_t i = 0; i < integerCount; ++i)
261  in.readInteger(v[i]);
262 
263  endAction();
264 }
265 
267 readFrobeniusInstance(Scanner& in, vector<mpz_class>& instance) {
268  beginAction("Reading Frobenius instance.");
269 
270  ::readFrobeniusInstance(in, instance);
271 
272  endAction();
273 }
274 
276 (Scanner& in, BigIdeal& ideal, vector<mpz_class>& instance) {
277  beginAction("Reading frobenius instance with Grobner basis.");
278 
279  fourti2::readGrobnerBasis(in, ideal);
280  ::readFrobeniusInstance(in, instance);
281 
282  if (instance.size() != ideal.getVarCount() + 1) {
283  if (instance.empty())
285  (in, "The Grobner basis is not followed by a Frobenius instance.");
286  else {
287  // Note that we add one since the first entry of the rows encoding
288  // the Grobner basis is chopped off.
289  FrobbyStringStream errorMsg;
290  errorMsg << "The Grobner basis has "
291  << ideal.getVarCount() + 1
292  << " entries, and the Frobenius instance should then also have "
293  << ideal.getVarCount() + 1
294  << " entries, but in fact it has "
295  << instance.size()
296  << " entries.";
297  reportSyntaxError(in, errorMsg);
298  }
299  }
300 
301  endAction();
302 }
303 
305 writeFrobeniusInstance(FILE* out, vector<mpz_class>& instance) {
306  beginAction("Writing Frobenius instance.");
307 
308  for (size_t i = 0; i < instance.size(); ++i) {
309  if (i > 0)
310  fputc(' ', out);
311  gmp_fprintf(out, "%Zd", instance[i].get_mpz_t());
312  }
313  fputc('\n', out);
314 
315  endAction();
316 }
317 
318 bool IOFacade::isValidLatticeFormat(const string& format) {
319  beginAction("Validating lattice format name.");
320 
321  bool valid = (format == "4ti2" || format == "fplll");
322 
323  endAction();
324 
325  return valid;
326 }
327 
329 readLattice(Scanner& in, BigIdeal& ideal) {
330  beginAction("Reading lattice basis.");
331 
332  if (in.getFormat() == "4ti2")
333  fourti2::readLatticeBasis(in, ideal);
334  else if (in.getFormat() == "fplll")
335  fplll::readLatticeBasis(in, ideal);
336  else {
337  ASSERT(false);
338  }
339 
340  endAction();
341 }
342 
344 writeLattice(FILE* out, const BigIdeal& ideal, const string& format) {
345  beginAction("Writing lattice basis.");
346 
347  if (format == "4ti2")
348  fourti2::writeLatticeBasis(out, ideal);
349  else if (format == "fplll")
350  fplll::writeLatticeBasis(out, ideal);
351  else {
352  ASSERT(false);
353  }
354 
355  endAction();
356 }
void exceptionSafePushBack(Container &container, auto_ptr< Element > pointer)
auto_ptr< IOHandler > createIOHandler(const string &prefix)
Returns an IOHandler for the format whose name has the given prefix.
Definition: IOHandler.cpp:145
void swap(BigIdeal &ideal)
Definition: BigIdeal.cpp:305
size_t getVarCount() const
Definition: BigIdeal.h:148
const VarNames & getNames() const
Definition: BigIdeal.cpp:253
virtual void consume(const vector< mpz_class > &term)=0
This is the super class of all facades.
Definition: Facade.h:32
void beginAction(const char *message)
Prints message to standard error if printing is turned on, and records the time when the action start...
Definition: Facade.cpp:38
void endAction()
Prints to standard error the time since the last call to beginAction.
Definition: Facade.cpp:51
A replacement for stringstream.
bool isValidLatticeFormat(const string &format)
Definition: IOFacade.cpp:318
void writeFrobeniusInstance(FILE *out, vector< mpz_class > &instance)
Definition: IOFacade.cpp:305
void readFrobeniusInstance(Scanner &in, vector< mpz_class > &instance)
Definition: IOFacade.cpp:267
void writePolynomial(const BigPolynomial &polynomial, IOHandler *handler, FILE *out)
Definition: IOFacade.cpp:205
bool isValidMonomialIdealFormat(const string &format)
Definition: IOFacade.cpp:42
void readPolynomial(Scanner &in, BigPolynomial &polynomial)
Definition: IOFacade.cpp:192
void readFrobeniusInstanceWithGrobnerBasis(Scanner &in, BigIdeal &ideal, vector< mpz_class > &instance)
Definition: IOFacade.cpp:276
void writeIdeal(const BigIdeal &ideal, IOHandler *handler, FILE *out)
Definition: IOFacade.cpp:156
void writeTerm(const vector< mpz_class > &term, const VarNames &names, IOHandler *handler, FILE *out)
Definition: IOFacade.cpp:218
void readIdeals(Scanner &in, vector< BigIdeal * > &ideals, VarNames &names)
Insert the ideals that are read into the parameter ideals.
Definition: IOFacade.cpp:132
void readLattice(Scanner &in, BigIdeal &ideal)
Definition: IOFacade.cpp:329
void readIdeal(Scanner &in, BigTermConsumer &consumer)
Read an ideal from in and feed it to consumer.
Definition: IOFacade.cpp:81
void readSquareFreeIdeal(Scanner &in, SquareFreeIdeal &ideal)
Read a square free ideal from in and place it in the parameter ideal.
Definition: IOFacade.cpp:116
void readSatBinomIdeal(Scanner &in, SatBinomConsumer &consumer)
Read a saturated binomial ideal from in and feed it to consumer.
Definition: IOFacade.cpp:57
void writeIdeals(const vector< BigIdeal * > &ideals, const VarNames &names, IOHandler *handler, FILE *out)
Definition: IOFacade.cpp:168
IOFacade(bool printActions)
Definition: IOFacade.cpp:38
void writeLattice(FILE *out, const BigIdeal &ideal, const string &format)
Definition: IOFacade.cpp:344
void readVector(Scanner &in, vector< mpz_class > &v, size_t integerCount)
Definition: IOFacade.cpp:256
bool readAlexanderDualInstance(Scanner &in, BigIdeal &ideal, vector< mpz_class > &term)
Definition: IOFacade.cpp:230
An IOHandler implements input and output for some format in such a way that client code does not need...
Definition: IOHandler.h:41
void writeTerm(const vector< mpz_class > &term, const VarNames &names, FILE *out)
Definition: IOHandler.cpp:72
auto_ptr< BigTermConsumer > createIdealWriter(FILE *out)
Definition: IOHandler.cpp:89
auto_ptr< CoefBigTermConsumer > createPolynomialWriter(FILE *out)
Definition: IOHandler.cpp:110
void requireSquareFree()
auto_ptr< SquareFreeIdeal > releaseSquareFreeIdeal()
Returns the least recently read ideal that has not been released.
bool empty() const
Returns true if there are ideals stored.
Definition: InputConsumer.h:74
const VarNames & getRing() const
Returns the current ring.
Definition: InputConsumer.h:91
auto_ptr< BigIdeal > releaseBigIdeal()
Returns the least recently read ideal that has not been released.
Represents a saturated binomial ideal.
Definition: SatBinomIdeal.h:28
SatBinomRecorder records all consumed saturated binomials into a passed-in ideal.
This class offers an input interface which is more convenient and for some purposes more efficient th...
Definition: Scanner.h:50
const string & getFormat() const
Definition: Scanner.h:61
auto_ptr< IOHandler > createIOHandler() const
Definition: Scanner.cpp:42
void readInteger(mpz_class &integer)
Read an arbitrary-precision integer.
Definition: Scanner.h:238
void swap(SquareFreeIdeal &ideal)
Defines the variables of a polynomial ring and facilities IO involving them.
Definition: VarNames.h:40
void reportSyntaxError(const Scanner &scanner, const string &errorMsg)
Definition: error.cpp:44
void readGrobnerBasis(Scanner &scanner, BigIdeal &basis)
Definition: fourti2.cpp:44
void readLatticeBasis(Scanner &scanner, BigIdeal &basis)
Definition: fourti2.cpp:63
void writeLatticeBasis(FILE *out, const BigIdeal &basis)
Definition: fourti2.cpp:80
void readLatticeBasis(Scanner &scanner, BigIdeal &basis)
Definition: fplllIO.cpp:26
void writeLatticeBasis(FILE *out, const BigIdeal &basis)
Definition: fplllIO.cpp:62
#define ASSERT(X)
Definition: stdinc.h:86