NAME
gpr - graph pattern scanning and processing language
SYNOPSIS
gpr [-icV?] [ -o
outfile ] [ -a args ] [ 'prog' |
-f progfile ] [ files ]
DESCRIPTION
gpr is a graph stream editor inspired by
awk. It copies input graphs to its output, possibly
transforming their structure and attributes, creating new graphs,
or printing arbitrary information. The graph model is that provided
by libagraph(3)
dot language.
Basically, gpr traverses each input graph, denoted by
$G, visiting each node and edge, matching it with the
predicate-action rules supplied in the input program. The rules are
evaluated in order. For each predicate evaluating to true, the
corresponding action is performed. During the traversal, the
current node or edge being visited is denoted by $.
For each input graph, there is a target subgraph, denoted by
$T, initially empty and used to accumulate chosen entities,
and an output graph, $O, used for final processing and then
written to output. By default, the output graph is the target
graph. The output graph can be set in the program or, in a limited
sense, on the command line.
OPTIONS
The following options are supported:
- -a args
- The string args is split into whitespace-separated
tokens, with the individual tokens available as strings in the
gpr program as ARGV[0],...,ARGV[ARGC-1].
- -c
- Use the source graph as the output graph.
- -i
- Derive the node-induced subgraph extension of the output graph
in the context of its root graph.
- -o outfile
- Causes the output to be written to the specified file; by
default, output is written to stdout.
- -f progfile
- Use the contents of the specified file as the program to
execute on the input. If -f is not given, gpr will
use the first non-option argument as the program.
- -v
- Causes the program to print version information and exit.
- -?
- Causes the program to print usage information and
exit.
OPERANDS
The following operand is supported:
- files
- Names of files containing 1 or more graphs in dot language. If
no -f option is given, the first name is removed from the
list and used as the input program. If the list of files is empty,
the standard input will be used.
PROGRAMS
A gpr program consists of list of
predicate-action clauses, having one of the forms:
- BEGIN { action }
- BEG_G { action }
- N [ predicate ] { action }
- E [ predicate ] { action }
- END_G { action }
- END { action }
A program can contain at most one of each of the BEGIN,
BEG_G, END_G and END clauses. There can be any
number of N and E statements, the first applied to
nodes, the second to edges. The top-level semantics of a gpr
program are: Evaluate the BEGIN clause, if any. For each
input graph G {
Set G as the current graph and current object.
Evaluate the BEG_G clause, if any.
For each node and edge in
G {
Set the node or edge as the current object.
Evaluate the N or
E clauses, as appropriate.
}
Set G as the current object.
Evaluate the END_G clause, if any.
} Evaluate the END clause, if any. The actions of the
BEGIN, BEG_G, END_G and END clauses are
performed when the clauses are evaluated. For N or E
clauses, either the predicate or action may be omitted. If there is
no predicate with an action, the action is performed on every node
or edge, as appropriate. If there is no action and the predicate
evaluates to true, the associated node or edge is added to the
target graph.
Predicates and actions are sequences of statements in the C
dialect supported by libexpr(3)
library. The only difference between predicates and actions is that
the former must have a type that may interpreted as either true or
false. Here the usual C convention is followed, in which a non-zero
value is considered true. This would include non-empty strings and
non-empty references to nodes, edges, etc. However, if a string can
be converted to an integer, this value is used.
In addition to the usual C base types (void, int, char, string,
float, long, unsigned and double), gpr provides the
graph-based types node_t, edge_t, graph_t and obj_t. The obj_t type
can be viewed as a supertype of the other 3 concrete types; the
correct base type is maintained dynamically. Besides these base
types, the only other supported type expressions are (associative)
arrays.
Constants follow C syntax, but strings may be quoted with either
"..." or '...'. In certain contexts, string values
are interpreted as patterns for the purpose of regular expression
matching. Patterns use ksh(1) file
match pattern syntax. gpr uses C++ comments.
A statement can be a declaration of a function, a variable or an
array, or an executable statement. For declarations, there is a
single scope. Array declarations have the form:
-
type array [ var ]
where the var is optional. Executable statements can be
one of the following:
-
{ [ statement ... ] }
expression # commonly var = expression
if( expression ) statement [ else statement ]
for( expression ; expression ; expression ) statement
for( array [ var ]) statement
while( expression ) statement
switch( expression ) case statements
break [ expression ]
continue [ expression ]
return [ expression ]
In the second form of the for statement, the variable
var is set to each value used as an index in the specified
array and then the associated statement is evaluated.
Function definitions can only appear in the BEGIN clause.
Expressions include the usual C expressions. String comparisons
using == and != treat the right hand operand as a
pattern. gpr will attempt to use an expression as a string
or numeric value as appropriate.
Expressions of graphical type (i.e., graph_t, node_t, edge_t,
obj_t) may be followed by a field reference in the form of
.name. The resulting value is the value of the
attribute named name of the given object. In addition, in
certain contexts an undeclared, unmodified identifier is taken to
be an attribute name. Specifically, such identifiers denote
attributes of the current node or edge, respectively, in N
and E clauses, and the current graph in BEG_G and
END_G clauses.
As usual in the libagraph(3)
model, attributes are string-valued. In addition, gpr
supports certain pseudo-attributes of graph objects, not
necessarily string-valued. These reflect intrinsic properties of
the graph objects and cannot be assigned to.
- head
- the head of an edge.
- tail
- the tail of an edge.
- name
- the name of an edge, node or graph. The name of an edge has the
form
"<tail-name><edge-op><head-name>[
<key>]", where <edge-op> is
"->" or "--" depending on whether the graph is
directed or not. The bracket part
[<key>] only appears if the edge has a
non-trivial key.
- indegree
- the indegree of a node.
- outdegree
- the outdegree of a node.
- degree
- the degree of a node.
- root
- the root graph of an object. The root of a root graph is
itself.
- parent
- the parent graph of a subgraph. The parent of a root graph is
NULL
- n_edges
- the number of edges in the graph
- n_nodes
- the number of nodes in the graph
- directed
- true if the graph is directed
- strict
- true if the graph is strict
BUILT-IN FUNCTIONS
The following functions are built into gpr. Those
functions returning references to graph objects return NULL in case
of failure.
Graphs and subgraph
- graph(s, t)
- creates a graph whose name is s and whose type is
specified by the string t. Ignoring case, characters U, D,
S, N have the interpretation undirected, directed, strict, and
non-strict, respectively. If t is empty, a directed,
non-strict graph is generated.
- subg(g, s)
- creates a subgraph in graph g with name s. If the
subgraph already exists, it is returned.
- isSubg(g, s)
- returns the subgraph in graph g with name s, if
it exists.
- fstsubg(g)
- returns the first subgraph in graph g.
- nxtsubg(sg)
- returns the next subgraph after sg.
- isDirect(g)
- returns true if and only if g is directed.
- isStrict(g)
- returns true if and only if g is strict.
- nNodes(g)
- returns the number of nodes in g.
- nEdges(g)
- returns the number of edges in g.
Nodes
- node(g, s)
- creates a node in graph g of name s. If such a
node already exists, it is returned.
- subnode(g, n)
- inserts the node n into the subgraph g. Returns
the node.
- fstnode(g)
- returns the first node in graph g.
- nxtnode(n)
- returns the next node after n.
- isNode(g, s)
- looks for a node in graph g of name s. If such a
node exists, it is returned.
Edges
- edge(t, h, s)
- creates an edge with tail node t, head node h and
name s. If the graph is undirected, the distinction between
head and tail nodes is unimportant. If such an edge already exists,
it is returned.
- subedge(g, e)
- inserts the edge e into the subgraph g. Returns
the edge.
- isEdge(t, h,
s)
- looks for an edge with tail node t, head node h
and name s. If the graph is undirected, the distinction
between head and tail nodes is unimportant. If such an edge exists,
it is returned.
- fstout(n)
- returns the first out edge of node n.
- nxtout(e)
- returns the next out edge after e.
- fstin(n)
- returns the first in edge of node n.
- nxtin(e)
- returns the next in edge after e.
- fstedge(n)
- returns the first edge of node n.
- nxtedge(e)
- returns the next edge after e.
Graph I/O
- write(g)
- prints g in dot format on the output stream.
- writeG(g, fname)
- prints g in dot format into the file fname.
- fwriteG(g, fd)
- prints g in dot format onto the open stream denoted by
the integer fd.
- readG(fname)
- returns a graph read from the file fname. The graph
should be in dot format.
- freadG(fd)
- returns the next graph read from the open stream fd.
Return NULL at end of file.
Graph miscellany
- delete(g, x)
- deletes object x from graph g. If g is
NULL, the function uses the root graph of x. If x is
a graph or subgraph, it is closed unless x is locked.
- isIn(g, v)
- returns true if v is in subgraph g. If v
is a graph, this indicates that g is the parent graph of
v.
- clone(g, x)
- creates a clone of object x in graph g. In
particular, the new object has the same name/value attributes and
structure as the original object. If an object with the same key as
x already exists, its attributes are overlaid by those of
x and the object is returned. If an edge is cloned, both
endpoints are implicitly cloned. If a graph is cloned, all nodes,
edges and subgraphs are implicitly cloned. If x is a graph,
g may be null, in which case the cloned object will be a new
root graph.
- copy(g, x)
- creates a copy of object x in graph g, where the
new object has the same name/value attributes as the original
object. If an object with the same key as x already exists,
its attributes are overlaid by those of x and the object is
returned. Note that this is a shallow copy. If x is a graph,
none of its nodes, edges or subgraphs are copied into the new
graph. If x is an edge, the endpoints are created if
necessary, but they are not cloned. If x is a graph,
g may be null, in which case the cloned object will be a new
root graph.
- induce(g)
- extends g to its node-induced subgraph extension in its
root graph.
- compOf(g, x)
- returns the connected component of the graph g
containing node x, as a subgraph of g. The subgraph
only contains the nodes. One can use induce to add the
edges. The function fails if x is not in g.
- lock(g, v)
- implements graph locking. If the integer v is positive,
the graph is set so that future calls to delete have no
immediate effect. If v is zero, the graph is unlocked. If
there has been a call to delete while the graph was locked,
the graph is closed. If v is negative, nothing is done. In
all cases, the previous lock value is returned.
Strings
- sprintf(fmt, expr,
...)
- returns the string resulting from formatting expr ...
according to the (3)
format fmt
- gsub(str, pat)
- gsub(str, pat,
repl)
- returns str with all substrings matching pat
deleted or replaced by repl, respectively.
- sub(str, pat)
- sub(str, pat,
repl)
- returns str with the leftmost substring matching
pat deleted or replaced by repl, respectively. The
characters '^' and '$' may be used at the beginning and end,
respectively, of pat to anchor the pattern to the beginning
or end of str.
- substr(str, idx)
- substr(str, idx,
len)
- returns the substring of str starting at position
idx to the end of the string or of length len,
respectively.
- length(s)
- returns the length of the string s.
- index(s, t)
- returns the index of the character in string s where the
leftmost copy of string t can be found, or -1 if t is
not a substring of s.
- match(s, p)
- returns the index of the character in string s where the
leftmost match of pattern p can be found, or -1 if no
substring of s matches p.
- canon(s)
- returns a version of s appropriate to be used as an
identifier in a dot file.
- xOf(s)
- returns the string "x" if s has the form "x,y".
- yOf(s)
- returns the string "y" if s has the form "x,y".
- llOf(s)
- returns the string "llx,lly" if s has the form
"llx,lly,urx,ury".
- urOf(s)
- returns the string "urx,ury" if s has the form
"llx,lly,urx,ury".
I/O
- print( expr, ... )
- prints a string representation of each argument in turn onto
stdout, followed by a newline. Returns 0 on success.
- printf([fd], fmt,
expr, ...)
- prints the string resulting from formatting expr ...
according to the (3)
format fmt. Returns 0 on success. By default, it prints on
stdout. If the optional integer fd is given, output
is written on the open stream associated with fd.
- openF(s, t)
- opens the file s as an I/O stream. The string argument
s specifies how the file is opened. The arguments are the
same as for the C function (3)
- returns an integer denoting the stream,
- or -1 on error.
As usual, streams 0, 1 and 2 are already open as stdin,
stdout, and stderr, respectively. Since gpr
may use stdin to read the input graphs, the user should
avoid using this stream.
- closeF(fd)
- closes the open stream denoted by the integer fd.
Streams 0, 1 and 2 cannot be closed.
- readL(fd)
- returns the next line read from the input stream fd. It
returns the empty string "" on end of file. Note that the newline
character is left in the returned string.
Miscellaneous
- exit([expr])
- causes gpr to exit with the exit code expr.
expr defaults to 0 if omitted.
- sqrt(d)
- returns the square root of the double d.
BUILT-IN VARIABLES
gpr provides certain special, built-in variables, whose
values are set automatically by gpr depending on the
context. Except as noted, the user cannot modify their values.
$ denotes the current object (node, edge, graph) depending
on the context. It is not available in BEGIN or END
clauses.
- $F
- is the name of the current input file.
- $G
- denotes the current graph being processed. It is not available
in BEGIN or END clauses.
- $O
- denotes the output graph. Before graph traversal, it is
initialized to the target graph. After traversal and any
END_G actions, if it refers to a non-empty graph, that graph
is printed. It is only valid in N, E and END_G
clauses. The output graph may be set by the user.
- $T
- denotes the current target graph. It is a subgraph of $G
and is available only in N, E and END_G
clauses.
- $tgtname
- denotes the name of the target graph. By default, it is set to
"gpr_result". If used multiple times during the execution of
gpr, the name will be appended with an integer. The target
graph name may be set by the user.
- $tvroot
- indicates the starting node for a depth-first traversal of the
graph (cf. $tvtype below). The default value is NULL for
each input graph.
- $tvtype
- indicates how gpr traverses a graph. If it has the value
TV_flat, gpr will a simple, flat traversal, with
graph objects visited in seemingly arbitrary order. If it has the
value TV_dfs, gpr will traverse the graph using a
depth-first search. By default, it is set to TV_flat, The
traversal type may be set by the user.
- ARGC
- denotes the number of arguments specified by the -a
args command-line argument.
- ARGV
- denotes the array of arguments specified by the -a
args command-line argument. The ith argument is given
by ARGV[i].
EXAMPLES
gpr -i 'N[color=="blue"]' file.dot Generate the
node-induced subgraph of all nodes with color blue. gpr -c
'N[color=="blue"]{color = "red"}' file.dot Make all blue nodes red.
BEGIN { int n, e; int tot_n = 0; int tot_e = 0; } BEG_G {
n = nNodes($G);
e = nEdges($G);
printf ("%d nodes %d edges %s, n, e, $G.name);
tot_n += n;
tot_e += e; } END { printf ("%d nodes %d
edges total, tot_n, tot_e) } Version of the program gc. gpr
-c "" Equivalent to nop. BEG_G { graph_t g = graph ("merge",
"S"); } E {
node_t h = clone(g,$.head);
node_t t = clone(g,$.tail);
edge_t e = edge(t,h,"");
e.weight = e.weight + 1; } END_G {
$O = g; } Produces a strict version of the input graph, where the
weight attribute of an edge indicates how many edges from the input
graph the edge represents. BEGIN {node_t n; int deg[]}
E{deg[head]++; deg[tail]++; } END_G {
for (deg[n]) {
printf ("deg[%s] = %d, n.name, deg[n]);
} } Computes the degrees of nodes with edges.
BUGS
When the program is given as a command line argument,
the usual shell interpretation takes place, which may affect some
of the special names in gpr. To avoid this, it is best to
wrap the program in single quotes.
There is a single scope and the extent of all variables is the
entire life of the program. It might be preferable for scope to
reflect the natural nesting of the clauses, or for the program to
at least reset locally declared variables.
The expr library does not support NULL strings. This means we
can't distinguish between empty and NULL edge keys. For the
purposes of looking up and creating edges, we translate "" to be
NULL, since this latter value is necessary in order to look up any
edge with a matching head and tail.
The language inherits the usual C problems such as dangling
references and the confusion between '=' and '=='.
AUTHOR
Emden R. Gansner <erg@research.att.com>
SEE ALSO
awk(1), gc(1), dot(1), nop(1), libexpr(3),
libagraph(3)