Reasons for choosing Java agents
Rule agents are the default choice of agent, but you might have good reasons to choose a different type of agent.
In most cases, a rule agent is likely to be your first choice of agent. In a rule agent, you can express what you want to do more compactly than by using a Java™ agent. A major difference between rule agents and Java agents is that Java agents by default are stateless. Rule agents store the state of previously processed events and values of related entities in the working memory, and this data is available in a stateful way to rule conditions. To maintain state in a Java agent, you must add attributes to the bound entity.
The following examples give some valid reasons to use a Java agent rather than a rule agent:
- You might want to access the system in some way that is not possible
in rules. For example, if you want to call a web service. Important: Introducing latency in this way might have severe performance impacts on high-throughput systems.
- You might want to calculate some complex computation (complicated loops, data handling, Java library calls), which is difficult to implement in rules.
- You might want to do something simple, and you are more comfortable writing Java code.
- You might want to write some logic that does not change often and this logic exists in Java.
- You might need to convert an inbound event that has some kind of mismatch between the real world and your model by implementing Java code.
- You might need to introspect an event before you know what to do with it. A Java agent with no bound entity can be used to avoid a bottleneck; multiple instances of a Java agent without an entity can run in parallel. You can then modify different entity types from the same event by emitting new events that are processed by different agents, in parallel or sequentially.
The following general guideline can also be used to make your choice of agent:
- Use a rule agent if you need event aggregation in your solution because the business model language makes these operations much simpler.
- Use a predictive agent, which is a kind of Java agent when you integrate with an external service.
The following sections highlight some techniques that you can use in your Java agents.
Use else if clauses to test multiple criteria
In a rule agent, it is not easy to stop processing
or suspend evaluation
when you
have multiple criteria to test. To achieve this goal in a rule agent, you might need to send
multiple specials when multiple criteria are met. In a Java agent, you can use else
if clauses to code this type of action.
The following code shows how to emit an event if two independent conditions on an entity are met:
public void processEvent(Event e) {
WidgetSale ws = (WidgetSale) e;
Widget widget = getBoundEntity();
boolean emitSpecial = false;
if (widget.getColor() == RED) {
emitSpecial = true;
}
else if (widget.getWeight() >= 10) {
emitSpecial = true;
}
if (emitSpecial) {
emit(new Special());
}
}
Pass an array that contains previous events to calculate some analytical logic
Java agents cannot access the event history that is stored in the run time. Therefore, if you need to do something complex with previous events you can collect them by using a rule agent, and then call the Java agent. You can use a concept in the business model definitions (BMD) to encapsulate the array and maintain a list of previous occurrences of an event.
For example, if a passenger journey event occurs and you want to check the previous journeys of that passenger, you can store the important details of each journey in a passenger journey concept. The concept has a many to one relationship with a passenger. You can then work with that list in a Java agent.
To collect a list of loan request events in a concept, define the concept type in the BMD.
a loan request holder is a concept.
a loan request holder has some loan requests.
The loan request event type is a company business event.
a company event is a business event time-stamped by a datetime.
a loan request is a company event.
a loan request is related to a client.
a loan request has an application.
The application attribute of the loan request event is a concept with all the loan information in it. You can then create an array of this concept instead of using the array of events.
an application is a concept.
an application has a reference number.
an application has a dealer id.
an application has an applicant.
...
Define an event type that has the array as an attribute.
a check previous loan requests is a company event.
a check previous loan requests has a client number.
a check previous loan requests has a loan request , named the current request.
a check previous loan requests has a loans request holder, named the previous requests.
You can then write a rule in a rule agent to instantiate the loans request holder concept.
when a loan request occurs , called 'current request'
definitions
set 'previous requests' to all loan requests after 90 days before 'current request' ;
if
the number of 'previous requests' is more than 1
then
define 'previous loan requests' as a new loans request holder ;
set the loan requests of 'previous loan requests' to 'previous requests' ;
emit a new check previous loan requests where
the client number is the id of 'the client',
the current request is 'current request',
the previous requests is 'previous loan requests' ;
The check previous loan requests event in the Java agent, checks the status of the previous loan request in a loan entity:
public void process(Event event) throws AgentException {
if (event instanceof CheckPreviousLoanRequests) {
checkPreviousLoanRequests((CheckPreviousLoanRequests) event);
return;
}
private void checkPreviousLoanRequests(CheckPreviousLoanRequests event) throws AgentException {
LoanRequest currentRequest = event.getCurrentRequest();
for (LoanRequest previousRequest : event.getPreviousRequests().getLoanRequests()) {
if (previousRequest.getApplication().getReferenceNumber().equals(currentRequest.getApplication().getReferenceNumber()))
continue;
Loan loan = (Loan) previousRequest.getLoan().resolve();
//previously declined loan
if (loan.getData().getStatus() == company.LoanStatus.DECLINE) {
Loan currentLoan = (Loan) currentRequest.getLoan().resolve();
currentLoan.getData().setProductPolicyDecision(company.PolicyDecision.REVIEW);
currentLoan.getData().addTo_productPolicyMessages("Previously declined loan in last 90 days");
updateBoundEntity(currentLoan);
}
if (previousRequest.getApplication().getDealerId().equals(currentRequest.getApplication().getDealerId()) && loan.getData().getStatus() == company.LoanStatus.APPROVE) {
Loan currentLoan = (Loan) currentRequest.getLoan().resolve();
currentLoan.getData().setProductPolicyDecision(company.PolicyDecision.REVIEW);
currentLoan.getData().addTo_productPolicyMessages("Approved loan from same dealer in last 90 days");
updateBoundEntity(currentLoan);
}
}
}
}