Skip to content
Permalink
Browse files

JME part: remove "expected variable names property"

closes #345

I can't remember ever using a variable name that wasn't in the correct
answer in the "expected variable names" box.

So, I've got rid of that property, and the variables in the correct answer are used automatically instead.
  • Loading branch information...
christianp committed Feb 6, 2019
1 parent f47d529 commit 3c1799878cc124f3e07e89166ca308d8832da193
@@ -707,7 +707,6 @@ def __init__(self,marks=0,prompt=''):
self.minLength.length = 0
self.mustHave = StringRestriction('musthave',0,'Your answer does not contain all required elements.')
self.notAllowed = StringRestriction('notallowed',0,'Your answer contains elements which are not allowed.')
self.expectedVariableNames = StringRestriction('expectedvariablenames')
self.mustMatchPattern = PatternRestriction('mustmatchpattern')

def loadDATA(self, builder, data):
@@ -731,12 +730,6 @@ def loadDATA(self, builder, data):
self.mustHave = builder.string_restriction('musthave',data['musthave'],self.mustHave)
if haskey(data,'notallowed'):
self.notAllowed = builder.string_restriction('notallowed',data['notallowed'],self.notAllowed)
if haskey(data,'expectedvariablenames'):
self.expectedVariableNames = StringRestriction('expectedvariablenames')
try:
self.expectedVariableNames.strings = list(case_insensitive_get(data,'expectedvariablenames'))
except TypeError:
raise ExamError('expected variable names setting %s is not a list' % data['expectedvariablenames'])
if haskey(data,'mustmatchpattern'):
self.mustMatchPattern = builder.pattern_restriction('mustmatchpattern',data['mustmatchpattern'],self.mustMatchPattern)

@@ -786,7 +779,6 @@ def toxml(self):
answer.append(self.minLength.toxml())
answer.append(self.mustHave.toxml())
answer.append(self.notAllowed.toxml())
answer.append(self.expectedVariableNames.toxml())
answer.append(self.mustMatchPattern.toxml())

return part
@@ -54,8 +54,8 @@ correctVariables (Variables used in the correct answer):
set(findvars(correctCompare))

unexpectedVariables (Unexpected variables used in the student's answer):
let(uvars, filter(not (x in settings["expectedVariableNames"]),x,list(studentVariables)),
assert(len(settings["expectedVariableNames"])=0 or len(uvars)=0,
let(uvars, filter(not (x in correctVariables),x,list(studentVariables)),
assert(not settings["checkVariableNames"] or len(settings["expectedVariableNames"])=0 or len(uvars)=0,
warn(translate("part.jme.unexpected variable name",["name":uvars[0]]))
);
uvars
@@ -35,7 +35,6 @@ var JMEPart = Numbas.parts.JMEPart = function(path, question, parentPart)
settings.valueGenerators = {};
settings.mustHave = [];
settings.notAllowed = [];
settings.expectedVariableNames = [];
}
JMEPart.prototype = /** @lends Numbas.JMEPart.prototype */
{
@@ -124,13 +123,6 @@ JMEPart.prototype = /** @lends Numbas.JMEPart.prototype */
}

tryGetAttribute(settings,xml,parametersPath,['checkVariableNames','showPreview']);
var expectedVariableNamesNode = xml.selectSingleNode('answer/expectedvariablenames');
if(expectedVariableNamesNode)
{
var nameNodes = expectedVariableNamesNode.selectNodes('string');
for(var i=0; i<nameNodes.length; i++)
settings.expectedVariableNames.push(Numbas.xml.getTextContent(nameNodes[i]).toLowerCase().trim());
}
},
loadFromJSON: function(data) {
var p = this;
settings.valueGenerators = {};
settings.mustHave = [];
settings.notAllowed = [];
settings.expectedVariableNames = [];
}
JMEPart.prototype = /** @lends Numbas.JMEPart.prototype */
{
}

tryGetAttribute(settings,xml,parametersPath,['checkVariableNames','showPreview']);
var expectedVariableNamesNode = xml.selectSingleNode('answer/expectedvariablenames');
if(expectedVariableNamesNode)
{
var nameNodes = expectedVariableNamesNode.selectNodes('string');
for(var i=0; i<nameNodes.length; i++)
settings.expectedVariableNames.push(Numbas.xml.getTextContent(nameNodes[i]).toLowerCase().trim());
}
},
loadFromJSON: function(data) {
var p = this;
@@ -1,6 +1,7 @@
Numbas.queueScript('display/parts/jme',['display-base','part-display','util','jme-display','jme'],function() {
var display = Numbas.display;
var extend = Numbas.util.extend;
var jme = Numbas.jme;
/** Display code for a {@link Numbas.parts.JMEPart}
* @constructor
* @augments Numbas.display.PartDisplay
@@ -29,7 +30,7 @@ Numbas.queueScript('display/parts/jme',['display-base','part-display','util','jm
* @member {observable|TeX} correctAnswerLaTeX
* @memberof Numbas.display.JMEPartDisplay
*/
this.correctAnswerLaTeX = Numbas.jme.display.exprToLaTeX(this.correctAnswer,p.settings.answerSimplification,p.question.scope);
this.correctAnswerLaTeX = jme.display.exprToLaTeX(this.correctAnswer,p.settings.answerSimplification,p.question.scope);
ko.computed(function() {
p.storeAnswer(this.studentAnswer());
},this);
@@ -43,7 +44,7 @@ Numbas.queueScript('display/parts/jme',['display-base','part-display','util','jm
return '';
this.removeWarnings();
try {
var tex = Numbas.jme.display.exprToLaTeX(studentAnswer,'',p.question.scope);
var tex = jme.display.exprToLaTeX(studentAnswer,'',p.question.scope);
if(tex===undefined)
throw(new Numbas.Error('display.part.jme.error making maths'));
}
@@ -52,19 +53,20 @@ Numbas.queueScript('display/parts/jme',['display-base','part-display','util','jm
return '';
}
if(p.settings.checkVariableNames) {
var tree = Numbas.jme.compile(studentAnswer,p.question.scope);
var usedvars = Numbas.jme.findvars(tree);
var tree = jme.compile(studentAnswer,p.question.scope);
var usedvars = jme.findvars(tree);
var failExpectedVariableNames = false;
var expectedVariableNames = jme.findvars(jme.compile(this.correctAnswer));
var unexpectedVariableName;
for(var i=0;i<usedvars.length;i++) {
if(!p.settings.expectedVariableNames.contains(usedvars[i])) {
if(!expectedVariableNames.contains(usedvars[i])) {
failExpectedVariableNames = true;
unexpectedVariableName = usedvars[i];
break;
}
}
if( failExpectedVariableNames ) {
var suggestedNames = unexpectedVariableName.split(Numbas.jme.re.re_short_name);
var suggestedNames = unexpectedVariableName.split(jme.re.re_short_name);
if(suggestedNames.length>3) {
var suggestion = [];
for(var i=1;i<suggestedNames.length;i+=2) {
@@ -101,4 +103,4 @@ Numbas.queueScript('display/parts/jme',['display-base','part-display','util','jm
}
};
display.JMEPartDisplay = extend(display.PartDisplay,display.JMEPartDisplay,true);
})
})

0 comments on commit 3c17998

Please sign in to comment.
You can’t perform that action at this time.