src/HOL/SMT/lib/scripts/run_smt_solver.pl
author boehmes
Tue, 02 Feb 2010 18:10:41 +0100
changeset 34983 e5cb3a016094
permissions -rw-r--r--
collect certificates in a single file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34983
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     1
#
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     2
# Author: Sascha Boehme, TU Muenchen
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     3
#
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     4
# Cache for prover results, based on discriminating problems using MD5.
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     5
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     6
use strict;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     7
use warnings;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     8
use Digest::MD5;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
     9
use LWP;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    10
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    11
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    12
# arguments
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    13
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    14
my $certs_file = shift @ARGV;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    15
my $location = shift @ARGV;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    16
my $result_file = pop @ARGV;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    17
my $problem_file = $ARGV[-1];
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    18
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    19
my $use_certs = not ($certs_file eq "-");
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    20
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    21
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    22
# create MD5 problem digest
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    23
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    24
my $problem_digest = "";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    25
if ($use_certs) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    26
  my $md5 = Digest::MD5->new;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    27
  open FILE, "<$problem_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    28
    die "ERROR: Failed to open '$problem_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    29
  $md5->addfile(*FILE);
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    30
  close FILE;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    31
  $problem_digest = $md5->b64digest;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    32
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    33
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    34
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    35
# lookup problem digest among existing certificates and
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    36
# return a cached result (if there is a certificate for the problem)
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    37
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    38
if ($use_certs and -e $certs_file) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    39
  my $cached = 0;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    40
  open CERTS, "<$certs_file" or die "ERROR: Failed to open '$certs_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    41
  while (<CERTS>) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    42
    if (m/(\S+) (\d+)/) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    43
      if ($1 eq $problem_digest) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    44
        my $start = tell CERTS;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    45
        open FILE, ">$result_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    46
          die "ERROR: Failed to open '$result_file ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    47
        while ((tell CERTS) - $start < $2) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    48
          my $line = <CERTS>;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    49
          print FILE $line;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    50
        }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    51
        close FILE;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    52
        $cached = 1;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    53
        last;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    54
      }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    55
      else { seek CERTS, $2, 1; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    56
    }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    57
    else { die "ERROR: Invalid file format in '$certs_file'"; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    58
  }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    59
  close CERTS;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    60
  if ($cached) { exit 0; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    61
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    62
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    63
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    64
# invoke (remote or local) prover
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    65
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    66
if ($location eq "remote") {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    67
  # arguments
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    68
  my $solver = $ARGV[0];
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    69
  my @options = @ARGV[1 .. ($#ARGV - 1)];
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    70
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    71
  # call solver
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    72
  my $agent = LWP::UserAgent->new;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    73
  $agent->agent("SMT-Request");
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    74
  $agent->timeout(180);
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    75
  my $response = $agent->post($ENV{"REMOTE_SMT_URL"}, [
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    76
    "Solver" => $solver,
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    77
    "Options" => join(" ", @options),
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    78
    "Problem" => [$problem_file] ],
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    79
    "Content_Type" => "form-data");
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    80
  if (not $response->is_success) { die "HTTP-Error: " . $response->message; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    81
  else {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    82
    open FILE, ">$result_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    83
      die "ERROR: Failed to create '$result_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    84
    print FILE $response->content;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    85
    close FILE;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    86
  }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    87
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    88
elsif ($location eq "local") {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    89
  system "@ARGV >$result_file 2>&1";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    90
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    91
else { die "ERROR: No SMT solver invoked"; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    92
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    93
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    94
# cache result
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    95
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    96
if ($use_certs) {
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    97
  open CERTS, ">>$certs_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    98
    die "ERROR: Failed to open '$certs_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
    99
  print CERTS
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   100
    ("$problem_digest " . ((-s $result_file) + (length "\n")) . "\n");
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   101
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   102
  open FILE, "<$result_file" or
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   103
    die "ERROR: Failed to open '$result_file' ($!)";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   104
  foreach (<FILE>) { print CERTS $_; }
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   105
  close FILE; 
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   106
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   107
  print CERTS "\n";
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   108
  close CERTS;
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   109
}
e5cb3a016094 collect certificates in a single file
boehmes
parents:
diff changeset
   110