bin: add the --output to tools that output formulas

* src/bin/common_output.cc, src/bin/common_output.hh: Add option
--output.
* src/ltltest/ltlfilt.test, src/ltltest/rand.test: Add tests.
* NEWS, doc/org/ioltl.org: Document it.
This commit is contained in:
Alexandre Duret-Lutz 2015-02-15 23:50:48 +01:00
parent e22a800fe4
commit 78def4f8ca
6 changed files with 96 additions and 15 deletions

6
NEWS
View file

@ -44,6 +44,12 @@ New in spot 1.99a (not yet released)
- ltlfilt has a new --count option to count the number of matching - ltlfilt has a new --count option to count the number of matching
automata. automata.
- all tools that produce formulas or automata now have an --output
(a.k.a. -o) option to redirect that output to a file instead of
standard output. The name of this file can be constructed using
the same %-escape sequences that are available for --stats or
--format.
- ltlcross (and ltldo) have a list of hard-coded shorthands - ltlcross (and ltldo) have a list of hard-coded shorthands
for some existing tools. So for instance running for some existing tools. So for instance running
'ltlcross spin ...' is the same as running 'ltlcross spin ...' is the same as running

View file

@ -150,6 +150,7 @@ ltlfilt --help | sed -n '/Output options:/,/^$/p' | sed '1d;$d'
#+END_SRC #+END_SRC
#+RESULTS: #+RESULTS:
#+begin_example #+begin_example
-c, --count print only a count of matched formulas
-n, --max-count=NUM output at most NUM formulas -n, --max-count=NUM output at most NUM formulas
-q, --quiet suppress all normal output -q, --quiet suppress all normal output
-8, --utf8 output using UTF-8 characters -8, --utf8 output using UTF-8 characters
@ -158,14 +159,15 @@ ltlfilt --help | sed -n '/Output options:/,/^$/p' | sed '1d;$d'
"%f") "%f")
-l, --lbt output in LBT's syntax -l, --lbt output in LBT's syntax
--latex output using LaTeX macros --latex output using LaTeX macros
-o, --output=FORMAT send output to a file named FORMAT instead of
standard output. The first formula sent to a file
truncates it unless FORMAT starts with '>>'.
-p, --full-parentheses output fully-parenthesized formulas -p, --full-parentheses output fully-parenthesized formulas
-s, --spin output in Spin's syntax -s, --spin output in Spin's syntax
--spot output in Spot's syntax (default) --spot output in Spot's syntax (default)
--wring output in Wring's syntax --wring output in Wring's syntax
#+end_example #+end_example
# LocalWords: syntaxes LTL PSL num toc SRC ltl tgba sed FILENAME
The =--spot=, =--utf-8=, =--spin=, =--wring= options select different The =--spot=, =--utf-8=, =--spin=, =--wring= options select different
output syntaxes as seen in [[tab:formula-syntaxes][the above table]]. output syntaxes as seen in [[tab:formula-syntaxes][the above table]].
@ -203,5 +205,33 @@ from tool to tool:
| [[file:genltl.org][=genltl=]] | output formula | pattern name | pattern parameter | (empty) | (empty) | | [[file:genltl.org][=genltl=]] | output formula | pattern name | pattern parameter | (empty) | (empty) |
| [[file:randltl.org][=randltl=]] | output formula | (empty) | formula number | (empty) | (empty) | | [[file:randltl.org][=randltl=]] | output formula | (empty) | formula number | (empty) | (empty) |
By default everything is output to standard output, so that you can
redirect the output to a file, and pipe it to another tool. The the
=--output= (or =-o=) allows you to construct a filename using some of
the above =%=-sequences.
For instance the following invocation of [[file:randltl.org][=randltl=]] will create 5
random formulas, but in 5 different files:
#+BEGIN_SRC sh :results verbatim :exports both
randltl -n5 a b -o example-%L.ltl
wc -l example-*.ltl
#+END_SRC
#+RESULTS:
: 1 example-1.ltl
: 1 example-2.ltl
: 1 example-3.ltl
: 1 example-4.ltl
: 1 example-5.ltl
: 5 total
#+BEGIN_SRC sh :results verbatim :exports none
rm -f example-*.ltl
#+END_SRC
#+RESULTS:
# LocalWords: lbt LBT's filename UTF gfa GFa ltlfilt LBTT scheck # LocalWords: lbt LBT's filename UTF gfa GFa ltlfilt LBTT scheck
# LocalWords: utf associativity # LocalWords: utf associativity
# LocalWords: syntaxes LTL PSL num toc SRC ltl tgba sed FILENAME

View file

