Step 3 – Creating Business Scenario Scripts

In the previous steps you created the base Scenario.GetScenarios method, that AIM calls to get a list of your business scenarios.

You then created the basis of 1 scenario called Enquiry, which mapped to 2 functions – Make Request and Receive Request. This allowed AIM to highlight your model and show what functions are included in the scenario.

In this step we’ll build out that detail by defining scripts for those 2 functions. This is the final step to enable a simulation of the Sale scenario.

So, you will recall we defined 2 variables, make_request and receive_request.

var make_request = { func: "Make Request", method: "Customer.MakeRequest" };
var receive_request = { func: "Receive Request", method: "Sales.ReceiveRequest" };

What we now need to do is provide an implementation of those 2 methods – Customer.MakeRequest and Sales.ReceiveRequest.

Note that we could have called those whatever we liked — just call them whatever makes sense to you.

We’ll create the skeleton of our Customer.MakeRequest method first. Here it is.

class Customer {

  function MakeRequest(params) {

    var result = {};

return result;
}
}

A couple of initial call outs are:

  • The class is called Customer. We’ll start by adding a MakeRequest method, but can add other customer related methods in here as we build out our scenarios.
  • Methods always accept a params argument. You can call this by whatever name you like. It will contain the function’s input params as provided by AIM, and is always in JSON format.
  • We always return a JSON result.

Next, let’s fill out some more script in this method.

 

class Customer {

function MakeRequest(params) {

var result = {};

// define the customer request details
result["Request"] = {

// customer contact details
customer: {
name: "J.Smith",
phone: "555-5555"
},

// type of customer request
request_type: "Portfolio"
};

// include a message to show in AIM as the scenario runs
result["Messages"] = [
{
msg: "Customer request issued"
}
];

return result;
}
}

This is our now complete method. A few more callouts:

  • The line that begins result[“Request”] populates some Request data, which will be returned to AIM. The reason that we called it “Request” is because that’s what it’s called in our AIM model. If you return to AIM and hover over the Make Request function you’ll see that it outputs Request information. The function that you just wrote provides that information.
  • If you fail to provide information of type “Request”, then the simulation will halt because the information necessary for the simulation to continue has not been provided. In fact, all of the information inputs needed by a function as specified in AIM must be provided by your function.
  • The line that begins result[“Messages”] creates a business message that AIM will display as the scenario runs. “Messages” is a special element type that you can add any processing message, displayed by AIM on the right side of the page as your business scenario executes.

When you save the above script, the AIM Server console should show something similar to the following.

Convert string to JSObject: {}
Change: C:/AIM models/jscript/ins-scriptins-script.js
File: C:/AIM models/jscript/ins-scriptins-script.js Changed
compiler error count=0
- method - Scenario.GetScenarios
- method - Customer.MakeRequest

 

If you had instead made an editing mistake, and saved invalid JScript, then the console will display the line number of the error(s). You’ll may need some basic JScript knowledge to fix it.

Now, switch over to AIM, click the Scenarios tab, then click the play button next to the Sale scenario. You’ll notice that simulation gets further than before, however halts automatically when it gets to the Receive Request method with a message saying “Method Sales.ReceiveRequest not defined in JScript”.

Let’s define Sales.ReceiveRequest.

class Sales {

function ReceiveRequest(params) {

var result = {};

// Extract 'Request' information from input params
var req = params["Request"];

if (req.request_type == "Portfolio") {

// pull through customer data from the input param, and
// propagate it into the portfolio request.
result["Request/Portfolio"] = {
customer: req.customer
};

result["Messages"] = [
{
msg: "Created portfolio request"
}
];
}

return result;
}
}

 

A few callouts for this script are:

  • We’ve created “Request/Portfolio” information in the return result. This will cause the simulation to propagate data on towards the Send product portfolio function, rather than the Process Request function in your AIM model.
  • The customer data from the incoming params has been copied into the “Request/Portfolio” information to be propagated through the scenario.

 

 

 

Now, we’re going to extend our business scenario by adding in a 3rd function – Send product portfolio.

To do this, we need to:

  1. Create a function mapping
  2. Add the function mapping to the scenario.
  3. Write whatever new script is required.

The function mapping is fairly simple. It’s just a repeat of what we’ve done twice already.

Just define a new 3rd variable like this.

var make_request = { func: "Make Request", method: "Customer.MakeRequest" };
var receive_request = { func: "Receive Request", method: "Sales.ReceiveRequest" };
var send_product_portfolio = { func: "Send product portfolio", method: "PortfolioMgmt.SendProductPortfolio" };

 

Then, add that mapping into the Enquiry scenario.

 

result["scenarios"] = [
{
name: "Enquiry",
description: "Customers inquires about products",
mapping: [
make_request,
receive_request,
send_product_portfolio
]
}
];

Then, create the new script to match that defined in your function mapping.


class PortfolioMgmt {

function SendProductPortfolio(params) {

var result = {};

// Extract customer data from the incoming params
var cust = params["Request/Portfolio"].customer;

result["Messages"] = [
{
// We can incorporate data into messages that AIM will display
msg: "Sending product portfolio to " + cust.name
}
];

return result;
}
}

 

A few callouts for the above script are:

  • We have extracted the customer details from the incoming params object. The customer details were originally provided by the Make Request function, and have been propagated through the methods in this scenario.
  • We’ve incorporated data into the “Messages” collection, and AIM will display those on the right side of the page as our scenario executes. You can see this in the screenshot below.
  • If we now go back into AIM, there will be 3 functions highlighted to reflect the scripting modification we’ve just made. You can see this in the screenshot below.