/usr/share/perl5/AMC/ScoringEnv.pm is in auto-multiple-choice-common 1.2.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | # -*- perl -*-
#
# Copyright (C) 2012 Alexis Bienvenue <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice. If not, see
# <http://www.gnu.org/licenses/>.
package AMC::ScoringEnv;
# AMC::ScoringEnv object handles directives used in scoring
# strategies. Directives can be parsed from scoring strategy strings,
# and their value can be computed with different sets of variables'
# values (AMC-note uses one set of values for variables for the
# student answer sheet, and one set of values for a perfect answer).
use AMC::Basic;
use Text::ParseWords;
# The functions min and max will sometimes be used to evaluate the
# formulas: don't cut them off
sub min {
my (@values)=@_;
my $r=$values[0];
for(@values) { $r=$_ if($_<$r); }
return($r);
}
sub max {
my (@values)=@_;
my $r=$values[0];
for(@values) { $r=$_ if($_>$r); }
return($r);
}
sub new {
my ($class,@objects)=(@_);
my $self={error_hook=>'',
variables=>{},
directives=>{},
type=>0,
};
my $scalar_only=0;
for my $obj (@objects) {
if($obj->{scalar_only}) {
$scalar_only=1;
delete($obj->{scalar_only});
}
for my $k (keys %$obj) {
if(defined($self->{$k})) {
if(ref($self->{$k}) eq 'HASH') {
$self->{$k}={%{$obj->{$k}}}
if(!$scalar_only);
} else {
$self->{$k}=$obj->{$k};
}
}
}
}
$self->{errors}=[];
bless($self,$class);
return($self);
}
sub new_from_directives_string {
my ($class,$string)=@_;
my $self=$class->new();
$self->process_directives($string);
return($self);
}
sub error {
my ($self,$text)=@_;
debug $text;
push @{$self->{errors}},$text;
}
sub errors {
my ($self)=@_;
return(@{$self->{errors}});
}
sub n_errors {
my ($self)=@_;
return(1+$#{$self->{errors}});
}
sub clear_errors {
my ($self)=@_;
$self->{errors}=[];
}
sub clone {
my ($self)=@_;
return(AMC::ScoringEnv->new($self));
}
sub clone_directives {
my ($self)=@_;
return(AMC::ScoringEnv
->new({directives=>$self->{directives}},
{scalar_only=>1},
$self)
);
}
# set the type (a small integer) to be used for computations. Changing
# the type will move to different values for all variables handled by
# the object. All directives are kept unchanged, but their values will
# be computed again next time get_directive will be called, with the
# new values that will be set for all variables.
sub set_type {
my ($self,$type)=@_;
$self->unevaluate_directives() if($self->{type} != $type);
$self->{type}=$type;
}
# set variable value.
sub set_variable {
my ($self,$vv,$value,$rw,$unlock)=@_;
$self->{variables}->{$vv}=[] if(!$self->{variables}->{$vv});
if((!$unlock)
&& $self->{variables}->{$vv}->[$self->{type}]
&& !$self->{variables}->{$vv}->[$self->{type}]->{rw}) {
$self->error("Trying to set read-only variable $vv");
} else {
$self->{variables}->{$vv}->[$self->{type}]={value=>$value,rw=>$rw};
}
}
sub set_variables_from_hashref {
my ($self,$hashref,$rw)=@_;
for my $k (keys %$hashref) {
$self->set_variable($k,$hashref->{$k},$rw);
}
}
# is this variable defined?
sub defined_variable {
my ($self,$vv)=@_;
return($self->{variables}->{$vv}->[$self->{type}] ? 1 : 0);
}
# get variable value.
sub get_variable {
my ($self,$vv)=@_;
if($self->{variables}->{$vv}
&& $self->{variables}->{$vv}->[$self->{type}]) {
return($self->{variables}->{$vv}->[$self->{type}]->{value});
} else {
return(undef);
}
}
# parse directives strings
sub parse_defs {
my ($self,$string)=@_;
my @r=();
my $plain=0;
for my $def (quotewords(',+',0,$string)) {
if($def) {
if($def =~ /^\s*([.a-zA-Z0-9_-]+)\s*=\s*(.*)/) {
push @r,{key=>$1,value=>$2};
} else {
if($plain>0) {
$self->error("Not a definition string: $def");
} else {
$plain++;
push @r,{key=>"_PLAIN_",value=>$def};
}
}
}
}
return(\@r);
}
sub action_variable {
my ($self,$action,$key,$value)=@_;
if($action eq 'default') {
if(!$self->defined_variable($key)) {
debug "Default value for variable $key = $value";
$self->set_variable($key,
$self->evaluate($value),
1);
} else {
debug "Variable $key already set";
}
} elsif($action eq 'set') {
debug "Setting variable $key = $value";
$self->set_variable($key,
$self->evaluate($value),
0);
} elsif($action eq 'setx') {
debug "Overwriting variable $key = $value";
$self->set_variable($key,
$self->evaluate($value),
0,1);
} elsif($action eq 'requires') {
$self->error("Variable $key required")
if(!$self->defined_variable($key));
}
}
sub action_variables_from_directives {
my ($self,$action,$keys)=@_;
$keys=[$self->sorted_directives_keys] if(!$keys);
for my $key (@$keys) {
if($key =~ /^$action\.(.*)/) {
$self->action_variable($action,$1,
$self->{directives}->{$key}->{def});
}
}
}
sub variables_from_directives {
my ($self,%oo)=@_;
debug "Variables from internal directives";
my @keys=$self->sorted_directives_keys;
for my $a (qw/default set setx requires/) {
$self->action_variables_from_directives($a,\@keys)
if($oo{$a});
}
}
sub action_variables_from_parse {
my ($self,$parsed,$action)=@_;
my @other=();
for my $d (@$parsed) {
if($d->{key} =~ /^$action\.(.*)/) {
$self->action_variable($action,$1,$d->{value});
} else {
push @other,$d;
}
}
@$parsed=@other;
}
sub variables_from_parsed_directives {
my ($self,$parsed,%oo)=@_;
for my $a (qw/default set setx requires/) {
$self->action_variables_from_parse($parsed,$a)
if($oo{$a});
}
}
sub variables_from_directives_string {
my ($self,$string,%oo)=@_;
debug "Variables from directives $string";
$self->variables_from_parsed_directives
($self->parse_defs($string),%oo);
}
sub process_variables {
my ($self,$string)=@_;
$self->variables_from_parse($self->parse_defs($string));
}
sub unevaluate_directives {
my ($self)=@_;
for my $key (%{$self->{directives}}) {
$self->{directives}->{$key}->{evaluated}=0;
}
}
sub max_rank {
my ($self)=@_;
my $r=0;
for(keys %{$self->{directives}}) {
$r=$self->{directives}->{$_}->{rank}
if($self->{directives}->{$_}->{rank}>$r);
}
return($r);
}
sub sorted_directives_keys {
my ($self)=@_;
return(sort { $self->{directives}->{$a}->{rank}
<=> $self->{directives}->{$b}->{rank} }
(keys %{$self->{directives}}));
}
sub set_directive {
my ($self,$key,$value,$rank)=@_;
$rank=$self->max_rank()+1 if(!defined($rank));
debug "Setting directive {$rank} $key = $value";
$self->{directives}->{$key}={def=>$value,rank=>$rank};
}
sub directives_from_parse {
my ($self,$parsed)=@_;
my $rank=$self->max_rank()+1;
for my $d (@$parsed) {
$self->set_directive($d->{key},$d->{value},$rank++);
}
}
sub process_directives {
my ($self,$string)=@_;
$self->directives_from_parse($self->parse_defs($string));
}
sub evaluate {
my ($self,$string)=@_;
return(undef) if(!defined($string));
return('') if($string eq '');
my $string_orig=$string;
for my $vv (keys %{$self->{variables}}) {
$string =~ s/\b$vv\b/$self->{variables}->{$vv}->[$self->{type}]->{value}/g;
}
my $calc=eval($string);
$self->error("Syntax error (evaluation) : $string")
if(!defined($calc));
debug "Evaluation : $string_orig => $string => $calc"
if($string_orig ne $calc);
return($calc);
}
sub defined_directive {
my ($self,$key)=@_;
return($self->{directives}->{$key});
}
sub get_directive {
my ($self,$key)=@_;
if($self->{directives}->{$key}) {
if(!$self->{directives}->{$key}->{evaluated}) {
$self->{directives}->{$key}->{value}
=$self->evaluate($self->{directives}->{$key}->{def});
$self->{directives}->{$key}->{evaluated}=1;
}
return($self->{directives}->{$key}->{value});
} else {
return(undef);
}
}
1;
|