Skip to main content

Define a variant datastream

In this tutorial, you will define a VaraintType data source. You will

  • define a writable VaraintType data source, and
  • launch a solution, interact with and validate your datastream.

This lesson will assume that you have an empty project and asset which you can to deploy to a workspace named 06_04_02_define_a_varrianttype with the following command:

edk template deploy -ycw 06_04_02_define_a_varianttype

The variant type

In this tutorial we are going to consider the variant type used to represent a geometric object described in the

previous tutorial. There are three cases describing three kinds of geometry: points, circles and rectangles.

const GeometryType = VariantType({
point: NullType,
circle: FloatType,
rectangle: StructType({
width: FloatType,
height: FloatType,
}),
})

Each geometry has some associated data describing its dimensions (the size of the shape). A point does not have any dimensions, so we NullType to represent the absence of data. A circle is described by a radius, of type FloatType. A rectangle has both a width and height. We use a StructType in order to store multiple pieces of data.

Preparation

Create a directory in the project root called data, and within the directory place a file (called point.json) to write into the data stream. Place the following content into the file:

{
"type": "point",
"value": null
}

Define a writable VariantType data source

To define a writable VariantType data source you will perform the following steps:

  1. define a datasource using a similar pattern as your previous My Source definition.
  2. set the type of the data source to GeometryType using writable.
  3. add the datasource to a template

In an asset, perform the above steps to create the resulting Typescript code:

import { VariantType, FloatType, NullType, SourceBuilder, StructType, Template } from "@elaraai/core"

const GeometryType = VariantType({
point: NullType,
circle: FloatType,
rectangle: StructType({
width: FloatType,
height: FloatType,
}),
})

const my_source = new SourceBuilder("My Source")
.writeable(GeometryType);

export default Template(my_source)

Observe the resulting VariantType datastream

Write the contents of data/point.json to your data stream with the following command:

edk stream replace "Writeable.My Source" --file data/point.json -w 06_04_02_define_a_varianttype

You can validate to contents of the data stream by using the edk stream get command:

edk stream get "Writeable.My Source" -w 06_04_02_define_a_varianttype

Which will result in the contents of point.json in std out:

▹▹▹▹▹ Attempting to stream Writeable.My Source to stdout
{"type":"point","value":null}
✔ Download complete

Writing circles

Now make a new file called data/circle.json, with the contents:

{
"type": "circle",
"value": 10
}

The "value" field holds the associated data, in this cases the radius of the circle.

Write the contents of data/circle.json to your data stream with the following command:

edk stream replace "Writeable.My Source" --file data/circle.json -w 06_04_02_define_a_varianttype

Now validate to contents of the data stream by using the edk stream get command:

edk stream get "Writeable.My Source" -w 06_04_02_define_a_varianttype

Which results in:

▹▹▹▹▹ Attempting to stream Writeable.My Source to stdout
{"type":"circle","value":10}
✔ Download complete

Writing rectangles

Now make a new file called data/rectangle.json, with the contents:

{
"type": "rectangle",
"value": { "width": 5, "height": 4 }
}

Note how the "value" field now contains a struct (JSON object) with "width" and "height" fields, matching this case for GeometryType.

Write the contents of data/rectangle.json to your data stream with the following command:

edk stream replace "Writeable.My Source" --file data/rectangle.json -w 06_04_02_define_a_varianttype

Now validate to contents of the data stream by using the edk stream get command:

edk stream get "Writeable.My Source" -w 06_04_02_define_a_varianttype

Yielding:

▹▹▹▹▹ Attempting to stream Writeable.My Source to stdoutpe
{"type":"rectangle","value":{"width":5,"height":4}}
✔ Download complete

Additional exercises

Suppose we wanted to add a new geometry type for a square. How would you modify the type? Optionally, make this change to your project and try to write a square to the data stream.

Example Solution

The final solution for this tutorial is available below:

Next Steps

Now that you can deploy, write and read variants, in the

next lesson you will perform calculations on a variant data stream.