Skip to content

Latest commit

 

History

History
646 lines (445 loc) · 16.5 KB

File metadata and controls

646 lines (445 loc) · 16.5 KB

← README
← ACMEScript

ACMEScript - Macros and Variables

Type Macro / Variable Description
Basic argc Get number of arguments
argv Get script or procedure arguments
b64encode Encode a string as base64
datetime Get current date and time
in Test whether a string exists in another string
isDefined Test whether a variable, macro, or environment variable exists
jsonify Prepare a string for proper use in a JSON structure
loop Get the current while loop's loop count
lower Get a lower-case version of the provided string argument
match Evaluate an argument against a simple regular expression
nl Insert a newline character ]
random Generate a random number
result Get the last result of a while, procedure etc.
round Round a float number
runCount Get the number of script runs.
upper Get an upper-case version of the provided string argument
urlencode URL-encode a string
Storage storageGet Get a value from the persistent key/value storage
storageHas Test the existence of a key in the persistent key/value storage
oneM2M attribute Get the value of an attribute from a oneM2M resource
hasAttribute Test the existence of an attribute from a oneM2M resource
notification.originator Get a notification's originator
notification.resource Get a notification's resource
notification.uri Get a notification's URI
request.originator Get the assigned originator used in requests
response.resource Get the resource of the last oneM2M request
response.status Get the status of the last oneM2M request
CSE isIPython Check whether the runtime environment is IPython, e.g. Jupyter Notebook
cseStatus Get the current CSE runtime status
<any CSE configuration> Get the value of any of the CSE's configuration settings

The following builtin macros and variables are available.

Basic

argc

Usage: [argc]

Evaluates to the number of arguments to the script or the current scope.

Example:

if [> [argc] 2]
	logError Wrong number of arguments
	error
endif

argv

Usage:
[argv <n:integer>*]

Evaluates to the n-th argument to the script or a procedure. The index starts a 1, because the 0-th argument is the name of the script or the procedure. If the argv macro is called without an argument then the original string of arguments is returned (but without the script name).

Example:

print The name of the script is: [argv 0]
print The first argument is: [argv 1]
print All arguments: [argv]

b64encode

Usage:
[b64encode <string>]

Encode and return the argument as a base64 encoded string.

Example:

print [b64encode Hello, world]
# -> SGVsbG8sIHdvcmxk

datetime

Usage:
[datetime <format pattern>* ]

Evaluates to a UTC-based date/time string. With the optional format pattern one can specify the output format. The format is the same as the Python strftime() function. See https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior for further details.

The default is %Y%m%dT%H%M%S.%f, which evaluates to an ISO8901 timestamp.

Example:

print [datetime]
# -> 20220107T221625.771604
print [datetime %H:%M]
# -> 22:26

in

Usage:
[in <text> <string>]

Check whether a text can be found in a string. The macro returns true or false accordingly.

Example:

print [in Hello "Hello World"]

isDefined

Usage:
[isDefined <name> ]

Test whether a variable, macro, or environment variable exists.

Example:

if [isDefined response.status]
	print [response.status]
endif

jsonify

Usage:
[jsonify <string> ]

Prepare a string for proper use in a JSON structure (ie. escape newlines and quote characters).

Example:

requestAttributes 
	{	"sqi": "true",
		"rcn": 10,
		"fc/smf": "[jsonify [aSPARQLQuery]]"
	}

loop

Usage:
[loop]

Return the current while loop's loop count. This counter is automatically incremented by 1 for every iteration of a while loop. It starts at 0 when the while loop is entered.

Every (nested) while loop has its own loop counter.

It is not defined outside a while loop.

Example:

while [< [loop] 10]
	print [loop]
endwhile
print [loop]
# -> yields an error because "loop" is not defined outside a while loop

lower

Usage:
[lower <text:string>]

Return a lower-case version of the provided string text.

Example:

print [upper Hello]
# -> hello

match

Usage:
[match <text:string> <regex>]

Match the text against the regular expression regex, and return the result as true or false.

The regular expression format is a very simplified version that only supports the following expression operators:

  • ? : any single character
  • * : zero or more characters
  • + : one or more characters
  • \ : Escape an expression operator

Examples:

"hello" - "h?llo" -> true
"hello" - "h?lo" -> false
"hello" - "h*lo" -> true
"hello" - "h*" -> true
"hello" - "*lo" -> true
"hello" - "*l?" -> true

Example:

