src/HOL/TPTP/lib/Tools/tptp_graph
author sultana
Tue, 10 Apr 2012 06:45:15 +0100
changeset 47412 aac1aa93f1ea
child 47473 111cd6351613
permissions -rwxr-xr-x
added graph-conversion utility for TPTP files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47412
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     1
#!/usr/bin/env bash
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     2
#
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     3
# Author: Nik Sultana, Cambridge University Computer Lab
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     4
#
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     5
# DESCRIPTION: TPTP visualisation utility
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     6
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     7
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     8
PRG="$(basename "$0")"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
     9
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    10
#FIXME inline or move to settings
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    11
DOT2TEX=dot2tex
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    12
DOT=dot
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    13
SED=sed
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    14
PDFLATEX=pdflatex
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    15
[ -n "$ISABELLE_PDFLATEX" ] && PDFLATEX=$ISABELLE_PDFLATEX
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    16
DOT2TEX_VERSION="$($DOT2TEX -V 2> /dev/null)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    17
DOT_VERSION="$($DOT -V 2> /dev/null)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    18
SED_VERSION="$($SED --version | head -1 2> /dev/null)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    19
PDFLATEX_VERSION="$($PDFLATEX -version | head -1 2> /dev/null)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    20
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    21
function check_deps()
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    22
{
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    23
  #FIXME how well does this work across different platforms and/or versions of
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    24
  #      the tools?
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    25
  for DEP in DOT2TEX_VERSION DOT_VERSION SED_VERSION PDFLATEX_VERSION
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    26
  do
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    27
    eval DEP_VAL=\$$DEP
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    28
    if [ -z "$DEP_VAL" ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    29
      echo "$DEP not installed"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    30
    else
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    31
      echo "$DEP_VAL"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    32
    fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    33
  done
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    34
}
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    35
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    36
function usage() {
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    37
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    38
  echo "Usage: isabelle $PRG [OPTIONS] IN_FILE OUT_FILE"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    39
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    40
  echo "  Options are:"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    41
  echo "    -d           probe for dependencies"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    42
  echo "    -k           don't delete temp files, and print their location"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    43
  echo "    -n           print name of the generated file"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    44
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    45
  echo "  Produces a DOT/TeX/PDF from a TPTP problem/proof, depending on whether"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    46
  echo "  the extension of OUT_FILE is dot/tex/pdf."
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    47
  echo
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    48
  exit 1
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    49
}
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    50
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    51
OUTPUT_FORMAT=2
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    52
SHOW_TARGET=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    53
KEEP_TEMP=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    54
NON_EXEC=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    55
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    56
while getopts "dknX" OPT
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    57
do
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    58
  #FIXME could add "quiet" mode to send stderr (and some stdout) to /dev/null
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    59
  case "$OPT" in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    60
    n)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    61
      SHOW_TARGET=true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    62
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    63
    k)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    64
      KEEP_TEMP=true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    65
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    66
    X)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    67
      NON_EXEC=true
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    68
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    69
    d)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    70
      check_deps
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    71
      exit 0
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    72
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    73
  esac
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    74
done
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    75
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    76
shift $(($OPTIND - 1))
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    77
[ "$#" -ne 2 -o "$1" = "-?" ] && usage
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    78
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    79
case "${2##*.}" in
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    80
    dot)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    81
      OUTPUT_FORMAT=0
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    82
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    83
    tex)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    84
      OUTPUT_FORMAT=1
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    85
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    86
    pdf)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    87
      OUTPUT_FORMAT=2
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    88
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    89
    *)
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    90
      echo "Unrecognised output file extension."
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    91
      exit 1
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    92
      ;;
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    93
esac
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    94
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    95
function generate_dot()
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    96
{
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    97
  #FIXME using a thy might be more robust
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    98
  LOADER="use \"$TPTP_HOME/TPTP_Parser/ml_yacc_lib.ML\"; \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
    99
          use \"$TPTP_HOME/TPTP_Parser/tptp_syntax.ML\"; \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   100
          use \"$TPTP_HOME/TPTP_Parser/tptp_lexyacc.ML\"; \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   101
          use \"$TPTP_HOME/TPTP_Parser/tptp_parser.ML\"; \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   102
          (*\"$TPTP_HOME/TPTP_Parser/tptp_problem_name.ML\";*) \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   103
          use \"$TPTP_HOME/TPTP_Parser/tptp_proof.ML\"; \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   104
          use \"$TPTP_HOME/TPTP_Parser/tptp_to_dot.ML\"; \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   105
          TPTP_To_Dot.write_proof_dot \"$1\" \"$2\"; exit 0;"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   106
  "$ISABELLE_PROCESS" -e "$LOADER" Pure
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   107
}
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   108
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   109
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   110
if [ "$OUTPUT_FORMAT" -eq 0 ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   111
  [ -z "$NON_EXEC" ] && generate_dot "$1" "$2"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   112
  exit 0
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   113
fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   114
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   115
## set some essential variables
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   116
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   117
[ -z "$TMPDIR" ] && TMPDIR=/tmp
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   118
WORKDIR=""
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   119
while :
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   120
do
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   121
  WORKDIR="$TMPDIR/tptpgraph$RANDOM"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   122
  if [ ! -d "$WORKDIR" ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   123
    break
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   124
  fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   125
done
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   126
OUTPUT_FILENAME="$(basename "$2")"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   127
FILEDIR="$(cd "$(dirname "$2")"; cd "$(pwd -P)"; pwd)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   128
FILENAME="${OUTPUT_FILENAME%.*}"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   129
WD="$(pwd)"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   130
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   131
## generate and process files in temporary workdir, then move to destination dir
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   132
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   133
mkdir -p $WORKDIR
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   134
[ -z "$NON_EXEC" ] && generate_dot $1 "$WORKDIR/${FILENAME}.dot"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   135
cd $WORKDIR
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   136
if [ -z "$NON_EXEC" ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   137
  $DOT -Txdot "${FILENAME}.dot" \
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   138
  | $DOT2TEX -f pgf -t raw --crop > "${FILENAME}.tex"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   139
  $SED -i 's/_/\\_/g' "${FILENAME}.tex"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   140
fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   141
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   142
if [ "$OUTPUT_FORMAT" -eq 1 ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   143
  TARGET=$FILENAME.tex
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   144
else
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   145
  TARGET=$FILENAME.pdf
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   146
  [ -z "$NON_EXEC" ] && $PDFLATEX "${FILENAME}.tex"
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   147
fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   148
[ -z "$NON_EXEC" ] && mv $TARGET $WD
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   149
cd $WD
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   150
if [ -n "$KEEP_TEMP" ]; then
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   151
  echo $WORKDIR
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   152
else
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   153
  rm -rf $WORKDIR
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   154
fi
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   155
aac1aa93f1ea added graph-conversion utility for TPTP files
sultana
parents:
diff changeset
   156
[ -n "$SHOW_TARGET" ] && echo "$FILEDIR/$TARGET"