Table of Contents

Using a MAP property in a ValueObject

This article covers the advanced topic of using a MAP property in a ValueObject to pass multiple key/value data pairs to another component or bean in MIStudio or TransSECS. Using a ValueObject with a MAP property is a convenient way to pass multiple values to a script. This will be the focus of this article.

Some beans have mapTriggers (i.e., TriggeredHistorical) which requires a ValueObject input with a MAP property). There is a TriggeredHistorical example here: Scripting for Database Servers (in MIStudio and TransSECS) which needs a LinkedHashMap.

Setting up the ValueObject "MAP" Property

ValueObjects can have user defined “properties” attached to them by using the .setProperty() method on the ValueObject. For this article we will use a property called “MAP” which is a Java HashMap object. A LinkedHashMap is required by some ErgoTech beans as can be seen in the JavaDocs, but for this example we only need the regular HashMap.

To use a HashMap in your code you will need to start your script with this definition:

HashMap = Java.type("java.util.HashMap");

For this example we will create one ValueObject (a LongValueObject) and add a HashMap to it which will contain the values we want to pass to our script.

LongValueObject = Java.type("com.ergotech.vib.valueobjects.LongValueObject");
//fill a HashMap with our key,value pairs
 
map = new HashMap();
map.put("age",50);
map.put("name","Joe Doe");
map.put("color",java.awt.Color.green); //you can put Objects into the HashMap
 
//add the "map" to a ValueObject so we can pass this to the script
 
vo = new LongValueObject(100); //the value can be used by the script, or ignored
 
vo.setProperty("MAP",map); //this is all you need to do before you send the value to the script
 
//send it to the script
 
/Devices/UtilityServers_Servers/CalculateSomething->setValueObject(vo); //must use setValueObject so the MAP property is not striped (not, for example, setLongValue())

Using the MAP property in your Script

An example of a script which extracts the MAP property and gets the various values based on the key

if (incomingValue.getQuality()==0) {
 
 //following from the above example, incomingValue is a LongValueObject with a property called "MAP" which contains a HashMap of key/value pairs
 
vo = incomingValue;
map = incomingValue.getProperty("MAP");
 
//you can test what class map is as a sanity check
print("The MAP property is a "+map.getClass().getName()); //works as long as map is not null
 
value1 = map.get("name");
value2 = map.get("age");
color = map.get("color"); //this is an awt Color object
//test color, get the r,g,b values
print("color is:" + "r:"+color.getRed()+" g:"+color.getGreen()+" b:"+color.getBlue());
 
//you can use the values passed in the MAP property in your script....
 
}
incomingValue;