• No results found

Double-Pushout Approach

The DPO approach to graph transformation is an intuitive abstraction for manipulating graphs. This section is based on a number of works; the helpful monographs [61, 62], the dedicated chapter for the DPO approach in [46] and the original publication of the approach [63], but these are not referenced throughout the text as we intend this section to be a brief tutorial on the topic for a graph programmer, rather than a theoretician. For graph transformation with relabelling, this text uses the theory set out in [86] and developed for rule-based graph programming in [185, 224], although the underlying principles of natural pushouts and pullbacks are left for the reader. We firstly discuss this in the context of unlabelled graphs before introducing the concept of relabelling.

This section relies heavily on the following definitions of graph morphisms and particularly injective graph morphisms. An injective graph morphism can be understood as a function that maps elements of one graph to elements of another while preserving structure and without merging any two elements.

Definition 2 (Graph Morphism). A graph morphism f : G → H is a mapping from graph G = (VG, EG, sG, tG) to graph H = (VH, EH, sH, tH) that preserves structure. f consists of

two functions: a node mapping fV : VG → VH and an edge mapping fE : EG → EH. The

following conditions must hold:

1. f preserves sources (fV ◦ sG = sH◦ fE).

2. f preserves targets (fV ◦ tG= tH ◦ fE).

Definition 3 (Injective Graph Morphism). A graph morphism f : G → H is an injective graph morphism when node mapping fV and edge mapping fE are injective.

Additionally, we will use the notion of an inclusion: rules in the DPO approach consist of pairs of inclusions, where an inclusion maps each member of a subset to itself in a superset of that subset.

Definition 4 (Inclusion). A function f : A → B is an inclusion when A ⊆ B and ∀x ∈ A, f (x) = x.

1

1

1

Figure 2.2: A simple DPO rule r : L ← K → R. This rule matches a node with a looping edge, deletes that looping edge, and inserts a new node with an edge from the original node to that new node. The identifiers of nodes, indicated by integers to the bottom-right of each node, are used to visualise the inclusions K → L and K → R.

DPO approach for unlabelled graphs

The key construct of the DPO approach is the rule. A rule r is given by

r = L ← K → R (2.1)

where L, K and R are unlabelled graphs and K → L and K → R are inclusions. The intuition is that L is a pattern to match which will then be rewritten to R. Once a match for L has been found, K describes the elements of that match which will not be deleted, and R describes new elements to add to the match. Informally, a rule is applied to a graph by matching a subgraph, deleting some elements of that subgraph and inserting some new elements to that subgraph. Figure 2.2 shows a simple rule for transforming unlabelled graphs. A rule r is applied to a graph G using a graph morphism g to produce some new graph H. This graph transformation is denoted G ⇒r,g H. The graph morphism g : L → G is a

mapping from r’s L graph to the graph G, describing the match for L to apply r to. A rule r is applied as follows:

1. Choose an (injective) morphism g : L → G that satisfies the dangling condition (see below)

2. Delete elements according to r. This is done by deleting the elements of g(L − K) from G.

3. Add elements according to r. This is done by adding elements of R − K to G. Should an edge e ∈ R − K be added that uses a previously existing node n ∈ K as a source, the edge added to G uses g(n) as its source; the same holds if n is used as a target. A graph morphism g is typically injective in the DPO approach and consistently injective in the context of GP 2. One reason for this is that matching via injective morphisms is

L K R

G D H

g d h

Figure 2.3: The commutative diagram formed by the application of a DPO rule r = L ← K → R applied to graph G producing intermediate graph D and then resultant graph H.

more expressive [85]; some behaviour describable with rules applied with injective morphisms cannot be simulated with non-injective morphisms, for example producing the set of all loop- free graphs. Conversely, the behaviour of a rule r = L ← K → R applied with non-injective morphisms can be simulated with a finite rule-set Q(r), describing the possible merges of items in L, applied with injective morphisms.

This process of deletion and addition of graph elements according to a morphism can be shown to give rise to the commutative diagram of graph morphisms shown in Figure 2.3. The two squares are pushouts in the category of graphs in the sense of category theory. A direct derivation can be constructed by constructing a pushout complement of graph morphisms K → L and L →g G (deleting elements) and then constructing a pushout of K → R and

K →dD (inserting elements). However, the reader may not need to understand the underly-

ing category theory aspects to understand the intuition of the approach or the given diagram. Across the top, the component graphs of the rule applied are given. Morphism g maps of elements of L into G, and across the bottom, G ← D → H describes the transformation of G into H according to r and g.

