| author | desharna | 
| Wed, 22 Jun 2022 14:52:27 +0200 | |
| changeset 75585 | a789c5732f7a | 
| parent 62677 | 0df43889f496 | 
| child 83355 | 5f739af7fb4e | 
| permissions | -rwxr-xr-x | 
| 47412 | 1 | #!/usr/bin/env bash | 
| 2 | # | |
| 3 | # Author: Nik Sultana, Cambridge University Computer Lab | |
| 4 | # | |
| 5 | # DESCRIPTION: TPTP visualisation utility | |
| 6 | ||
| 7 | ||
| 8 | PRG="$(basename "$0")" | |
| 9 | ||
| 10 | #FIXME inline or move to settings | |
| 11 | DOT2TEX=dot2tex | |
| 12 | DOT=dot | |
| 47473 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 13 | PERL=perl | 
| 47412 | 14 | PDFLATEX=pdflatex | 
| 15 | [ -n "$ISABELLE_PDFLATEX" ] && PDFLATEX=$ISABELLE_PDFLATEX | |
| 16 | DOT2TEX_VERSION="$($DOT2TEX -V 2> /dev/null)" | |
| 47473 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 17 | DOT_VERSION="$($DOT -V 2>&1 | grep Graphviz)" | 
| 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 18 | PERL_VERSION="$($PERL -v | grep -e "v[0-9]\+." 2> /dev/null)" | 
| 47412 | 19 | PDFLATEX_VERSION="$($PDFLATEX -version | head -1 2> /dev/null)" | 
| 20 | ||
| 21 | function check_deps() | |
| 22 | {
 | |
| 23 | #FIXME how well does this work across different platforms and/or versions of | |
| 24 | # the tools? | |
| 47473 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 25 | for DEP in DOT2TEX DOT PERL PDFLATEX | 
| 47412 | 26 | do | 
| 47473 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 27 |     eval DEP_VAL=\$${DEP}
 | 
| 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 28 |     eval DEP_VERSION=\$${DEP}_VERSION
 | 
| 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 29 | if [ -z "$DEP_VERSION" ]; then | 
| 47412 | 30 | echo "$DEP not installed" | 
| 31 | else | |
| 47473 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 32 | echo "$DEP ($DEP_VAL) : $DEP_VERSION" | 
| 47412 | 33 | fi | 
| 34 | done | |
| 35 | } | |
| 36 | ||
| 37 | function usage() {
 | |
| 38 | echo | |
| 39 | echo "Usage: isabelle $PRG [OPTIONS] IN_FILE OUT_FILE" | |
| 40 | echo | |
| 41 | echo " Options are:" | |
| 42 | echo " -d probe for dependencies" | |
| 43 | echo " -k don't delete temp files, and print their location" | |
| 44 | echo " -n print name of the generated file" | |
| 45 | echo | |
| 46 | echo " Produces a DOT/TeX/PDF from a TPTP problem/proof, depending on whether" | |
| 47 | echo " the extension of OUT_FILE is dot/tex/pdf." | |
| 48 | echo | |
| 49 | exit 1 | |
| 50 | } | |
| 51 | ||
| 52 | OUTPUT_FORMAT=2 | |
| 53 | SHOW_TARGET="" | |
| 54 | KEEP_TEMP="" | |
| 55 | NON_EXEC="" | |
| 56 | ||
| 47473 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 57 | while getopts "dnkX" OPT | 
| 47412 | 58 | do | 
| 59 | #FIXME could add "quiet" mode to send stderr (and some stdout) to /dev/null | |
| 60 | case "$OPT" in | |
| 61 | n) | |
| 62 | SHOW_TARGET=true | |
| 63 | ;; | |
| 64 | k) | |
| 65 | KEEP_TEMP=true | |
| 66 | ;; | |
| 67 | X) | |
| 68 | NON_EXEC=true | |
| 69 | ;; | |
| 70 | d) | |
| 71 | check_deps | |
| 72 | exit 0 | |
| 73 | ;; | |
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 74 | *) | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 75 | exit 1 | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 76 | ;; | 
| 47412 | 77 | esac | 
| 78 | done | |
| 79 | ||
| 80 | shift $(($OPTIND - 1)) | |
| 81 | [ "$#" -ne 2 -o "$1" = "-?" ] && usage | |
| 82 | ||
| 83 | case "${2##*.}" in
 | |
