This a Groovy example that shows how to use the GenRocket API to build an absolute minimum Scenario from scratch and output one row to an XML file.


Activity Diagram

The activity diagram defines the steps and API calls necessary to load, modify and run the Scenario.

Example Using Native API

The following Groovy code example makes native API calls.

    

package com.genRocket.engine.examples

import com.genRocket.GenRocketException
import com.genRocket.engine.EngineAPI
import com.genRocket.engine.EngineManual
import com.genRocket.receiver.XMLFileReceiver

/**
 * Created by htaylor on 12/29/16.
 *
 * This a Groovy example that shows how to use the GenRocket API to build an
 * absolute minimum Scenario from scratch and output one row to an XML file.
 */
class ScenarioMinimum {

  public static void main(String[] args) {
    String scenario = '/home/userName/anyScenario.grs'
    String domainName = 'Minimum'

    EngineAPI api = new EngineManual()

    try {
      // Initialize Scenario
      api.scenarioLoad(scenario)
      api.scenarioClear()

      // Add Domain
      api.domainAdd(domainName)

      // Receiver must be added after Domains
      String receiverName = 'XMLFileReceiver'
      Map<String, String> parameters = [
        path: '#{resource.output.directory}',
        fileName:"${domainName}.xml"
      ]

      api.receiverAdd(domainName, XMLFileReceiver.canonicalName, receiverName, parameters)

      // Run Scenario
      api.scenarioRun()
    } catch (GenRocketException e) {
      println(e.getMessage())
    }
  }
}


Groovy Source Code Example Using REST

The following Groovy code example makes REST API calls.  To makes calls to REST, the GenRocket Realtime REST Engine needs to be started in a local terminal. This example also uses a helper method, makeRequestAndRetrieveResponse, that handles the actual HTTP request and response.


/**
 * Created by dsetia on 12/18/17.
 *
 * This a Groovy example that shows how to use the GenRocket API over REST to
 * build an absolute minimum Scenario from scratch and output one row to an XML file.
 */
class MinimumScenario {

  public static void main(String[] args) {
    String scenario = '/home/userName/anyScenario.grs'
    String domainName = 'Minimum'

    final String RESPONSE_TYPE = "OK"

    try {
      // Initialize Scenario
      Map requestParameters = [interfaceType: "Manual", methodName: "scenarioLoad", parameters: [scenario: scenario]]
      Map responseOutput = makeRequestAndRetrieveResponse(requestParameters)

      if (responseOutput.responseType == RESPONSE_TYPE) {
        makeRequestAndRetrieveResponse([interfaceType: "Manual", methodName: "scenarioClear"])

        makeRequestAndRetrieveResponse([interfaceType: "Manual", methodName: "domainAdd", parameters: [domainName: domainName]])
      } else {
        throw new GenRocketException("*** Unable to load scenario: ${scenario} ***")
      }

      // Receiver must be added after Domains
      String receiverName = 'XMLFileReceiver'
      Map<String, String> parameters = [
        path    : '#{resource.output.directory}',
        fileName: "${domainName}.xml".toString()
      ]

      makeRequestAndRetrieveResponse([interfaceType: "Manual", methodName: "receiverAdd", parameters: [
        domainName   : domainName,
        canonicalName: XMLFileReceiver.canonicalName,
        receiverName : receiverName,
        parameterMap : parameters
      ]])

      makeRequestAndRetrieveResponse([interfaceType: "Manual", methodName: "domainSetLoopCount", parameters: [domainName: domainName, loopCount: "100".toString()]])

      // Run Scenario
      makeRequestAndRetrieveResponse([interfaceType: "Manual", methodName: "scenarioRun"])
    } catch (GenRocketException e) {
      println(e.getMessage())
    }
  }