How to read element of managedObject in EPL App

Hello,
I would like to read each element of the managed object shown in the picture:

How can I iterate through it?
Specifically, I would like to extract the “childDevicesIds” and “email” values.
The code I currently have for printing this is as follows:

action onload() {
		integer reqId := com.apama.cumulocity.Util.generateReqId();
   
		com.apama.cumulocity.FindManagedObject request := 
		new com.apama.cumulocity.FindManagedObject;
		request.reqId := reqId;
			
		// Optionally provide the 'id' of the managed object
		request.deviceId := "1523844618";
			
		// Filter based on fragmentType
		request.params.add("fragmentType", "c8y_IsDevice");
			
		// Subscribe to com.apama.cumulocity.FindManagedObjectResponse.SUBSCRIBE_CHANNEL to 
		// listen for responses
		monitor.subscribe(com.apama.cumulocity.FindManagedObjectResponse.SUBSCRIBE_CHANNEL);
			
		// Listen for responses based on reqId
		on all com.apama.cumulocity.FindManagedObjectResponse(reqId=reqId) as response
		// Avoid listener leaks by terminating the listener on completion of the request
		and not com.apama.cumulocity.FindManagedObjectResponseAck(reqId=reqId)
		{
			log "Received ManagedObject " + response.managedObject.toString();
		}
			
		// Listen for com.apama.cumulocity.FindManagedObjectResponseAck, 
		// this indicates that all responses have been received
		on com.apama.cumulocity.FindManagedObjectResponseAck(reqId=reqId) 
		as requestCompleted
		{
			log "Received all ManagedObject(s) for request " 
			+ requestCompleted.reqId.toString() at INFO;
			
			// Request is completed and we can unsubscribe from this channel
			monitor.unsubscribe(com.apama.cumulocity.FindManagedObjectResponse.SUBSCRIBE_CHANNEL);
		}
			
		// Send request to find available managed objects
		send request to com.apama.cumulocity.FindManagedObject.SEND_CHANNEL;
	}
1 Like

Hi,

the childDeviceIds are a field of the managed object:
https://documentation.softwareag.com/pam/10.15.3/en/webhelp/related/ApamaDoc/com/apama/cumulocity/ManagedObject.html#childDeviceIds

so you can just do:

sequence<string> ids := response.managedObject.childDeviceIds;

The emails information is part of the “params” field:
https://documentation.softwareag.com/pam/10.15.3/en/webhelp/related/ApamaDoc/com/apama/cumulocity/ManagedObject.html#params

This is is a dictionary and you have multiple options to extract from it (assuming “email” is a top level element in params which is a bit hard to read from your screenshot):

// Option 1: do only if you KNOW that email exists, will crash your EPL App if it does not
sequence<any> emails := response.managedObject.params["email"];

// Option 2: safe; use if you only need to do something if the field exists
if(response.managedObject.params,hasKey("email") {
  sequence<any> emails := response.managedObject.params["email"];
}

// Option 3: safe; do if you can work with a default value
sequence<any> emails := response.managedObject.params.getOrDefault("email")

// Option 4: safe; do if you can define your own default value
sequence<any> emails := response.managedObject.params.getOr("email", new sequence<any>)

1 Like

I will attach here of the structure breakdown, to have a better understanding:


What I want to be able to parse is the array of two number that is underlined in red. How can I do that?
Thank you again.

1 Like

ok in that case the AnyExtractor might actually make things easier:
https://documentation.softwareag.com/pam/10.15.3/en/webhelp/related/ApamaDoc/com/apama/util/AnyExtractor.html

AnyExtractor ex := AnyExtractor.create(response.managedObject.params["emailConfiguration"]);
integer i1 := ex.getInteger("childDevicesIds[0]");
integer i2 := ex.getInteger("childDevicesIds[1]");

Important: if these IDs are supposed to be Cumulocity device ids you should change their type to string. While the device IDs are numerical they are stored as strings and there is no guarantee that they can be represented as integers.

With that code I get the following error:

 Error on line 414 in action getMember in event com.apama.util.AnyExtractor: ParseException - Unable to parse "childDevicesIds" as integer - /opt/softwareag/Apama/monitors/AnyExtractor.mon

What should I do?
Same things happens if I try with string, sequence.

Please have a read of the documentation I shared with that it should be easier for you than for me to do the right navigation as you understand your data model better than I. It looks like there is an additional array in your data model that I missed. This should work:

			AnyExtractor ex := AnyExtractor.create(resp.managedObject.params["emailConfiguration"]);
			integer i1 := ex.getInteger("[0].childDevicesIds[0]");
			integer i2 := ex.getInteger("[0].childDevicesIds[1]");

How can I get the size/length of the resp.managedObject.params[“emailConfiguration”]?

Please consult the documentation for fundamental EPL language features:
https://documentation.softwareag.com/pam/10.15.4/en/webhelp/pam-webhelp/

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.