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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines to MAXIMA CAS
                      3: #
1.7     ! www         4: # $Id: lonmaxima.pm,v 1.6 2006/12/11 17:37:43 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.4       bisitz     34: use LONCAPA;
1.1       www        35: 
                     36: sub connect {
                     37:    return IO::Socket::UNIX->new(Peer    => $Apache::lonnet::perlvar{'lonSockDir'}.'/maximasock',
                     38: 				Type    => SOCK_STREAM,
                     39: 				Timeout => 10);
                     40: }
                     41: 
                     42: sub disconnect {
                     43:     my ($socket)=@_;
                     44:     if ($socket) { close($socket); }
                     45: }
                     46: 
                     47: sub maximareply {
                     48:     my ($socket,$cmd)=@_;
                     49:     if ($socket) {
1.4       bisitz     50: 	print $socket &escape($cmd)."\n";
1.1       www        51:         my $reply=<$socket>;
                     52:         chomp($reply);
                     53:         if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
1.4       bisitz     54:         return &unescape($reply);
1.1       www        55:     } else {
                     56:         return 'Error: no connection.';
                     57:     }
                     58: }
                     59: 
                     60: sub blacklisted {
                     61:     my ($cmd)=@_;
                     62:     foreach my $forbidden ('save','load','plot','lisp','includ','compil','file','batch','stringout','translat','stout','stin','block','system') {
1.2       www        63: 	if ($cmd=~/$forbidden/s) { return 1; }
1.1       www        64:     } 
                     65:     return 0;
                     66: }
                     67: 
1.6       www        68: sub runscript {
                     69:     my ($socket,$fullscript)=@_;
                     70:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
1.7     ! www        71:     my $reply;
        !            72:     $fullscript=~s/[\n\r\l]//gs;
        !            73:     foreach my $line (split(/\;/s,$fullscript)) {
        !            74: 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
        !            75: 	if ($reply=~/^Error\:/) { return $reply; }
        !            76:     }
        !            77:     $reply=~s/\W//gs;
        !            78:     return $reply;
1.6       www        79: }
                     80: 
                     81: sub maxima_run {
1.7     ! www        82:     my ($script,$submission,$argument) = @_;
1.6       www        83:     my $socket=&connect();
1.7     ! www        84:     my $fullscript='';
        !            85:     my $submission_index=1;
        !            86:     foreach my $submission_component (split(/\s*\,\s*/,$submission)) {
        !            87: 	$fullscript.="RESPONSE[$submission_index]:$submission_component;\n";
        !            88: 	$submission_index++;
        !            89:     }
        !            90:     my $argument_index=1;
        !            91:     foreach my $argument_component (split(/\s*\,\s*/,$argument)) {
        !            92: 	$fullscript.="LONCAPALIST[$argument_index]:$argument_component;\n";
        !            93: 	$argument_index++;
        !            94:     }
        !            95:     $fullscript.=$script;
1.6       www        96:     my $reply=&runscript($socket,$fullscript);
                     97:     &disconnect($socket);
                     98:     if ($reply=~/^\s*true\s*$/) { return 'EXACT_ANS'; }
                     99:     if ($reply=~/^\s*false\s*/) { return 'INCORRECT'; } 
                    100:     return 'BAD_FORMULA';
                    101: }
                    102: 
1.1       www       103: sub compareterms {
                    104:     my ($socket,$terma,$termb)=@_;
                    105:     my $difference=$terma.'-'.$termb;
                    106:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
                    107:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
                    108:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
                    109:     if ($reply=~/^Error\:/) { return $reply; }
                    110:     return 'false';
                    111: }
1.3       bisitz    112: 
                    113: sub maxima_check {
1.5       albertel  114:     my ($response,$answer,$reterror) = @_;
1.4       bisitz    115:     my $socket=&connect();
                    116:     my $reply=&compareterms($socket,$response,$answer);
                    117:     &disconnect($socket);
                    118:     if ($reply eq 'true') { return 1; }
1.3       bisitz    119:     return 7;
                    120: }
1.1       www       121:  
                    122: 1;
                    123: __END__;

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