@ -52,6 +52,10 @@ static const argp_option options[] =
-20 }, -20 },
{ "format", OPT_FORMAT, "FORMAT", 0, { "format", OPT_FORMAT, "FORMAT", 0,
"specify how each line should be output (default: \"%f\")", -20 }, "specify how each line should be output (default: \"%f\")", -20 },
{ "output", 'o', "FORMAT", 0,
"send output to a file named FORMAT instead of standard output. The"
" first formula sent to a file truncates it unless FORMAT starts"
" with '>>'.", 0 },
{ 0, 0, 0, 0, 0, 0 } { 0, 0, 0, 0, 0, 0 }
}; };
@ -195,6 +199,9 @@ namespace
} }
static formula_printer* format = 0; static formula_printer* format = 0;
static std::ostringstream outputname;
static formula_printer* outputnamer = 0;
static std::map<std::string, std::unique_ptr<output_file>> outputfiles;
int int
parse_opt_output(int key, char* arg, struct argp_state*) parse_opt_output(int key, char* arg, struct argp_state*)
@ -208,6 +215,9 @@ parse_opt_output(int key, char* arg, struct argp_state*)
case 'l': case 'l':
output_format = lbt_output; output_format = lbt_output;
break; break;
case 'o':
outputnamer = new formula_printer(outputname, arg);
break;
case 'p': case 'p':
full_parenth = true; full_parenth = true;
break; break;
@ -237,10 +247,11 @@ parse_opt_output(int key, char* arg, struct argp_state*)
} }
void static void
output_formula(std::ostream& out, output_formula(std::ostream& out,
const spot::ltl::formula* f, const char* filename, int linenum, const spot::ltl::formula* f,
const char* prefix, const char* suffix) const char* filename = nullptr, int linenum = 0,
const char* prefix = nullptr, const char* suffix = nullptr)
{ {
if (!format) if (!format)
{ {
@ -257,15 +268,40 @@ output_formula(std::ostream& out,
} }
} }
void
::printable_formula::print(std::ostream& os, const char*) const
{
output_formula(os, val_);
}
void void
output_formula_checked(const spot::ltl::formula* f, output_formula_checked(const spot::ltl::formula* f,
const char* filename, int linenum, const char* filename, int linenum,
const char* prefix, const char* suffix) const char* prefix, const char* suffix)
{ {
if (output_format == quiet_output || output_format == count_output) if (output_format == count_output)
{
if (outputnamer)
throw std::runtime_error
("options --output and --count are incompatible");
return;
}
if (output_format == quiet_output)
return; return;
output_formula(std::cout, f, filename, linenum, prefix, suffix); std::ostream* out = &std::cout;
std::cout << '\n'; if (outputnamer)
{
outputname.str("");
formula_with_location fl = { f, filename, linenum, prefix, suffix };
outputnamer->print(fl);
std::string fname = outputname.str();
auto p = outputfiles.emplace(fname, nullptr);
if (p.second)
p.first->second.reset(new output_file(fname.c_str()));
out = &p.first->second->ostream();
}
output_formula(*out, f, filename, linenum, prefix, suffix);
*out << '\n';
// Make sure we abort if we can't write to std::cout anymore // Make sure we abort if we can't write to std::cout anymore
// (like disk full or broken pipe with SIGPIPE ignored). // (like disk full or broken pipe with SIGPIPE ignored).
check_cout(); check_cout();

View file

@ -23,9 +23,12 @@
#include "common_sys.hh" #include "common_sys.hh"
#include <argp.h> #include <argp.h>
#include <map>
#include <memory>
#include "ltlast/formula.hh" #include "ltlast/formula.hh"
#include "tgbaalgos/stats.hh" #include "tgbaalgos/stats.hh"
#include "common_output.hh" #include "common_output.hh"
#include "common_file.hh"
enum output_format_t { spot_output, spin_output, utf8_output, enum output_format_t { spot_output, spin_output, utf8_output,
lbt_output, wring_output, latex_output, lbt_output, wring_output, latex_output,
@ -38,9 +41,6 @@ extern const struct argp output_argp;
int parse_opt_output(int key, char* arg, struct argp_state* state); int parse_opt_output(int key, char* arg, struct argp_state* state);
void output_formula(std::ostream& os, const spot::ltl::formula* f,
const char* filename = 0, int linenum = 0,
const char* prefix = 0, const char* suffix = 0);
void output_formula_checked(const spot::ltl::formula* f, void output_formula_checked(const spot::ltl::formula* f,
const char* filename = 0, int linenum = 0, const char* filename = 0, int linenum = 0,
const char* prefix = 0, const char* suffix = 0); const char* prefix = 0, const char* suffix = 0);
@ -58,10 +58,7 @@ public:
} }
virtual void virtual void
print(std::ostream& os, const char*) const print(std::ostream& os, const char*) const;
{
output_formula(os, val_);
}
}; };
class aut_stat_printer: protected spot::stat_printer class aut_stat_printer: protected spot::stat_printer
@ -73,6 +70,8 @@ public:
declare('f', &formula_); // Override the formula printer. declare('f', &formula_); // Override the formula printer.
} }
using spot::formater::set_output;
std::ostream& std::ostream&
print(const spot::const_tgba_digraph_ptr& aut, print(const spot::const_tgba_digraph_ptr& aut,
const spot::ltl::formula* f = 0, const spot::ltl::formula* f = 0,

View file

@ -188,4 +188,8 @@ SPOT_STUTTER_CHECK=5 \
# bug in the bitvectors. # bug in the bitvectors.
../../bin/ltlfilt --stutter-invariant -f 'F(a & XXXXXX!a)' && exit 1 ../../bin/ltlfilt --stutter-invariant -f 'F(a & XXXXXX!a)' && exit 1
../../bin/ltlfilt -c -o 'foo' -f a 2>stderr && exit 1
grep 'ltlfilt: options --output and --count are incompatible' stderr
true true

View file

@ -130,3 +130,9 @@ run 2 $randltl -n3 0
run 0 $randltl -n1000 0 1 > out run 0 $randltl -n1000 0 1 > out
grep -q '"0"' out grep -q '"0"' out
grep -q '"1"' out grep -q '"1"' out
run 0 $randltl -n5 2 -o test-all.ltl
run 0 $randltl -n5 2 -o test-%L.ltl
cat test-1.ltl test-2.ltl test-3.ltl test-4.ltl test-5.ltl > test-cmp.ltl
diff test-cmp.ltl test-all.ltl