This article explains the usage of the library functions Graph_setNodeAttribute and Graph_setEdgeAttribute. These functions allow for a very flexible assignment of a value to an attribute of a graph element (node or edge). The attribute name and value, independent of the data type, are defined as input parameters.
Table of Contents
When to use
This library element assigns an attribute value specified by variable or directly in place to an attribute of a node or edge element. This method handles conversion to the target type and returns boolean true if the assignment executed correctly. An explicit cast to the type object is required for the value argument handed in, no matter which basic data type it is. These library elements are very useful when providing the input parameters as input variables, which allows for a very flexible assignment of values to attributes of different data types.
Graph_setNodeAttribute(NODE_INSTANCE, ATTRIBUTE_NAME, ATTRIBUTE_VALUE):SUCCESS
This is a function and needs to be called accordingly in rules, sequences, functions or procedures.
- NODE_INSTANCE:Node – the identifier for a node instance
- ATTRIBUTE_NAME:string – name of an attribute of the referenced element
- ATTRIBUTE_VALUE:object- value to be set, e.g. “hello” or 3.32 or true
- SUCCESS:boolean – true if assignment executed correctly
The library function returns a boolean true if the assignment of the attribute value to the attribute succeeded, else it returns false. The original data type, e.g. string or int, must be cast to the type object (see example below).
Equal to Graph_setNodeAttribute but for edge elements. Please see the description above.
//Set the value of an element attribute
def var success:boolean = Graph_setNodeAttribute(n, "name", (object)"abc");
//Assign the string "abc" to the attribute name of the node n of class Product
emit("Assignment of value to name attribute successful: " + success + "\n");
This example rule looks for all nodes of class Product (if called in ) in the graph (left-hand side) and sets the string abc as value of the attribute name. The success of the operation is subsequently displayed in the output pad by using the emit statement. After running the sequence, the value of the attribute name should be abc for each Product node. Note that the string value “abc” has to be explicitly cast to type object for the function to work.
Where to find the library elements
Both library elements are part of the Foundations/Graph library that comes with Soley Studio. Go to the Libraries tool and select the category Graph –> Handle Properties –> Graph_setNodeAttribute or Graph_setEdgeAttribute.