#!/bin/sh
# -*- coding: utf-8 -*-
# Copyright (C) 2009, 2010, 2012, 2014 Laboratoire de Recherche et
# Développement de l'Epita (LRDE).
# Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
# département Systèmes Répartis Coopératifs (SRC), Université Pierre
# et Marie Curie.
#
# This file is part of Spot, a model checking library.
#
# Spot 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 3 of the License, or
# (at your option) any later version.
#
# Spot 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/>.


. ./defs

set -e

autfilt=../../bin/autfilt
ltl2tgba=../../bin/ltl2tgba
randltl=../../bin/randltl

cat >input <<\EOF
HOA: v1
States: 3
Start: 0
AP: 3 "a" "b" "F\\G"
acc-name: generalized-Buchi 2
Acceptance: 2 Inf(0)&Inf(1)
properties: trans-labels explicit-labels state-acc deterministic
--BODY--
State: 0 {0 1}
[0&!1] 1
State: 1 {0}
[2] 2
State: 2
[t] 0
--END--
EOF

run 0 $autfilt --hoa input > stdout
diff stdout input

# Transition merging
cat >input <<\EOF
HOA: v1
States: 2
Start: 0
AP: 2 "a" "b"
acc-name: Buchi
Acceptance: 1 Inf(0)
properties: trans-labels explicit-labels trans-acc
--BODY--
State: 0
[0&1] 1 {0}
[!1] 1
[0&!1] 1 {0}
State: 1
[!1] 0
[1&0] 0 {0}
[0&!1] 0 {0}
--END--
EOF

cat >expected <<\EOF
HOA: v1
States: 2
Start: 0
AP: 2 "a" "b"
acc-name: Buchi
Acceptance: 1 Inf(0)
properties: trans-labels explicit-labels trans-acc
--BODY--
State: 0
[!1] 1
[0] 1 {0}
State: 1
[!1] 0
[0] 0 {0}
--END--
EOF

run 0 ../../bin/autfilt --merge-transitions --hoa input > stdout
cat stdout
run 0 ../../bin/autfilt -F stdout --isomorph expected

# Likewise, with a randomly generated TGBA.
run 0 ../../bin/randaut -S 20 a b -d 0.2 -a 0.2 -A 2 --hoa | tee input

# the first read-write can renumber the states
run 0 $autfilt --hoa --merge-transitions input > stdout
run 0 ../../bin/autfilt -F input --isomorph stdout

# But this second output should be the same as the first
run 0 $autfilt --hoa stdout > stdout2
diff stdout stdout2

# Find formula that can be translated into a 3-state automaton, and
# exercise both %M and %m.  The nonexistant file should never be
# open, because the input stream is infinite and autfilt should
# stop after 10 automata.
$randltl -n -1 a b |
    $ltl2tgba -H -F - |
    $autfilt -F- -F nonexistant --states=3 --edges=..10 --acc-sets=1.. \
	     --name='%M, %S states' --stats='<%m>, %e, %a' -n 10 > output
cat >expected <<EOF
<F(b | GF!a), 3 states>, 6, 1
<XFb, 3 states>, 4, 1
<XF!b, 3 states>, 4, 1
<G!b & XF!a, 3 states>, 4, 1
<F(b | GFa), 3 states>, 6, 1
<F(Ga | XG(!a & Fb)), 3 states>, 6, 1
<FG!b & F(b | GFb), 3 states>, 5, 1
<Ga | G!a, 3 states>, 4, 1
<a | G((!a & !b) | (a & b)), 3 states>, 4, 1
<Fb U G!a, 3 states>, 7, 1
EOF
diff output expected