As an example, consider the simple rule r given in Figure 2.2 applied to some graph, visualised as a commutative diagram in Figure 2.4. This rule matches a node with a looping edge, deletes that looping edge, and inserts a new node with an edge from the original node to that new node. Applying this to the given graph, there are two nodes with loops (with identifiers 2 and 4) where r could be applied, and so there are two possible morphisms to choose from. Choosing g, where g(1) = 2 such that node 2 is matched, gives a transformation where the loop on node 2 is removed and a new node is inserted with an edge from node 2 to that new node.

1 1 1 2 3 4 2 3 4 2 3 4 g

Figure 2.4: A simple DPO rule application G ⇒r,g H. Morphism g matches node 1 of r to

node 2 of G, with the looping edge on 1 matched to the looping edge on 2.

r a choice must be made over the two possible matches. Should the possible matches share common elements, and some of those elements be deleted by either transformation, then the choice of match would then “destroy” the other match, so this choice can effectively decide a path of computation.

Additionally, the DPO approach uses the dangling condition described in Definition 5. In informal terms, the dangling condition guarantees that any node deleted is not the source or target or any edge that is not deleted; no edge is left “dangling” when its source or target is removed. This condition limits which morphisms are available, such that if a morphism g would act as a match for r except that it fails the dangling condition, g is not considered a valid match for r.

1 5 1 1 2 3 4 6 2 3 4 g?

Figure 2.5: An attempted transformation that fails the dangling condition. For the rule r = L ← K → R and graph G, we have morphism g with g(1) = 2 and g(5) = 6. When the transformation is attempted, the deletion of node 6 leaves an edge with no target, and the resultant object clearly is not a graph.

Definition 5 (Dangling Condition). A graph morphism g : L → G for a rule r = L ← K → R and graph G = (V, E, s, t) satisfies the dangling condition if no edge in G − g(L − K) is incident to a node in g(L − K).

To see why the dangling condition is required, consider the diagram in Figure 2.5. The morphism g matches node 5 to node 6. If we attempt to construct the pushout complement thereby deleting node 6, we produce an object that is not a graph (there is an edge with no defined target), and clearly this cannot work as a valid transformation. Given a rule L ← K → R and graph morphism g : L → G, graph D (as shown in Figure 2.3) only exists if g satisfies the dangling condition. Moreover, in this circumstance D is uniquely determined up to isomorphism.

Of course, these rules are not used alone or in single steps. A Graph Transformation System (GTS) is a finite set of rules R = {r1, r2, ..., rn}. GTSs are also referred to as rule-sets. A

GTS R is applied to a graph G by firstly non-deterministically choosing some rule r ∈ R and then executing that rule according to the steps previously described.

As a final note, throughout this thesis we may use the term confluent when referring to rules and rule-sets. While we don’t describe the underlying theory behind confluence here (see [61]), the reader may understand it as meaning deterministic. That is, when a confluent

rule-set is applied to an input graph until there are no more matches for any of its constituent rules then that process is guaranteed to terminate and will always produce the same result graph for a given input graph.

Double-Pushout approach for labelled graphs

With the basic notion of graph transformation by the DPO approach, the idea of labels can be introduced. A labelled graph is defined over some label set L, the set of possible labels which can be associated with nodes and edges. Each node or edge in a labelled graph is associated with a label from L, causing the graph to describe structured data. A labelled graph is defined:

Definition 6 (Labelled Graph). A labelled graph G = (V, E, s, t, lV, lE) over some label set

L consists of a finite set of nodes V , a finite set of edges E, source function s : E → V , target function t : E → V , node label function lV : V → L associating each node with a label

and edge label function lE : E → L associating each edge with a label.

An unlabelled graph, as given in Definition 1, can be considered as a labelled graph where all items have the same distinguished label.

With labelled graphs, there are implications for graph morphisms; they must be label- preserving. If lLV is L’s node label function and lVG is G’s label function, then for all nodes n ∈ L’s node set VL, lVL(n) = lGV(g(n)): the same holds for edges.

In this context, rules of the form r = L ← K → R now consist of labelled graphs. From our current definitions, however, there is a possibility of relabelling preserved items. Additionally, simply treating relabelling as deletion of one node and creation of another is problematic as certain transformations would be forbidden by the dangling condition. To overcome this, we allow K to be partially labelled, that is, K0s label functions lV and lE are partial functions

and some nodes and edges are unlabelled. In practical terms, when a node or edge in L is relabelled, it firstly has its label removed and then a new label is added.

Consider Figure 2.6 as an example of a rule with relabelling. Here, a rule describes a node labelled 1 being relabelled to 2 In the interface K and intermediate graph D, the node is unlabelled, but at the end of the rule execution, all nodes are labelled.

1

1 1 21

1

2 23 24 2 23 24 22 23 24

g

Figure 2.6: A simple DPO rule application G ⇒r,g H with relabelling. Morphism g matches

node 1 of r to node 2 of G, so that node’s label is removed and updated to 2.