• Nem Talált Eredményt

Definition 68 Theinsert notification arrayINSERT : KI → 2S maps each insert key to the set of such snapshots, which have to be processed by incremental algorithms.

Definition 69 Given a metamodelM Mand a modelM,delete keysKD denote trigger conditions for initiating incremental algorithms when objects or links are deleted from modelM. Delete keys can be partitioned into two types (namely,K1D,K2D). Formally,KD =K1D∪K2D, andK1D∩K2D =∅.

• A delete keyh

ctype→ Ci

of typeK1D is triggered, if an objectcwhose direct type is a descendant of classCis deleted from modelM. Formally,K1D ⊆(VM × {type} ×VM M).

• A delete key h

a→A bi

of typeK2Dis triggered, if a link of typeAconnecting objectato objectb is deleted from modelM. Formally,K2D ⊆(VM ×Assoc×VM).

Definition 70 Thedelete notification arrayDELETE : KD → 2S maps each delete key to the set of such snapshots, which have to be modified by incremental algorithms.

8.3.5 Query results.

Aresult set(not shown in figures) is also maintained for eachLHSpattern to speed-up the queries of complete matchings initiated by the GT tool that use the services of the incremental pattern matching approach.

Definition 71 Given a modelM and a snapshot tree STLHS defined for the LHS by also using the search planSPdefined for the corresponding adorned search graphASG, theresult setRLHSfor the LHSgraphconsists of such snapshots of tree STLHS, which are located on its deepest possible level without being invalidated by any snapshots of anyNACs. Formally,RLHS ⊆STLHS, and

