||
Skip to end of metadata
Go to start of metadata

This extension facilitates you to use the machine learning models which you generate using WSO2 ML within WSO2 Complex Event Processor (CEP) for making predictions. Thereby, it  integrates WSO2 ML with WSO2 CEP, to perform realtime predictions on an event stream by applying a model generated by WSO2 ML. An input event stream which is received by an event receiver of WSO2 CEP is processed by executing an execution plan within WSO2 CEP. This execution plan is written using Siddhi language. It processes the input event stream by applying the model generated using WSO2 ML. The output of this processing which includes the prediction will be published to an output stream through an event publisher of WSO2 CEP. For more information on WSO2 CEP, go to WSO2 CEP Documentation.

Siddhi syntax for the extension

There are two possible Siddhi query syntaxes to use the extension in an execution plan as follows.

  1. < double | float|long|int|string|boolean >  predict(<string> pathToMLModel, <string> dataType)
    • Extension TypeStreamProcessor
    • Description: Returns an output event with the additional attribute with the response variable name of the model, set with the predicted value, using the feature values extracted from the input event.
    • ParameterpathToMLModel:    The file path or the registry path where ML model is located. If the model storage location is registry, the value of this this parameter should have the prefix  registry:
    • Parameter: dataType: Data type of the predicted value (double, float, long, integer/int, string, boolean/bool). 

    • Example predict(‘registry:/_system/governance/mlmodels/indian-diabetes-model’)

  2. < double | float|long|int|string|boolean >  predict(<string> pathToMLModel, <string> dataType<double> input)

    • Extension TypeStreamProcessor
    • Description: Returns an output event with the additional attribute with the response variable name of the model, set with the predicted value, using the feature values extracted from the input event.
    • ParameterpathToMLModel:  The file path or the registry path where ML model is located. If the model storage location is registry, the value of this parameter should have the prefix  registry:
    • Parameter: dataType: Data type of the predicted value (double, float, long, integer/int, string, boolean/bool).

    • Parameter input:  A variable attribute value of the input stream which is sent to the ML model as feature values for predictions. Function does not accept any constant values as input parameters. You can have multiple input parameters.   

    • Example predict(‘registry:/_system/governance/mlmodels/indian-diabetes-model’, NumPregnancies, TSFT, DPF, BMI, DBP, PG2, Age, SI2)

Siddhi query examples

A few Siddhi query examples that can be used in execution plans within WSO2 CEP based on the above syntaxes are as follows.

Example 1
@Import('InputStream:1.0.0')
define stream InputStream (NumPregnancies double, TSFT double, DPF double, BMI double, DBP double, PG2 double, Age double, SI2 double);
@Export('PredictionStream:1.0.0')
define stream PredictionStream (NumPregnancies double, TSFT double, DPF double, BMI double, DBP double, PG2 double, Age double, SI2 double, Class double);
from InputStream#ml:predict('registry:/_system/governance/ml/indian-diabetes-model', 'double')
select *
insert into PredictionStream;
Example 2
@Import('InputStream:1.0.0')
define stream InputStream (NumPregnancies double, TSFT double, DPF double, BMI double, DBP double, PG2 double, Age double, SI2 double);
@Export('PredictionStream:1.0.0')
define stream PredictionStream (NumPregnancies double, TSFT double, DPF double, BMI double, DBP double, PG2 double, Age double, SI2 double, Class double);
from InputStream#ml:predict('registry:/_system/governance/ml/indian-diabetes-model', 'double', NumPregnancies, TSFT, DPF, BMI, DBP, PG2, Age, SI2)
select *
insert into PredictionStream;
Example 3
@Import('InputStream:1.0.0')
define stream InputStream (NumPregnancies double, TSFT double, DPF double, BMI double, DBP double, PG2 double, Age double, SI2 double);
@Export('OutStream:1.0.0')
define stream OutStream (Class double);
from InputStream#ml:predict('registry:/_system/governance/ml/indian-diabetes-model', 'double', NumPregnancies, TSFT, DPF, BMI, DBP, PG2, Age, SI2)
select Class
insert into OutStream;

