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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines to MAXIMA CAS
                      3: #
1.4     ! bisitz      4: # $Id: lonmaxima.pm,v 1.3 2006/06/12 13:51:13 bisitz 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: 
                     68: sub compareterms {
                     69:     my ($socket,$terma,$termb)=@_;
                     70:     my $difference=$terma.'-'.$termb;
                     71:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
                     72:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
                     73:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
                     74:     if ($reply=~/^Error\:/) { return $reply; }
                     75:     return 'false';
                     76: }
1.3       bisitz     77: 
                     78: sub maxima_check {
                     79:     my ($response,$answer,$type,
                     80:         $tol_type,$tol,
                     81:         $sig_lbound,$sig_ubound,
                     82:         $ans_fmt,$unit,$calc,$id_list,
                     83:         $points,
                     84:         $randomseed,
                     85:         $reterror) = @_;
1.4     ! bisitz     86:     my $socket=&connect();
        !            87:     my $reply=&compareterms($socket,$response,$answer);
        !            88:     &disconnect($socket);
        !            89:     if ($reply eq 'true') { return 1; }
1.3       bisitz     90:     return 7;
                     91: }
1.1       www        92:  
                     93: 1;
                     94: __END__;

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