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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines to MAXIMA CAS
                      3: #
1.6     ! www         4: # $Id: lonmaxima.pm,v 1.5 2006/06/13 14:57:54 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.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'; }
        !            71:     return &maximareply($socket,$fullscript);
        !            72: }
        !            73: 
        !            74: sub maxima_run {
        !            75:     my ($script,$submission,$arguments) = @_;
        !            76:     my $socket=&connect();
        !            77:     my $fullscript=$script;
        !            78:     my $reply=&runscript($socket,$fullscript);
        !            79: #    &Apache::lonnet::logthis('Script:'.$fullscript);
        !            80: #    &Apache::lonnet::logthis('Sub:'.$submission);
        !            81: #    &Apache::lonnet::logthis('Arg:'.$arguments);
        !            82: 
        !            83:     &disconnect($socket);
        !            84:     if ($reply=~/^\s*true\s*$/) { return 'EXACT_ANS'; }
        !            85:     if ($reply=~/^\s*false\s*/) { return 'INCORRECT'; } 
        !            86:     return 'BAD_FORMULA';
        !            87: }
        !            88: 
1.1       www        89: sub compareterms {
                     90:     my ($socket,$terma,$termb)=@_;
                     91:     my $difference=$terma.'-'.$termb;
                     92:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
                     93:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
                     94:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
                     95:     if ($reply=~/^Error\:/) { return $reply; }
                     96:     return 'false';
                     97: }
1.3       bisitz     98: 
                     99: sub maxima_check {
1.5       albertel  100:     my ($response,$answer,$reterror) = @_;
1.4       bisitz    101:     my $socket=&connect();
                    102:     my $reply=&compareterms($socket,$response,$answer);
                    103:     &disconnect($socket);
                    104:     if ($reply eq 'true') { return 1; }
1.3       bisitz    105:     return 7;
                    106: }
1.1       www       107:  
                    108: 1;
                    109: __END__;

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