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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines to MAXIMA CAS
                      3: #
1.20    ! riegler     4: # $Id: lonmaxima.pm,v 1.19 2007/08/13 09:34:35 albertel 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)=@_;
1.19      albertel   63:     foreach my $forbidden ('save','load','plot','lisp','includ','compil',
1.15      albertel   64: 			   'file','batch','stringout','translat','stout',
1.20    ! riegler    65: 			   'stin','block','system','concat','read','inchar',
        !            66: 			   'outchar','ttyoff','with_stdout','writefile') {
1.2       www        67: 	if ($cmd=~/$forbidden/s) { return 1; }
1.1       www        68:     } 
                     69:     return 0;
                     70: }
                     71: 
1.6       www        72: sub runscript {
                     73:     my ($socket,$fullscript)=@_;
                     74:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
1.7       www        75:     my $reply;
                     76:     $fullscript=~s/[\n\r\l]//gs;
                     77:     foreach my $line (split(/\;/s,$fullscript)) {
                     78: 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
                     79: 	if ($reply=~/^Error\:/) { return $reply; }
                     80:     }
1.14      www        81:     $reply=~s/^\s*//gs;
                     82:     $reply=~s/\s*$//gs;
1.15      albertel   83:     &Apache::lonxml::debug("maxima $fullscript \n reply $reply");
1.7       www        84:     return $reply;
1.6       www        85: }
                     86: 
1.9       www        87: sub maxima_cas_formula_fix {
                     88:    my ($expression)=@_;
                     89:    return &Apache::response::implicit_multiplication($expression);
                     90: }
                     91: 
1.6       www        92: sub maxima_run {
1.7       www        93:     my ($script,$submission,$argument) = @_;
1.6       www        94:     my $socket=&connect();
1.12      www        95:     my @submissionarray=split(/\s*\,\s*/,$submission);
                     96:     for (my $i=0;$i<=$#submissionarray;$i++) {
                     97:         my $n=$i+1;
                     98:         my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
                     99:         $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
1.7       www       100:     }
1.12      www       101:     my @argumentarray=@{$argument};
                    102:     for (my $i=0;$i<=$#argumentarray;$i++) {
                    103:         my $n=$i+1;
                    104:         my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
                    105:         $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
1.7       www       106:     }
1.12      www       107:     my $reply=&runscript($socket,$script);
1.6       www       108:     &disconnect($socket);
1.13      raeburn   109:     if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
                    110:     if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
1.6       www       111:     return 'BAD_FORMULA';
                    112: }
                    113: 
1.11      www       114: sub maxima_eval {
                    115:     my ($script) = @_;
                    116:     my $socket=&connect();
                    117:     my $reply=&runscript($socket,$script);
                    118:     &disconnect($socket);
                    119:     return $reply;
                    120: }
                    121: 
                    122: 
1.1       www       123: sub compareterms {
                    124:     my ($socket,$terma,$termb)=@_;
1.16      albertel  125:     my $difference=$terma.'-('.$termb.')';
1.1       www       126:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
                    127:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
                    128:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
                    129:     if ($reply=~/^Error\:/) { return $reply; }
                    130:     return 'false';
                    131: }
1.3       bisitz    132: 
                    133: sub maxima_check {
1.5       albertel  134:     my ($response,$answer,$reterror) = @_;
1.4       bisitz    135:     my $socket=&connect();
                    136:     my $reply=&compareterms($socket,$response,$answer);
                    137:     &disconnect($socket);
1.17      albertel  138:     # integer to string mappings come from capaParser.h
                    139:     # 1 maps to 'EXACT_ANS'
1.4       bisitz    140:     if ($reply eq 'true') { return 1; }
1.17      albertel  141:     # 7 maps to 'INCORRECT'
1.3       bisitz    142:     return 7;
                    143: }
1.1       www       144:  
                    145: 1;
                    146: __END__;

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