--- loncom/homework/lonmaxima.pm 2006/03/15 20:44:54 1.2 +++ loncom/homework/lonmaxima.pm 2006/12/18 21:12:51 1.10 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Interface routines to MAXIMA CAS # -# $Id: lonmaxima.pm,v 1.2 2006/03/15 20:44:54 www Exp $ +# $Id: lonmaxima.pm,v 1.10 2006/12/18 21:12:51 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -31,6 +31,8 @@ package Apache::lonmaxima; use strict; use IO::Socket; use Apache::lonnet; +use Apache::response(); +use LONCAPA; sub connect { return IO::Socket::UNIX->new(Peer => $Apache::lonnet::perlvar{'lonSockDir'}.'/maximasock', @@ -46,11 +48,11 @@ sub disconnect { sub maximareply { my ($socket,$cmd)=@_; if ($socket) { - print $socket &Apache::lonnet::escape($cmd)."\n"; + print $socket &escape($cmd)."\n"; my $reply=<$socket>; chomp($reply); if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; } - return &Apache::lonnet::unescape($reply); + return &unescape($reply); } else { return 'Error: no connection.'; } @@ -64,6 +66,46 @@ 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/\W//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 $fullscript=''; + my $submission_index=1; + foreach my $submission_component (split(/\s*\,\s*/,$submission)) { + $fullscript.="RESPONSE[$submission_index]:".&maxima_cas_formula_fix($submission_component).";\n"; + $submission_index++; + } + my $argument_index=1; + foreach my $argument_component (@{$argument}) { + $fullscript.="LONCAPALIST[$argument_index]:".&maxima_cas_formula_fix($argument_component).";\n"; + $argument_index++; + } + $fullscript.=$script; + my $reply=&runscript($socket,$fullscript); + &disconnect($socket); + if ($reply=~/^\s*true\s*$/) { return 'EXACT_ANS'; } + if ($reply=~/^\s*false\s*/) { return 'INCORRECT'; } + return 'BAD_FORMULA'; +} + sub compareterms { my ($socket,$terma,$termb)=@_; my $difference=$terma.'-'.$termb; @@ -73,6 +115,15 @@ sub compareterms { if ($reply=~/^Error\:/) { return $reply; } return 'false'; } + +sub maxima_check { + my ($response,$answer,$reterror) = @_; + my $socket=&connect(); + my $reply=&compareterms($socket,$response,$answer); + &disconnect($socket); + if ($reply eq 'true') { return 1; } + return 7; +} 1; __END__;