6.3.3 Do
loop
The do loop iterates
over a set of things, navigating from the current element. For example one could
query the relationships of an object like this:
do >relType
{/* Operations for relationships */ }
As with
foreach, level numbers can modify the
stack level from which the search starts: see Section
6.1.9. More complex type patterns can be
specified as in Section
6.1.6, and the
resulting elements can be filtered and ordered as in Section
6.3.4.
Note that do
loops differ from foreach.
Foreach will always look at the current
graph, and can only iterate over elements directly contained in that graph:
objects, or roles, relationships and ports in bindings.
Do and
dowhile will look at the current
element, and iterate over those instances of the specified type that are
attached to the current element in the current context. For this reason,
foreach will normally be the first loop
in a generator, and sub-loops will be
do or
dowhile, until you follow a link into a
new graph. It is also possible to use
do and
dowhile directly at the graph level,
e.g. do .() will then iterate over all
objects in the graph.
The dowhile
clause is a variant of do, differing
only in that it skips any trailing literal strings or
newlines on its last iteration over a
given set of elements. This is useful for avoiding trailing separators such as
commas or spaces after the last item to be output, e.g.
do .Process {id ', '} produces “1.0, 1.1, 1.2,”
dowhile .Process {id ', '} produces “1.0, 1.1, 1.2”
Do
and dowhile can also with an argument
of a complex property, either a collection or a property pointing to an object
or other non-property. With a collection, the loop is executed once for each
item in the collection. With a property whose value is an object or other
non-property, the loop is executed once for that object; if there is no object
attached, the loop is not executed.
Special
do and
dowhile loops also exist for iterating
over lines of text properties, variables etc., over all properties of an
element, over decompositions and explosions, nested objects, the stack, and all
graphs: see the following subsections.