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

View of /trunk/pg/lib/Parser/Context/Variables.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2687 - (download) (as text) (annotate)
Fri Aug 27 00:28:27 2004 UTC (15 years, 3 months ago) by dpvc
File size: 2470 byte(s)
Added ability to have linear adaptive parameters in the function
answer checker.  It only works for real-valued functions, though.
To use a parameter, use

       Context()->variables->add(a=>'Parameter');

and then use 'a' as a variable within your answer.  The student will
not be allowed to enter the parameter, but the professor will.

Note that the correct answer will show the full professor's answer,
including the parameters, even though the student can't type it.  Is
this the right behaviour?

    1 #########################################################################
    2 #
    3 #  Implements the list of known variables and their types
    4 #
    5 package Parser::Context::Variables;
    6 use strict;
    7 use vars qw(@ISA %type);
    8 @ISA = qw(Value::Context::Data);
    9 
   10 #
   11 #  The named types for variables
   12 #    (you can use arbitary types by supplying an
   13 #     instance of the type rather than a name)
   14 #
   15 %type = (
   16   'Real'    => $Value::Type{number},
   17   'Complex' => $Value::Type{complex},
   18   'Point2D' => Value::Type('Point',2,$Value::Type{number}),
   19   'Point3D' => Value::Type('Point',3,$Value::Type{number}),
   20   'Vector2D' => Value::Type('Vector',2,$Value::Type{number}),
   21   'Vector3D' => Value::Type('Vector',3,$Value::Type{number}),
   22   'Parameter' => $Value::Type{number},
   23 );
   24 
   25 sub init {
   26   my $self = shift;
   27   $self->{dataName} = 'variables';
   28   $self->{name} = 'variable';
   29   $self->{Name} = 'Variable';
   30   $self->{namePattern} = '[a-zA-Z]';
   31   $self->{pattern} = $self->{namePattern};
   32 }
   33 
   34 #
   35 #  Our pattern should match ANY variable name
   36 #    (Parser takes care of reporting unknown ones)
   37 #
   38 sub update {
   39   my $self = shift;
   40   $self->{pattern} = $self->{namePattern};
   41 }
   42 
   43 #
   44 #  If the type is one of the names ones, use it's known type
   45 #  Otherwise if it is a Value object use its type,
   46 #  Otherwise, if it is a signed number, use the Real type
   47 #  Otherwise report an error
   48 #
   49 sub create {
   50   my $self = shift; my $value = shift; my @extra;
   51   return $value if ref($value) eq 'HASH';
   52   if (defined($type{$value})) {
   53     @extra = (parameter => 1) if $value eq 'Parameter';
   54     $value = $type{$value};
   55   } elsif (Value::isValue($value)) {
   56     $value = $value->typeRef;
   57   } elsif ($value =~ m/$self->{context}{pattern}{signedNumber}/) {
   58     $value = $type{'Real'};
   59   } else {
   60     Value::Error("Unrecognized variable type '$value'");
   61   }
   62   return {type => $value, @extra};
   63 }
   64 
   65 #
   66 #  Return a variable's type
   67 #
   68 sub type {
   69   my $self = shift; my $x = shift;
   70   return $self->{context}{variables}{$x}{type};
   71 }
   72 
   73 #
   74 #  Get the names of all variables
   75 #
   76 sub variables {
   77   my $self = shift; my @names;
   78   foreach my $x ($self->SUPER::names)
   79     {push(@names,$x) unless $self->{context}{variables}{$x}{parameter}}
   80   return @names;
   81 }
   82 
   83 #
   84 #  Get the names of all parameters
   85 #
   86 sub parameters {
   87   my $self = shift; my @names;
   88   foreach my $x ($self->SUPER::names)
   89     {push(@names,$x) if $self->{context}{variables}{$x}{parameter}}
   90   return @names;
   91 }
   92 
   93 #########################################################################
   94 
   95 1;
   96 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9