cat >input <<EOF
HOA: v1
States: 10
Start: 0
AP: 1 "a"
acc-name: generalized-Buchi 3
Acceptance: 3 Inf(0)&Inf(1)&Inf(2)
properties: trans-labels explicit-labels trans-acc
--BODY--
State: 0
[t] 0 {0}
[!0] 1 {0}
[!0] 2 {0}
[0] 3 {0}
[0] 3 {0 2}
[!0] 2 {0 2}
[!0] 4 {1}
[!0] 5 {1}
[!0] 6 {1}
[!0] 6 {1 2}
[!0] 7 {1}
[!0] 8 {1}
[!0] 9 {1}
[!0] 9 {1 2}
[!0] 4 {0 1}
[!0] 5 {0 1}
[!0] 6 {0 1}
[!0] 6 {0 1 2}
[!0] 7 {0 1}
[!0] 8 {0 1}
[!0] 9 {0 1}
[!0] 9 {0 1 2}
State: 1
[0] 3 {0 2}
State: 2
[!0] 2 {0 2}
[!0] 6 {1 2}
[!0] 9 {1 2}
[!0] 6 {0 1 2}
[!0] 9 {0 1 2}
State: 3
[!0] 1 {0 2}
[!0] 2 {0}
[0] 3 {0 2}
[!0] 2 {0 2}
[!0] 5 {1 2}
[!0] 6 {1}
[!0] 6 {1 2}
[!0] 8 {1 2}
[!0] 9 {1}
[!0] 9 {1 2}
[!0] 5 {0 1 2}
[!0] 6 {0 1}
[!0] 6 {0 1 2}
[!0] 8 {0 1 2}
[!0] 9 {0 1}
[!0] 9 {0 1 2}
State: 4
[!0] 4 {1}
[!0] 5 {1}
[!0] 6 {1}
[!0] 6 {1 2}
[!0] 7
[!0] 8
[!0] 9
[!0] 9 {2}
[!0] 7 {1}
[!0] 8 {1}
[!0] 9 {1}
[!0] 9 {1 2}
State: 5
State: 6
[!0] 6 {1 2}
[!0] 9 {2}
[!0] 9 {1 2}
State: 7
[0] 0 {0}
[0] 3 {0}
[0] 3 {0 2}
State: 8
[0] 3 {0 2}
State: 9
--END--
EOF

cat >expected <<EOF
HOA: v1
name: "63->32 edges, 64->33 transitions"
States: 10
Start: 0
AP: 1 "a"
acc-name: generalized-Buchi 3
Acceptance: 3 Inf(0)&Inf(1)&Inf(2)
properties: trans-labels explicit-labels
--BODY--
State: 0
[t] 0 {0}
[!0] 1 {0}
[!0] 2 {0 2}
[0] 3 {0 2}
[!0] 4 {0 1}
[!0] 5 {0 1}
[!0] 6 {0 1 2}
[!0] 7 {0 1}
[!0] 8 {0 1}
[!0] 9 {0 1 2}
State: 1 {0 2}
[0] 3
State: 2
[!0] 2 {0 2}
[!0] 6 {0 1 2}
[!0] 9 {0 1 2}
State: 3
[!0] 1 {0 2}
[!0] 2 {0 2}
[0] 3 {0 2}
[!0] 5 {0 1 2}
[!0] 6 {0 1 2}
[!0] 8 {0 1 2}
[!0] 9 {0 1 2}
State: 4
[!0] 4 {1}
[!0] 5 {1}
[!0] 6 {1 2}
[!0] 7 {1}
[!0] 8 {1}
[!0] 9 {1 2}
State: 5
State: 6 {1 2}
[!0] 6
[!0] 9
State: 7
[0] 0 {0}
[0] 3 {0 2}
State: 8 {0 2}
[0] 3
State: 9
--END--
EOF

$autfilt --merge -Hm input --name="%E->%e edges, %T->%t transitions" > output
diff output expected


cat <<EOF | $ltl2tgba -x degen-skip=1 -F- --ba -H > tmp.hoa
a U b
false
!b && Xb && GFa
EOF
$autfilt <tmp.hoa --stats='"%M","%w"' > output
cat >expected <<EOF
"a U b","cycle{b}"
"0",""
"!b & X(b & GFa)","!b; cycle{a & b}"
EOF
diff output expected
