Annotation of loncom/homework/lonmaxima.pm, revision 1.9
1.1 www 1: # The LearningOnline Network with CAPA
2: # Interface routines to MAXIMA CAS
3: #
1.9 ! www 4: # $Id: lonmaxima.pm,v 1.8 2006/12/15 21:10:13 www 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);
54: if ($reply=~/^Incorrect/) { $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)=@_;
63: foreach my $forbidden ('save','load','plot','lisp','includ','compil','file','batch','stringout','translat','stout','stin','block','system') {
1.2 www 64: if ($cmd=~/$forbidden/s) { return 1; }
1.1 www 65: }
66: return 0;
67: }
68:
1.6 www 69: sub runscript {
70: my ($socket,$fullscript)=@_;
71: if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
1.7 www 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;
1.6 www 80: }
81:
1.9 ! www 82: sub maxima_cas_formula_fix {
! 83: my ($expression)=@_;
! 84: return &Apache::response::implicit_multiplication($expression);
! 85: }
! 86:
1.6 www 87: sub maxima_run {
1.7 www 88: my ($script,$submission,$argument) = @_;
1.6 www 89: my $socket=&connect();
1.7 www 90: my $fullscript='';
91: my $submission_index=1;
92: foreach my $submission_component (split(/\s*\,\s*/,$submission)) {
1.9 ! www 93: $fullscript.="RESPONSE[$submission_index]:".&maxima_cas_formula_fix($submission_component).";\n";
1.7 www 94: $submission_index++;
95: }
96: my $argument_index=1;
1.8 www 97: foreach my $argument_component (@{$argument}) {
1.7 www 98: $fullscript.="LONCAPALIST[$argument_index]:$argument_component;\n";
99: $argument_index++;
100: }
101: $fullscript.=$script;
1.6 www 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:
1.1 www 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: }
1.3 bisitz 118:
119: sub maxima_check {
1.5 albertel 120: my ($response,$answer,$reterror) = @_;
1.4 bisitz 121: my $socket=&connect();
122: my $reply=&compareterms($socket,$response,$answer);
123: &disconnect($socket);
124: if ($reply eq 'true') { return 1; }
1.3 bisitz 125: return 7;
126: }
1.1 www 127:
128: 1;
129: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>