15 Exhaustjve Graph Rewrite Systems (XGRS) for Refactorings and - - PowerPoint PPT Presentation

15 exhaustjve graph rewrite systems xgrs for refactorings
SMART_READER_LITE
LIVE PREVIEW

15 Exhaustjve Graph Rewrite Systems (XGRS) for Refactorings and - - PowerPoint PPT Presentation

Fakultt Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie Prof. Amann - Softwaretechnologie II 15 Exhaustjve Graph Rewrite Systems (XGRS) for Refactorings and Other Transformatjons Prof. Dr. Uwe Amann 1)


slide-1
SLIDE 1

Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie – Prof. Aßmann - Softwaretechnologie II

(c) Prof. U. Aßmann Model Structurings

15 Exhaustjve Graph Rewrite Systems (XGRS) for Refactorings and Other Transformatjons

  • Prof. Dr. Uwe Aßmann

Softwaretechnologie Technische Universität Dresden Version 15-0.4, 02.01.16 1) Termination of EARS 2) Termination of AGRS 3) SGRS 4) XGRS 5) Refactoring Example

slide-2
SLIDE 2

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Obligatory Literature

  • [Aßmann00] Uwe Aßmann. Graph rewrite systems for program optimization. ACM

Transactions on Programming Languages and Systems (TOPLAS), 22(4):583-637, June 2000.

http://portal.acm.org/citation.cfm?id=363914

  • Alexander Christoph. Graph rewrite systems for software design transformations. In M.

Aksit, editor, Proceedings of Net Object Days 2002, Erfurt, Germany, October 2002.

  • Alexander Christoph. GREAT - a graph rewriting transformation framework for designs.

Electronic Notes in Theoretical Computer Science (ENTCS), 82 (4), April 2003.

  • Alexander Christoph. Describing horizontal model transformations with graph rewriting
  • rules. In Uwe Aßmann, Mehmet Aksit, and Arend Rensink, editors, MDAFA, volume 3599
  • f Lecture Notes in Computer Science, pages 93-107. Springer, 2004.
  • Tom Mens. On the Use of Graph Transformations for Model Refactorings. GTTSE 2005,

Springer, LNCS 4143

http://www.springerlink.com/content/5742246115107431/

slide-3
SLIDE 3

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

class Person { .. } class Course { Person teacher = new Person(“Jim”); Person student = new Person(“John”); }

Remember: Rename Refactorings in Programs

U Dr es de n, Pro f. U. Aß ma nn Mo el Co nsi ste ncy 3

Definition Reference (Use) Refactor the name Person to Human, using bidirectional use-def-use links:

class Human { .. } class Course { Human teacher = new Human(“Jim”); Human student = new Human(“John”); }

slide-4
SLIDE 4

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

Refactoring as Graph Transformatjon

  • Refactoring works always in the same way:

Change a defjnition

Find all dependent references

Change them

Recurse handling other dependent defjnitions

  • Refactoring can be supported by Graph Rewrite Tools

The Use-Def-Use-graph (UDUG) forms the basis of refactoring tools

Build up the UDUG with graph analysis (EARS)

Rewrite it with graph rewriting (XGRS)

U Dr es de n, Pro f. U. Aß ma nn Mo el Co nsi ste ncy 4

slide-5
SLIDE 5

Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie – Prof. Aßmann - Softwaretechnologie II

(c) Prof. U. Aßmann Model Structurings

15.1 Terminatjon and Confmuence of EARS

A Fujaba GRS (in one activity of the storyboard) may terminate and deliver a unique result.

slide-6
SLIDE 6

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Problems with GRS

With graph rewriting for model and program analysis, refactoring, and transformation, there are some problems:

  • Termination: The rules of a GRS G are applied in chaotic order to the

manipulated graph. When does G terminate for a start graph?

Idea: can we „forcedly“ terminate the rewriting?

Idea: identify a termination graph which stops the rewriting when completed

  • Non-convergence (indeterminism): when does a GRS deliver a deterministic

solution (unique normal form)?

Can we automatically select a “standard” normal form?

Idea: unique normal forms by rule stratification

