Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Methods

At a high-level, methods are pieces of code that take in arguments or parameters and can return other objectsnew values. To define a method on a C3.ai Type, use the following syntax, method name followed by a colon ':' followed by a function signature, (e.g., function() or function(wattage: !decimal, bulbType: !string)), followed by a colon ':', followed by a return C3.ai Type return parameter, along with an implementation language and runtime execution execution location

Function Signatures

Instead of a Type name following the name as with fields, methods have a function signature. the syntax looks likeHere's the syntax for a method's function signature:

Code Block
function(parName1: [!] parType1, parName2: [!] parType2):

we We see the keyword 'function' followed by a comma-separated list of argument definitions. An argument definition consists of a parameter input parameters. To define an input parameter, use the following syntax - argument name followed by a colon ':', followed by a Type name. Optionallyby argument type (e.g., bulbID: string). Optionally, you can also include add an exclamation point '!' before the parameter argument type to indicate that that the argument is required.

Functions can be implemented in JavaScript, Python, or R. 

 input parameters, output parameters, implementation language, and execution location.

Return Types

After the function signature we have a return type. This must be a C3 type. The function must return an object of the appropriate type in the .

Language/Environment Specification

Finally, we end with a language and runtime execution. 

Language Specifications

The function itself is NOT implemented in the .c3typ file; rather in a separate .js (JavaScript), .py (Python) or .r (R) files stored in the same directory as the .c3type file. 

Return Parameter

Following the function signature is a return parameter. Return parameters must be a C3.ai Type. 

Implementation Language/Execution Location

Finally, let's discuss the implementation language and execution location.

Language Specifications

Methods can be implemented in JavaScript, Python, or R.

  • py: Use the 'py' keyword to indicate this is a Python functionmethod.
  • js: Use the 'js' keyword to indicate this is a JavaScript functionmethod.
  • r: Use the 'r' keyword to indicate this is an R method.

CurrentlyCurrently, these are the only two languages which three programming languages natively supported by the C3 AI Suite supports for native method definitions

Runtime Execution

Then, this is followed by an environment specification. for JavaScript the only two environments are:

to define methods. 

Execution Location

Methods can be executed in three locations:        

  • server: Indicates the method will run on server: This JavaScript function is executed within the C3 AI Suite by a worker node.
  • client: This JavaScript function is executed at the client browser.
  • Server should be used for methods, requiring high processing power (e.g., methods that fetch or create large amounts of data)
  • client: Indicates the method will run on the client's browser.
  • all: Indicates the method will run wherever it is called from. If the call comes from the browser, the function will execute in the browser. If the call comes from a server, the execution will happen in the server.

Please note, R and Python methods can only be executed on the server. Javascript methods can be executed in either the server or client.

Additionally, for For Python, we need to specify a valid 'ActionRuntime', for the python function to be executed in. ( ActionRuntimes are defined within the in C3 AI Suite, and new ones ActionRuntimes can be defined as part of in a C3.ai Package. They , and are essentially conda environments. ) For example, 'server' will use the 'py-server' ActionRuntime.

Function Definitions

FinallyAs mentioned above, the function definition goes itself is defined in a different file from the Type definition .c3typ file. It should have The name of this file should be the same base name as the .c3typ definition file, where the method is defined (e.g., SmartBulb.js and SmartBulb.c3typ), but have an extension appropriate for the language of implementation a different extension, per the function's implementation language (e.g., 'py' for Python, 'js' for JavaScript, 'r' for R). Within, the file must contain functions whose name matches the method name from the Type definition exactlyThe function names in this file must exactly match the method names in the .c3typ file. For example, if we have define a Type defined like so:C3.ai Type as suuch: 

Code Block
entity type NewType {
	field: double
	funcA: function(num: !int) : float js server
    funcB: function(num: !int, name: string) : double py server
}

This type C3.ai Type will be defined in a file named called 'NewType.c3typ',  There will also have to be . We also need two other files, 'NewType.js' and 'NewType.py', in the same directory as 'NewType.c3typ'. Here's a peek inside:

'NewType.js'

...