File:  [LON-CAPA] / loncom / homework / lonmaxima.pm
Revision 1.19: download - view: text, annotated - select for diffs
Mon Aug 13 09:34:35 2007 UTC (16 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- revert 1.18
- add read to banned words

    1: # The LearningOnline Network with CAPA
    2: # Interface routines to MAXIMA CAS
    3: #
    4: # $Id: lonmaxima.pm,v 1.19 2007/08/13 09:34:35 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 Apache::response();
   35: use LONCAPA;
   36: 
   37: sub connect {
   38:    return IO::Socket::UNIX->new(Peer    => $Apache::lonnet::perlvar{'lonSockDir'}.'/maximasock',
   39: 				Type    => SOCK_STREAM,
   40: 				Timeout => 10);
   41: }
   42: 
   43: sub disconnect {
   44:     my ($socket)=@_;
   45:     if ($socket) { close($socket); }
   46: }
   47: 
   48: sub maximareply {
   49:     my ($socket,$cmd)=@_;
   50:     if ($socket) {
   51: 	print $socket &escape($cmd)."\n";
   52:         my $reply=<$socket>;
   53:         chomp($reply);
   54:         if ($reply=~/^Incorrect/) { $reply='Error: '.$reply; }
   55:         return &unescape($reply);
   56:     } else {
   57:         return 'Error: no connection.';
   58:     }
   59: }
   60: 
   61: sub blacklisted {
   62:     my ($cmd)=@_;
   63:     foreach my $forbidden ('save','load','plot','lisp','includ','compil',
   64: 			   'file','batch','stringout','translat','stout',
   65: 			   'stin','block','system','concat','read') {
   66: 	if ($cmd=~/$forbidden/s) { return 1; }
   67:     } 
   68:     return 0;
   69: }
   70: 
   71: sub runscript {
   72:     my ($socket,$fullscript)=@_;
   73:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
   74:     my $reply;
   75:     $fullscript=~s/[\n\r\l]//gs;
   76:     foreach my $line (split(/\;/s,$fullscript)) {
   77: 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
   78: 	if ($reply=~/^Error\:/) { return $reply; }
   79:     }
   80:     $reply=~s/^\s*//gs;
   81:     $reply=~s/\s*$//gs;
   82:     &Apache::lonxml::debug("maxima $fullscript \n reply $reply");
   83:     return $reply;
   84: }
   85: 
   86: sub maxima_cas_formula_fix {
   87:    my ($expression)=@_;
   88:    return &Apache::response::implicit_multiplication($expression);
   89: }
   90: 
   91: sub maxima_run {
   92:     my ($script,$submission,$argument) = @_;
   93:     my $socket=&connect();
   94:     my @submissionarray=split(/\s*\,\s*/,$submission);
   95:     for (my $i=0;$i<=$#submissionarray;$i++) {
   96:         my $n=$i+1;
   97:         my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
   98:         $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
   99:     }
  100:     my @argumentarray=@{$argument};
  101:     for (my $i=0;$i<=$#argumentarray;$i++) {
  102:         my $n=$i+1;
  103:         my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
  104:         $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
  105:     }
  106:     my $reply=&runscript($socket,$script);
  107:     &disconnect($socket);
  108:     if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
  109:     if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
  110:     return 'BAD_FORMULA';
  111: }
  112: 
  113: sub maxima_eval {
  114:     my ($script) = @_;
  115:     my $socket=&connect();
  116:     my $reply=&runscript($socket,$script);
  117:     &disconnect($socket);
  118:     return $reply;
  119: }
  120: 
  121: 
  122: sub compareterms {
  123:     my ($socket,$terma,$termb)=@_;
  124:     my $difference=$terma.'-('.$termb.')';
  125:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
  126:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
  127:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
  128:     if ($reply=~/^Error\:/) { return $reply; }
  129:     return 'false';
  130: }
  131: 
  132: sub maxima_check {
  133:     my ($response,$answer,$reterror) = @_;
  134:     my $socket=&connect();
  135:     my $reply=&compareterms($socket,$response,$answer);
  136:     &disconnect($socket);
  137:     # integer to string mappings come from capaParser.h
  138:     # 1 maps to 'EXACT_ANS'
  139:     if ($reply eq 'true') { return 1; }
  140:     # 7 maps to 'INCORRECT'
  141:     return 7;
  142: }
  143:  
  144: 1;
  145: __END__;

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