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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2664 - (download) (as text) (annotate)
Sat Aug 21 22:02:14 2004 UTC (15 years, 6 months ago) by dpvc
File size: 2826 byte(s)
Added a file to perform WeBWorK-specific modifications to the
Parser/Value packages.  (I've tried to make these independent of
WeBWorK, so you can use them in other perl code if you want to.)

The parameters for fuzzy reals and some of the other parameters now
are taken from the pg->{ansEvalDefaults} values (as defined in
global.conf or course.conf).  More still needs to be done with this,
however.

    1 #########################################################################
    2 #
    3 #  The basic context (Parser::Context is a subclass of this)
    4 #
    5 
    6 package Value::Context;
    7 my $pkg = "Value::Context";
    8 use strict;
    9 
   10 #
   11 #  Create a new Context object and initialize its data lists
   12 #
   13 sub new {
   14   my $self = shift; my $class = ref($self) || $self;
   15   my $context = bless {
   16     flags => {},
   17     pattern => {
   18       number => '(?:\d+(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?',
   19       signedNumber => '[-+]?(?:\d+(?:\.\d*)?|\.\d+)(?:E[-+]?\d+)?',
   20     },
   21     format => {
   22       number => '%g',  # default format for Reals
   23     },
   24     error => {
   25       string => '',
   26       pos => undef,
   27       message => '',
   28       flag => 0,
   29     },
   30     data => {
   31       hashes => ['lists'],
   32       arrays => ['data'],
   33       values => ['flags','pattern','format'],
   34     },
   35   }, $class;
   36   my %data = (lists=>{},flags=>{},@_);
   37   $context->{_lists} = new Value::Context::Lists($context,%{$data{lists}});
   38   $context->{_flags} = new Value::Context::Flags($context,%{$data{flags}});
   39   $context->{_initialized} = 1;
   40   $context->update;
   41   return $context;
   42 }
   43 
   44 #
   45 #  Implemented in subclasses
   46 #
   47 sub update {}
   48 
   49 #
   50 #  Access to the data lists
   51 #
   52 sub lists     {(shift)->{_lists}}
   53 sub flags     {(shift)->{_flags}}
   54 sub flag      {(shift)->{_flags}->get(shift)}
   55 
   56 #
   57 #  Make a copy of a Context object
   58 #
   59 sub copy {
   60   my $self = shift;
   61   my $context = $self->new();
   62   $context->{_initialized} = 0;
   63   foreach my $data (@{$context->{data}{hashes}}) {
   64     $context->{$data} = {};
   65     foreach my $x (keys %{$self->{$data}}) {
   66       $context->{$data}{$x} = {%{$self->{$data}{$x}}};
   67     }
   68     $context->{"_$data"}->update;
   69   }
   70   foreach my $data (@{$context->{data}{arrays}}) {
   71     $context->{$data} = {};
   72     foreach my $x (keys %{$self->{$data}}) {
   73       $context->{$data}{$x} = [@{$self->{$data}{$x}}];
   74     }
   75   }
   76   foreach my $data (@{$context->{data}{values}}) {
   77     $context->{$data} = {%{$self->{$data}}};
   78   }
   79   $context->{_initialized} = 1;
   80   return $context;
   81 }
   82 
   83 #
   84 #  Make a copy with additional initialization
   85 #  (defined in subclasses)
   86 #
   87 sub initCopy {shift->copy(@_)}
   88 
   89 #
   90 #  Make stringify produce TeX or regular strings
   91 #
   92 sub texStrings {shift->flags->set(StringifyAsTeX=>1)}
   93 sub normalStrings {shift->flags->set(StringifyAsTeX=>0)}
   94 
   95 #
   96 #  Clear error flags
   97 #
   98 sub clearError {
   99   my $error = (shift)->{error};
  100   $error->{string} = '';
  101   $error->{pos} = undef;
  102   $error->{message} = '';
  103   $error->{flag} = 0;
  104 }
  105 
  106 #
  107 #  Set the error flags
  108 #
  109 sub setError {
  110   my $error = (shift)->{error};
  111   $error->{message} = shift;
  112   $error->{string} = shift;
  113   $error->{pos} = shift;
  114   $error->{flag} = 1;
  115 }
  116 
  117 #########################################################################
  118 #
  119 #  Load the subclasses.
  120 #
  121 
  122 use Value::Context::Data;
  123 
  124 #########################################################################
  125 
  126 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9