The following will explain the power of ROB-EX Scripting by showing how a simple script is created and executed.

Overview

The Scripting option allows the full ROB-EX API to be called and manipulated through the Groovy Scripting language. The Groovy language is a powerful and easy to use scripting language supporting both all standard Java features as well as unique power features well suited for scripting.

Scripting in ROB-EX Includes

  • an online Scripting console (menu “Functions->Script Console”) with syntax highlighting for development and evaluation of scripts, i.e. write a script on the fly and evaluate it without restarting ROB-EX
  • a macro command ExecuteScript that will allow a script to be executed in a normal macro
  • the ability to automatically load and execute scripts during startup of the client

Scripting in ROB-EX requires a valid license for the ROB-EX API Developers Kit. With a valid license the menu “Functions->Script console” is enabled.

A small tutorial

Selecting menu item “Functions->Script console” will open up the GroovyConsole.

With a ROB-EX test plan loaded into the client (for testing it is recommended to logout from a possible Multiuser server) try to copy/paste the following code into the script console (the upper part of the GroovyConsole window).

Example 1

import org.joda.time.DateTime;
import gantt.plugin.BaseModelAPI;

print "Hello World!"

def bAPI = pluginAPI.getBaseModelAPI()

// find all operations with start before now and update their start date to NOW

bAPI.getOperationList().findAll{o -> o.getStart().isBeforeNow() && o.type==-2 && o.getProductionOrder() != null && o.getResource() != null}
  .each{o -> o.setStart(new DateTime())}

Except for the import statements in the top, this script only consists of a single line that iterates through all operations starting before now and which has a type of -2. For each of these operations the current start time is set to the current date and time.

The variable pluginAPI is a variable that is passed into the script from the ROB-EX client, i.e. it is always there without it having to be defined anywhere. The pluginAPI variable is of type gantt.plugin.PluginAPI, and will allow the script to query and manage basically any part of the ROB-EX client.

Before executing the script press the “View” menu and use the following recommended settings:

To run the script select menu “Script->Run” or press Ctrl+R as a shortcut. The result in your ROB-EX plan is that all operations in the plan that previously had a starting date prior to NOW, starts at time NOW.

It is best practice to place groovy scripts in the “custom” directory, alongside with other customer specific configurations.

Example 2

The example demonstrates how to get currently selected operations and production orders from the Gantt chart.

import org.joda.time.DateTime
import gantt.basemodel.common.ProductionOrder
import gantt.basemodel.common.Operation
import gantt.plugin.BaseModelAPI
import gantt.plugin.PluginAPI
import gantt.plugin.WindowAPI
import groovy.transform.Field

// The @Field annotation will make the field accessible in both closures and methods
@Field def PluginAPI pApi = pluginAPI
@Field def WindowAPI wApi = pApi.getWindowAPI()
@Field def BaseModelAPI bApi = pApi.getBaseModelAPI()

// based on selected operations, create a unique sorted list of production orders
def uniqueOrderList = wApi.getSelectedOperations()
 .collect(new HashSet()) { opr -> opr.getProductionOrder() }
 // The <=> (the UFO operator) refers to the compareTo method of the Comparable interface
 .sort { ProductionOrder a, ProductionOrder b -> a.getName() <=> b.getName() }

uniqueOrderList
 .each { po -> println "order=$po" }

// print information about selected operations with a start time after time now
wApi.getSelectedOperations()
 .findAll { opr -> opr.getStart().isAfter(DateTime.now()) }
 .each { opr -> println "selected after now opr=" + opr.toDebug() + " assigned to resource=$opr.selectedResource" }

// demo of the two different ways to declare and use methods/closures 
def v1 = 3
def v2 = 6

// this is a closure - declared as a variable
def sumMethod1 = { int a, int b ->
  println "m1 bApi=$bApi"

  return a + b
}

// this is a regular Java method
int sumMethod2(int a, int b) {
  println "m2 bApi=$bApi"

  return a + b;
}

println "sum 1=" + sumMethod1(v1, v2)
println "sum 2=" + sumMethod2(v1, v2)

println "script is finished"

Automatically loading scripts during client startup

From ROB-EX 4.2 and forward scripts can be automatically loaded and executed once, which is useful for scripts that register themselves as call back listeners (i.e. you need to ensure that they will only register a single call back listener). A script is automatically loaded when they are placed in the following directory of the client.

<sch_dir>\custom\scripts\beforePlugins

Groovy script files placed in this directory will be loaded and executed before the user plugin is loaded. Scripts are loaded in alphabetical order, so use a naming convention like “01_script.groovy”, “02_script.groovy” etc. to control in which order scripts are loaded.
This option is useful in case the script configures or initializes resources that the user plugin depends on (as an example a connection to a database or some other data source).

<sch_dir>\custom\scripts\afterPlugins

Groovy script files placed in this directory will be loaded and executed after the user plugin has been loaded and initialized but before the plan has been loaded. Scripts are loaded in alphabetical order, so use a naming convention like “01_script.groovy”, “02_script.groovy” etc. to control in which order scripts are loaded.
This option is useful for situations where the script depends on some initialization having taken place in a user plugin.

To see an example of a script being loaded during startup see the “Oven Scheduling” example in <sch_dir>\data\scripts\scheduling\ovenScheduling. This script demonstrates both how to create a macro from a script loaded during startup and it also shows how to override the standard scheduling rules in order to support scheduling of oven type resources.

More scripting examples

A number of scripting examples demonstrating how to use scripting for data cleanup, interfacing via SQL, Excel etc. are included as part of the standard Client installation. The examples are available in the <sch_client_dir>\data\scripts directory.

Feedback

Was this helpful?

Yes No
You indicated this topic was not helpful to you ...
Could you please leave a comment telling us why? Thank you!
Thanks for your feedback.

Post your comment on this topic.

Post Comment