∀sLHSk ∈STLHS : sLHSk ∈RLHS⇐⇒LHSk=LHS∧ ∀sNACi ∈[

i

STNACi : (sNACi, sLHSk)6∈I

! .

8.4 Operations for incremental pattern matching

During the incremental operation phase, the snapshot tree is maintained by four main methods.

(a) Theinsert()method (defined later by Algorithm 8.2) is responsible for the possible exten-sion of the current matching for proper subpatternGk to create a new matching for one larger subpatternGk+1.

(b) Thevalidate()method (defined later by Algorithm 8.3) is responsible for the recursive ex-tension of insert operations to all (larger) subpatterns.

(c) Thedelete()method (defined later by Algorithm B.1) removes the whole matching subtree rooted at the current snapshot for subpatternGk.

(d) Theinvalidate()method (defined later by Algorithm B.2) is responsible for the recursive deletion of all children snapshots of the current snapshot.

These methods are called by the pattern matching engine (see Algorithm 8.1) when modification events arrive from the model repository.

136 CHAPTER 8. INCREMENTAL GRAPH TRANSFORMATION

Algorithm 8.1The core algorithm of the incremental pattern matching engine

1: for alla→e b∈∆EM do

2: for alls∈DELETE

at(e)→ b

do

3: delete(s)

4: end for

5: end for

6: for allc∈∆VMdo

7: for alln

C ∈VM M |C^ t(c)o do

8: for alls∈DELETE h

ctype→ Ci do

9: delete(s)

10: end for

11: end for

12: end for

13: for allc∈∆VM+do

14: for all n

C ∈VM M |C^ t(c)o do

15: for alls∈INSERT

h∗type→ Ci do

16: insert(s, c)

17: end for

18: end for

19: end for

20: for alla→e b∈∆EM+ do

21: for alls∈INSERT

at(e)→ ∗

do

22: insert(s, b)

23: end for

24: for alls∈INSERT

t(e)→ b

do

25: insert(s, a)

26: end for

27: end for

8.4. OPERATIONS FOR INCREMENTAL PATTERN MATCHING 137

• Delete notification. If a linka →e b of type t(e) connecting object a to b is removed from the model, then the delete() method is invoked with every snapshot being notified by the entry DELETE

at(e)→ b

(lines 1–5). If an object c of type t(c) is removed from the model, then thedelete()method is invoked with every snapshot being notified by the entries DELETE

h

ctype→ Ci

, in whichCiterates over all ancestors oft(c)(lines 6–12).

• Insert notification. If an object c of type t(c) is added to the model, then for all ancestors C of typet(c) theinsert()method is invoked with objectc as a second parameter, and all snapshots being notified by the entriesINSERT

h∗type→ Ci

as first parameter (lines 13–19). If a linka →e bof typet(e)connecting objectatobis added to the model, then theinsert() method is invoked with every matching defined by entryINSERT

at(e)→ ∗

and with objectb as its parameters (lines 21–23). Then the same method is invoked with every matching defined by entryINSERT

t(e)→ b

and with objectaas its parameters (lines 24–26).

8.4.1 Incremental operations on an example

Prior to the detailed discussion of the algorithms, we first exemplify the process by using our running example of Fig. 8.4. Let us suppose that a classc1is added to packagepin the model by user interaction initiated by the system designer. The pattern matching engine is notified about this activity in two steps.

First a notification arrives about the insertion of an objectc1of typeClass(see Fig. 8.4(c)) followed by the insertion of anEOlink connectingc1top(see Fig. 8.4(e)). Modifications are denoted by thick lines.

Step 1. When the objectc1of type Class is inserted, the pattern matching engine looks up entries retrieved by insert keys[∗,type,ModelElement],[∗,type,Namespace], and[∗,type,Class].

The last entry triggers the possible extension of snapshot1by mapping pattern nodeCto objectc1

by invoking theinsert()method with snapshot1, and objectc1as parameters. As this is a matching for subpatternLHS1, a new snapshot3is created and added to the (snapshot) tree as a child of snapshot

1, and the mappingCtoc1is recorded.

Then snapshot 3is inserted into the delete notification array with delete key [c1,type,Class].

This means that whenever objectc1, which conforms to classClass(i.e., the object that has been just added) is removed, this snapshot should be deleted.

Effects of adding a new snapshot to the tree are recursively extended to find matchings for larger subpatterns by callingvalidate(). Snapshot3can be further extended (as shown by corresponding new entries being added to the insert notification array pointing to snapshot3), whenever anEOlink leading out ofc1or a newPackageis added to the modelin the future.

As also thecurrent contentof the model may extend snapshot3, we initiate the possible extensions of this matching by checking the existence of at least theEOlinks leading out of objectc1.2As no such links exist in our example, the algorithm terminates with the snapshot tree presented in Fig. 8.4(d).

2Note that the insert key generation and the possible further extension of snapshot3are guided by the one larger subpat-ternLHS2.

138 CHAPTER 8. INCREMENTAL GRAPH TRANSFORMATION

Step 2. When theEOlink connectingc1topis inserted (as shown by the thick line of Fig. 8.4(e)), snapshot3is first extended to a new snapshot4by mapping pattern nodePto objectpand by executing a sequence ofinsert()andvalidate()method calls as shown in Fig. 8.5.

pm:IPatternMatcher

5-LHS3: Snapshot

Step 2: Notification from GT tool: EO edge from "c1" to "p" has been inserted

3-LHS1: Snapshot

Complete matching found for LHS (repr. by LHS3) 4-LHS2: Snapshot

1.1.1.1: validate() 1: insert()

1.1.1: insert() 1.1: validate()

Figure 8.5: Sequence diagram showing edge insertion into theLHSpattern

This time, matching extension is propagated to another new snapshot5by assigning pattern node

Sto objectsby invoking theinsert()method with snapshot4and objectsas parameters, as the current model already contained a schemasand aReflink connectingptos.

In addition, both new snapshots are appropriately registered in both the insert and delete notification arrays, and the binding array is updated accordingly. The corresponding snapshot tree is shown in Fig. 8.4(f).

At this point, snapshot 5 represents a (complete) matching for the LHS pattern, so the GT rule

ClassRulecan be applied.

Step 3. The result of applyingClassRuleon the matching represented by snapshot5can be observed in Fig. 8.4(g) after the insertion of 3 objects and 5 links, processed one by one by the pattern matching engine.

Let us suppose that tablet1is inserted first. At this point, the insert notification array is consulted by retrieving snapshots that can be found at locations[∗,type,ModelElement],[∗,type,Namespace], [∗,type,Class], and[∗,type,Table]. The latter two returns snapshots1and2, respectively, so the insert()method is invoked with these snapshots and tablet1as input parameters. When snapshot1

is processed, nothing changes in the snapshot tree. On the other hand, when snapshot2is trying to be extended, then snapshot6is created as a child.

In the following step, snapshot7is added as a child of snapshot6, if theReflink connecting class

c1to tablet1 is inserted next into the model. As snapshot7represents a (complete) matching for the NACpattern, snapshot3must be invalidated by deleting all its descendant snapshots in the tree. When all the new elements are added, the data structure will reflect the situation in Fig. 8.4(h).

8.4. OPERATIONS FOR INCREMENTAL PATTERN MATCHING 139

8.4.2 Insert method

The insert method (shown by Algorithm 8.2) is responsible for the possible extension of the current partial matching for proper subpattern Gk to compute a new partial matching for subpattern Gk+1. If the current snapshot represents a complete matching for patternG, then the method immediately terminates as matchings for patternGcan never be further extended.

Algorithm 8.2The snapshot insertion algorithminsert(sGk, c) PROCEDURE insert(sGk, c)

1: ifGk⊂Gthen

2: {IfsmG

k is a matching for a proper subpatternGk, and, thus, it isnota matching for patternG}

3: ifcheckGraphMorphism(sGk, c)then

4: {If matchingsmG

k can be successfully extended by mapping the (k+1)th pattern nodevk+1 to objectc}

5: sGk+1 :=copyMatchings(sGk, c){Copy current matchings to the new matching}

6: addDeleteEntries(sGk+1){New delete entries for matchings of condition edges}

7: if6 ∃sNAC∈S

iSTNACi : (sNAC, sGk+1)∈Ithen

8: validate(sGk+1){Extend the new matching if not invalidated by anyNACs}

9: end if

10: end if

11: end if

• The insert method is invoked with the current snapshotsGkand an objectc, which is supposed to be the mapping of the (k+1)th pattern node in a newpotential matching, which also contains all mappings defined by the matching for subpatternGkbeing represented by the current snapshot.

• Since the current snapshot already represents a matching for the kth subpattern Gk, only mappings of the (k+1)th pattern node and its incoming and outgoing condition edges, which have just been defined by the new potential matching, are required to be checked by the checkGraphMorphism()method.

• If the potential matching is a correct graph morphism (and the checkGraphMorphism() returns true), the potential matching can be considered as a new matching for subpattern Gk+1. As such, a new snapshot is created and inserted into the snapshot tree by invoking the copyMatchings()method with the current snapshotsGk and objectcas input parameters.

• The new snapshot is added to the delete notification array at all locations defined by the mappings of the (k+1)th pattern node and its incoming and outgoing condition edges.

• If the new snapshot is being invalidated by any (complete) matchings of anyNACpatterns, then theinsert()method terminates.

• Otherwise, thevalidate()method is invoked on the new snapshot trying to recursively ex-tend the matching it represents.

8.4.3 Validate method

The validate method (shown by Algorithm 8.3) is responsible for the recursive extension of insert operations. It is invoked either when a new snapshot has been inserted into the snapshot tree and its

140 CHAPTER 8. INCREMENTAL GRAPH TRANSFORMATION

further extensions have to be checked (see Algorithm 8.2), or when extensions of the current matching possibly become valid due to the removal of a (complete) matching for an embeddedNACpattern (by theinvalidate()method).

Algorithm 8.3The snapshot validation algorithmvalidate(sGk) PROCEDURE validate(sGk)

1: ifGk =Gthen

2: {IfsmG

k is a(complete) matchingfor patternG}

3: ifG=LHSthen

4: {IfsmG

kis a (complete) matching for anLHSpattern}

5: R0LHS:=RLHS∪ {sGk}{Add the snapshot to the results}

6: else

7: {IfsmG

kis a (complete) matching for aNACpattern}

8: for all

s∈STLHS |sGk hsi ∧(sGk, s)∈/ I do

9: if∀sNAC ∈S

iSTNACi : (sNAC, s)6∈Ithen

10: invalidate(s) {If snapshot s has not been invalidated yet by any other snapshots sNAC, then invalidates.}

11: end if

k is apartialmatching for patternG}

17: addInsertEntries(sGk){Add insert entries}

18: propagateInsert(sGk){Propagate it to find a mapping of the next pattern node}

19: end if

• If snapshotsGk represents a (complete) matching for aLHSpattern, then the current snapshot is inserted into the result setRLHS.

• If snapshot sGk represents a complete matching for aNACpattern, then all snapshotssof the LHS pattern whose partial matching maps the shared pattern nodes to the same objects as the current matching have to be invalidated, if they have not been invalidated yet.

• By invoking the addInsertEntries() method, the current snapshot sGk is added to the insert notification array at locations defined by theone largersubpatternGk+1. In this sense, the snapshot is inserted at locations

h by the (k+1)th pattern nodevk+1and its incoming and outgoing condition edgesu→z vk+1and vk+1z w, respectively.

• In thepropagateInsert()method, insertion is attempted to be propagated to a matching for the one larger subpatternGk+1. In this sense, an arbitrary incoming or outgoing condition edge of the (k+1)th pattern node is selected from subpatternGk+1. If an incoming condition edge has been chosen, then we lookup all type conformant links leading out of the matched source objectsmG

k(u)of condition edgeu →z vk+1 and try to extend the current matching by mapping