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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Interface routines to MAXIMA CAS
                      3: #
1.33    ! raeburn     4: # $Id: lonmaxima.pm,v 1.32 2017/03/05 19:30:56 raeburn 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.9       www        34: use Apache::response();
1.4       bisitz     35: use LONCAPA;
1.1       www        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) {
1.4       bisitz     51: 	print $socket &escape($cmd)."\n";
1.1       www        52:         my $reply=<$socket>;
                     53:         chomp($reply);
1.30      raeburn    54:         if ($reply=~/^Incorrect/i) { $reply='Error: '.$reply; }
1.4       bisitz     55:         return &unescape($reply);
1.1       www        56:     } else {
                     57:         return 'Error: no connection.';
                     58:     }
                     59: }
                     60: 
                     61: sub blacklisted {
                     62:     my ($cmd)=@_;
1.25      bisitz     63:     foreach my $forbidden (
1.28      raeburn    64:         '\? ','\?','%i\d+','%o','batch','block'
1.25      bisitz     65:        ,'compil','concat','describe','display2d','file','inchar'
                     66:        ,'includ','lisp','load','outchar','plot','quit'
                     67:        ,'read','reset','save','stin','stout','stringout'
                     68:        ,'system','translat','ttyoff','with_stdout','writefile'
                     69:      ) {
1.2       www        70: 	if ($cmd=~/$forbidden/s) { return 1; }
1.1       www        71:     } 
                     72:     return 0;
                     73: }
                     74: 
1.23      www        75: sub maxima_allowed_libraries {
                     76:    return (
                     77:       "absimp","affine","atensor","atrig1","augmented_lagrangian","contrib_ode","ctensor","descriptive","diag",
                     78:       "eigen","facexp","fft","fourie","functs","ggf","grobner","impdiff","ineq","interpol","itensor","lapack",
                     79:       "lbfgs","lindstedt","linearalgebra","lsquares","makeOrders","mnewton","mchrpl","ntrig","orthopoly",
                     80:       "quadpack","rducon","romberg","scifac","simplex","solve_rec","sqdnst","stats","sterling","sym","units",
                     81:       "vect","zeilberger");
                     82: }
                     83: 
                     84: sub maxima_is_allowed_library {
                     85:     my ($library)=@_;
                     86:     foreach my $allowed_library (&maxima_allowed_libraries()) {
                     87:        if ($library eq $allowed_library) { return 1; }
                     88:     }
                     89:     return 0;
                     90: }
                     91: 
1.6       www        92: sub runscript {
1.23      www        93:     my ($socket,$fullscript,$libraries)=@_;
1.6       www        94:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
1.7       www        95:     my $reply;
                     96:     $fullscript=~s/[\n\r\l]//gs;
1.23      www        97:     if ($libraries) {
                     98:        foreach my $library (split(/\s*\,\s*/,$libraries)) {
                     99:           unless ($library=~/\w/) { next; }
                    100:           if (&maxima_is_allowed_library($library)) {
                    101:               $reply=&maximareply($socket,'load('.$library.')$'."\n");
                    102:               if ($reply=~/^Error\:/) { return $reply; }
                    103:           } else { 
                    104:              return 'Error: blacklisted'; 
                    105:           }
                    106:        }
                    107:     }
1.7       www       108:     foreach my $line (split(/\;/s,$fullscript)) {
                    109: 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
                    110: 	if ($reply=~/^Error\:/) { return $reply; }
                    111:     }
1.14      www       112:     $reply=~s/^\s*//gs;
                    113:     $reply=~s/\s*$//gs;
1.15      albertel  114:     &Apache::lonxml::debug("maxima $fullscript \n reply $reply");
1.7       www       115:     return $reply;
1.6       www       116: }
                    117: 
1.9       www       118: sub maxima_cas_formula_fix {
                    119:    my ($expression)=@_;
1.29      www       120:    $expression=&Apache::response::implicit_multiplication($expression);
                    121:    $expression=~s/([^\w\%])pi/$1\%pi/gs;
                    122:    return $expression;
1.9       www       123: }
                    124: 
1.6       www       125: sub maxima_run {
1.23      www       126:     my ($script,$submission,$argument,$libraries) = @_;
1.6       www       127:     my $socket=&connect();
1.12      www       128:     my @submissionarray=split(/\s*\,\s*/,$submission);
                    129:     for (my $i=0;$i<=$#submissionarray;$i++) {
                    130:         my $n=$i+1;
                    131:         my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
                    132:         $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
1.7       www       133:     }
1.12      www       134:     my @argumentarray=@{$argument};
                    135:     for (my $i=0;$i<=$#argumentarray;$i++) {
                    136:         my $n=$i+1;
                    137:         my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
                    138:         $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
1.7       www       139:     }
1.23      www       140:     my $reply=&runscript($socket,$script,$libraries);
1.6       www       141:     &disconnect($socket);
1.13      raeburn   142:     if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
                    143:     if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
1.6       www       144:     return 'BAD_FORMULA';
                    145: }
                    146: 
1.11      www       147: sub maxima_eval {
1.23      www       148:     my ($script,$libraries) = @_;
1.11      www       149:     my $socket=&connect();
1.23      www       150:     my $reply=&runscript($socket,$script,$libraries);
1.11      www       151:     &disconnect($socket);
                    152:     return $reply;
                    153: }
                    154: 
                    155: 
1.1       www       156: sub compareterms {
                    157:     my ($socket,$terma,$termb)=@_;
1.33    ! raeburn   158:     if ($terma =~ m{[.*^/\s]+(|\(\s*\((|[^(]+))$}) {
1.32      raeburn   159:         return 'Error: blacklisted';
                    160:     }
1.16      albertel  161:     my $difference=$terma.'-('.$termb.')';
1.1       www       162:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
                    163:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
                    164:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
                    165:     if ($reply=~/^Error\:/) { return $reply; }
                    166:     return 'false';
                    167: }
1.3       bisitz    168: 
                    169: sub maxima_check {
1.5       albertel  170:     my ($response,$answer,$reterror) = @_;
1.4       bisitz    171:     my $socket=&connect();
                    172:     my $reply=&compareterms($socket,$response,$answer);
                    173:     &disconnect($socket);
1.17      albertel  174:     # integer to string mappings come from capaParser.h
                    175:     # 1 maps to 'EXACT_ANS'
1.4       bisitz    176:     if ($reply eq 'true') { return 1; }
1.22      www       177:     # 11 maps to 'BAD_FORMULA'
                    178:     if ($reply=~/^Error\:/) { return 11; }
1.17      albertel  179:     # 7 maps to 'INCORRECT'
1.3       bisitz    180:     return 7;
                    181: }
1.1       www       182:  
                    183: 1;
                    184: __END__;

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