Skip to main content

General Functions

A set a non-specific helper functions

clone

Creates a new exact copy of another variable

Syntax

var = clone(other)

Example

obj = Object()
obj.name = "John"
obj.value = 23

// Clone the obj variable
clone = clone(obj)

// Check to see that it is exactly the same
assertEquals("John", clone.name)
assertEquals(23, clone.value)

// Note - this will fail as they are not the same object
assertEquals(obj, clone)

evaluate

Evaluates a string expression and returns the result

Syntax

result = evaluate(string)

Example

a=10
b=12
op="+"

result = evaluate("a" + op + "b")
print result

random

Generates a random real number between 0 and 1

Syntax

realnumber = random()

Example

val = random()

randomInteger

Generates a random integer number between 0 and the supplied number - 1

Syntax

integer = randomInteger(max)

This returns an integer between 0 and max-1

Example

// random number between 1 and 10
random = randomInteger(10) + 1

ref

Creates a reference to an active variable. This is used when you don’t want to have a static object inside another object, instead this creates a dynamic reference to an object which is fetched whenever the property is requested.

Syntax

reference = ref(var)

or

reference = ref(service, id)

Example

// Create an invoice type
invoice = type
    description as String()
    amount as Scalar()
end

// Create a customer type
customer = type
    name as String()
    invoices as List()
end

// Create a customer
john = object as customer
    name = "John Doe"
end

// Create an invoice
inv1 = object as invoice
    description = "Widget"
    amount = 12.36
end

// Add a reference to the invoice
john.invoices.add(ref(inv1))

You can also add references as variables on any dynamic variable, e.g.

sc = SmartCurve("baseCurve", "(BASE+OFFER)/2")
sc.OFFER = ref("data", "otherCurve")

variable

The variable function is used to work with dynamic variable names

Syntax

Var = variable(String)

Result

The variable that is named as the input string

Example

name="This is it"
variable(clean(name)) = TimeSeries("DAILY")
variable(clean(name)).category = "TEST"
variable(clean(name)).add("2021-05-27", 12.2)
print variable(clean(name)).get("2021-05-27").value
12.2

variables

Returns an array of Objects that gives information about all the variables in the current session.

The object contains 3 fields:

  • name - the name of the variable
  • scope - the scope of the variable, Global being the main scope
  • type - the type of the variable as returned by the typeOf function

Syntax

variables()

Example

a = "Hello"
d = Date()
print variables()
[
{
"name": "a",
"scope": "Global",
"type": "Scalar"
}
{
"name": "d",
"scope": "Global",
"type": "Date"
}
{
"name": "PROCESS",
"scope": "Global",
"type": "Process"
}
]