Kotlin Listings

This chapter contains Kotlin code listings that are equivalent to the Groovy listings presented in the previous chapters. Please note that not all Groovy listings have a corresponding Kotlin equivalent in this chapter, as some could not be automatically transpiled due to language-specific features or transpilation limitations.

Communication API

scriptTask("taskName") {
  code {
    // ICommunicationApi is available as "communication" property
    var communication = domain.communication
  }
}
scriptTask("taskName") {
  code {
    domain.communication {
      // ICommunicationApi is available inside this Closure
    }
  }
}

Diagnostics API

scriptTask("taskName") {
  code {
    // IDiagnosticsApi is available as "diagnostics" property
    var diagnostics = domain.diagnostics
  }
}
scriptTask("taskName") {
  code {
    domain.diagnostics {
      // IDiagnosticsApi is available here
    }
  }
}

Mode Management API

scriptTask("taskName") {
  code {
    // IModeManagementApi is available as "modeManagement" property
    var modeManagement = domain.modeManagement
  }
}
scriptTask("taskName") {
  code {
    domain.modeManagement {
      // IModeManagementApi is available inside this Closure
    }
  }
}

Runtime System API

scriptTask("taskName") {
  code {
    // IRuntimeSystemApi is available as "runtimeSystem" property
    var runtimeSystem = domain.runtimeSystem
  }
}
scriptTask("taskName") {
  code {
    domain.runtimeSystem {
      // IRuntimeSystemApi is available inside this Closure
    }
  }
}
scriptTask("createPortTerminator", DV_PROJECT){
  code {
    transaction {
      domain.runtimeSystem {
        // select the component port via the simple API and call terminate()
        // this API is more strict and throws an exception if the selected port cannot be terminated
        var terminatedPort = componentPort("COMPOSITIONTYPE", "rDelegationSRPort1").terminate()

        scriptLogger.info("Terminated component port '{0}'.", terminatedPort.getName())
      }
    }
  }
}
scriptTask("removePortTerminator", DV_PROJECT){
  code {
    transaction {
      domain.runtimeSystem {
        // select the component port via the simple API and call terminate()
        // this API is more strict and throws an exception if the selected port has no port terminator to remove
        var removedTerminatorPort = componentPort("App1_1", "pSRPort1").removePortTerminator()

        scriptLogger.info("Terminated component port '{0}'.", removedTerminatorPort.getName())
      }
    }
  }
}

Domain API

scriptTask("taskName") {
  code {
    // IDomainApi is available as "domain" property
    var domainApi = domain
  }
}
scriptTask("taskName") {
  code {
    domain {
      // IDomainApi is available inside this Closure
    }
  }
}

Project Handling API

scriptTask("taskName") {
  code {
    // IProjectHandlingApi is available as "projects" property
    var projectHandlingApi = projects
  }
}
scriptTask("taskName") {
  code {
    projects {
      // IProjectHandlingApi is available inside this Closure
    }
  }
}

Script Task Creation

scriptTask("TaskName"){
  code{
    // Task execution code here
  }
}
scriptTask("TaskName"){
  taskDescription("The description of the task")

  code { }
}
scriptTask("TaskName", DV_APPLICATION){
  code{
    // Task execution code here
  }
}
scriptTask("TaskName", DV_PROJECT){
  code{
    // Task execution code here
  }
}

Script Logging

scriptTask("TaskName"){
  code{
    // Use the scriptLogger to log messages
    scriptLogger.info("My script is running")
    scriptLogger.warn("My Warning")
    scriptLogger.error("My Error")
    scriptLogger.debug("My debug message")
    scriptLogger.trace("My trace message")

    // Also log an Exception as second argument
    scriptLogger.error("My Error", RuntimeException("MyException"))
  }
}
scriptTask("TaskName"){
  code{ argument ->
    // Use the format methods to insert data
    scriptLogger.info("My script {0} with:{1}", scriptTask, argument)
  }
}

Transactions

scriptTask("TaskName", DV_PROJECT){
  code {
    // Switch to the transactions API
    transactions {
      // Check if a transaction is running
      transaction {
        // Now a transaction is running
      }
    }
    // Or the short form
    transactions.isTransactionRunning()
  }
}

Model Synchronization

// Execute the model synchronization
modelSynchronization.synchronize()

// Or more elaborated, but means the same
modelSynchronization {
  if (synchronizationRequired) {
    synchronize()
  }
}

Unresolved Reference API

scriptTask("AccessAsPropertyTask") {
  code {
    // IUnresolvedReferenceApi is available as unresolvedReferences property
    var unresolvedReferencesApi = unresolvedReferences
  }
}
scriptTask("AccessLikeScopeTask") {
  code {
    unresolvedReferences {
      // IUnresolvedReferenceApi is available inside this Closure
    }
  }
}