Predict Mediator captures data from messages communicate via WSO2 ESB, and passes them to a generated model to carry out predictions.
Predict Mediator configuration
The syntax of the predict mediator configuration is as follows.
<predict> <model storage-location="string"/> <features> <feature name="string" expression="XPath | JSONPath">+ </features> <predictionOutput property="string"/> </predict>
The properties of the above configuration are described below.
Property | Description |
---|---|
<model storage-location> | ML model storage location is either the file system or Registry. If the model is stored in the Registry, storage-location should have the prefix |
<name> | Name of the feature according to the generated model. |
<expression> | The XPath or JSONPath expression to extract the feature value from the payload. |
<predictionOutput property> | The message context property name, to which you need to set the prediction output value. |
Installing ML features in WSO2 ESB
Follow the steps below to install the ML feature in WSO2 ESB.
- Download WSO2 ESB version 4.9.x or later.
- Start the WSO2 ESB server as follows.
- on Linux, run
<ESB_HOME>/bin/wso2server.sh
on MS Windows, run
<ESB_HOME>/bin/wso2server.bat
- on Linux, run
- Log in to the WSO2 ESB management console using
admin/admin
credentials, and click Configure. - Click Features, and then click Repository Management.
- Click Add Repository, and enter the details as shown below to add the <P2 repository>.
- Click Add.
- Click Available Features tab, and select the repository added in the previous step.
Deselect the Group features by category check box.
Click Find Features. It can take a while to list out all the available features in the feature repository. Once listed, select the following features.
- Machine Learner Core
- Machine Learner Commons
- Machine Learner Database Service
- Metrics Group
- Predict Mediator Aggregate
If you can't see this feature, retry with one of the following suggestions:
- Try adding a more recent P2 repository. The repository you added could be deprecated.
- Check for the feature in the Installed Features tab.
Once the features are selected, click Install to proceed with the installation.
Click Next, select I accept the terms of the license agreement option, and then click Next.
Click Restart Now, and then click Yes in the message which pops up. Wait a few seconds until the sever restarts, and refresh the screen.
Adding a proxy service to WSO2 ESB
Follow the steps below to create a sample Pass Through Proxy Service to test the Predict mediator.
- Log in to the WSO2 ESB management console using
admin/admin
credentials. - Click Main, and then click Proxy Service.
- Click Pass Throgh Proxy.
- Enter the details as shown below.
- Enter a name for Proxy Service Name (E.g.
PredictMediatirTestProxy
). - Enter http://localhost:9000/services/SimpleStockQuoteService for Target URL.
- Enter a name for Proxy Service Name (E.g.
Click Create . You view the proxy service created as shown below.
Follow the steps below to view the source view of the proxy service configuration you added.
- Log in to the WSO2 ESB management console using
admin/admin
credentials. - Click Main, and then click List in the Services menu.
Click the corresponding Source View link of the WSDL based proxy service you added above. You view its configuration in the source view as follows.
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="PredictMediatirTestProxy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true"> <target> <inSequence> <log level="custom"> <property name="before-predict-mediator" expression="fn:concat('ML Mediator Prediction : ',get-property('result'))"/> </log> <predict> <model storage-location="/Users/praneesha/Documents/Product_Packs/ML/wso2ml-1.0.0-SNAPSHOT/samples/ml-model-usage/src/main/resources/downloaded-ml-model"/> <features> <feature name="SI2" expression="$body/features/SI2"/> <feature name="PG2" expression="$body/features/PG2"/> <feature name="Age" expression="$body/features/Age"/> <feature name="DBP" expression="$body/features/DBP"/> <feature name="BMI" expression="$body/features/BMI"/> <feature name="DPF" expression="$body/features/DPF"/> <feature name="TSFT" expression="$body/features/TSFT"/> <feature name="NumPregnancies" expression="$body/features/NumPregnancies"/> </features> <predictionOutput property="result"/> </predict> <log level="custom"> <property name="after-predict-mediator" expression="fn:concat('ML Mediator Prediction : ',get-property('result'))"/> </log> <drop/> </inSequence> <outSequence> <send/> </outSequence> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </target> <description/> </proxy>
- Log in to the WSO2 ESB management console using
Adding Log, Predict and Drop Mediators to the Sequence
Follow the steps below to add log, predict and drop mediators to the sequence.
- Log in to the WSO2 ESB management console using
admin/admin
credentials. - Click Main, and then click List in the Services menu.
- Click the corresponding Design View link of the WSDL based proxy service you added above.
- Click Next in the Step 1 of 3 - Basic Settings screen to proceed to step 2.
- In the Step 2 of 3 - In Sequence and Endpoint Options screen, Select Define Inline, and then click the Create link as shown below.
- In the Design in Sequence screen, click the Add Child option which is next to the Root element.
- Select Core, and then select Log from the drop down menu as shown below.
- Enter the following details in the Log Mediator section as shown below.
Select
INFO
for Log Category.Select
Custom
for Log Level.Click Add Property.
Enter
before-predict-mediator
for Property Name.Select
Expression
from the Property Value drop down list.Enter the following expression for Value/Expression:
fn:concat('ML Mediator Prediction : ',get-property('result'))
Click Update.
- Click on the Log element, and then click the Add Sibling option which is next to it.
- Select Agent and then select Predict from the drop down menu as shown below.
Enter the path to generated model, which you want to use in predictions for Model Storage Location as shown below.
You can give the folder path of the
<ML_HOME>/samples/model-usage/src/main/resources/downloaded-ml-model
sample model.- Click Load Model. It loads the features list of the model as shown below.
Enter the following XPath (or JSONPath) expressions in the Expression field for the displayed features list associated with the model, to extract the feature values from the mediating message.
Feature name XPath expression Age $body/features/Age
BMI $body/features/BMI
DBP $body/features/DBP
DPF $body/features/DPF
NumPregnancies $body/features/NumPregnancies
PG2 $body/features/PG2
SI2 $body/features/SI2
TSFT $body/features/TSFT
If you are defining a namespace in the XPATH expression, click the corresponding Namespaces link of the features to add namespaces. Then, in the Namespace Editor, add any number of namespace prefixes and URL that you have used in the expression as shown below.
In the Prediction Output section, the message context property name (e.g.
result
), to which the prediction output value needs to be set as shown below.- Click Update.
- Click on the Predict element, and then click the Add Sibling option which is next to it.
- Select Core and then select Log from the drop down menu as shown below.
- Enter the following details in the Log Mediator section as shown below.
Select
INFO
for Log Category.Select
Custom
for Log Level.Click Add Property.
Enter
after-predict-mediator
for Property Name.Select
Expression
from the Property Value drop down list.Enter the following expression for Value/Expression:
fn:concat('ML Mediator Prediction : ',get-property('result'))
Click Update.
- Click on the Log element, and then click the Add Sibling option which is next to it.
- Select Core and then select Drop from the drop down menu as shown below.
- Click Save & Close.
Click Next in the Step 2 of 3 - In Sequence and Endpoint Options screen.
Click Finish in the Step 3 of 3 0 Out Sequence and Fault Sequence Options screen.
Making predictions using Predict Mediator
Follow the steps below to make predictions using the Predict mediator.
- Log in to the WSO2 ESB management console using
admin/admin
credentials. - Click Main, and then click List in the Services menu.
- Click the corresponding Try this service link of the proxy service (i.e.
PredictMediatorTestProxy
) you added as shown below.
Enter the following XML message as the Request as shown below.
<features xmlns:ns="http://ws.apache.org/axis2"> <NumPregnancies>1</NumPregnancies> <TSFT>30</TSFT> <DPF>0.529</DPF> <BMI>34.6</BMI> <DBP>70</DBP> <PG2>115</PG2> <Age>32</Age> <SI2>96</SI2> </features>
You view the output of the Log mediators in the CLI in which you ran WSO2 ESB server as shown below.