#!/bin/sh
# -*- coding: utf-8 -*-
# Copyright (C) 2013-2016, 2018 Laboratoire de Recherche et
# Développement de l'Epita (LRDE).
#
# 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/>.

# Do some quick translations to make sure the neverclaims produced by
# spot actually look correct!  We do that by parsing them via ltlcross.
# ltl2neverclaim-lbtt.test does the same with LBTT if it is installed.

. ./defs
set -e


# DRA generated with
#  ltlfilt -f 'a U b' -l | ltl2dstar --ltl2nba=spin:path/ltl2tgba@-s - -
cat >dra.dstar <<EOF
DRA v2 explicit
Comment: "Safra[NBA=2]"
States: 3
Acceptance-Pairs: 1
Start: 0
AP: 2 "a" "b"
---
State: 0
Acc-Sig:
1
0
2
2
State: 1
Acc-Sig: -0
1
1
1
1
State: 2
Acc-Sig: +0
2
2
2
2
EOF

run 0 ../ikwiad -d -XD dra.dstar | tee stdout

cat >expected <<EOF
digraph "" {
  rankdir=LR
  label="Fin(0) & Inf(1)\n[Rabin 1]"
  labelloc="t"
  node [shape="circle"]
  I [label="", style=invis, width=0]
  I -> 0
  0 [label="0"]
  0 -> 0 [label="a & !b"]
  0 -> 1 [label="!a & !b"]
  0 -> 2 [label="b"]
  1 [label="1\n{0}"]
  1 -> 1 [label="1"]
  2 [label="2\n{1}"]
  2 -> 2 [label="1"]
}
EOF

diff expected stdout


run 0 ../ikwiad -d -XDB -R3 dra.dstar | tee stdout

cat >expected <<EOF
digraph "" {
  rankdir=LR
  label="\n[Büchi]"
  labelloc="t"
  node [shape="circle"]
  I [label="", style=invis, width=0]
  I -> 0
  0 [label="0"]
  0 -> 0 [label="a & !b"]
  0 -> 1 [label="b"]
  1 [label="1", peripheries=2]
  1 -> 1 [label="1"]
}
EOF

diff expected stdout



# DSA generated with
#   ltlfilt -f 'FGa' -l |
#   ltl2dstar --automata=streett --ltl2nba=spin:path/ltl2tgba@-s - -
cat >dsa.dstar <<EOF
DSA v2 explicit
Comment: "Streett{Safra[NBA=2]}"
States: 3
Acceptance-Pairs: 1
Start: 1
AP: 1 "a"
---
State: 0
Acc-Sig: +0
0
2
State: 1
Acc-Sig:
0
0
State: 2
Acc-Sig:
0
2
EOF

run 0 ../ikwiad -d -XDB dsa.dstar | tee stdout

cat >expected <<EOF
digraph "" {
  rankdir=LR
  label="\n[Büchi]"
  labelloc="t"
  node [shape="circle"]
  I [label="", style=invis, width=0]
  I -> 1
  0 [label="0"]
  0 -> 0 [label="!a"]
  0 -> 2 [label="a"]
  1 [label="1"]
  1 -> 0 [label="1"]
  2 [label="2"]
  2 -> 0 [label="!a"]
  2 -> 2 [label="a"]
  2 -> 3 [label="a"]
  3 [label="3", peripheries=2]
  3 -> 3 [label="a"]
}
EOF

diff expected stdout

# These one could be reduced to 2 5 0 0 and 3 8 1 0
test "`dstar2tgba -D dsa.dstar --stats '%s %t %p %d'`" = "4 8 0 0"
test "`dstar2tgba -DC dsa.dstar --stats '%s %t %p %d'`" = "5 11 1 0"



