File:  [LON-CAPA] / loncom / homework / lonmaxima.pm
Revision 1.24: download - view: text, annotated - select for diffs
Thu Aug 21 11:11:31 2008 UTC (15 years, 8 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Extended blacklist:
Added some commands to the not allowed maxima commands blacklist.
These commands are not needed, don't make sense in a problem and confuse the system,
e.g. display other values than expected, code ran too long error, etc.

    1: # The LearningOnline Network with CAPA
    2: # Interface routines to MAXIMA CAS
    3: #
    4: # $Id: lonmaxima.pm,v 1.24 2008/08/21 11:11:31 bisitz 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','inchar',
   66: 			   'outchar','ttyoff','with_stdout','writefile',
   67: 			   'reset','quit','\? ','\?','display2d','%i','%o','describe') {
   68: 	if ($cmd=~/$forbidden/s) { return 1; }
   69:     } 
   70:     return 0;
   71: }
   72: 
   73: sub maxima_allowed_libraries {
   74:    return (
   75:       "absimp","affine","atensor","atrig1","augmented_lagrangian","contrib_ode","ctensor","descriptive","diag",
   76:       "eigen","facexp","fft","fourie","functs","ggf","grobner","impdiff","ineq","interpol","itensor","lapack",
   77:       "lbfgs","lindstedt","linearalgebra","lsquares","makeOrders","mnewton","mchrpl","ntrig","orthopoly",
   78:       "quadpack","rducon","romberg","scifac","simplex","solve_rec","sqdnst","stats","sterling","sym","units",
   79:       "vect","zeilberger");
   80: }
   81: 
   82: sub maxima_is_allowed_library {
   83:     my ($library)=@_;
   84:     foreach my $allowed_library (&maxima_allowed_libraries()) {
   85:        if ($library eq $allowed_library) { return 1; }
   86:     }
   87:     return 0;
   88: }
   89: 
   90: sub runscript {
   91:     my ($socket,$fullscript,$libraries)=@_;
   92:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
   93:     my $reply;
   94:     $fullscript=~s/[\n\r\l]//gs;
   95:     if ($libraries) {
   96:        foreach my $library (split(/\s*\,\s*/,$libraries)) {
   97:           unless ($library=~/\w/) { next; }
   98:           if (&maxima_is_allowed_library($library)) {
   99:               $reply=&maximareply($socket,'load('.$library.')$'."\n");
  100:               if ($reply=~/^Error\:/) { return $reply; }
  101:           } else { 
  102:              return 'Error: blacklisted'; 
  103:           }
  104:        }
  105:     }
  106:     foreach my $line (split(/\;/s,$fullscript)) {
  107: 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
  108: 	if ($reply=~/^Error\:/) { return $reply; }
  109:     }
  110:     $reply=~s/^\s*//gs;
  111:     $reply=~s/\s*$//gs;
  112:     &Apache::lonxml::debug("maxima $fullscript \n reply $reply");
  113:     return $reply;
  114: }
  115: 
  116: sub maxima_cas_formula_fix {
  117:    my ($expression)=@_;
  118:    return &Apache::response::implicit_multiplication($expression);
  119: }
  120: 
  121: sub maxima_run {
  122:     my ($script,$submission,$argument,$libraries) = @_;
  123:     my $socket=&connect();
  124:     my @submissionarray=split(/\s*\,\s*/,$submission);
  125:     for (my $i=0;$i<=$#submissionarray;$i++) {
  126:         my $n=$i+1;
  127:         my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
  128:         $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
  129:     }
  130:     my @argumentarray=@{$argument};
  131:     for (my $i=0;$i<=$#argumentarray;$i++) {
  132:         my $n=$i+1;
  133:         my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
  134:         $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
  135:     }
  136:     my $reply=&runscript($socket,$script,$libraries);
  137:     &disconnect($socket);
  138:     if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
  139:     if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
  140:     return 'BAD_FORMULA';
  141: }
  142: 
  143: sub maxima_eval {
  144:     my ($script,$libraries) = @_;
  145:     my $socket=&connect();
  146:     my $reply=&runscript($socket,$script,$libraries);
  147:     &disconnect($socket);
  148:     return $reply;
  149: }
  150: 
  151: 
  152: sub compareterms {
  153:     my ($socket,$terma,$termb)=@_;
  154:     my $difference=$terma.'-('.$termb.')';
  155:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
  156:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
  157:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
  158:     if ($reply=~/^Error\:/) { return $reply; }
  159:     return 'false';
  160: }
  161: 
  162: sub maxima_check {
  163:     my ($response,$answer,$reterror) = @_;
  164:     my $socket=&connect();
  165:     my $reply=&compareterms($socket,$response,$answer);
  166:     &disconnect($socket);
  167:     # integer to string mappings come from capaParser.h
  168:     # 1 maps to 'EXACT_ANS'
  169:     if ($reply eq 'true') { return 1; }
  170:     # 11 maps to 'BAD_FORMULA'
  171:     if ($reply=~/^Error\:/) { return 11; }
  172:     # 7 maps to 'INCORRECT'
  173:     return 7;
  174: }
  175:  
  176: 1;
  177: __END__;

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