Getting the value of a shared aggregate
If you define a shared aggregate in a business model definition (BMD) file, you can access the value in a Java™ agent. You can use the value in your Java code to evaluate conditions or to determine actions.
Before you begin
The shared aggregate must be defined in the BMD file.
a trader is a business entity identified by an id.
a trader has an indicator (numeric).
The BMD statement defines indicator as a shared
aggregate:the indicator of a trader is aggregated from purchases,
where this trader comes from the buyer of each purchase
as the number of purchases
available for 10 weeks .
The
Java agent must define the relationship to the
trader entity in the agent descriptor:
'MyJavaAgent' is an agent related to a trader,
processing events :
- purchase, where this trader comes from the trader of this purchase
About this task
The getter method that you use to access the value is determined by the name of the shared aggregate in the BMD file. The method has the parameter now, which can be the current time stamp of an event or a time point in the available period that is specified in the aggregate statement.
The value of the aggregate query might be null, for example if there is no event within the aggregate's horizon. If the time argument is null, the current time is used.
You can access the value of a shared aggregate only for a bound entity of the agent.
Procedure
public void process(Event event) throws AgentException {
if (event instanceof PurchaseInquiry) {
PurchaseInquiry inquiry = (PurchaseInquiry) event;
Trader trader = (Trader) getBoundEntity();
try {
Object value = trader.getIndicator(inquiry.getTimestamp());
System.out.println("The indicator of trader: " + trader.getName() + " is " + value);
In
this example, the getTimestamp method returns the current time stamp from a
purchase inquiry event. Example
trader.getIndicator(inquiry.getTimestamp())
trader.getIndicator(ZonedDateTime.now())
trader.getIndicator(ZonedDateTime.parse("2015-06-25T14:30:00Z"));
timePeriod = TimePeriod.create(now.plusMinutes(5), now.plusMinutes(15));
value = trader.getIndicator(timePeriod);
The shared aggregate might contain a grouped by expression. In the following example, the aggregated values are grouped by the country of the purchase events, where country is an enumeration attribute of purchase events.
the total price of a cart is aggregated from purchase events,
where this cart comes from the cart of each purchase event
as the total price of all purchase events
grouped by the country of each purchase event
available for 1 year.
To return a value from this aggregate for a specific country, you can call one of the following methods:
public java.lang.Double getTotalPrice(com.ibm.ia.time.TimePeriod arg0, group.Country arg1)
public java.lang.Double getTotalPrice(java.time.ZonedDateTime arg0, group.Country arg1)
To call this method, you can use the following code:
RequestEvent request = (RequestEvent) event;
Cart entity= (Cart) getBoundEntity();
Double value = entity.getTotalPrice(event.get$Timestamp(), request.getCountry());
System.out.println("The shared aggregate for entity " + entity.get$Id() + ", value: " + value + ", using key: " + request.getCountry());
If the grouped by criterion was an entity instead of an enumeration, you would use one of the following methods to return the value of the aggregate:
public java.lang.Double getTotalPrice(com.ibm.ia.time.TimePeriod arg0, com.ibm.ia.model.Relationship<group.Country> arg1)
public java.lang.Double getTotalPrice(java.time.ZonedDateTime arg0, com.ibm.ia.model.Relationship<group.Country> arg1)