Examples
Command Cache
This example shows how to use a private database to store
Mathematica commands and query the data from Java and
Mathematica.
If you find that the examples in this section do not work as shown, you may need to install or restore the example database with the
DatabaseExamples` package, as described in
"Using the Example Databases".
The example code is loaded from the
Examples subdirectory of
DatabaseLink.
The command cache allows you to store
Mathematica expressions as typeset box expressions data in a database.
StoreCommand is used to store the boxes in the database.
| Out[2]= |  |
The data can then be retrieved from the database using
CommandCache[].
| Out[3]= |  |
Several attributes of each command are stored in the database. Each command is given an ID, generated when it is stored. The expression is stored as a string formatted with
FullForm; this allows it to be reused in
Mathematica. In addition, an image of the expression is saved as well as the time at which the data was stored.
GetCommandAttributes can be used to get the attribute names. These can be used to filter the data returned.
| Out[4]= |  |
| Out[5]= |  |
The following example finds all results that contain
Power.
| Out[6]= |  |
| Out[7]= |  |
| Out[8]= |  |
A command can also be retrieved using its ID.
| Out[9]= |  |
Another feature of this package is a Java GUI you can use to browse the data. It provides functionality for managing the data and pasting the data into a notebook.
| Out[10]= |  |
The GUI is automatically updated when new commands are added to the database.
Likewise, the GUI is automatically updated when commands are removed.
ClearCommandCache can be used to remove all the data in the command cache.
| Out[12]= |  |
Graph Database
This example shows a database that stores material generated by
Mathematica. Here the material involves graphs and a number of properties of these graphs. Even though the number of graphs is not extremely large, generating the properties of these graphs can take a significant amount of time. This demonstrates the value of a database for persistent storage of the results of computations.
Using the Graph Database
To use the graph database you need to load the package.
The first time the package is used you will need to run the
RestoreGraphDatabase command. If you find that the examples in this section do not work as shown, you should also run this command. This command can take a long time to run, but is only necessary once.
The properties of the graphs stored in the database are given by the
GraphProperties function.
| Out[2]= |  |
Now, you can make a query from the database. This is done with the
GraphQuery command. The following returns all complete graphs.
| Out[3]= |  |
The format used for the graphs is that provided by the
Combinatorica package. This draws a picture of the graph and also returns a symbolic object that could be used for further computation by
Mathematica.
The following returns the first three regular graphs.
| Out[4]= |  |
The following finds Hamiltonian graphs, returning their diameter, girth, and edge information. As before, a picture of the graph is also drawn.
| Out[5]= |  |
One important aspect of this example package is that it shows commands that are specific to the issue of finding graphs rather than general database commands. The details of the database interactions are all placed in the implementation.
The Graph Database Package
The graph database package provides three functions.
| GraphProperties[] | list all graph properties within the database |
| GraphQuery[{props}] | search the database for graphs that match props |
| RestoreGraphDatabase[] | restore the data in the database (can be slow) |
Graph database package functions.
The
RestoreGraphDatabase function recomputes all the data in the database and can take some time to complete. Generally you do not want to run this, unless you have corrupted the database in some way.