[system] / trunk / pg / lib / Value / WeBWorK.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Value/WeBWorK.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5060 - (download) (as text) (annotate)
Thu Jun 28 20:42:53 2007 UTC (12 years, 7 months ago) by dpvc
File size: 3252 byte(s)
Use Value->context rather than $$Value::context.

    1 #############################################################
    2 #
    3 #  Definitions specific to WeBWorK.
    4 #
    5 
    6 ##################################################
    7 #
    8 #  Convert a student answer to a formula, with error trapping.
    9 #  If the result is undef, there was an error (message is in Context()->{error} object)
   10 #
   11 
   12 package Parser;
   13 
   14 sub Formula {
   15   my $f = shift;
   16   my $v = eval {Value->Package("Formula")->new($f)};
   17   reportEvalError($@) unless defined($v) || Value->context->{error}{flag};
   18   return $v;
   19 }
   20 
   21 #
   22 #  Evaluate a formula, with error trapping.
   23 #  If the result is undef, there was an error (message is in Context()->{error} object)
   24 #  If the result was a real, make it a fuzzy one.
   25 #
   26 sub Evaluate {
   27   my $f = shift;
   28   return unless defined($f);
   29   my $v = eval {$f->eval(@_)};
   30   if (defined($v)) {$v = Value::makeValue($v)}
   31     else {reportEvalError($@) unless $f->{context}{error}{flag}}
   32   return $v;
   33 }
   34 
   35 #
   36 #  Remove backtrace and line number, since these
   37 #  will be reported in the student
   38 #
   39 sub Parser::reportEvalError {
   40   my $error = shift; my $fullerror = $error;
   41   $error =~ s/ at \S+\.\S+ line \d+(\n|.)*//;
   42   $error =~ s/ at line \d+ of (\n|.)*//;
   43   Value->context->setError($error);
   44   if (Value->context->{debug}) {
   45     $fullerror =~ s/\n/<BR>/g;
   46     warn $fullerror;
   47   }
   48 }
   49 
   50 package main;
   51 
   52 #####################################################
   53 #
   54 # Use PG random number generator rather than perl
   55 #
   56 sub Value::Formula::PGseedRandom {
   57   my $self = shift;
   58   return if $self->{PGrandom};
   59   $self->{PGrandom} = new PGrandom($self->{context}->flag('random_seed'));
   60 }
   61 sub Value::Formula::PGgetRandom {shift->{PGrandom}->random(@_)}
   62 
   63 #####################################################
   64 #
   65 #  Initialize contexts with WW default data
   66 #
   67 
   68 my @wwEvalFields = qw(
   69   functAbsTolDefault
   70   functNumOfPoints
   71   functRelPercentTolDefault
   72   functZeroLevelDefault
   73   functZeroLevelTolDefault
   74   functMaxConstantOfIntegration
   75   numAbsTolDefault
   76   numFormatDefault
   77   numRelPercentTolDefault
   78   numZeroLevelDefault
   79   numZeroLevelTolDefault
   80   useBaseTenLog
   81 );
   82 
   83 sub Parser::Context::copy {
   84   my $self = shift;
   85   my $context = Value::Context::copy($self,@_);
   86   return $context unless $Parser::installed;  # only do WW initialization after parser is fully loaded
   87   return $context if $context->{WW} && scalar(keys %{$context->{WW}}) > 0;
   88   my $envir = eval('\\%main::envir');
   89   return $context unless $envir && scalar(keys(%{$envir})) > 0;
   90   my $ww = $context->{WW} = {}; push @{$context->{data}{values}}, 'WW';
   91   return $context if $Value::_no_WeBWorK_; # hack for command-line debugging
   92   foreach my $x (@wwEvalFields) {$context->{WW}{$x} = $envir->{$x}}
   93   $context->flags->set(
   94      tolerance      => $ww->{numRelPercentTolDefault} / 100,
   95      zeroLevel      => $ww->{numZeroLevelDefault},
   96      zeroLevelTol   => $ww->{numZeroLevelTolDefault},
   97      num_points     => $ww->{functNumOfPoints} + 2,
   98      max_adapt      => $ww->{functMaxConstantOfIntegration},
   99      useBaseTenLog  => $ww->{useBaseTenLog},
  100   );
  101   $context->{format}{number} = $ww->{numFormatDefault} if $ww->{numFormatDefault} ne '';
  102   $context;
  103 }
  104 
  105 #############################################################
  106 
  107 use Value::AnswerChecker;
  108 
  109 #############################################################
  110 
  111 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9