Adding global variables

Describes how to add a global variable.

About this task

You can use the ilog.rules.studio.javascript plug-in in conjunction with Eclipse to automate tasks on a preconfigured workspace or a folder containing the rule projects.

The following example demonstrates how to add a global variable named out to replace a fully qualified call to:

java.lang.System.out.println(<text message>);

Procedure

To add global variables:

  1. On the File menu, click New Project.
  2. Under the Plug-in Development category, select Plug-in Project and then click Next.
  3. In the project name field, enter a name and then click Next.
  4. Click Finish.
  5. Click Yes to open the Plug-in Development perspective.
  6. Click the Dependencies tab, and under Required Plug-ins click Add.
  7. Add ilog.rules.studio.eclipsemonkey to the dependencies.
  8. Click the Extension Points tab and then click Add.
  9. In the Extension Point ID and Extension Point Name fields, enter ilog.rules.studio.eclipsemonkey.dom and then click Finish.
  10. Click the plugin.xml tab and then enter the following text:
    <extension point="ilog.rules.studio.eclipsemonkey.dom">
       <dom
          class="javascriptsystem.dom.SystemOutDOMFactory"
          name="System.out"
          variableName="out"/>
    </extension>
    

    The extension declares the factory that creates the Java™ instance on which the JavaScript method calls are forwarded when used with the out variable.

Results

The plug-in Extensions tab now includes the new plug-in project:

Plug-in Extensions tab

You must now add the implementation of the factory. Factories contain a single method named getDOMroot. This method creates the Java instance of the class to which the method calls are forwarded.

The following SystemOutDOMFactory example returns System.out static instances:

package javascriptsystem.dom;

import ilog.rules.studio.eclipsemonkey.dom.IMonkeyDOMFactory;

public class SystemOutDOMFactory implements IMonkeyDOMFactory {
  public Object getDOMroot() {
    return System.out;
  }
}

By adding this global variable, you simplify each print command to the standard output. You can also create a more complex object that provides methods such as formatting text messages with parameters, as shown in the following example:

function main() {
    // instead of java.lang.System.out.println("my message");
    out.println("my message");
}