...
Each method of a C3 Type is defined by first giving the 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 Type, along with a language and environment specification.
Function Signatures
Return Types
...
Instead of a Type name following the name as with fields, methods have a function signature. the syntax looks like:
Code Block |
---|
function(parName1: [!] parType1, parName2: [!] parType2): |
we see 'function' followed by a comma separated list of argument definitions. An argument definition consists of a parameter name followed by a colon ':', followed by a Type name. Optionally, you can also include an '!' before the parameter type to indicate that that argument is required.
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 environment specification.
Language Specifications
- py: Use the 'py' keyword to indicate this is a Python function.
- js: Use the 'js' keyword to indicate this is a JavaScript function.
Currently, these are the only two languages which the C3 AI Suite supports for native method definitions
Environment Specification
Then, this is followed by an environment specification. for JavaScript the only two environments are:
- 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.
For Python, we need to specify a valid 'ActionRuntime' for the python function to be executed in. (ActionRuntimes are defined within the C3 AI Suite, and new ones can be defined as part of a C3 Package. They are essentially conda environments.) For example, 'server' will use the 'py-server' ActionRuntime.
Function Definitions
Finally, the function definition goes in a different file from the Type definition .c3typ file. It should have the same base name as the .c3typ definition file, but have an extension appropriate for the language of implementation (e.g., 'py' for Python, 'js' for JavaScript). Within, the file must contain functions whose name matches the method name from the Type definition exactly. For example, if we have a Type defined like so:
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 will be in a file named 'NewType.c3typ', There will also have to be two other files, 'NewType.js' and 'NewType.py'. Here's a peek inside:
'NewType.js'
Code Block |
---|
function funcA(num) {
...
} |
'NewType.py'
Code Block |
---|
function funcB(num, name='default') {
...
} |
C3.ai Developer Resources on Methods:
- Developer Documentation
- C3.ai Academy Videos
Inheritance
Annotations
Examples
...