An Introduction to Graph Rewriting Thomas Huining Feng CHESS, UC Berkeley May 1, 2007 Inspired by Tutorial Introduction to Graph Transformation: A Software Engineering Perspective. Luciano Baresi, Reiko Heckel. ICGT 2002
PacMan: a Motivating Example : Field Field Type : Ghost Ghost : PacMan PacMan : Marble Marble
Our 1 st Rule: pmove b : Field a : Field : PacMan b : Field a : Field : PacMan 3 pmove LHS (Left Hand Side) RHS (Right Hand Side) : Field : Ghost : PacMan : Marble Host Graph Redex Redex finding is a sub-graph isomorphism problem). Multiple redexes?
Our 2 nd Rule: gmove b : Field a : Field : Ghost b : Field a : Field : Ghost gmove : Field : Ghost : PacMan : Marble
Combining pmove and gmove pmove and gmove do not “interfere” with each other. I.e., applying any one does not affect applicability of the other. 5 b : Field a : Field : PacMan b : Field a : Field : PacMan pmove b : Field a : Field : Ghost b : Field a : Field : Ghost gmove
catch Rule 6 b : Field a : Field : Ghost b : Field a : Field : Ghost catch : PacMan : Field : Ghost : PacMan : Marble
catch is actually a “sub-case” of gmove, because: LHS(gmove) LHS(catch) Assign priority (almost always the same for sub-cases): P(catch) > P(gmove) Important Decision: Which Rule to Choose? 7 b : Field a : Field : Ghost b : Field a : Field : Ghost gmove b : Field a : Field : Ghost b : Field a : Field : Ghost catch : PacMan
Attribute Binding: collect Rule 8 : Field : Ghost : PacMan 3 : marble : Marble : PacMan 4 : marble
Here, m on the LHS serves as a bound variable; on the RHS, it refers to the value bound to. Attribute Binding: collect Rule 9 b : Field a : Field : PacMan m : marble b : Field a : Field : PacMan m+1 : marble collect : Marble : Field : Ghost : PacMan 3 : marble : Marble Field Type Ghost Marble PacMan int marble
Completing the PacMan Game 10 b : Field a : Field : PacMan b : Field a : Field : PacMan pmove b : Field a : Field : Ghost b : Field a : Field : Ghost gmove b : Field a : Field : Ghost b : Field a : Field : Ghost catch b : Field a : Field : PacMan m : marble b : Field a : Field : PacMan m+1 : marble collect : Marble : PacMan sub-case interfering sub-case n P Priority:
Common components in the problem – Host graph – A set of rewriting rules LHS and RHS Attribute binding and transfer Application condition Embedding information Host Graph – Redex The Graph Rewriting Problem 11 m < BAG_SIZE b : Field a : Field : PacMan m : marble b : Field a : Field : PacMan m+1 : marble : Marble
The Graph Rewriting Problem 12 Common components in the problem: – Host graph – A set of rewriting rules LHS and RHS Attribute binding and transfer Application condition Embedding information Rule application 1.In the host graph, find all redexes according to the rules. Match LHS Check application condition 2.Choose a redex and a rule to apply. 3.Apply the rule. Generate a substitution (unique) isomorphic to RHS Compute new attributes Make substitution (“bridge” w.r.t embedding information)
More on Application Decision We have seen priorities (as a partial order of rules). Applications may require other approaches. – More flexible rule choosing: Combine with control structure Event-driven Combine with state machine 13 C? YN e1e2 C1?C2?
More on Application Decision We have seen priorities (as a total order of rules). Applications may require other approaches. – Maze walking (goal-directed searching) 14 Field Entrance Kid Exit
More on Application Decision We have seen priorities (as a total order of rules). Applications may require other approaches. – Shortest path (optimizing an objective function) 15 StartEnd Man 00 1* Start Man int time End Field int delay b : Field d : delay a : Field : Man t : time b : Field d : delay a : Field : Man t+d : time
Conclusion Key idea is simple. User-friendly. Expressive. – Transformation problems – Analysis problems – Search problems – Optimization problems Complexity due to sub-graph isomorphism on the LHS. Rules need to be carefully designed and chosen at run time. – Depending on the problem and the goal. Problem formulation and algorithm are application dependent. 16