if [match hello h*o[]
	print found
endif
# -> hello

nl

Usage:
print Hello[nl]World

Insert a newline character. This can be used to produce multi-line strings.

Example:

print Hello[nl]World
# -> Hello
#    World

random

Usage:
[random]
[random <end> ]
[random <start> <end> ]

Generate a random number. If no argument is given then a random number in the range [0.0, 1.0] will be generated.

If only one argument is given then this argument is treated as the end of the range [0.0, <end>].

If two argument are given then these arguments are treated as the start and end of the range [<start>, <end>].

Example:

print [random]
# -> random number in the range [0.0, 1.0] 
print [random 10]
# -> random number in the range [0.0, 10.0] 
print [random -5 5]
# -> random number in the range [-5.0, 5.0]

result

Usage:
[result]

Evaluates to the result of the last scope, or nothing. See also Context, Scopes, Arguments, and Results.

Example:

# Define a procedure that only returns the string "nothing"
procedure getNothing
endprocedure nothing

# Call the procedure and print the result afterwards
getNothing
print [result]
# -> nothing

round

Usage:
[round <number:float> [ < ndigits> ] ]

Get a number rounded to ndigits precision after the decimal point. If ndigits is omitted, then this macro returns the nearest integer. ndigits may be negative.

Example:

print [round 1.6]
# -> 2
print [round 1.678]
# -> 1.68
print [round 2361.678 -2]
# -> 2400.0

runCount

Usage:
[runCount]

Evaluates to the number of runs of the script.

Example:

print [runCount]
# -> 42

round

Usage:
[round <number:float> [ < ndigits> ] ]

Get a number rounded to ndigits precision after the decimal point. If ndigits is omitted, then this macro returns the nearest integer. ndigits may be negative.

Example:

print [round 1.6]
# -> 2
print [round 1.678]
# -> 1.68
print [round 2361.678 -2]
# -> 2400.0

upper

Usage:
[upper <text:string>]

Return a upper-case version of the provided string text.

Example:

print [upper hello]
# -> HELLO

urlencode

Usage:
[urlencode <string> ]

URL-encode a string.

Example:

print [urlencode Hello World]
# -> Hello+World

Storage

These macros help to access key/values in the persistent storage.

storageGet

Usage:
[storageGet <key:string>]

Evaluates to the value stored for the key key that has previously been stored with the storagePut command in the persistent storage. If the key does not exist then the script is terminated with an error.

Example:

storagePut aKey aValue
print [storageGet aKey]
# -> aValue

storageHas

Usage [storageHas <key:string>]

Evaluates to the string true or false, depending whether the provided key exists in the persistent storage.

Example:

storagePut aKey aValue
if [storageHas aKey]
	print [storageGet aKey]
endif
# -> aValue

oneM2M

attribute

Usage:
[attribute <key:pattern> <resource:JSON>]

This macro finds a structured key in the JSON structure resource and returns its value. This could be a single value or a JSON structure. If key does not exists or could not be found then the script terminates with an error.

key can be the name of a JSON element, or one of the following pattern elements

  • It is possible to address a specific element in an array. This is done by specifying the element as {n}.

    Example:
print [attribute m2m:cin/{1}/lbl/{0} [response.resource]]
  • If an element is specified as {} then all elements in that array are returned in an array.

    Example:
print [attribute m2m:cin/{1}/lbl/{} [response.resource]]
  • If an element is specified as {_} and is targeting a dictionary then a single random path is chosen. This can be used to skip, for example, unknown first elements in a structure.

    Example:
print [attribute {_}/rn [response.resource]]

hasAttribute

Usage:
[hasAttribute <key:pattern> <resource:JSON>]

This macro checks whether an attribute exists in a JSON structure. It evaluates to true or false, respectively.

See the the description of the attribute macro for an explanation of the key pattern.

notification.originator

Usage:
[notification.originator≤6

Get a notification's originator. This variable is only set in a script that is a notification target.

Example:

print [notification.originator]
# -> ... the notification's originator ...

notification.resource

Usage:
[notification.resource]

Get a notification's resource. This variable is only set in a script that is a notification target.

Example:

printJSON [notification.resource]
# -> ... the notification's resource ...

notification.uri

Usage:
[notification.uri]

Get a notification's target URI. This variable is only set in a script that is a notification target.

Example:

print [notification.uri]
# -> ... the notification's URI ...

response.resource

Usage:
[response.resource]

Evaluates to the resource returned by the last oneM2M request, the debug message, or nothing.

Example:

retrieve cse-in
print [response.resource]
# -> ... the retrieved resource ...

request.originator

Usage:
[request.originator]

Evaluates to the assigned originator for all following oneM2M requests. See also the command ORIGINATOR.

Example:

print [request.originator]
# -> ... the originator ...

response.status

Usage:
[response.status]

Evaluates to the response status code returned by the last oneM2M request, or nothing.

Example:

retrieve cse-in
print [response.status]
# -> ... the request's status code ...

CSE

cseStatus

Usage:
[cseStatus]

Return the CSE's runtime status. This is one of the following values:

  • STOPPED
  • STARTING
  • RUNNING
  • STOPPING
  • RESETTING

Example:

# Only reset when CSE is running
if [== [cseStatus] RUNNING]
	reset
endif

isIPython

Usage:
[isIPython]

This macro evaluates to true if the CSE is currently running in an IPython environment, such as Jupyter Notebooks, or to false otherwise.

Example:

if [isIPython]
	print Running in IPython
endif

Configuration Settings

Usage:
[<CSE configuration>]

Any of the CSE's configuration settings that are defined in Configuration can be used as a variable. It evaluates to the respective configuration value.

Some of the configuration settings can also be set to a new value.
Attention: Assigning wrong values to configuration settings can do a lot of harm to the CSE and the stored data, even render it non-functional.
Also note, that not all configuration changes may have an immediate effect, but will stay in effect after the script terminated.

Example:

print [cse.type]
# -> IN
print [logging.level]
# -> DEBUG

← ACMEScript
← README