Annotation of loncom/homework/lonr.pm, revision 1.4
1.1 www 1: # The LearningOnline Network with CAPA
2: # Interface routines to R CAS
3: #
1.4 ! www 4: # $Id: lonr.pm,v 1.3 2009/04/18 16:03:17 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::lonr;
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'}.'/rsock',
39: Type => SOCK_STREAM,
40: Timeout => 10);
41: }
42:
43: sub disconnect {
44: my ($socket)=@_;
45: if ($socket) { close($socket); }
46: }
47:
48: sub rreply {
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 (
1.3 www 64: 'read','write','scan','save','socket','connections',
65: 'open','close',
66: 'plot','X11','windows','quartz',
1.2 www 67: 'postscript','pdf','png','jpeg',
68: 'dev\.list','dev\.next','dev\.prev','dev\.set',
1.3 www 69: 'dev\.off','dev\.copy','dev\.print','graphics',
70: 'library','package','source','sink','objects',
1.4 ! www 71: 'Sys\.','unlink','file\.','on\.exit','error',
! 72: 'q\(\)'
1.1 www 73: ) {
74: if ($cmd=~/$forbidden/s) { return 1; }
75: }
76: return 0;
77: }
78:
79: sub r_allowed_libraries {
1.2 www 80: return ('boot','class','cluster','datasets','KernSmooth','MASS',
81: 'methods','mgcv','nlme','nnet','rpart','spatial',
82: 'splines','stats','stats4','survival');
1.1 www 83: }
84:
85: sub r_is_allowed_library {
86: my ($library)=@_;
87: foreach my $allowed_library (&r_allowed_libraries()) {
88: if ($library eq $allowed_library) { return 1; }
89: }
90: return 0;
91: }
92:
93: sub runscript {
94: my ($socket,$fullscript,$libraries)=@_;
95: if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
96: my $reply;
97: $fullscript=~s/[\n\r\l]//gs;
98: if ($libraries) {
99: foreach my $library (split(/\s*\,\s*/,$libraries)) {
100: unless ($library=~/\w/) { next; }
101: if (&r_is_allowed_library($library)) {
102: $reply=&rreply($socket,'library('.$library.');'."\n");
103: if ($reply=~/^Error\:/) { return $reply; }
104: } else {
105: return 'Error: blacklisted';
106: }
107: }
108: }
109: foreach my $line (split(/\;/s,$fullscript)) {
110: if ($line=~/\w/) { $reply=&rreply($socket,$line.";\n"); }
111: if ($reply=~/^Error\:/) { return $reply; }
112: }
113: $reply=~s/^\s*//gs;
114: $reply=~s/\s*$//gs;
115: &Apache::lonxml::debug("r $fullscript \n reply $reply");
116: return $reply;
117: }
118:
119: sub r_cas_formula_fix {
120: my ($expression)=@_;
121: return &Apache::response::implicit_multiplication($expression);
122: }
123:
124: sub r_run {
125: my ($script,$submission,$argument,$libraries) = @_;
126: my $socket=&connect();
127: my @submissionarray=split(/\s*\,\s*/,$submission);
128: for (my $i=0;$i<=$#submissionarray;$i++) {
129: my $n=$i+1;
130: my $fixedsubmission=&r_cas_formula_fix($submissionarray[$i]);
131: $script=~s/RESPONSE\[$n\]/$fixedsubmission/gs;
132: }
133: my @argumentarray=@{$argument};
134: for (my $i=0;$i<=$#argumentarray;$i++) {
135: my $n=$i+1;
136: my $fixedargument=&r_cas_formula_fix($argumentarray[$i]);
137: $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
138: }
139: my $reply=&runscript($socket,$script,$libraries);
140: &disconnect($socket);
141: if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
142: if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; }
143: return 'BAD_FORMULA';
144: }
145:
146: sub r_eval {
147: my ($script,$libraries) = @_;
148: my $socket=&connect();
149: my $reply=&runscript($socket,$script,$libraries);
150: &disconnect($socket);
151: return $reply;
152: }
153:
154:
155: sub compareterms {
156: my ($socket,$terma,$termb)=@_;
157: my $difference=$terma.'-('.$termb.')';
158: if (&blacklisted($difference)) { return 'Error: blacklisted'; }
159: my $reply=&rreply($socket,$difference.';');
160: if ($reply=~/^\s*0\s*$/) { return 'true'; }
161: if ($reply=~/^Error\:/) { return $reply; }
162: return 'false';
163: }
164:
165: sub r_check {
166: my ($response,$answer,$reterror) = @_;
167: my $socket=&connect();
168: my $reply=&compareterms($socket,$response,$answer);
169: &disconnect($socket);
170: # integer to string mappings come from capaParser.h
171: # 1 maps to 'EXACT_ANS'
172: if ($reply eq 'true') { return 1; }
173: # 11 maps to 'BAD_FORMULA'
174: if ($reply=~/^Error\:/) { return 11; }
175: # 7 maps to 'INCORRECT'
176: return 7;
177: }
178:
179: 1;
180: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>