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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines to MAXIMA CAS
                      3: #
1.14    ! www         4: # $Id: lonmaxima.pm,v 1.13 2007/04/19 17:40:43 raeburn 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:     }
1.14    ! www        78:     $reply=~s/^\s*//gs;
        !            79:     $reply=~s/\s*$//gs;
1.7       www        80:     return $reply;
1.6       www        81: }
                     82: 
1.9       www        83: sub maxima_cas_formula_fix {
                     84:    my ($expression)=@_;
                     85:    return &Apache::response::implicit_multiplication($expression);
                     86: }
                     87: 
1.6       www        88: sub maxima_run {
1.7       www        89:     my ($script,$submission,$argument) = @_;
1.6       www        90:     my $socket=&connect();
1.12      www        91:     my @submissionarray=split(/\s*\,\s*/,$submission);
                     92:     for (my $i=0;$i<=$#submissionarray;$i++) {
                     93:         my $n=$i+1;
                     94:         my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
                     95:         $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
1.7       www        96:     }
1.12      www        97:     my @argumentarray=@{$argument};
                     98:     for (my $i=0;$i<=$#argumentarray;$i++) {
                     99:         my $n=$i+1;
                    100:         my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
                    101:         $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
1.7       www       102:     }
1.12      www       103:     my $reply=&runscript($socket,$script);
1.6       www       104:     &disconnect($socket);
1.13      raeburn   105:     if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
                    106:     if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
1.6       www       107:     return 'BAD_FORMULA';
                    108: }
                    109: 
1.11      www       110: sub maxima_eval {
                    111:     my ($script) = @_;
                    112:     my $socket=&connect();
                    113:     my $reply=&runscript($socket,$script);
                    114:     &disconnect($socket);
                    115:     return $reply;
                    116: }
                    117: 
                    118: 
1.1       www       119: sub compareterms {
                    120:     my ($socket,$terma,$termb)=@_;
                    121:     my $difference=$terma.'-'.$termb;
                    122:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
                    123:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
                    124:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
                    125:     if ($reply=~/^Error\:/) { return $reply; }
                    126:     return 'false';
                    127: }
1.3       bisitz    128: 
                    129: sub maxima_check {
1.5       albertel  130:     my ($response,$answer,$reterror) = @_;
1.4       bisitz    131:     my $socket=&connect();
                    132:     my $reply=&compareterms($socket,$response,$answer);
                    133:     &disconnect($socket);
                    134:     if ($reply eq 'true') { return 1; }
1.3       bisitz    135:     return 7;
                    136: }
1.1       www       137:  
                    138: 1;
                    139: __END__;

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