Variables in Sequences – Creating and Assigning

Prelude: GrGen.net Modeling Language and Syntax Varieties

Soley software solutions are built upon the modeling language GrGen.net. This is a very flexible and powerful language for graph modeling and transformation. However, it uses different syntaxes for rules, sequences and functions/procedures. In the following article, we will explain you how to use variables in sequences. The article here explains how variables are created and assigned inside rules. You’ll find a brief overview over the main syntax in our Cheat Sheet.


Creating variables and direct assignment of values

Here is how to create variables in a sequence and how to directly assign values to them:

sequence testSeq1 {
   stringvar1:string ;>  //Create a string variable (primitive type) named stringvar1
   stringvar1 = "some string" ;>  //Assign a value to the variable
   stringvar2:string = "some other string" ;>  //Create and assign in one go
   stringarr1:array<string> = array<string>[];>  //Create an empty string array variable (container type)
   materials1:set<string> = set<string>{} ;>  //Create an empty string set variable (container type)
   materials2:set<string> = set<string>{"a","b","c"}  //Create and fill in one go  
}

The given examples cover strings, but the process is the same for other primitive types, element types (such as node or edge classes) or container types (such as arrays, sets, maps or deques).

NOTE: When creating container type variables you should always declare them as empty. Containers contain NULL by default which can cause errors when trying to operate with them. A container defined as empty will not cause such errors.

Assigning return values to primitive type variables

You can also assign return values of functions, procedures or rules to primitive type variables created in sequences:

sequence testSeq2 {
   stringvar:string ;>  //Create a string variable
   { stringvar = someFunction() } ;>  //Assigning the return value of a function
   { (stringvar) = someProcedure() } ;>  //Assigning the return value(s) of a procedure
   (stringvar) = someRule  //Assigning the return value of a rule
}

As you can see from above examples, you have to pay close attention to the parenthesis.

For functions: To call a function in a sequence it needs to be enclosed in curly brackets. Functions can only have one return value. Thus, assigning the return value to a variable can be done without further parenthesis within the curly brackets.

For procedures: To call a procedure in a sequence it needs to be enclosed in curly brackets. Procedures can have multiple return values. Thus, assigning the return value(s) to one or more variables requires the variable(s) to be enclosed in parenthesis within the curly brackets.
If the procedure returns more than one value, list the variable separated with commas ( { ( variable1, variable2, … ) = someProcedure }. Order the variables according to the definition in your procedure.

For rules: A rule can be called in a sequence without any parenthesis. Rules can have multiple return values. Thus, assigning the return value(s) to one or more variables requires the variable(s) to be enclosed in parenthesis.
If the rule returns more than one value, list the variables separated with commas ( { ( variable1, variable2, … ) = someProcedure }. Order the variables according to the definition in your rule.

Assigning multiple return values to container type variables

You can also assign multiple return values of functions, procedures or rules to container type variables created in sequences:

sequence testSeq3 {
   (allStrArray:array<string>) = [someRule] ;>  //Create a string array and fill it with the string return value of a rule "someRule"
   uniqueStrSet:set<string> = set<string>{} ;>  //Create an empty string set
   for{str:string in allStrArray;  //Do a for loop over all elements in the array
      {uniqueStrSet.add(str)}  //Use the set operation .add() to add the elements to the set
   }
}

rule someRule1 ():(string){
   n:MyNodeClass;

   modify{
      return(n.stringattribute);
   }
}

NOTE: You can only create arrays with several return values, not sets! If you want to create a set, first build an array and then add the elements to a set. Remember that arrays can store duplicate elements (e.g. [3,3]) while sets only contain unique values. 

Was this article helpful?

Related Articles