Developing Apama Applications > Developing Apama Applications in Java > Overview of Apama Java Applications > About event types > About event parameters that are complex types
About event parameters that are complex types
It is possible in both EPL and Java to declare a field of an event definition to be a complex type. For example, the SequenceEvent definition below defines an event that is constructed from a sequence of DataHolder events, which in turn contain a string and an integer. This is defined in EPL in two events thus:
event DataHolder {
  string name;
  integer age;
}
 
event SequenceEvent {
  sequence <DataHolder> complex;
}
An example constructed SequenceEvent event is show below:
SequenceEvent([DataHolder("kap", 1), DataHolder("gbs", 2)])
The equivalent event definitions for the above in Java are defined below:
import com.apama.jmon.Event;
 
public class DataHolder extends Event {
  /** Event fields */
  public String name;
  public long age;
   
  /** No argument constructor
  */
  public DataHolder () {
    this("", 0);
   }
   /** Construct a DataHolder object and set the instance variables
   */
   public DataHolder (String n, long a) {
     name = n;
     age = a;
   }
}
 
import com.apama.jmon.Event;
 
public class SequenceEvent extends Event {
 
   /** Event field */
   public DataHolder[] people;
 
   /** No argument constructor
   */
   public SequenceEvent() {
     this( new DataHolder[]{} );
   }
 
   /** Construct a SequenceEvent object and set the instance variable
   */
   public SequenceEvent(DataHolder[] p) {
     this.people = p;
   }
}
Sample Java code to create and emit a SequenceEvent event is shown below:
  s = new SequenceEvent(new DataHolder[] {new DataHolder("kap", 1),
   new DataHolder("gbs", 2)} );
s.emit();
Events can also include Map types, which are equivalent to EPL dictionary types. When you use Map types, Apama recommends that you use generic maps whenever you can. For example, in EPL the following event is a dictionary of dictionaries and each internal dictionary is a sequence of SimpleEvent types:
event ComplexEvent {
  dictionary <string,
    dictionary <string, sequence<SimpleEvent> > > complex;
}
You can implement this in Java as follows:
import java.util.Map;
import java.util.HashMap;
import com.apama.jmon.Event;
import com.apama.jmon.annotation.EventType;
 
@EventType(description = "Event that contains a field with a complex structure")
public class ComplexEvent extends Event {
 
   /** Event field */
   public Map<String, Map<String, SimpleEvent[]>> complex;
   /**
   * No argument constructor
   */
   public ComplexEvent() {
     this(new HashMap<String, Map<String, SimpleEvent[]>>());
   }
 
   /**
   * Construct a ComplexEvent object, set the instance variable complex
   *
   * @param complex The dictionary/Map to use as the field value
   */
   public ComplexEvent(
     Map<String, Map<String, SimpleEvent[]>> complex) {
       this.complex = complex;
     }
}
This example is provided in its complete form as a sample. It is distributed in the folder samples/java_monitor/complex_event/.
Copyright © 2013 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or Terracotta Inc., San Francisco, CA, USA, and/or Software AG (Canada) Inc., Cambridge, Ontario, Canada, and/or, Software AG (UK) Ltd., Derby, United Kingdom, and/or Software A.G. (Israel) Ltd., Or-Yehuda, Israel and/or their licensors.