# DRA generated with
#  ltlfilt -f 'Ga | Fb' -l | ltl2dstar --ltl2nba=spin:path/ltl2tgba@-Ds - -
# (State name and comments added by hand to test the parser.)
cat >dra.dstar <<EOF
DRA v2 explicit
Comment: "Union{Safra[NBA=1],Safra[NBA=2]}"
States: 5
Acceptance-Pairs: 2
Start: 0
AP: 2 "a" "b"
---
State: 0 "bla"
Acc-Sig:
1
2  /* This is a comment */
3
4
State: 1 "foo"
Acc-Sig: -0
1
1 // This is another comment.
3
3
State: 2 "baz"
Acc-Sig: +0
1
2
// more
/// comment
3
4
State: 3 "str\n\"ing"
Acc-Sig: -0 +1
3  /***
   **** Some multiline comment
   ***/
3
3
3
State: 4 "more\"string\""
Acc-Sig: +0 +1
3
4
3
4
/* Same automaton with DSA instead of DRA, no comments, and less \n */
DSA v2 explicit
States: 5
Acceptance-Pairs: 2
Start: 0
AP: 2 "a" "b"
---
State: 0 "bla" Acc-Sig: 1 2 3 4
State: 1 "foo" Acc-Sig: -0 1 1 3 3
State: 2 "baz" Acc-Sig: +0 1 2 3 4
State: 3 "str\n\"ing" Acc-Sig: -0 +1 3 3 3 3
State: 4 "more\"string\"" Acc-Sig: +0 +1 3 4 3 4
EOF

run 0 autfilt -dA -B dra.dstar | tee stdout

cat >expected <<EOF
digraph "" {
  rankdir=LR
  node [shape="circle"]
  I [label="", style=invis, width=0]
  I -> 0
  0 [label="0"]
  0 -> 1 [label="!a & !b"]
  0 -> 2 [label="a & !b"]
  0 -> 3 [label="!a & b"]
  0 -> 4 [label="a & b"]
  1 [label="1"]
  1 -> 1 [label="!b"]
  1 -> 3 [label="b"]
  2 [label="2", peripheries=2]
  2 -> 1 [label="!a & !b"]
  2 -> 2 [label="a & !b"]
  2 -> 3 [label="!a & b"]
  2 -> 4 [label="a & b"]
  3 [label="3", peripheries=2]
  3 -> 3 [label="1"]
  4 [label="4", peripheries=2]
  4 -> 3 [label="!a"]
  4 -> 4 [label="a"]
}
digraph "" {
  rankdir=LR
  node [shape="circle"]
  I [label="", style=invis, width=0]
  I -> 0
  0 [label="0"]
  0 -> 1 [label="!a & !b"]
  0 -> 2 [label="a & !b"]
  1 [label="1", peripheries=2]
  1 -> 1 [label="!b"]
  2 [label="2"]
  2 -> 1 [label="!a & !b"]
  2 -> 2 [label="a & !b"]
}
EOF

diff expected stdout

cat >expected <<EOF
3 12 1 1 dra.dstar:1.1-42.70
2 4 0 1 dra.dstar:43.1-54.1
EOF

dstar2tgba --name=%F:%L -D dra.dstar --stats '%s %t %p %d %m' > out
cat out
diff expected out


# This has caused a crash at some point when dealing with 0-sized
# bitsets to represent acceptance sets.
cat >aut.dsa <<EOF
DSA v2 explicit
Comment: "Streett{Safra[NBA=1]}"
States: 1
Acceptance-Pairs: 0
Start: 0
AP: 0
---
State: 0
Acc-Sig:
0
EOF
run 0 dstar2tgba --name=%F --dot=Ant aut.dsa | tee stdout

cat >expected<<EOF
digraph "aut.dsa" {
  rankdir=LR
  label="aut.dsa"
  labelloc="t"
  node [shape="circle"]
  I [label="", style=invis, width=0]
  I -> 0
  0 [label="0"]
  0 -> 0 [label="1\n{0}"]
}
EOF

diff expected stdout
