File:
[LON-CAPA] /
loncom /
homework /
lonmaxima.pm
Revision
1.13:
download - view:
text,
annotated -
select for diffs
Thu Apr 19 17:40:43 2007 UTC (17 years, 4 months ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_3_99_0,
HEAD
- Make check for true or false case insensitive (maxima/clisp on SuSE returns upper case; maxima/gcl on Fedora returns lower case).
- regexp for false now /^\s*false\s*$/ (i.e., $ termination added - assuming there's no \n here). Should there be a chomp($reply)?
1: # The LearningOnline Network with CAPA
2: # Interface routines to MAXIMA CAS
3: #
4: # $Id: lonmaxima.pm,v 1.13 2007/04/19 17:40:43 raeburn 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 @submissionarray=split(/\s*\,\s*/,$submission);
91: for (my $i=0;$i<=$#submissionarray;$i++) {
92: my $n=$i+1;
93: my $fixedsubmission=&maxima_cas_formula_fix($submissionarray[$i]);
94: $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
95: }
96: my @argumentarray=@{$argument};
97: for (my $i=0;$i<=$#argumentarray;$i++) {
98: my $n=$i+1;
99: my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
100: $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
101: }
102: my $reply=&runscript($socket,$script);
103: &disconnect($socket);
104: if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
105: if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; }
106: return 'BAD_FORMULA';
107: }
108:
109: sub maxima_eval {
110: my ($script) = @_;
111: my $socket=&connect();
112: my $reply=&runscript($socket,$script);
113: &disconnect($socket);
114: return $reply;
115: }
116:
117:
118: sub compareterms {
119: my ($socket,$terma,$termb)=@_;
120: my $difference=$terma.'-'.$termb;
121: if (&blacklisted($difference)) { return 'Error: blacklisted'; }
122: my $reply=&maximareply($socket,'trigsimp(trigreduce('.$difference.'));');
123: if ($reply=~/^\s*0\s*$/) { return 'true'; }
124: if ($reply=~/^Error\:/) { return $reply; }
125: return 'false';
126: }
127:
128: sub maxima_check {
129: my ($response,$answer,$reterror) = @_;
130: my $socket=&connect();
131: my $reply=&compareterms($socket,$response,$answer);
132: &disconnect($socket);
133: if ($reply eq 'true') { return 1; }
134: return 7;
135: }
136:
137: 1;
138: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>