Prerequisites

Set up the following prerequisites before starting the configurations.

  1. Download WSO2 ML, and start the server. For instructions, see Getting Started.

Generate a model using WSO2 ML which you will use to make the predictions. For instructions on generating a model in WSO2 ML, see Generating Models.

When following the instructions in Generating Models , do the following changes.

  • In step 9, select RANDOM FOREST CLASSIFICATION as the algorithm name and Class as the response variable.
  • In step 11, enter 50 in the Num Trees parameter and leave the other parameter values unchanged

 

  1. Download WSO2 CEP, and start the server. For instructions, see Getting Started.

Installing required features in WSO2 CEP

  1. Install the WSO2 Machine Learner (ML) Features. For detailed instructions to install WSO2 ML features, see Installing Machine Learner Features.

Creating the input stream

Follow the steps below to create the input stream in WSO2 CEP.

  1. Log in to the CEP management console using the following URL, if you are not already logged in:  https://<CEP_HOME>:9443/carbon/
  2. Click  Main, and then click Event Streams in the Event Processor menu.
  3. Click  Add Event Stream .
  4. Enter details of the stream definition that you want to create as shown below.
  5. Click  Add Event Stream ,   to create the event stream in the system.  You view the new  input stream added to the list of all available event streams as shown below .

Creating the output stream

Follow the steps below to create the output stream in WSO2 CEP.

  1. Log in to the CEP management console using the following URL, if you are not already logged in: https://<CEP_HOME>:<CEP_PORT>/carbon/
  2. Click Main, and then click Event Streams in the Event Processor menu.
  3. Click Add Event Stream.
  4. Enter details of the stream definition that you want to create as shown below.
  5. Click Add Event Stream, to create the event stream in the system. You view the new output stream added to the list of all available event streams as shown below .

Creating the execution plan

Follow the steps below to create the execution plan in WSO2 CEP.

  1. Log in to the CEP management console using the following URL, if you are not already logged in: https://<CEP_HOME>:<CEP_PORT>/carbon/
  2. Click Main, and then click Execution Plans in the Streaming Analytics menu.
  3. Click Add Execution Plan.
  4. Select InputStream:1.0.0 for Import Stream from the list, and click Import.
  5. Enter the name of the output stream (PredictionStream) for Value Of, select PredictionStream:1.0.0 from the Stream ID list, and click Export for Export Stream.

  6. Enter the following Siddhi query as shown below.

    Replace your file or registry path where to locate the downloaded ML model as the first argument and the response variable data-type as the second variable of the predict() function in the below execution plan as described in Siddhi syntax for the extension .

    from InputStream#ml:predict('registry:/_system/governance/ml/indian-diabetes-model', 'double')
    select *
    insert into PredictionStream;

    Click Validate Query Expressions, to validate the query you entered.

  7. Click Add Execution Plan to save the execution plan in the system. You view the execution plan added to the list of all available execution plans as shown below.

Creating an event publisher

Follow the steps below to create an event publisher in WSO2 CEP.

  1. Log in to the CEP management console using the following URL, if you are not already logged in: https://<CEP_HOME>:<CEP_PORT>/carbon/
  2. Click Main, and then click Execution Plans in the Event menu.
  3. Click Add Event Publisher.
  4. Enter the details as shown below to create a logger type event publisher.
  5. Click Add Event Publisher, to create the event publisher in the system. You view the new event publisher added to the list of available event publishers. 
  6. Click the corresponding Enable Tracing button as shown below.

Simulating events

Follow the steps below to simulate the sending of events in WSO2 CEP.

  1. Log in to the CEP management console using the following URL, if you are not already logged in: https://<CEP_HOME>:<CEP_PORT>/carbon/
  2. Click Tools, and then click Event Simulator.
  3. Select InputStream:1.0.0 for the Event Stream Name.
  4. Enter the feature values to predict, in the Stream Attributes input fields as shown below.

  5. Click Send, to send the events. You view the values of the output stream named  PredictionStream logged by the event publisher in the back end console logs of WSO2 CEP as shown below.

  • No labels