File:  [LON-CAPA] / loncom / homework / lonmaxima.pm
Revision 1.9: download - view: text, annotated - select for diffs
Fri Dec 15 22:11:43 2006 UTC (17 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Make formula fix work with <mathresponse>

    1: # The LearningOnline Network with CAPA
    2: # Interface routines to MAXIMA CAS
    3: #
    4: # $Id: lonmaxima.pm,v 1.9 2006/12/15 22:11:43 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 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','file','batch','stringout','translat','stout','stin','block','system') {
   64: 	if ($cmd=~/$forbidden/s) { return 1; }
   65:     } 
   66:     return 0;
   67: }
   68: 
   69: sub runscript {
   70:     my ($socket,$fullscript)=@_;
   71:     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
   72:     my $reply;
   73:     $fullscript=~s/[\n\r\l]//gs;
   74:     foreach my $line (split(/\;/s,$fullscript)) {
   75: 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
   76: 	if ($reply=~/^Error\:/) { return $reply; }
   77:     }
   78:     $reply=~s/\W//gs;
   79:     return $reply;
   80: }
   81: 
   82: sub maxima_cas_formula_fix {
   83:    my ($expression)=@_;
   84:    return &Apache::response::implicit_multiplication($expression);
   85: }
   86: 
   87: sub maxima_run {
   88:     my ($script,$submission,$argument) = @_;
   89:     my $socket=&connect();
   90:     my $fullscript='';
   91:     my $submission_index=1;
   92:     foreach my $submission_component (split(/\s*\,\s*/,$submission)) {
   93: 	$fullscript.="RESPONSE[$submission_index]:".&maxima_cas_formula_fix($submission_component).";\n";
   94: 	$submission_index++;
   95:     }
   96:     my $argument_index=1;
   97:     foreach my $argument_component (@{$argument}) {
   98: 	$fullscript.="LONCAPALIST[$argument_index]:$argument_component;\n";
   99: 	$argument_index++;
  100:     }
  101:     $fullscript.=$script;
  102:     my $reply=&runscript($socket,$fullscript);
  103:     &disconnect($socket);
  104:     if ($reply=~/^\s*true\s*$/) { return 'EXACT_ANS'; }
  105:     if ($reply=~/^\s*false\s*/) { return 'INCORRECT'; } 
  106:     return 'BAD_FORMULA';
  107: }
  108: 
  109: sub compareterms {
  110:     my ($socket,$terma,$termb)=@_;
  111:     my $difference=$terma.'-'.$termb;
  112:     if (&blacklisted($difference)) { return 'Error: blacklisted'; }
  113:     my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
  114:     if ($reply=~/^\s*0\s*$/) { return 'true'; }
  115:     if ($reply=~/^Error\:/) { return $reply; }
  116:     return 'false';
  117: }
  118: 
  119: sub maxima_check {
  120:     my ($response,$answer,$reterror) = @_;
  121:     my $socket=&connect();
  122:     my $reply=&compareterms($socket,$response,$answer);
  123:     &disconnect($socket);
  124:     if ($reply eq 'true') { return 1; }
  125:     return 7;
  126: }
  127:  
  128: 1;
  129: __END__;

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