slide-7
SLIDE 7

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Additjve Terminatjon

  • A termination subgraph is a subgraph of the manipulated graph, which is step by step

completed

  • Conditions in the additive case:

nodes of termination (sub-)graph are not added (remain unchanged)

its edges are only added

  • If the termination graph is complete, the system terminates
slide-8
SLIDE 8

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

FlattenInheritance Hierarchy()

Transitjvising (Flatuening) the Inheritance Hierarchy

  • Does this rule terminate?

Yes, because EARS complete graphs and shorten paths

“is-a” is the termination subgraph

  • Fujaba GRS rule “FlattenInheritanceHierarchy”:

A:Class C:Class B:Class <<create>>

[Christoph04]

slide-9
SLIDE 9

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Run-Time Derivatjon (Snapshots): Transitjvising the Inheritance Hierarchy

  • Ex.: A simple class inheritance tree (acyclic) is “shortened”
  • “is-a” is completed step by step

Student:Class Object:Class Person:Class Professor:Class Freshman:Class Student:Class Object:Class Person:Class Professor:Class Freshman:Class

1 2

slide-10
SLIDE 10

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Transitjvising the Inheritance Hierarchy

  • Ex.: A simple class inheritance tree (acyclic) is “shortened”

Student:Class Object:Class Person:Class Professor:Class Freshman:Class

3 4

Student:Class Object:Class Person:Class Professor:Class Freshman:Class

slide-11
SLIDE 11

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Transitjvising the Inheritance Hierarchy

  • If every indirect path is shortened, rewriting stops

5 END

Student:Class Object:Class Person:Class Professor:Class Freshman:Class

slide-12
SLIDE 12

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Example: Collect Subexpressions

  • EARS also work on bipartite graphs
  • Query to build up the use-defjnition-use graph (UDUG) between Statements and

Expressions:

''Find all subexpressions which are reachable from a statement'‘

  • Features of graph rewrite system:

terminating, strong confmuent

convergent (unique normal form)

recursive

// F-Datalog: ExprsOfStmt(Stmt:Statement,Expr:Expr) :- Child(Stmt,Expr). ExprsOfStmt(Stmt:Statement,Expr:Expr) :- Child(Stmt,Expr2), Descendant(Expr2,Expr). // Descendant is transitive closure of Child Descendant(Expr1:Expr,Expr2:Expr) :- Child(Expr1,Expr2). Descendant(Expr1:Expr,Expr2:Expr) :- Descendant(Expr1,Expr3), Child(Expr3,Expr2). // F-Datalog: ExprsOfStmt(Stmt:Statement,Expr:Expr) :- Child(Stmt,Expr). ExprsOfStmt(Stmt:Statement,Expr:Expr) :- Child(Stmt,Expr2), Descendant(Expr2,Expr). // Descendant is transitive closure of Child Descendant(Expr1:Expr,Expr2:Expr) :- Child(Expr1,Expr2). Descendant(Expr1:Expr,Expr2:Expr) :- Descendant(Expr1,Expr3), Child(Expr3,Expr2).

slide-13
SLIDE 13

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

EARS CollectExpressions

  • Two transitive closures, specifjed as path abbreviations

Stmt:Statement Expr:Expr <<create>> ExprsOfStmt Stmt:Statement Expr2:Expr Child Child <<create>> ExprsOfStmt Expr:Expr Descendant Expr1:Expr Expr3:Expr Descendant <<create>> Descendant Expr2:Expr Child Expr1:Expr Expr2:Expr <<create>> Descendant Child

Collect Expressions()

slide-14
SLIDE 14

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Const Plus Assign Var 1 X Expr

Executjon of „Reachable Subexpressions“

  • Start situation

Child

1

Child

slide-15
SLIDE 15

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Executjon of „Reachable Subexpressions“

  • Why do such graph rewrite systems terminate? Answer: ExprsOfStmt and Descendants

are termination subgraphs, completed step by step

Descendants ExprsOfStmt 2 Descendants Const Plus Assign Var 1 X Expr Child

slide-16
SLIDE 16

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Executjon of „Reachable Subexpressions“

