File:  [LON-CAPA] / loncom / homework / lonmaxima.pm
Revision 1.5: download - view: text, annotated - select for diffs
Tue Jun 13 14:57:54 2006 UTC (17 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_X, version_2_2_99_1, version_2_2_99_0, version_2_2_2, version_2_2_1, version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
- moving code from modules into main

    1: # The LearningOnline Network with CAPA
    2: # Interface routines to MAXIMA CAS
    3: #
    4: # $Id: lonmaxima.pm,v 1.5 2006/06/13 14:57:54 albertel Exp $
    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;
   34: use LONCAPA;
   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) {
   50: 	print $socket &escape($cmd)."\n";
   51:         my $reply=<$socket>;
   52:         chomp($reply);
   53:         if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
   54:         return &unescape($reply);
   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') {
   63: 	if ($cmd=~/$forbidden/s) { return 1; }
   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: }
   77: 
   78: sub maxima_check {
   79:     my ($response,$answer,$reterror) = @_;
   80:     my $socket=&connect();
   81:     my $reply=&compareterms($socket,$response,$answer);
   82:     &disconnect($socket);
   83:     if ($reply eq 'true') { return 1; }
   84:     return 7;
   85: }
   86:  
   87: 1;
   88: __END__;

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