Versions Compared

Key

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

This sample demonstrates a simple greeting service which uses service for recommending dosage for a patient through the use of business rules. 

Info
titleNote

Before executing this sample, it is recommended that you refer to section Exposing Rules as Services which explains in detail the process of writing and deploying a business rule.

Sample Rule Definition

Rules

Rule 1 : If the time is between 12 and 18, say good afternoonrecommended medication is one of Amoxicillin, Cefuroxime, and Levofloxacin, and if the patient is older than 15 or younger than 60 years, 500mg every 24 hours for 14 days.

Rule 2 : If the time is between 6 and 12, say good morning.Rule 3 : If the time is between 18 and 24, say good nightrecommended medication is one of Amoxicillin, Cefuroxime, and Levofloxacin, and if the patient is older than 15 or younger than 60 years, and is the patient's Creatinine Level is 1.5, then 250 mg every 24 hours for 14 days.

Facts

There is one fact , which is "A user logged-in into the system". Also, "GreetingMessage" is used to present the result of the rule execution.

 

and a result - patient and dosage.

Code Block
languagejava
package samples.greeting;
import java.util.Calendarheathcare;

/**
 * UserPatient
 */
public class UserPatient {

    private String name  medication;

    private int age;

    private double creatinineLevel;

    public String getNamegetMedication() {
        return namemedication;
    }

    public void setNamesetMedication(String namemedication) {
        this.namemedication = namemedication;
    }

    public int nowgetAge() {
        return age;
    }

  Calendar  calpublic =void Calendar.getInstance()setAge(int age) {
        this.age = age;
    }

  return cal.get(Calendar.HOUR_OF_DAY)  public double getCreatinineLevel() {
        return creatinineLevel;
    }
}

    public void setCreatinineLevel(double creatinineLevel) {
        this.creatinineLevel = creatinineLevel;
    }
}

package samples.greetingheathcare;

/**
 * greeting messageDose
 */
public class GreetingMessageDose {

    private String message;
;
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Rule Service Configuration (service.rsl)

An in-line rule set is used within the service.rsl file as follows. 

Code Block
languagehtml/xml
<ruleService
        name="GreetingServiceHealthCareService"
        xmlns="http://wso2.org/carbon/rules"
        targetNamespace="http://com.test/greetingHeathCareService">
    <ruleSet>
        <rule resourceType="regular" sourceType="inline">
            <![CDATA[
                package HeathCareService
greeting
                import samples.greetingheathcare.GreetingMessagePatient;
                import samples.greetingheathcare.UserDose;

                rule "IsRule Morningone" no-loop true
                when
                   user :Patient( User() medication in ( "Amoxicillin", "Cefuroxime", "Levofloxacin" ) ) && ( ( age  > 16 ) && ( age eval((6 < user.now()< 60 ) ) && (user.now()< 12)) creatinineLevel == 0 )  )
                then
                   GreetingMessage Dose msg = new GreetingMessageDose();
                    msg.setMessage("Good500 Morningmg every "24 + user.getName() + " !!! ");hours for 14 days");
                    insertLogical(msg);
                end

                rule "IsRule afternoontwo" no-loop true
                when
                   user :Patient( User() medication in ( "Amoxicillin", "Cefuroxime",               eval((12 <= user.now())"Levofloxacin" ) ) && (user.now () <age 18))                then
                   GreetingMessage msg = new GreetingMessage();
                   msg.setMessage("Good Afternoon  " + user.getName() + " !!! ");
                   insertLogical(msg);
               end

               rule "Is Night" no-loop true
               when
                   user : User()
                   eval( (18 <= user.now()) && (user.now() < 24))> 16) && ( age < 60 ) ) && ( ( creatinineLevel > 0 ) &&  ( creatinineLevel < 1.5 ) ) )
                then
then                    GreetingMessageDose msg = new GreetingMessageDose();
                    msg.setMessage("Good250 Nightmg every "24 + user.getName() + " !!! hours for 14 days");
                    insertLogical(msg);
                end
            ]]>
        </rule>
    </ruleSet>
    <operation name="greetMerecommendDose">
        <input wrapperElementName="userpatientDetail" namespace="http://com.test/greetingpatientDetail">
            <fact elementName="userpatientDetail" namespace="http://com.test/greetingpatientDetail" type="samples.greetingheathcare.UserPatient"></fact>
        </input>
        <output wrapperElementName="greetingMessagepatientDetailRespone" namespace="http://com.test/greetingpatientDetail">
            <fact elementName="greetingMessagerecommendDose" namespace="http://com.test/greetingpatientDetail" type="samples.greetingheathcare.GreetingMessageDose"></fact>
        </output>
    </operation>
</ruleService>

 

Deploying and Testing the Service

1. Deploy the rule service through the BRS management console. You can follow either of the two methods:

  • Bundle all artifacts in an .aar file and upload it (Rule Service -> Upload menu).
  • Create using the Rule Service wizard UI (Rule Service -> Create menu).

The above steps are discussed in detail in section Exposing Rules as Services.

2. After deployment, the service will appear in the "Deployed Services" window of the BRS management console. Access the service's dashboard by clicking on it.

3. In the "Client Operations" widget of the dashboard, invoke the Try-it tool to test the service.

4. In the service's TryIt window, issue a request similar to the following.

 
Code Block
languagehtml/xml
<medication>Cefuroxime</medication>
<age>54</age>
<creatinineLevel>1.2</creatinineLevel>

Image Added

Similarly, send another request:

Code Block
languagehtml/xml
<name>your name</name>
Image Removed
<medication>Amoxicillin</medication> 
<age>20</age>

5. Alternatively, you can use code generation link in the  "Client Operations" widget of the dashboard to invoke the service. A client using generated stub codes is shown below where the codes were generated with option "Unpacks the databinding classes". 

Code Block
languagejava
package org.wso2.carbon.samples;

import org.apache.axis2.AxisFault;
import java.lang.Stringorg.wso2.carbon.samples.healthCareService.patientDetail.Dose;
import org.wso2.carbon.samples.healthCareService.patientDetail.Patient;
import org.wso2.carbon.samples.greetingServicehealthCareService.greetingpatientDetail.*PatientDetail;
import org.apache.axis2.AxisFault.wso2.carbon.samples.healthCareService.stub.HealthCareServiceStub;
import java.rmi.RemoteException;

public class GreetingServiceTestCaseHealthCareServiceTestCase {

    public static void main(String[] args) {

        try {
            GreetingServiceStubHealthCareServiceStub greetingServiceStubhealthCareServiceStub =
                    new GreetingServiceStubHealthCareServiceStub("http://localhost:9763/services/GreetingServiceHealthCareService");

            UserEPatientDetail userRequestpatientDetail = new UserEPatientDetail();
            UserPatient userpatient = new User(Patient();
            patient.setAge(43);
            patient.setCreatinineLevel(1.0);
            userpatient.setNamesetMedication("your nameLevofloxacin");

            UserPatient[] userspatients = new UserPatient[1];
            userspatients[0] = userpatient;
            userRequestpatientDetail.setUsersetPatientDetail(userspatients);

            GreetingMessageDose[] greetingMessagesdoses = greetingServiceStubhealthCareServiceStub.greetMerecommendDose(userspatients);
            String result = greetingMessagesdoses[0].getMessage();
            System.out.println(result);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }

   } }
Excerpt
hiddentrue

Greeting HealthCare service sample of the WSO2 Business Rules Server samples guide.