numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
202 stars 117 forks source link

Add a convenience function to compile rulesets in an extension #424

Closed graemechester closed 5 years ago

graemechester commented 8 years ago

After writing a set of Boolean algebra simplification rules, I tried to include these by following the documentation on adding extensions. An error message occurred when the additional rules were used in a question, stating that the rules were not defined.

The cause appears to be the following line which executes after extensions have been processed and resets the scope rules to the built-in set:- exam.js/line 94: this.scope.rulesets = Numbas.util.copyobj(Numbas.jme.display.simplificationRules);

Bypassing this line in the debugger allows the rules to work, since this.scope.rulesets already contains the built-in rules prior to this line, plus any defined in extensions.

christianp commented 8 years ago

Can you send me your extension, please?

graemechester commented 8 years ago

For sure:- booleanlogic.zip

The question I am testing with is "Boolean jme" https://numbas.mathcentre.ac.uk/question/share/view/36ffa251-8e7e-4ae6-b359-6d3eb0ddfdfe

christianp commented 8 years ago

Yep, looks like that line was very old and not needed - the built-in rulesets are included in Numbas.jme.builtinScope. Not sure how I managed to write that bit of the documentation without encountering this problem.

graemechester commented 8 years ago

Thanks Christian, that works well now. It might be good to have a function to add extension rulesets similar to the one for functions, though I’m probably the only person who would use it at the moment.

Kind regards,

Graeme Chester

Dr. E.G. Chester, School of EEE, Newcastle University Tel: 0191 208 7325 Email: graeme.chester@ncl.ac.ukmailto:graeme.chester@ncl.ac.uk

From: Christian Lawson-Perfect [mailto:notifications@github.com] Sent: 18 January 2016 14:29 To: numbas/Numbas Numbas@noreply.github.com Cc: Graeme Chester graeme.chester@newcastle.ac.uk Subject: Re: [Numbas] Simplification rules defined by extensions are overwritten by default set (#424)

Yep, looks like that line was very old and not needed - the built-in rulesets are included in Numbas.jme.builtinScope. Not sure how I managed to write that bit of the documentation without encountering this problem.

— Reply to this email directly or view it on GitHubhttps://github.com/numbas/Numbas/issues/424#issuecomment-172540483.

christianp commented 8 years ago

Do you mean in the editor? If you're talking about the extension .js file, what would you like it to look like? You can assume that extension.scope.rulesets is empty at creation, and you're free to override it, so you can miss out a few of the lines of boilerplate you've got in your extension.

Your code could go:

var rulesets = {
   'basic-logic': [ ... ],
   'double-negation': [ ... ],
   ...
}
var all = [];
for(var x in rulesets) {
   extension.scope.rulesets[x] = Numbas.jme.display.compileRules(rulesets[x]);
   all.push(x);
}
extension.scope.rulesets.allboolean = Numbas.jme.display.collectRuleset(all,rulesets);

Would you like something that does compileRules on a whole dictionary, missing out that for loop?

Should the all ruleset be added to each time you extend a Scope object, so it's always guaranteed to contain every rule in the scope?

graemechester commented 8 years ago

I wasn’t thinking of the editor, just the extension .js file. I’ll probably tidy up my example extension when time permits, but I was thinking it would be easier to be able to write something along the lines of

var rulesets = { … }

extension.scope.addRuleset(rulesets,{includeAll:true});

which would do compileRules for everything and add it to the scope. I’m not sure if the rules should always be added to the all ruleset, probably they should but it could be conditional on an argument to the function. If there were a need to use all rules except the extensions at some point, it would be awkward if they are automatically added; it would be very easy however to include the list all,extension in the rulesets when inclusion is needed.

Kind regards,

Graeme Chester

Dr. E.G. Chester, School of EEE, Newcastle University Tel: 0191 208 7325 Email: graeme.chester@ncl.ac.ukmailto:graeme.chester@ncl.ac.uk

From: Christian Lawson-Perfect [mailto:notifications@github.com] Sent: 19 January 2016 10:34 To: numbas/Numbas Numbas@noreply.github.com Cc: Graeme Chester graeme.chester@newcastle.ac.uk Subject: Re: [Numbas] Simplification rules defined by extensions are overwritten by default set (#424)

Do you mean in the editor? If you're talking about the extension .js file, what would you like it to look like? You can assume that extension.scope.rulesets is empty at creation, and you're free to override it, so you can miss out a few of the lines of boilerplate you've got in your extension.

Your code could go:

var rulesets = {

'basic-logic': [ ... ],

'double-negation': [ ... ],

...

}

var all = [];

for(var x in rulesets) {

extension.scope.rulesets[x] = Numbas.jme.display.compileRules(rulesets[x]);

all.push(x);

}

extension.scope.rulesets.allboolean = Numbas.jme.display.collectRuleset(all,rulesets);

Would you like something that does compileRules on a whole dictionary, missing out that for loop?

Should the all ruleset be added to each time you extend a Scope object, so it's always guaranteed to contain every rule in the scope?

— Reply to this email directly or view it on GitHubhttps://github.com/numbas/Numbas/issues/424#issuecomment-172811973.