Command syntax

Each rule contains a sequence of commands introduced by the keyword commands, separated by semicolon symbol ; and surrounded by braces.

Node deletion

The following command removes the A node and all its incident edges.

del_node A

Node creation

To create a new node, the command is add_node. The command below creates a new node and give it the identifier A until the end the rule application.

add_node A

Moreover, if the node must be placed at a specific position in the linear order of the nodes, two syntax are available: the new node B (resp. C) is placed on the immediate left (resp. right) of the node N.

add_node B :< N
add_node C :> N

Edge deletion

To delete an edge, the del_edge command can refer either to the full description of the edge or to an identifier e given in the pattern:

del_edge A -[obj]-> B;
del_edge e;

NOTE: for the first syntax, if the corresponding edge does not exists, an exception is raised and the full rewriting process is stopped.

Add a new edge

The basic syntax to add a new edge is:

add_edge N -[suj]-> M

It is also possible to give a name to the newly created edge, in order to manipulate it in the next commands. For instance the two commands below create a new edge f and copy the edge label of some other edge e.

add_edge f: N -[suj]-> M;    % this supposes that `f` is a fresh name in the rule
f.label = e.label;           % this supposes that `e` is a known edge in the rule

NB: the syntax above (add_edge f:…) has changed in version 1.4, please see here for info about migration.

Edge redirection

Commands are available to move globally incident edges of some node of the pattern. keywords are shift_in, shift_out and shift, respectively for moving in-edges, out-edges and all incident edges.

Brackets can be used to select the set of edges to move according to their labelling.

  shift A ==> B
  shift_out B =[suj|obj]=> C
  shift_in C =[^suj|obj]=> D

The action of the 3 commands above are respectively:

Modification of an existing edge

In commands, it is possible to manipulate subpart of edges. If the pattern binds the identifier e to some edge (with the syntax e: X -[…]-> Y), the following commands can be used:



Example: copy an edge feature value

Rule: copy.grs:

rule main {
  pattern { e1: N -[1=X]-> M; e2: M -[!2]-> T }
  commands { e2.2 = e1.2 }
}
Input graph: copy.json Rewritten graph
input output


The next couple of examples show that modifying an edge before copying it is different from the reverse order

Example: modify and edge and copy it

Rule: modify_and_copy.grs:

rule main {
  pattern { e1: N -[1=X]-> M; M -> T }
  commands {
    e1.2 = U;
    add_edge new_e: M -> T;
    new_e.label = e1.label;
  }
}

Input graph: copy.json Rewritten graph
input output

Example: copy and edge and modify it

Rule: copy_and_modify.grs:

rule main {
  pattern { e1: N -[1=X]-> M; M -> T }
  commands {
    add_edge new_e: M -> T;
    new_e.label = e1.label;
    e1.2 = U;
  }
}

Input graph: copy.json Rewritten graph
input output


Example: reverse an edge

GRS: reverse.grs:

rule r {
  pattern { N -[X]-> M; e: N -[Y|Z]-> M }
  commands {
    add_edge new_e: M -> N;
    new_e.label = e.label;
    del_edge e;
  }
}
strat main { Onf (r) }

Input graph: reverse.json Rewritten graph
input output

By contrast, with the GRS: fail_reverse.grs:

rule r {
  pattern { N -[X]-> M; e: N -[Y|Z]-> M }
  commands {
    del_edge e;
    add_edge new_e: M -> N;
    new_e.label = e.label;  % <== this command cannot be executed because `e` doesn't exist anymore
  }
}
strat main { Onf (r) }

the command grew transform -grs fail_reverse.grs applied to the same graph produces the error:

MESSAGE : [file: fail_reverse.grs, line: 6] Unknown identifier "e"


Effective commands

There are situations where commands may not be properly defined. Consider the rule r below and and a graph with two nodes A and B linked by two edges X and Y both going from A to B. The rule R can be applied to the graph but the command add_edge can not be applied (the edge labelled Y already exists).

rule r {
  pattern { N -[X]-> M }
  commands { add_edge N -[Y]-> M }
}

The fact that a rule is effective or not depends on the graph on which the rule is applied.

In fact, to deal with this case, Grew has two running modes with different behaviors.

  1. In the default mode, ineffective commands are simply ignored and the rewriting goes on with the next commands or rules.
  2. In the safe mode (set by the -safe_commands argument), an ineffective command stop the rewriting with and produces an exection error.

List of ineffective Commands

Command which may be ineffective are:

Note that it is always possible to define a Graph Rewriting System with only effective commands following the procedure: