Programmatic Content Generation - Maple Help

Home : Support : Online Help : System : Information : Updates : Maple 2015 : Programmatic Content Generation

 Programmatic Content Generation

New subpackages and commands within the DocumentTools package now allow fully programmatic functionality to create and make use of Worksheet and Document content, including:

 • Customized Tables
 • Applications containing Embedded Components and Code Edit Regions
 • Sections and subsections, Document Blocks and Execution Groups, customized text, and input and output
 • Insertion into the current document, and launching or saving as a new document

This functionality is already in use by several other Maple commands, including Explore, ImageTools:-Embed, and the new Tabulate command.

Layout elements

The DocumentTools:-Layout subpackage contains commands for constructing basic elements of a Document or Worksheet.

Commands currently available and documented are as indicated by the following command to load the subpackage:

 > $\mathrm{with}\left(\mathrm{DocumentTools}:-\mathrm{Layout}\right)$
 $\left[{\mathrm{Cell}}{,}{\mathrm{Column}}{,}{\mathrm{DocumentBlock}}{,}{\mathrm{Equation}}{,}{\mathrm{Font}}{,}{\mathrm{Group}}{,}{\mathrm{Image}}{,}{\mathrm{InlinePlot}}{,}{\mathrm{Input}}{,}{\mathrm{Output}}{,}{\mathrm{Row}}{,}{\mathrm{Section}}{,}{\mathrm{Table}}{,}{\mathrm{Textfield}}{,}{\mathrm{Worksheet}}\right]$ (1.1)

These commands can be used to construct a structure of nested Maple function calls which comprise an XML format representation of a complete Document or Worksheet. The resulting document content can be inserted, launched, or saved.

The following is a simple example of construction and insertion of such content. This example uses the constructor commands Worksheet, Table, Row, Textfield, and Equation.

 > $\mathrm{E1}:=\mathrm{Equation}\left('\sum _{i=1}^{n}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}i',\mathrm{style}=\mathrm{TwoDimInput}\right):$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{E2}≔\mathrm{Equation}\left(\mathrm{factor}\left(\sum _{i=1}^{n}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}i\right),\mathrm{style}=\mathrm{TwoDimInput},\mathrm{typesetting}=\mathrm{extended}\right):$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}T≔\mathrm{Textfield}\left("The sum of the first n positive integers",\mathrm{E1},"is equal to",\mathrm{E2},\mathrm{alignment}=\mathrm{centered}\right):\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{xml}≔\mathrm{Worksheet}\left(\mathrm{Table}\left(\mathrm{Row}\left(T\right),\mathrm{alignment}=\mathrm{center},\mathrm{width}=50\right)\right):

The InsertContent command can be used to insert this content directly into the current document.

 > $\mathrm{DocumentTools}:-\mathrm{InsertContent}\left(\mathrm{xml}\right):$

 The sum of the first n positive integers $\sum _{i=1}^{n}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}i$ is equal to $\frac{n\left(n+1\right)}{2}$

Embedded Components and application authoring

The DocumentTools:-Components subpackage contains commands for constructing Embedded Components as content.

 > $\mathrm{with}\left(\mathrm{DocumentTools}:-\mathrm{Components}\right)$
 $\left[{\mathrm{Button}}{,}{\mathrm{CheckBox}}{,}{\mathrm{CodeEditRegion}}{,}{\mathrm{ComboBox}}{,}{\mathrm{DataTable}}{,}{\mathrm{Dial}}{,}{\mathrm{Label}}{,}{\mathrm{ListBox}}{,}{\mathrm{MathContainer}}{,}{\mathrm{Meter}}{,}{\mathrm{Microphone}}{,}{\mathrm{Plot}}{,}{\mathrm{RadioButton}}{,}{\mathrm{RotaryGauge}}{,}{\mathrm{Shortcut}}{,}{\mathrm{Slider}}{,}{\mathrm{Speaker}}{,}{\mathrm{TextArea}}{,}{\mathrm{ToggleButton}}{,}{\mathrm{VideoPlayer}}{,}{\mathrm{VolumeGauge}}\right]$ (2.1)

An interactive application can be completely programmatically constructed as content.

The following example consists of constructing a Plot and a Button component, each with their own action code. Each time the mouse cursor is clicked within the Plot component a new data point is added to the list assigned to the global name L, and a least squares linear fit of this data is plotted. Clicking on the Button clears the plot.

The application is inserted into the current document for this first example. This example uses the commands Plot and Button to construct the components' contents.

This example also uses two procedures, update and clear, which must be defined (and left as defined) for the inserted application to function. These procedures are called whenever the respective components are clicked.

 >
 >

