# The LearningOnline Network with CAPA
# Interface routines to MAXIMA CAS
#
# $Id: lonmaxima.pm,v 1.11 2006/12/19 14:24:05 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA 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.
#
# LON-CAPA 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 LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
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',
Type => SOCK_STREAM,
Timeout => 10);
}
sub disconnect {
my ($socket)=@_;
if ($socket) { close($socket); }
}
sub maximareply {
my ($socket,$cmd)=@_;
if ($socket) {
print $socket &escape($cmd)."\n";
my $reply=<$socket>;
chomp($reply);
if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
return &unescape($reply);
} else {
return 'Error: no connection.';
}
}
sub blacklisted {
my ($cmd)=@_;
foreach my $forbidden ('save','load','plot','lisp','includ','compil','file','batch','stringout','translat','stout','stin','block','system') {
if ($cmd=~/$forbidden/s) { return 1; }
}
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 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;
if (&blacklisted($difference)) { return 'Error: blacklisted'; }
my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
if ($reply=~/^\s*0\s*$/) { return 'true'; }
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__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>