| 84 | dot) | |
| 85 | OUTPUT_FORMAT=0 | |
| 86 | ;; | |
| 87 | tex) | |
| 88 | OUTPUT_FORMAT=1 | |
| 89 | ;; | |
| 90 | pdf) | |
| 91 | OUTPUT_FORMAT=2 | |
| 92 | ;; | |
| 93 | *) | |
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 94 |       echo "Unrecognised output file extension \".${2##*.}\"."
 | 
| 47412 | 95 | exit 1 | 
| 96 | ;; | |
| 97 | esac | |
| 98 | ||
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 99 | ## set some essential variables, prepare the work directory | 
| 47412 | 100 | |
| 101 | WORKDIR="" | |
| 102 | while : | |
| 103 | do | |
| 47473 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 104 | #FIXME not perfectly reliable method, but probably good enough | 
| 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 105 |   WORKDIR="${ISABELLE_TMP_PREFIX}-tptpgraph$RANDOM"
 | 
| 
111cd6351613
aligned tptp_graph dependencies to Isabelle conventions;
 sultana parents: 
47412diff
changeset | 106 | [ ! -d "$WORKDIR" ] && break | 
| 47412 | 107 | done | 
| 108 | OUTPUT_FILENAME="$(basename "$2")" | |
| 109 | FILEDIR="$(cd "$(dirname "$2")"; cd "$(pwd -P)"; pwd)" | |
| 110 | FILENAME="${OUTPUT_FILENAME%.*}"
 | |
| 111 | WD="$(pwd)" | |
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 112 | mkdir -p $WORKDIR | 
| 47412 | 113 | |
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 114 | function generate_dot() | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 115 | {
 | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 116 | LOADER="tptp_graph_$RANDOM" | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 117 | echo "theory $LOADER imports \"$TPTP_HOME/TPTP_Parser\" \ | 
| 53392 
a1a45fdc53a3
updated syntax to use 'ML_file' rather than 'uses';
 sultana parents: 
53386diff
changeset | 118 | begin ML_file \"$TPTP_HOME/TPTP_Parser/tptp_to_dot.ML\" \ | 
| 
a1a45fdc53a3
updated syntax to use 'ML_file' rather than 'uses';
 sultana parents: 
53386diff
changeset | 119 | ML {* TPTP_To_Dot.write_proof_dot \"$1\" \"$2\" *} end" \
 | 
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 120 | > $WORKDIR/$LOADER.thy | 
| 62677 | 121 | isabelle process -T "$WORKDIR/$LOADER" -l Pure | 
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 122 | } | 
| 47412 | 123 | |
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 124 | function cleanup_workdir() | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 125 | {
 | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 126 | if [ -n "$KEEP_TEMP" ]; then | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 127 | echo $WORKDIR | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 128 | else | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 129 | rm -rf $WORKDIR | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 130 | fi | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 131 | } | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 132 | |
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 133 | if [ "$OUTPUT_FORMAT" -eq 0 ]; then | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 134 | [ -z "$NON_EXEC" ] && generate_dot "$1" "$2" | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 135 | cleanup_workdir | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 136 | exit 0 | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 137 | fi | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 138 | |
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 139 | ## generate and process files in temporary workdir, then move required | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 140 | ## output file to destination dir | 
| 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 141 | |
| 47412 | 142 | [ -z "$NON_EXEC" ] && generate_dot $1 "$WORKDIR/${FILENAME}.dot"
 | 
| 143 | cd $WORKDIR | |
| 144 | if [ -z "$NON_EXEC" ]; then | |
| 145 |   $DOT -Txdot "${FILENAME}.dot" \
 | |
| 53386 | 146 | | $DOT2TEX -f pgf -t raw --crop \ | 
| 147 |   | $PERL -w -p -e 's/_/\\_/g' > "${FILENAME}.tex"
 | |
| 47412 | 148 | fi | 
| 149 | ||
| 150 | if [ "$OUTPUT_FORMAT" -eq 1 ]; then | |
| 151 | TARGET=$FILENAME.tex | |
| 152 | else | |
| 153 | TARGET=$FILENAME.pdf | |
| 154 |   [ -z "$NON_EXEC" ] && $PDFLATEX "${FILENAME}.tex"
 | |
| 155 | fi | |
| 156 | [ -z "$NON_EXEC" ] && mv $TARGET $WD | |
| 157 | cd $WD | |
| 47517 
6bc4c66d8c1b
improved tptp_graph robustness by relying on thy;
 sultana parents: 
47473diff
changeset | 158 | cleanup_workdir | 
| 47412 | 159 | |
| 160 | [ -n "$SHOW_TARGET" ] && echo "$FILEDIR/$TARGET" |