3 Descendants ExprsOfStmt Descendants Const Plus Assign Var 1 X Expr Child

slide-17
SLIDE 17

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Executjon of „Reachable Subexpressions“

4 Descendants ExprsOfStmt Descendants Const Plus Assign Var 1 X Expr Child

slide-18
SLIDE 18

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Executjon of „Reachable Subexpressions“

5 Descendants ExprsOfStmt Descendants Const Plus Assign Var 1 X Expr Child

slide-19
SLIDE 19

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

EARS - Simple Edge-Additjve GRS

  • EARS (Edge addition rewrite systems) only add edges to graphs

They can be used for the construction of graphs

  • For the building up analysis information about a program or a model
  • For abstract interpretation on an abstract domain represented by a graph
  • terminating: terminating on the fjnite lattice of subgraphs of the manipulated graph

Added edges form the termination subgraph

  • strongly confmuent: direct derivations can always be interchanged.
  • congruent: unique normal form (result)
  • ==> If a Fujaba activity contains an EARS, it terminates and delivers a unique result
slide-20
SLIDE 20

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Name and Type Analysis with EARS

  • EARS are very useful for program analysis problems
  • Uses of names must be linked to their defjnitions

procedures, methods

classes, types

  • Name analysis looks up used names in the context

Search

Lookup in tables

Reachability analysis: if a defjnition of a used name is reachable, then it forms a use-def edge in the use-def graph

slide-21
SLIDE 21

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Data-fmow Analysis with EARS

  • EARS are very useful for program analysis problems
  • Every distributive data-fmow problem (abstract interpretation problem) on fjnite-height

powerset lattices can be represented by an EARS

defjned/used-data-fmow analysis

partial redundancies

local analysis and preprocessing:

  • EARS are equivalent to binary F-Datalog
  • EARS work for other analysis problems, which can be expressed with F-Datalog-

queries

equivalence classes on objects

alias analysis

program fmow analysis

slide-22
SLIDE 22

Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie – Prof. Aßmann - Softwaretechnologie II

(c) Prof. U. Aßmann Model Structurings

15.2 Terminatjon of Additjve GRS (AGRS)

  • Sometimes, during

refactoring and transformations, we must allow for node additions, nodes which should represent new information

slide-23
SLIDE 23

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Example: Allocatjon of Register Objects for Storing the Result of Expressions in Statements

  • Query: ''Allocate a register object for every subexpression of a

statement which has a result and link the expression to the statement''

if ExprsOfStmt(Stmt,Expr), HasResult(Expr) then ObjectExprs(Stmt,Expr), RegisterObject := new Register; UsedReg(Expr,RegisterObject) ;

  • Features: terminating

Stmt Expr Stmt Register Expr ExprsOfStmt HasResult =true UsedReg Object Exprs

slide-24
SLIDE 24

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Const Plus Assign Var 1 X Expr

  • ObjectExprs is the “termination subgraph”, i.e., the subgraph which cannot grow out of

bound

slide-25
SLIDE 25

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Const Plus Assign Var 1 X Expr Register Register Register Register Object Exprs UsedReg UsedReg UsedReg UsedReg

  • ObjectExprs is the “termination subgraph”, i.e., the subgraph which cannot grow out of

bound

slide-26
SLIDE 26

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

A Derivatjon with the Terminatjon Subgraph Will Stop

[Aßmann00]

slide-27
SLIDE 27

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

slide-28
SLIDE 28

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Edge-Accumulatjve Rules and AGRS

  • A GRS is called edge-accumulative (an AGRS) if

all rules are edge-accumulative and

no rule adds nodes to the termination-subgraph nodes of another rule.

  • Edge-accumulative rules are defjned on label sets of nodes and edges in rules
  • This criterion statically decidable
slide-29
SLIDE 29

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

The Terminatjon Subgraph of the Examples

Collection of subexpressions: T = ({Stmt,Expr}, {ExprsOfStmt, Descendant} ) Allocation of register objects: T = ({Proc,Expr}, {ObjectExprs} )

