공유 집계 값 가져오기

비즈니스 모델 정의(BMD) 파일에서 공유 집계를 정의하는 경우 Java™ 에이전트에서 해당 값에 액세스할 수 있습니다. Java 코드에서 이 값을 사용하여 조건을 평가하거나 조치를 결정할 수 있습니다.

시작하기 전에

공유 집계는 BMD 파일에 정의되어야 합니다.

예를 들어, 다음 BMD에서 trader 엔티티에는 indicator 속성이 있습니다.
a trader is a business entity identified by an id.
a trader has an indicator (numeric).
BMD 명령문은 indicator를 공유 집계로 정의합니다.
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 .
Java 에이전트는 에이전트 디스크립터에서 trader 엔티티에 대한 관계를 정의해야 합니다.
'MyJavaAgent' is an agent related to a trader, 
processing events:
     - purchase, where this trader comes from the trader of this purchase

이 태스크 정보

값에 액세스하기 위해 사용하는 getter 메소드는 BMD 파일의 공유 집계 이름으로 판별됩니다. 이 메소드에는 집계 명령문에 지정된 사용 가능 기간의 특정 시점 또는 이벤트의 현재 시간소인일 수 있는 now 매개변수가 있습니다.

집계 조회의 값이 널일 수 있습니다. 예를 들어, 집계의 지평에 이벤트가 없는 경우입니다. 시간 인수가 널이면 현재 시간이 사용됩니다.

에이전트에 바인드된 엔티티에 대해서만 공유 집계 값에 액세스할 수 있습니다.

프로시저

에이전트에 대한 Java 코드에서 getter 메소드를 호출하여 공유 집계 값에 액세스하십시오.
예를 들어, getIndicator 메소드를 사용하여 indicator 값에 액세스할 수 있습니다.
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);
	        	
이 예에서 getTimestamp 메소드는 구매 조회 이벤트에서 현재 시간소인을 리턴합니다.

예

Getter 메소드 내에서 현재 시간소인을 기반으로 집계 값을 리턴하려는 경우 다음 접근법 중 하나를 사용할 수 있습니다.
trader.getIndicator(inquiry.getTimestamp())
trader.getIndicator(ZonedDateTime.now())
사용 가능 기간의 특정 시점을 기반으로 하여 집계 값을 리턴하려면 다음 방법을 사용할 수 있습니다.
trader.getIndicator(ZonedDateTime.parse("2015-06-25T14:30:00Z"));
사용 가능 기간의 일정 기간을 기반으로 하여 집계 값을 리턴하려면 com.ibm.ia.time.TimePeriod를 지정하는 다음 방법을 사용할 수 있습니다.
timePeriod = TimePeriod.create(now.plusMinutes(5), now.plusMinutes(15));
value = trader.getIndicator(timePeriod);

공유 집계에는 grouped by 표현식이 포함될 수 있습니다. 다음 예제에서 집계 값은 purchase events의 country 기준으로 그룹이 지정되며, 여기서 country는 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.

이 집계에서 특정 국가의 값을 리턴하기 위해 다음 메소드 중 하나를 호출할 수 있습니다.

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)

이 메소드를 호출하기 위해 다음 코드를 사용할 수 있습니다.

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());
 

grouped by 조건이 열거 대신 엔티티인 경우, 다음 메소드 중 하나를 사용하여 집계의 값을 리턴할 수 있습니다.

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)