Skip to content
Web

Using AmiScript

AmiScript is 3forge's inbuilt scripting language incorporating both Java and SQL-like syntax. AmiScript allows developers to design and build dashboards for complex data handling using familiar syntax.

This page details several tools for AmiScript to streamline development.

Custom Methods

Custom Methods are user-defined methods that can be used anywhere throughout a layout. This is useful for maintaining readable code and for repeating logic.

To view and create your custom methods, go to Dashboard -> Custom Methods. An example is also listed below.

Note

  1. Each method should be contained in its own nested block ({}).
  2. Each method must have a ; after its definition.
  3. Methods can be overloaded -- the same method name can be used if it has a different input definition.
  4. AMIDB methods cannot be called from Web. They are distinct from one another.

Example

{
  int sum(int a, int b) {
    return a + b;
  };
  double sum(double a, double b) {
    return a + b;
  };
  List flatten(List input) {
    List flat = new List();
    for (List l: input) {
      for (Object x: l) {
        flat.add(x);
      }
    }
    return flat;
  };
}

This custom method block declares three methods:

  1. sum()

    • summing two integers
  2. sum()

    • an overloaded sum that takes two doubles
  3. flatten()

    • flattening a list

Pasting them into 3forge's Custom Methods window will look like this:

Then, in any window that takes AmiScript, call the methods as you would any standard AmiScript method:

Debugging

Any portion of AmiScript can be debugged using breakpoints:

  1. Add a new breakpoint by clicking on the line number of the line you would like to debug.

  2. Enter Debug Mode by pressing the "test" button.

  3. Action once your breakpoint has been hit, use the Debugger tab at the bottom of the screen to understand the state of your code, when done press "continue" to advance to the next breakpoint

Embedding Python

3forge makes use of Jython to use Python libraries within 3forge.

Note

This functionality is not widely available. If you require this for your use case, we strongly recommend contacting us at support@3forge.com first.

Loading Python Modules in AmiScript

To import Python modules, use an extern block:

1
2
3
4
5
6
7
8
9
{
  double n = 0;
  extern python {{
    import math
    n = math.pow(2,3)
  }}; 

  session.log(n);
}

You can then use standard Python syntax in the extern block.

By default 3forge looks for Python Libraries in the amione/lib directory of your 3forge installation.

Alternatively, you can also set the JYTHONPATH environment variable and 3forge will load libraries from there.

Using Python in AmiScript

Below is an example of how you can pass in variables and tables and modify and return them using Python:

{
 CREATE TABLE inTable AS USE ds=AMI EXECUTE SELECT * FROM __TABLE; 

 //INPUT VARIABLES
 String findEngine="TEXT";
 Table inTable=select * from inTable;
 //OUTPUT VARIABLES
 int foundCount;
 Table outTable=new Table("out","Name String"); 

 //Loop through each row of inTable and add TableName to outTable for records that have a PersistEngine equal to findEngine Variable
 //foundCount is incremented for each record added to outTable
 extern python{{
   foundCount = 0
   for row in inTable.getRows():
     if(row.get("PersistEngine") == "TEXT"):
       outTable.addRow(row.get("TableName"))
       foundCount+=1
 }}; 

 CREATE TABLE outTable=outTable;
 session.log("found: "+foundCount);
}