[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 2688 - (download) (as text) (annotate)
Fri Aug 27 19:42:59 2004 UTC (15 years, 3 months ago) by dpvc
File size: 3060 byte(s)
Added in functMaxConstantOfIntegration support in the adaptive
parameter answer checking.  Also added a flag to the formula answer
checker that allows checking a formula up to a constant (for
integration problems).  This really just adds a new parameter to the
context and adds that to the formula, so there is nothing deep about
this.

	ANS(Formula("2x")->cmp(upToConstant=>1));

Finally, don't look for adaptive parameters if they aren't actually
used in the professor's formula (even if they are defined).

    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::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 );
   81 
   82 sub Value::Context::initCopy {
   83   my $self = shift;
   84   my $context = $self->copy(@_);
   85   return $context if $context->{WW};
   86   $context->{WW} = {}; push @{$context->{data}{values}}, 'WW';
   87   return $context if $Value::_no_WeBWorK_; # hack for command-line debugging
   88   return $context unless $Parser::installed;  # only do WW initialization after parser is fully loaded
   89   foreach my $x (@wwEvalFields) {$context->{WW}{$x} = eval('$main::envir{'.$x.'}');}
   90   my $ww = $context->{WW};
   91   $context->flags->set(
   92      tolerance    => $ww->{numRelPercentTolDefault} / 100,
   93      zeroLevel    => $ww->{numZeroLevelDefault},
   94      zeroLevelTol => $ww->{numZeroLevelTolDefault},
   95      num_points   => $ww->{functNumOfPoints} + 2,
   96      max_adapt    => $ww->{functMaxConstantOfIntegration},
   97   );
   98   $context->{format}{number} = $ww->{numFormatDefault} if $ww->{$numFormatDefault} ne '';
   99   $context;
  100 }
  101 
  102 #############################################################
  103 
  104 use Value::AnswerChecker;
  105 
  106 #############################################################
  107 
  108 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9