Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit e227339bb0beb669d1170f046df2c9ca384e24a0 @filonenko-mikhail committed Jan 23, 2012
Showing with 124 additions and 0 deletions.
  1. +30 −0 README
  2. +13 −0 examples/client.php
  3. +35 −0 examples/js-maxima-rpc-client.html
  4. +5 −0 maxima-json-rpc.asd
  5. +41 −0 src/defmodule.lisp
30 README
@@ -0,0 +1,30 @@
+maxima-json-rpc provide API for evaluating maxima expression through HTTP POST JSON-RPC. *Isolated* multiuser accesing is not possible now.
+
+Licence: MIT
+
+Author: Michael Filonenko <filonenko.mikhail at gmail dot com>
+
+Depends on:
+ embeddable-maxima
+ restas
+ cl-json
+
+Usage:
+ git clone ...
+ sbcl
+ (pushnew "/path/to/maxima-json-rpc/" asdf:*central-registry*)
+ (pushnew "/path/to/embeddable-maxima/" asdf:*central-registry*)
+ (ql:quickload :maxima-json-rpc)
+ (restas:start :maxima-json-rpc :port 9090)
+
+
+PHP example using JSON-RPC PHP http://jsonrpcphp.org:
+<?php
+ require_once 'jsonRPCClient.php';
+ $maxima = new jsonRPCClient('http://127.0.0.1:9090/jsonrpc');
+ print "Maxima evaluator\n";
+ print "Evaluate ratsimp(x^2 + 2*x + 1 - (x + 1)^2)\n"
+ print $maxima->evaluate("ratsimp(x^2 + 2*x + 1 - (x + 1)^2)");
+?>
+
+See examples/ for javascript/jquery/jquery-json-rpc example.
13 examples/client.php
@@ -0,0 +1,13 @@
+// Uses JSON-RPC PHP http://jsonrpcphp.org
+
+<?php
+ require_once 'jsonRPCClient.php';
+ $maxima = new jsonRPCClient('http://127.0.0.1:9090/jsonrpc');
+ print "Maxima evaluator\n";
+ print "Evaluate ratsimp(x^2 + 2*x + 1 - (x + 1)^2)\n";
+ print $maxima->evaluate("ratsimp(x^2 + 2*x + 1 - (x + 1)^2)");
+
+ print "Evaluate x^2 + 2*x + 1 + (x + 1)^2 in environment x=2\n";
+ print $maxima->evaluate("ev(x^2 + 2*x + 1 + (x + 1)^2, x=2)");
+?>
+
35 examples/js-maxima-rpc-client.html
@@ -0,0 +1,35 @@
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset='utf-8'" />
+
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
+ <script type="text/javascript" src="https://raw.github.com/datagraph/jquery-jsonrpc/master/jquery.jsonrpc.js"></script>
+
+ <title>Maxima json rpc example</title>
+
+ <script type="text/javascript">
+ $(document).ready(function() {
+ $.jsonRPC.setup({
+ endPoint: '/jsonrpc',
+ namespace: ''
+ });
+ $("input#evaluate").click(function() {
+ $.jsonRPC.request('evaluate', {
+ params: [$("textarea#input").val()],
+ success: function(data) {
+ $("<p />").text(data.result).appendTo($("p#result"));
+ },
+ error: function(data) {
+ $("<p />").text(data.error.message).appendTo($("p#result"));
+ }
+ });
+ });
+ });
+ </script>
+ </head>
+ <body>
+ <p id="result"></p>
+ <p><textarea name="input" id="input"></textarea></p>
+ <p><input name="evaluate" id="evaluate" value="evaluate" type="button"/></p>
+ </body>
+</html>
5 maxima-json-rpc.asd
@@ -0,0 +1,5 @@
+(defsystem maxima-json-rpc
+ :depends-on (#:restas #:embeddable-maxima
+ #:cl-json)
+ :components ((:module "src"
+ :components ((:file "defmodule")))))
41 src/defmodule.lisp
@@ -0,0 +1,41 @@
+(restas:define-module :maxima-json-rpc
+ (:use #:cl #:restas #:json #:json-rpc))
+
+(in-package :maxima-json-rpc)
+
+
+
+(defun ensure-valid-maxima-input (input)
+ "Returns string with appended ';', if input string does not have maxima command terminator at the end."
+ (let* ((input-trimmed (string-trim '(#\Space #\Newline #\Tab) input))
+ (last-char (char input-trimmed (1- (length input-trimmed)))))
+ (if (and (not (char= #\; last-char)) (not (char= #\$ last-char)))
+ (concatenate 'string input-trimmed ";")
+ input-trimmed)))
+
+(defun maxima-ast-from-string (input)
+ (maxima::macsyma-read-string (ensure-valid-maxima-input input)))
+
+(defmacro with-2d-output (&body body)
+ `(let ((maxima::$display2d nil))
+ ,@body))
+
+(defun-json-rpc evaluate :explicit (text)
+ "Evaluate maxima expression"
+ (let ((result (make-array '(0) :element-type 'base-char :fill-pointer 0 :adjustable t)))
+ (with-output-to-string (*standard-output* result)
+ (with-2d-output
+ (maxima::displa
+ (maxima::mfuncall 'maxima::$ev (maxima-ast-from-string text)))))
+ result))
+
+(define-route jsonrpc ("jsonrpc"
+ :method :post
+ ;;:content-type "application/json"
+ )
+ "json rpc route"
+ (let ((*json-rpc-version* +json-rpc-2.0+))
+ (invoke-rpc (hunchentoot:raw-post-data :force-text t))))
+
+(define-route example ("example")
+ (merge-pathnames "examples/js-maxima-rpc-client.html" (asdf:component-pathname (asdf:find-system :maxima-json-rpc))))

0 comments on commit e227339

Please sign in to comment.
Something went wrong with that request. Please try again.