File:  [LON-CAPA] / loncom / homework / lonmaxima.pm
Revision 1.8: download - view: text, annotated - select for diffs
Fri Dec 15 21:10:13 2006 UTC (17 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
More elegant

    1: # The LearningOnline Network with CAPA
    2: # Interface routines to MAXIMA CAS
    3: #
    4: # $Id: lonmaxima.pm,v 1.8 2006/12/15 21:10:13 www 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 runscript {
   69:     my ($socket,$fullscript)=@_;
   70:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
   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;
   79: }
   80: 
   81: sub maxima_run {
   82:     my ($script,$submission,$argument) = @_;
   83:     my $socket=&connect();
   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 (@{$argument}) {
   92: 	$fullscript.="LONCAPALIST[$argument_index]:$argument_component;\n";
   93: 	$argument_index++;
   94:     }
   95:     $fullscript.=$script;
   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: 
  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: }
  112: 
  113: sub maxima_check {
  114:     my ($response,$answer,$reterror) = @_;
  115:     my $socket=&connect();
  116:     my $reply=&compareterms($socket,$response,$answer);
  117:     &disconnect($socket);
  118:     if ($reply eq 'true') { return 1; }
  119:     return 7;
  120: }
  121:  
  122: 1;
  123: __END__;

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