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.

For example, in the following BMD, the entity trader has the attribute indicator:
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.

You can access the value of a shared aggregate only for a bound entity of the agent.

Procedure

In the Java code for your agent, call the getter method to access the value of the shared aggregate.
For example, you can access the value of indicator by using the getIndicator method:
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

To return the aggregate value based on the current time stamp within the getter method, you can use either of the following approaches:
trader.getIndicator(inquiry.getTimestamp())
trader.getIndicator(ZonedDateTime.now())
To return the aggregate value based on a specific time point in the available period, you can use the following approach:
trader.getIndicator(ZonedDateTime.parse("2015-06-25T14:30:00Z"));
To return the aggregate value based on a time period in the available period, you can use the following approach, which specifies a com.ibm.ia.time.TimePeriod:
timePeriod = TimePeriod.create(now.plusMinutes(5), now.plusMinutes(15));
value = trader.getIndicator(timePeriod);