Use Element Class as Variable Types – nodetype, edgetype, elementtype


For all functions available in Soley Studio (those originally included in GrGen and those developed by Soley developers), it is now possible to use an element class (node class or edge class) as a variable type, like any other types. Three new types are available: edgetypenodetype, or elementtype. The latter is a type that can contain any edge type or node type. A variable of one of these three types can be used as an input in all rules, sequences, and procedures.

Example for nodetype

The following procedure uses an input parameter of type nodetype, adds a node of this type, writes the number of nodes of this type to a variable and returns it. The functions add() and countNodes() are built-in GrGen functions.

procedure addANode(var someNodeClass:nodetype) : (int) {
  (def nn:Node) = add(someNodeClass);
  def var i:int = countNodes(someNodeClass);

Examples for edgetype

The following rule uses three input parameters,  the first being of type edgetype, the second a node of class MyNodeClass and the third a node of class MyNodeClass. The rule builds an edge of type edgetype between the two nodes (source and target).

// Call with e.g. addAnEdge(Contains, n1, n2)
rule addAnEdge(var varEdgeClass:edgetype, sourceNode:MyNodeClass, targetNode:MyNodeClass) { 
     def ?-ee:AEdge-?;
     eval { (yield ee) = add(varEdgeClass, sourceNode, targetNode); }

As another example, the next rule takes in a parameter of type edgetype, compares if an edge is of this type, i.e. if it is of this edge class, and deletes the edge if true.

rule  deleteEdgeByType(var varEdgeClass:edgetype) {
  if{varEdgeClass == typeof(ee);}
    delete (ee);

Example for elementtype

It is also possible to use elementtype, which is a type that can contain any edge type or node type. The following procedure takes in a node of class Node and returns its type.

procedure pgetType (n:Node) : (elementtype) {

Tips & Tricks

Name of a type variable needed as a string
Unlike in rules, the typeof() function returns a string when used in sequences. Here is a trick on how to create the string from the type variable:

 sequence emitIfNodeIsOfClass(someNodeClass:nodetype) {
    for{n:Node in nodes(Node);
      if{ {{ (""+someNodeClass == typeof(n)) }};  //workaround to convert someNodeClass to string 
        {emit(n + " is of class " + someNodeClass + "\n")}

The type types can often be combined with the Graph_getNodeAttribute and Graph_setNodeAttribute library functions .

Was this article helpful?

Related Articles