slide-30
SLIDE 30

Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie – Prof. Aßmann - Softwaretechnologie II

(c) Prof. U. Aßmann Model Structurings

15.3 Subtractjve GRS (SGRS)

slide-31
SLIDE 31

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Subtractjve Terminatjon

  • Conditions in the subtractive case:

the nodes of the termination subgraph are not added (remain unchanged)

its edges are only deleted

  • If the termination subgraph is empty, the system terminates
  • Results in:

edge-subtractive GRS (ESGRS)

subtractive GRS (SGRS)

slide-32
SLIDE 32

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Constant Folding as Graph Rewrite Rule

Const Plus Const 1 2 Const 3

slide-33
SLIDE 33

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Peephole Optjmizatjon as Subtractjve XGRS

Const Plus Incr 1 X IncrIncr Var X Var X

next

slide-34
SLIDE 34

Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie – Prof. Aßmann - Softwaretechnologie II

(c) Prof. U. Aßmann Model Structurings

15.4 Exhaustjve GRS (XGRS)

slide-35
SLIDE 35

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

The Nature of Exhaustjve Graph Rewritjng (XGRS)

AGRS and SGRS make up eXhaustive Graph Rewrite Systems (XGRS)

In an XGRs, all redexes in the termination-subgraph are consumed step by step.

  • The termination-subgraph is either completed or consumed

Edge-accumulative systems may create new redex parts in the termination-subgraph, but

  • there will be at most as many of them as the number of edges in the

termination-subgraph.

Subtractive systems do not create sub-redexes in the termination-subgraph but destroy them.

  • XGRS can only be used to specify algorithms which

perform a fjnite number of actions depending on the size of the host graph.

slide-36
SLIDE 36

Fakultät Informatik - Institut Software- und Multimediatechnik - Softwaretechnologie – Prof. Aßmann - Softwaretechnologie II

(c) Prof. U. Aßmann Model Structurings

15.5 Refactoring Example “Pull-Up Features into Common Superclass”

[Christoph04]

slide-37
SLIDE 37

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Step 1: Flatuening the Inheritance Hierarchy

  • This rule terminates, due to path contraction and subtraction
  • The rule, FlattenClassHierarchy, has a unique normal form

A:Class C:Class B:Class <<create>> <<delete>>

FlattenClassHierarchy

slide-38
SLIDE 38

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Step 2: Pull-Up-Method Refactoring

A:Class B:Class <<create>> base-type contains C:Attribute NewSuper:Class contains <<create>> <<create>>

  • Additive Step: Create a new base class for common attributes; mark this as the new

“base-type” of the attribute

  • The rule, Mark-Pull-Up, has a unique normal form

NewSuper.Name := “<A.name>_<B.name>_Base”

Mark-Pull-Up

slide-39
SLIDE 39

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Step 3

  • Edge-Additive Step: alternate case: a class A has attributes that should be moved up

anyway

  • The rule, Mark-Pull-Up-2, has a unique normal form

A:Class base-type contains C:Attribute B:Class <<create>>

Mark-Pull-Up-2

slide-40
SLIDE 40

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Step 4

  • Subtractive Step: do the real “pull-up” into the superclass
  • The rule, Pull-Up-Features, has a unique normal form

A:Class base-type <<delete>> contains C:Attribute B:Class

Pull-Up-Attributes

slide-41
SLIDE 41

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

Puttjng it All Together

  • The rule sequence
  • { FlattenClassHierarchy , Mark-Pull-Up, Mark-Pull-Up-2, Pull-Up-Features }
  • is terminating (XGRS) and confmuent
  • has a unique result, the desired refactored class hierarchy
  • We specifjed a refactoring with only 4 rules
slide-42
SLIDE 42

S

  • f

t w a r e t e c h n

  • l
  • g

i e I I

(c) Prof. U. Aßmann

The End

  • Many model and program transformations can be specifjed by terminating XGRS
  • Termination criteria build on a termination subgraph that is completed or deleted during

the transformation

  • Refactorings on the UDUG can be described with graph transformations
  • Fujaba storyboards allow for chaining XGRS, so that the overall chain terminates