49 usage(std::ostream& os) {
50 os <<
"usage: gecode-instance -spec SPEC [options] FILE...\n"
52 <<
"Reads instance files with the reader spec in file SPEC and\n"
53 <<
"prints the data they contain.\n"
56 <<
" -spec SPEC file with the reader spec\n"
57 <<
" -summary print names, types, and shapes (default)\n"
58 <<
" -dzn print the data as MiniZinc data\n"
59 <<
" -list list the instances in each file, and the\n"
60 <<
" first failure of each invalid one\n"
61 <<
" -param N=V give value V to spec parameter N; repeat,\n"
62 <<
" or separate by commas, for several; use\n"
63 <<
" instance=K or instance=NAME to select an\n"
64 <<
" instance in a file with several\n"
65 <<
" -check SPEC check that spec file SPEC parses\n"
66 <<
" -help print this help\n";
72 usage_error(
const std::string& m) {
73 std::cerr <<
"gecode-instance: " << m <<
"\n";
80 print_shape(std::ostream& os,
const Instance::Data& d,
const std::string& n) {
86 if (t == Instance::ValueType::INT) os << d.integer(n);
87 else if (t == Instance::ValueType::FLOAT) os << d.number(n);
88 else os <<
'"' << d.text(n) <<
'"';
91 os << tn <<
" array " << d.size(n);
95 int rows = d.size(n), lo = 0, hi = 0;
96 for (int i=0; i<rows; i++) {
97 int l = d.size(n, {i});
98 if ((i == 0) || (l < lo)) lo = l;
99 if ((i == 0) || (l > hi)) hi = l;
101 os << tn <<
" array " << rows <<
" x " << lo;
102 if (hi != lo) os <<
".." << hi;
103 for (int k=2; k<d.depth(n); k++)
112 option(
const char* a) {
114 if ((o.size() > 2) && (o[0] ==
'-') && (o[1] ==
'-'))
121 valued(
const std::string& o) {
122 return (o ==
"-spec") || (o ==
"-param") || (o ==
"-check");
127 is_file(
const std::string& o) {
128 return (o.size() <= 1) || (o[0] !=
'-');
134 if (mode ==
"list") {
136 int n =
static_cast<int>(c.
integer(
"instances"));
137 std::cout << f <<
": " << n <<
" instance" << (n == 1 ?
"" :
"s") <<
"\n";
139 bool names = c.
has(
"name"), failed = c.
has(
"failed");
140 for (
int i=0; (names || failed) && (i<n); i++) {
141 std::string m = failed ? c.
text(
"failed", {i}) : std::string();
142 if (!names && m.empty())
144 std::cout <<
" " << (i+1);
146 std::cout <<
" " << c.
text(
"name", {i});
148 std::cout <<
" (failed: " << m <<
")";
159 if (!spec.
name().empty())
160 std::cout <<
" [" << spec.
name() <<
"]";
162 for (
int n=0; n<d.
entries(); n++) {
163 std::cout <<
" " << d.
name(n) <<
": ";
164 print_shape(std::cout, d, d.
name(n));
173 std::string mode =
"summary", spec_file;
177 for (
int i=1; i<argc; i++) {
178 std::string o = option(argv[i]);
179 if (valued(o) && (i+1 >= argc))
180 return usage_error(
"option " + o +
" needs a value");
181 if ((o ==
"-summary") || (o ==
"-dzn") || (o ==
"-list")) {
183 }
else if (o ==
"-spec") {
184 spec_file = argv[++i];
185 }
else if (o ==
"-param") {
187 params.
set(argv[++i]);
189 return usage_error(std::string(
"-param: ") + e.
what());
191 }
else if (o ==
"-check") {
193 }
else if (o ==
"-help") {
196 }
else if (!is_file(o)) {
197 return usage_error(std::string(
"unknown option ") + argv[i]);
203 return usage_error(
"no files given");
207 for (
int i=1; i<argc; i++) {
208 std::string o = option(argv[i]);
210 std::string f = argv[++i];
212 std::cout << f <<
": ok";
213 if (!c.
name().empty())
214 std::cout <<
" (format " << c.
name() <<
")";
216 }
else if (valued(o)) {
218 }
else if (is_file(o)) {
219 if (spec_file.empty())
220 return usage_error(
"no reader spec given (use -spec SPEC)");
225 process(argv[i], spec, params, mode);
229 std::cerr << e.
what() << std::endl;
231 }
catch (
const std::exception& e) {
233 std::cerr <<
"gecode-instance: " << e.
what() << std::endl;
Named values read from an instance.
void dzn(std::ostream &os) const
Print all entries as MiniZinc data.
ValueType type(const std::string &n) const
Return the value type of entry n.
std::string text(const std::string &n, const IntArgs &path=IntArgs()) const
Return the string n at path.
int depth(const std::string &n) const
Return the depth of entry n (0 for scalars).
bool has(const std::string &n) const
Test whether there is an entry n.
std::string name(int i) const
Return name of entry i, in the order they were defined.
long long int integer(const std::string &n, const IntArgs &path=IntArgs()) const
Return the integer n at path.
int entries(void) const
Return number of entries.
Exception for errors in specs and instance files
virtual const char * what(void) const noexcept override
Return the full message.
Values for the param statements of a spec.
void set(const std::string &name, const std::string &value)
Set parameter name to value.
Reader spec for a textual instance format.
static Spec load(const std::string &path)
Load the spec in file path.
Data read(const std::string &path, const Params &p=Params()) const
Read instance file path.
std::string name(void) const
Return the format name declared by the spec (may be empty).
Data catalog(const std::string &path, const Params &p=Params()) const
List the instances in file path.
int main(int argc, char *argv[])
Reading instance files described by reader specs.
ValueType
Type of the values in a data entry.
@ FLOAT
Floating point numbers.
Gecode toplevel namespace