--- loncom/homework/lonmaxima.pm 2006/06/13 14:57:54 1.5 +++ loncom/homework/lonmaxima.pm 2007/05/26 15:51:08 1.14 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Interface routines to MAXIMA CAS # -# $Id: lonmaxima.pm,v 1.5 2006/06/13 14:57:54 albertel Exp $ +# $Id: lonmaxima.pm,v 1.14 2007/05/26 15:51:08 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -31,6 +31,7 @@ package Apache::lonmaxima; use strict; use IO::Socket; use Apache::lonnet; +use Apache::response(); use LONCAPA; sub connect { @@ -65,6 +66,56 @@ sub blacklisted { return 0; } +sub runscript { + my ($socket,$fullscript)=@_; + if (&blacklisted($fullscript)) { return 'Error: blacklisted'; } + my $reply; + $fullscript=~s/[\n\r\l]//gs; + foreach my $line (split(/\;/s,$fullscript)) { + if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); } + if ($reply=~/^Error\:/) { return $reply; } + } + $reply=~s/^\s*//gs; + $reply=~s/\s*$//gs; + return $reply; +} + +sub maxima_cas_formula_fix { + my ($expression)=@_; + return &Apache::response::implicit_multiplication($expression); +} + +sub maxima_run { + my ($script,$submission,$argument) = @_; + my $socket=&connect(); + my @submissionarray=split(/\s*\,\s*/,$submission); + for (my $i=0;$i<=$#submissionarray;$i++) { + my $n=$i+1; + my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]); + $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs; + } + my @argumentarray=@{$argument}; + for (my $i=0;$i<=$#argumentarray;$i++) { + my $n=$i+1; + my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]); + $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs; + } + my $reply=&runscript($socket,$script); + &disconnect($socket); + if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; } + if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } + return 'BAD_FORMULA'; +} + +sub maxima_eval { + my ($script) = @_; + my $socket=&connect(); + my $reply=&runscript($socket,$script); + &disconnect($socket); + return $reply; +} + + sub compareterms { my ($socket,$terma,$termb)=@_; my $difference=$terma.'-'.$termb;