WeBWorK Main Forum

How do multiple questions/answers in a PGML problem with multiple Contexts?

How do multiple questions/answers in a PGML problem with multiple Contexts?

by Christian Seberino -
Number of replies: 4
How do multiple questions/answers in a PGML problem with multiple Contexts?

I'm specifically having problems with ImplicitEquation and Inequality answers since they each demand their own DIFFERENT context?!

cs
In reply to Christian Seberino

Re: How do multiple questions/answers in a PGML problem with multiple Contexts?

by Alex Jordan -
Hi Christian,

Setting a context is not as global of a step to take as you might think it is. If I may speak in a kind of pseudocode:
Set a Context1;
Define a $MathObject1;

Set a Context2;
Define a $MathObject2;
Now at this point in your code, $MathObject1 still has all of its properties from Context1. When $MathObject1 was created, all of those properties, including its answer checker, were set as an internal part of $MathObject1.

If, however, at this point in your code you did something like:
$MathObject1 = Compute($MathObject1);
then at this point the Compute command is computing via Context2, and so your $MathObject1 will lose its Context1 properties.

So if I understand your question and if you are using PGML for the body:
Set a Context1;
Define a $MathObject1;

Define a $PerlScalar;

Set a Context2;
Define a $MathObject2;

BEGIN_PGML
Our first answer is [_____]{$MathObject1}

Our second answer is [_____]{$MathObject2}

A third answer could be [_____]{$PerlScalar} but note that it will be put through the current Context2 Compute command before it is checked against the student's answer.

END_PGML

As a side note, using ImplicitEquation requires more attention than usual to things like limits and test points for Formula checking.
 
In reply to Alex Jordan

Re: How do multiple questions/answers in a PGML problem with multiple Contexts?

by Christian Seberino -
Beautiful.  Thanks.  That answered my question.  So assignments "freeze" context information in the objects created.  I get it now.

cs
In reply to Christian Seberino

Re: How do multiple questions/answers in a PGML problem with multiple Contexts?

by Davide Cervone -
One slight clarification. It is not actually that the context is "frozen", but each MathObject knows the context in which it was created, and continues to use that context for its checking and other operations. What it actually has is a reference to the context that was in effect when it was created, not a copy of the context, so if that context is modified after the object is created, those changes do affect the object. But changing what the active context is doesn't affect the object.

Davide
In reply to Alex Jordan

Re: How do multiple questions/answers in a PGML problem with multiple Contexts?

by Davide Cervone -
Another slight clarification: the command
    Context("Context1");
    $MathObject = Compute("...");
    Context("Context2");
    $MathObject1 = Compute($MathObject1);
doesn't really recompute $MathObject1, it just switches what context it is associated with (but doesn't re-parse the object using the new rules). So, for example,
    Context("Vector");
    $f = Compute("<x,x+1>");
    Context("Numeric");
    $f = Compute($f);
doesn't cause an error, it simply forces the formula for $f to think it is from the Numeric context. If
    ANS($f->cmp);
were used, the student would have no way to enter the correct answer, since the vector could not be entered in Numeric context.

On the other hand,

    Context("Vector");
    $f = Compute("<x,x+1>");
    Context("Numeric");
    $f = Compute("$f");
would cause an error, because here "$f" means $f is first stringified, and then Compute() re-parses the string, leading to an error, since < is not defined in Numeric context.

This is one of the places where the difference between $f and "$f" is crucial.

Davide