Annotation of loncom/homework/lonmaxima.pm, revision 1.11

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines to MAXIMA CAS
                      3: #
1.11    ! www         4: # $Id: lonmaxima.pm,v 1.10 2006/12/18 21:12:51 www Exp $
1.1       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28:  
                     29: package Apache::lonmaxima;
                     30:  
                     31: use strict;
                     32: use IO::Socket;
                     33: use Apache::lonnet;
1.9       www        34: use Apache::response();
1.4       bisitz     35: use LONCAPA;
1.1       www        36: 
                     37: sub connect {
                     38:    return IO::Socket::UNIX->new(Peer    => $Apache::lonnet::perlvar{'lonSockDir'}.'/maximasock',
                     39: 				Type    => SOCK_STREAM,
                     40: 				Timeout => 10);
                     41: }
                     42: 
                     43: sub disconnect {
                     44:     my ($socket)=@_;
                     45:     if ($socket) { close($socket); }
                     46: }
                     47: 
                     48: sub maximareply {
                     49:     my ($socket,$cmd)=@_;
                     50:     if ($socket) {
1.4       bisitz     51: 	print $socket &escape($cmd)."\n";
1.1       www        52:         my $reply=<$socket>;
                     53:         chomp($reply);
                     54:         if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
1.4       bisitz     55:         return &unescape($reply);
1.1       www        56:     } else {
                     57:         return 'Error: no connection.';
                     58:     }
                     59: }
                     60: 
                     61: sub blacklisted {
                     62:     my ($cmd)=@_;
                     63:     foreach my $forbidden ('save','load','plot','lisp','includ','compil','file','batch','stringout','translat','stout','stin','block','system') {
1.2       www        64: 	if ($cmd=~/$forbidden/s) { return 1; }
1.1       www        65:     } 
                     66:     return 0;
                     67: }
                     68: 
1.6       www        69: sub runscript {
                     70:     my ($socket,$fullscript)=@_;
                     71:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
1.7       www        72:     my $reply;
                     73:     $fullscript=~s/[\n\r\l]//gs;
                     74:     foreach my $line (split(/\;/s,$fullscript)) {
                     75: 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
                     76: 	if ($reply=~/^Error\:/) { return $reply; }
                     77:     }
                     78:     $reply=~s/\W//gs;
                     79:     return $reply;
1.6       www        80: }
                     81: 
1.9       www        82: sub maxima_cas_formula_fix {
                     83:    my ($expression)=@_;
                     84:    return &Apache::response::implicit_multiplication($expression);
                     85: }
                     86: 
1.6       www        87: sub maxima_run {
1.7       www        88:     my ($script,$submission,$argument) = @_;
1.6       www        89:     my $socket=&connect();
1.7       www        90:     my $fullscript='';
                     91:     my $submission_index=1;
                     92:     foreach my $submission_component (split(/\s*\,\s*/,$submission)) {
1.9       www        93: 	$fullscript.="RESPONSE[$submission_index]:".&maxima_cas_formula_fix($submission_component).";\n";
1.7       www        94: 	$submission_index++;
                     95:     }
                     96:     my $argument_index=1;
1.8       www        97:     foreach my $argument_component (@{$argument}) {
1.10      www        98: 	$fullscript.="LONCAPALIST[$argument_index]:".&maxima_cas_formula_fix($argument_component).";\n";
1.7       www        99: 	$argument_index++;
                    100:     }
                    101:     $fullscript.=$script;
1.6       www       102:     my $reply=&runscript($socket,$fullscript);
                    103:     &disconnect($socket);
                    104:     if ($reply=~/^\s*true\s*$/) { return 'EXACT_ANS'; }
                    105:     if ($reply=~/^\s*false\s*/) { return 'INCORRECT'; } 
                    106:     return 'BAD_FORMULA';
                    107: }
                    108: 
1.11    ! www       109: sub maxima_eval {
        !           110:     my ($script) = @_;
        !           111:     my $socket=&connect();
        !           112:     my $reply=&runscript($socket,$script);
        !           113:     &disconnect($socket);
        !           114:     return $reply;
        !           115: }
        !           116: 
        !           117: 
1.1       www       118: sub compareterms {
                    119:     my ($socket,$terma,$termb)=@_;
                    120:     my $difference=$terma.'-'.$termb;
                    121:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
                    122:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
                    123:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
                    124:     if ($reply=~/^Error\:/) { return $reply; }
                    125:     return 'false';
                    126: }
1.3       bisitz    127: 
                    128: sub maxima_check {
1.5       albertel  129:     my ($response,$answer,$reterror) = @_;
1.4       bisitz    130:     my $socket=&connect();
                    131:     my $reply=&compareterms($socket,$response,$answer);
                    132:     &disconnect($socket);
                    133:     if ($reply eq 'true') { return 1; }
1.3       bisitz    134:     return 7;
                    135: }
1.1       www       136:  
                    137: 1;
                    138: __END__;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>