Next, the two components are constructed. Their respective action code, executed whenever the components are clicked upon, call the aforementioned procedures.

 > $P:=\mathrm{Plot}\left(\mathrm{plot}\left(\left[\left[0,0\right]\right],\mathrm{color}=\mathrm{white},\mathrm{axes}=\mathrm{normal},\mathrm{view}=\left[0..10,0..10\right]\right),\mathrm{identity}="Plot0",\mathrm{clickdefault},\mathrm{clickaction}="update\left(%Plot0\right);"\right):$$B:=\mathrm{Button}\left("Clear",\mathrm{identity}="Button0",\mathrm{action}="unassign\left(\text{'}:-L\text{'}\right); clear\left(%Plot0\right);"\right):$

The content construction is then completed, forming a full worksheet in XML representation. Next, this is inserted into the current worksheet.

 > $\mathrm{xmlapp}:=\mathrm{Worksheet}\left(\mathrm{Group}\left(\mathrm{Input}\left(\mathrm{Textfield}\left(B,P\right)\right)\right)\right):$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{DocumentTools}:-\mathrm{InsertContent}\left(\mathrm{xmlapp}\right):$

In the second example, the same application is regenerated, but now the defining of the supporting procedures update and clear is done within the constructed application itself. This allows the application to function even if it is launched or saved as a new worksheet.

In the following revision, the procedure definitions are part of the action code of the two components. An alternative approach is to put those procedure definitions into a Code Edit Region constructed as an additional part of the content.

 >
 >
 > $\mathrm{xmlapp2}:=\mathrm{Worksheet}\left(\mathrm{Group}\left(\mathrm{Input}\left(\mathrm{Textfield}\left(\mathrm{B2},\mathrm{P2}\right)\right)\right)\right):$

The ContentToString command is used to create a text string of an actual worksheet. This string can be launched directly in a new window, or saved as a worksheet file.

 > $\mathrm{appstring}:=\mathrm{DocumentTools}:-\mathrm{ContentToString}\left(\mathrm{xmlapp2}\right):$

This worksheet string can be launched directly in a new window using the Display command from the Worksheet package. In such a new window,the application functions independently of any definitions inside the current worksheet.

The following invocation uses the global name syntax :-Worksheet in order to reference the package by that name rather than the export by that same name in the previously loaded Layout package.

 > $:-\mathrm{Worksheet}:-\mathrm{Display}\left(\mathrm{appstring}\right)$

The test string of the worksheet can also be saved as a worksheet file.

 > $\mathrm{fname}:=\mathrm{FileTools}:-\mathrm{TemporaryFile}\left(\right):$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{FileTools}:-\mathrm{Text}:-\mathrm{WriteFile}\left(\mathrm{fname},\mathrm{appstring}\right):$

The saved file is an application that functions independently of any definitions made in the current worksheet.

 > $:-\mathrm{Worksheet}:-\mathrm{DisplayFile}\left(\mathrm{fname}\right)$

The Tabulate command

The Tabulate command allows indexed structures, such as lists, Vectors, Matrices, or Arrays to be formatted with their elements becoming the entries in the cells of an inserted Table GUI element.

 > $M:=\mathrm{LinearAlgebra}:-\mathrm{RandomMatrix}\left(6,2,\mathrm{generator}=0..100\right)$
 ${M}{:=}\left[\begin{array}{rr}{99}& {91}\\ {30}& {63}\\ {22}& {89}\\ {55}& {86}\\ {77}& {76}\\ {82}& {49}\end{array}\right]$ (3.1)
 > $\mathrm{DocumentTools}:-\mathrm{Tabulate}\left(M,\mathrm{widthmode}=\mathrm{pixels},\mathrm{width}=100\right)$

 $99$ $91$ $30$ $63$ $22$ $89$ $55$ $86$ $77$ $76$ $82$ $49$

 > $A:=\left[\mathrm{plot}\left(\frac{\mathrm{sin}\left(x\right)}{x}\right),{\sum }_{i=1}^{N}\phantom{\rule[-0.0ex]{5.0px}{0.0ex}}f\left(i\right),\mathrm{plot3d}\left({y}^{2}\mathrm{sin}\left(x\right),x=-\mathrm{π}..\mathrm{π},y=-2..2\right)\right]:$
 > $\mathrm{DocumentTools}:-\mathrm{Tabulate}\left(A,\mathrm{width}=60\right)$

 ${\sum }_{i=1}^{N}f\left(i\right)$

 > $\mathrm{DocumentTools}:-\mathrm{Tabulate}\left(\left[\left[\mathrm{plot3d}\left({y}^{2}\mathrm{sin}\left(x\right),x=-\mathrm{π}..\mathrm{π},y=-2..2,\mathrm{axes}=\mathrm{none}\right)\right]\right],\mathrm{fillcolor}="black",\mathrm{width}=40\right)$