Introduction
This example shows a very simple flow element which takes a date of birth and returns an star sign. Although a basic (and not all that useful) flow element, the example demonstrates how you can start to implement your own flow elements.
Download Example
The source code used in this example is available here:
Dependencies
The flow element will need a reference to the Pipeline core package
FiftyOne.Pipeline.Core
NuGet package.
pipeline.core
Maven package from com.51degrees
. To include this, add the following to the <dependencies>
section of the project's pom.xml
:
fiftyone.pipeline.core
composer package to the composer.json, and required it in the source with include("../vendor/autoload.php");
fiftyone.pipeline.core
NPM package to the package.json, and required it in the source with require("fiftyone.pipeline.core")
. Data
The element data being added to the flow data by this flow element is a star sign. So this should have its own 'getter' in its element data.
In this example, only one value is populated. So an interface IStarSign
will extend IElementData
to add a 'getter' for it.
Now the internal implementation of it will implement this 'getter' and add a 'setter' for the flow element to use.
Java supports interfaces, so the element data should have a public interface and a package private concrete implementation with easy 'setters'.
In this example, only one value is populated. So an interface StarSignData
will extend ElementData
to add a 'getter' for it.
Now the internal implementation of it will implement this 'getter' and add a 'setter' for the flow element to use.
Note that this concrete implementation of StarSignData
sits in the same package as the flow element, not the StarSignData
interface, as it only needs to be accessible by the flow element.
elementDataDictionary
can be used. elementDataDictionary
can be used. Flow Element
Now the actual flow element needs to be implemented. For this, the flow element's base class can be used which deals with most of the logic. The element properties, evidence keys and the processing are all that need implementing.
FlowElementBase
(and partially implements IFlowElement
). This has the type arguments of IStarSignData
- the interface extending element data which will be added to the flow data, and IElementPropertyMetaData
- as we only need the standard metadata for element properties.
This needs a constructor matching the FlowElementBase
class. So it takes a logger, and an element data factory which will be used to construct an IStarSignData
:
The Init
method in this example will simply initialize a list of star signs with the start and end dates of each star sign and add each to a list of a new class named StarSign
which has the following simple implementation:
Note that the year of the start and end date are both set to 1, as the year should be ignored, but the year 0 cannot be used in a DateTime
.
The new Init
method looks like this:
Now the abstract methods can be implemented to create a functional flow element.
First let's define a class which extends FlowElementBase
(which partially implements FlowElement
). This has the type arguments of StarSignData
- the interface extending element data which will be added to the flow data, and ElementPropertyMetaData
- as we only need the standard metadata for element properties.
This needs a constructor matching the FlowElementBase
class. So it takes a logger, and an element data factory which will be used to construct a StarSignData
:
The init
method in this example will simply initialize a list of star signs with the start and end dates of each star sign and add each to a list of a new class named StarSign
which has the following simple implementation:
Note that the year of the start and end date are both set to 0, as the year should be ignored.
The new init
method looks like this:
Now the abstract methods can be implemented to create a functional flow element.
flowElement
:
Now the abstract methods can be implemented to create a functional flow element.
flowElement
.
This needs a constructor matching the flowElement
class which initializes the element:
Now the abstract methods can be implemented to create a functional flow element.
Builder
Now the flow element needs one final thing, an element builder to construct it. This only needs to provide the flow element with a logger and an element data factory - as this example has no extra configuration options.
Build
. This returns a new flow element which the element builder provides with a logger and an element data factory.
The element data factory is implemented in the element builder class to make use of the same logger factory.
build
. This returns a new flow element which the element builder provides with a logger and an element data factory.
The element data factory is implemented in the element builder class to make use of the same logger factory.
Usage
to give an output of:
to give an output of:
To print the star sign of the user.
To print the star sign of the user.
to give an output of:
Next Steps
The example for Custom On-premise Engine shows how you can extend the functionality of this flow element to use a data file.
The flow element can also be upgraded to use evidence supplied by the client, as shown in the Custom Client-Side Evidence example.
To delegate the processing and logic to a cloud service, refer to the Custom Cloud Engine example.