Page tree

Versions Compared

Key

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

...

With a Canonical Type defined to receive new data into the C3 AI Suite, we need to move this data into the data model. Transform types define this operation. A Transform type firstly, mixes the destination type, and then uses the `transforms` keyword followed by the source canonical type to indicate where it should take data from. Secondly, Transform types support a special syntax for their fields. This syntax defines an expression for each field which takes data from the source type, and produces a result to be stored in the target type in the given field. Let's look at an example and discuss some of the syntax.

Code Block
/*
 * Copyright 2009-2020 C3 (www.c3.ai). All Rights Reserved.
 * This material, including without limitation any software, is the confidential trade secret and proprietary
 * information of C3 and its licensors. Reproduction, use and/or distribution of this material in any form is
 * strictly prohibited except as set forth in a written license agreement with C3 and/or its authorized distributors.
 * This material may be covered by one or more patents or pending patent applications.
 */

/**
 * This type encapsulates the data flow from the {@link CanonicalSmartBulb} to the {@link SmartBulb} type.
 */
type TransformCanonicalSmartBulbToSmartBulb mixes SmartBulb transforms CanonicalSmartBulb {

  id:              ~ expression "SN"
  manufacturer:    ~ expression {id: "Manufacturer"}
  bulbType:        ~ expression "BulbType"
  wattage:         ~ expression "Wattage"
  startDate:       ~ expression "StartDate"
  latitude:        ~ expression "Latitude"
  longitude:       ~ expression "Longitude"
  lumensUOM:       ~ expression "{ \"id\": \"'lumen'\" }"
  powerUOM:        ~ expression "{ \"id\": \"'watt'\" }"
  temperatureUOM:  ~ expression "{ \"id\": \"'degrees_fahrenheit'\" }"
  voltageUOM:      ~ expression "{ \"id\": \"'volt'\" }"

}

There are a few different types of fields you might see in a Transform type some of which are visible here. Let's go through them

1. Constant

Constant fields are not present in the current example, but they would appear without the '~ expression' notation. For example, it might look like this:

Code Block
id: "ID"
value: 1.5

In this case, the field is simply set to the constant value listed.

2. Copy Property of Canonical Type

Many transforms are simply copying the value to the right field in the destination type. This is done with the syntax '~ expression "<origin_field>"', Let's take a look at the first field, 'id':

Code Block
id: ~ expression "SN"

Here, the `~` means the result of the expression is stored at the field. This is true all of the fields here. Then the expression is evaluated in the context of the source type. So in this case, the property SN is evaluated and copied to the 'id' field.

3. Fetch field Type via a key

We often want to link our destination types to other types which are related. In this example, the SmartBulbtype contains a field 'manufacturer' which is of type Manufacturer. How can we grab the correct Manufacturer? This is done with another expression, this time using the '{}' notation. Let's have a look:

Code Block
manufacturer: ~ expression {id: "Manufacturer"}

This means, we fetch the Manufacturer by the 'id' key using the value in the expression "Manufacturer" which in this case is the value of the "Manufacturer" field in the source type for the transform. So the Manufacturer field from the source type is used to find the Manufacturer Type with id matching, and this is set as the value of the 'manufacturer' field of the destination type.

Finally, we can do the same but use a constant as the key. for example:

Code Block
lumensUOM: ~ expression "{ \"id\": \"'lumen'\" }"

This can also be written

Code Block
lumensUOM: ~ expression { id: "'lumen'" }

Here, the expression is evaluated with a constant key. This means the Unit Type with id 'lumen' will always be used for the lumensUOM field of the destination type.

C3.ai resources on Canonical Transform Types

Application Types

Basic Data Sources

...