[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm lonmaxima.pm response.pm

www lon-capa-cvs-allow@mail.lon-capa.org
Sat, 14 Jun 2008 16:34:54 -0000


www		Sat Jun 14 12:34:54 2008 EDT

  Modified files:              
    /loncom/homework	default_homework.lcpm lonmaxima.pm response.pm 
  Log:
  Bug #5738: be able to load libraries into Maxima
  
  
Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.136 loncom/homework/default_homework.lcpm:1.137
--- loncom/homework/default_homework.lcpm:1.136	Wed Jun 11 20:46:52 2008
+++ loncom/homework/default_homework.lcpm	Sat Jun 14 12:34:54 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
 #
-# $Id: default_homework.lcpm,v 1.136 2008/06/12 00:46:52 www Exp $
+# $Id: default_homework.lcpm,v 1.137 2008/06/14 16:34:54 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -424,10 +424,12 @@
 }
 
 sub cas {
-    my ($system,$input)=@_;
+    my ($system,$input,$library)=@_;
     my $output;
     if ($system eq 'maxima') {
-       $output=&maxima_eval($input);
+       $output=&maxima_eval($input,$library);
+    } else {
+       $output='Error: unrecognized CAS';
     }
     return $output;
 }
Index: loncom/homework/lonmaxima.pm
diff -u loncom/homework/lonmaxima.pm:1.22 loncom/homework/lonmaxima.pm:1.23
--- loncom/homework/lonmaxima.pm:1.22	Tue Jun 10 09:31:38 2008
+++ loncom/homework/lonmaxima.pm	Sat Jun 14 12:34:54 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Interface routines to MAXIMA CAS
 #
-# $Id: lonmaxima.pm,v 1.22 2008/06/10 13:31:38 www Exp $
+# $Id: lonmaxima.pm,v 1.23 2008/06/14 16:34:54 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -70,11 +70,39 @@
     return 0;
 }
 
+sub maxima_allowed_libraries {
+   return (
+      "absimp","affine","atensor","atrig1","augmented_lagrangian","contrib_ode","ctensor","descriptive","diag",
+      "eigen","facexp","fft","fourie","functs","ggf","grobner","impdiff","ineq","interpol","itensor","lapack",
+      "lbfgs","lindstedt","linearalgebra","lsquares","makeOrders","mnewton","mchrpl","ntrig","orthopoly",
+      "quadpack","rducon","romberg","scifac","simplex","solve_rec","sqdnst","stats","sterling","sym","units",
+      "vect","zeilberger");
+}
+
+sub maxima_is_allowed_library {
+    my ($library)=@_;
+    foreach my $allowed_library (&maxima_allowed_libraries()) {
+       if ($library eq $allowed_library) { return 1; }
+    }
+    return 0;
+}
+
 sub runscript {
-    my ($socket,$fullscript)=@_;
+    my ($socket,$fullscript,$libraries)=@_;
     if (&blacklisted($fullscript)) { return 'Error: blacklisted'; }
     my $reply;
     $fullscript=~s/[\n\r\l]//gs;
+    if ($libraries) {
+       foreach my $library (split(/\s*\,\s*/,$libraries)) {
+          unless ($library=~/\w/) { next; }
+          if (&maxima_is_allowed_library($library)) {
+              $reply=&maximareply($socket,'load('.$library.')$'."\n");
+              if ($reply=~/^Error\:/) { return $reply; }
+          } else { 
+             return 'Error: blacklisted'; 
+          }
+       }
+    }
     foreach my $line (split(/\;/s,$fullscript)) {
 	if ($line=~/\w/) { $reply=&maximareply($socket,$line.";\n"); }
 	if ($reply=~/^Error\:/) { return $reply; }
@@ -91,7 +119,7 @@
 }
 
 sub maxima_run {
-    my ($script,$submission,$argument) = @_;
+    my ($script,$submission,$argument,$libraries) = @_;
     my $socket=&connect();
     my @submissionarray=split(/\s*\,\s*/,$submission);
     for (my $i=0;$i<=$#submissionarray;$i++) {
@@ -105,7 +133,7 @@
         my $fixedargument=&maxima_cas_formula_fix($argumentarray[$i]);
         $script=~s/LONCAPALIST\[$n\]/$fixedargument/gs;
     }
-    my $reply=&runscript($socket,$script);
+    my $reply=&runscript($socket,$script,$libraries);
     &disconnect($socket);
     if ($reply=~/^\s*true\s*$/i) { return 'EXACT_ANS'; }
     if ($reply=~/^\s*false\s*$/i) { return 'INCORRECT'; } 
@@ -113,9 +141,9 @@
 }
 
 sub maxima_eval {
-    my ($script) = @_;
+    my ($script,$libraries) = @_;
     my $socket=&connect();
-    my $reply=&runscript($socket,$script);
+    my $reply=&runscript($socket,$script,$libraries);
     &disconnect($socket);
     return $reply;
 }
Index: loncom/homework/response.pm
diff -u loncom/homework/response.pm:1.191 loncom/homework/response.pm:1.192
--- loncom/homework/response.pm:1.191	Fri May 30 12:19:49 2008
+++ loncom/homework/response.pm	Sat Jun 14 12:34:54 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # various response type definitons response definition
 #
-# $Id: response.pm,v 1.191 2008/05/30 16:19:49 www Exp $
+# $Id: response.pm,v 1.192 2008/06/14 16:34:54 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -500,11 +500,13 @@
 					   $token);
 	$result.=&Apache::edit::text_arg('Argument Array:',
 					 'args',$token);
+        $result.=&Apache::edit::text_arg('Libraries:',
+                                         'libraries',$token);
 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
     } elsif ($target eq 'modified') {
 	my $constructtag;
 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
-						  $safeeval,'answerdisplay','cas','args');
+						  $safeeval,'answerdisplay','cas','args','libraries');
 	if ($constructtag) {
 	    $result = &Apache::edit::rebuild_tag($token);
 	}
@@ -551,7 +553,8 @@
 	    my $cas = &Apache::lonxml::get_param('cas',$parstack,$safeeval);
             if ($cas eq 'maxima') {
                 my $args = [&Apache::lonxml::get_param_var('args',$parstack,$safeeval)];
-                $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args);
+                $award=&Apache::lonmaxima::maxima_run($Apache::response::custom_answer[-1],$response,$args,
+                                                      &Apache::lonxml::get_param('libraries',$parstack,$safeeval));
             }
 	    if (!&Apache::inputtags::valid_award($award)) {
 		$error = $award;