490 likes | 659 Views
2.5 Graph Grammars. Organisatorisches. Klausurtermin: Fr, 22.02.2013 Voraussichtlich 11:00 Uhr Hinweis: Es gibt nur einen Klausurtermin! Mögliche Konflikte bis 14.12.2012 an Uwe Pohlmann (upohl (at) upb.de) melden. Graph Grammars. Example Describing program states as graphs
E N D
Organisatorisches • Klausurtermin: Fr, 22.02.2013 • Voraussichtlich 11:00 Uhr • Hinweis: Es gibt nur einen Klausurtermin! • Mögliche Konflikte bis 14.12.2012 an Uwe Pohlmann (upohl (at) upb.de) melden. Graph Grammars
Graph Grammars Example Describing program states as graphs Describing program behavior through graph transformations Models and graphs Graph grammars Story diagrams Graph Grammars
Example: Graph Grammars in every day life • Most children know graph grammars • Every Lego build instruction is a graph grammar Graph Grammars
Example Example: simulation of a track-based transportation system :Shuttle :Shuttle :Track :Track :Track :Switch :Track Concrete syntax: Abstract syntax: isOn turnNext isOn next next next Graph Grammars
Object Graph The object graph represents the state of a program The behavior of a program is a change of the state A change of the object graph :Shuttle :Shuttle :Track :Track :Track :Switch :Track isOn turnNext isOn next next next Graph Grammars
Object Graph The object graph represents the state of a program The behavior of a program is a change of the state A change of the object graph :Shuttle :Shuttle :Track :Track :Track :Switch :Track Example: Moving a shuttle isOn turnNext isOn isOn next next next Graph Grammars
Object Graph The program behavior can be described by rules for transforming the object graph :Shuttle :Shuttle :Shuttle :Track :Track :Track :Track :Track :Switch :Track move <<create>> isOn isOn <<delete>> next isOn turnNext isOn next next next Graph Grammars
Motivation Graph transformation rules allow us: To describe the behavior of systems in an abstract way (e.g. RailCab) To model and analyze them To describe the semantics of pointer programs (OO) To validate or verify them To model the program behavior visually (Executable code can be generated from them) :Shuttle :Track :Track move <<create>> isOn isOn <<delete>> next Graph Grammars
Graph Grammars Example Describing program states as graphs Describing program behavior through graph transformations Models and graphs Graph grammars Story diagrams Graph Grammars
Instance Model and Class Model next isOn 1..1 0..1 turnNext :Shuttle Shuttle :Shuttle isOn :Track turnNext isOn :Track Track :Track Switch :Switch :Track next next next Class Diagram: Object graph: Graph Grammars
Instance Model and Class Model next Track isOn 1..1 0..1 turnNext :Shuttle Shuttle :Shuttle isOn :Track turnNext isOn :Track Switch :Track :Switch :Track next next next Class Diagram: Type Graph Object graph: Typed Graph (edges are typed, too) Graph Grammars
Graph Grammars Example Describing program states as graphs Describing program behavior through graph transformations Models and graphs Graph grammars Story diagrams Graph Grammars
Graph Grammars • A graph grammar consists of • A set of graph grammar rules (production rules) • A start graph • A type graph • A graph grammar describes how to produce valid sets of graphs (sets may be infinite) • Synonyms: • Graph Rewriting System or Graph Transformation System start graph rule: move :Shuttle isOn :Track next :Track :Shuttle <<create>> next next isOn <<delete>> isOn :Track :Track next :Track :Track next Graph Grammars
Graph Grammar Rule • A graph grammar rule consists of • A left-hand side and right-hand side typed graph move lhs: rhs: :Shuttle :Shuttle v2 v2 ::= isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next move Short hand form: :Shuttle <<create>> identifier isOn <<delete>> isOn next :Track :Track Graph Grammars
Graph Grammar Rule Shuttle Switch Track • A graph grammar rule consists of • A left-hand side and right-hand side typed graph 1 next isOn 1 1 turnNext (edges are typed, too) move lhs: rhs: :Shuttle :Shuttle v2 v2 ::= isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Graph Grammar Rule Application :Shuttle :Shuttle :Track :Track :Track :Switch :Track isOn turnNext isOn next next next move lhs: rhs: :Shuttle :Shuttle v2 v2 ::= isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Graph Grammar Rule Application :Shuttle :Shuttle :Track :Track :Track :Switch :Track isOn turnNext isOn next next next 1. Match lhs in host graph move lhs: rhs: :Shuttle :Shuttle v2 v2 ::= isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Graph Grammar Rule Application :Shuttle :Shuttle :Track :Track :Track :Switch :Track isOn turnNext isOn next next next 2. Remove nodes and edges which are in lhs, but not in rhs move lhs: rhs: :Shuttle :Shuttle v2 v2 ::= isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Graph Grammar Rule Application :Shuttle :Shuttle :Track :Track :Track :Switch :Track isOn turnNext isOn next next next 3. Create nodes and edges which are in rhs, but not in lhs move lhs: rhs: :Shuttle :Shuttle v2 v2 ::= isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Non-Determinism :Shuttle :Shuttle :Track :Track :Track :Track • Example 1: When to move which shuttle? • Not determined! isOn isOn next next next move lhs: rhs: :Shuttle :Shuttle v2 v2 ::= isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Non-Determinism next isOn 1 1 turnNext :Shuttle Shuttle :Track Track Switch :Switch :Track • Example 2: Go straight or turn? • Not determined! 1 turnNext isOn (remember: a switch is also a track) next move turn :Shuttle <<create>> :Shuttle <<create>> isOn <<delete>> isOn isOn <<delete>> isOn :Track :Track :Track :Switch next turnNext Graph Grammars
Negative Application Conditions :Shuttle :Shuttle :Track :Track :Track :Track • Example: Do not crash with other shuttle isOn isOn next next next move :Shuttle <<create>> :Shuttle isOn <<delete>> isOn isOn :Track :Track next Graph Grammars
Attribute Condition Track Shuttle Switch • Example 1: A broken shuttle cannot move 1 next isOn failure: boolean 1 1 turnNext move <<create>> :Shuttle failure=false isOn isOn <<delete>> :Track :Track next Graph Grammars
Attribute Condition Track Shuttle Switch • Example 2: May follow faster shuttles on Track 1 next isOn broken: boolean speed: integer 1 1 turnNext move <<create>> s1:Shuttle s2:Shuttle speed ≤ s2.speed isOn <<delete>> isOn isOn :Track :Track next Graph Grammars
Graph Grammars: Overview • Non-deterministic rule application • Negative application conditions • Conditions on attribute values • Inheritance in the type graph • There may be other extensions depending on the problem domain… • Ordered lists • Qualified associations • „maybe“-conditions • Now a bit more formally… Graph Grammars
Definition: Directed Graph • G = (V, E, s, t) • V– finite set of nodes (vertices) • E– finite set of edges • s: E→V – source function • t: E→V – target function v3 Example: V = {v1, v2, v3} E = {e1, e2, e3} s(e1) = v1 t(e1) = v2 e3 e2 v1 v2 e1 Graph Grammars
Definition: Labeled Graph • GL = (G,LV, LE) • G – Directed Graph • LV: V →∑– labeling function for vertices • LE: E →∑– labeling function for edges • ∑ –set of labels v1 shuttle Example: V = {v1, v2} E = {e1} s(e1) = v1 t(e1) = v2 LV(v1) = “shuttle” LV(v2) = “track” LE(e1) = “isOn” e1 isOn v2 track Graph Grammars
Definition: Morphism :Shuttle Shuttle :Track Track • Given two graphs Gi=(Vi,Ei,si,ti), i∈{1, 2} • A graph morphism f: G1→G2, f = (fV , fE) consists of two functions • fV: V1→ V2 • fE : E1→ E2 preserving the source and target functions: • fV ◦ s1 = s2◦ fE and fV ◦t1 = t2 ◦fE v12 v22 e12 G2: isOn = = v11 v21 G1: isOn e11 Given two functions f and g then f◦g is a function that maps a value x to f(g(x)) Graph Grammars
Definition: Morphism :Shuttle Shuttle :Track Track • Given two graphs Gi=(Vi,Ei,si,ti), i ∈ {1, 2} • A graph morphism f: G1→G2, f = (fV , fE) consists of two functions • fV: V1→ V2 • fE : E1→ E2 preserving the source and target functions: • fV ◦ s1 = s2◦ fE and fV ◦t1 = t2 ◦fE v12 v22 e12 G2: isOn = = v11 v21 G1: isOn e11 Example: fV(s1(e11)) = s2(fE(e11)) fV(t1(e11)) = t2(fE(e11)) Graph Grammars
Definition: Typed Graph Track :Shuttle Shuttle :Shuttle :Track :Track :Track Switch :Switch :Track • A Typed Graph GTyped = (G, type) consists of • a graph G and • a graph morphism to a type graph type: G → GType 1 next Note: A morphism is a total function (fV, fE are total), i.e. every element in the domain (typed graph) has to be related to exactly one element of theco-domain (type graph) isOn 1 1 turnNext Type Graph Typed Graph isOn turnNext isOn next next next Graph Grammars
Matching a Left-hand Side :Shuttle :Track :Track :Track :Track • Matching a graph inside another “host” graph • Find a typed isomorphism to a subgraph of the host graph subgraph isOn next next next isomorphism lhs: :Shuttle v2 ::= … isOn :Track :Track v1 v3 next Graph Grammars
Definition: Subgraph • A subgraph GSub = (VSub, ESub, sSub, tSub) of G= (V, E, s, t) is a graph with • VSub V • ESub E • sSub = s • tSub = t • If GSub is a subgraph of G, we may also write GSub G. Means that the domain of the source and target function for the subgraph is reduced to the edges which are in it. ESub ESub Graph Grammars
Definition: Isomorphism :Shuttle :Track :Track :Track :Track • A graph morphism f: G1→G2, f = (fV , fE) with fV, fE bijective is called a graph isomorphism. subgraph isOn next next next isomorphism lhs: :Shuttle v2 ::= … isOn :Track :Track v1 v3 next Graph Grammars
Definition: Typed Isomorphism Track :Track :Track • Both the graph and host graph are typed over the same type Graph GType: • Lettype = (typeV, typeE)be a graph morphism and • f = (fV, fE) is an isomorphism, then f is a typed isomorphism when • typeV(v) = typeV(fV(v)) • typeE(e) = typeE(fE(e)) next 1 next :Track :Track next Graph Grammars
Definition: Graph Grammar Rule • A graph grammar rule r is defined as a partial morphism r : L→R with L GType the lhs graph of r, R GType the rhs graph of r, GTypethe set of all graphs typed over GType such that there is a maximum common subgraph TL of L and R, with: • TL GType: TL L and TL R and • TLnot empty Graph Grammars
Definition: Graph Grammar Rule L (lhs graph) R (rhs graph) :Shuttle :Shuttle v2 v2 isOn isOn :Track :Track :Track :Track v1 v3 v1 v3 next next partial morphism r : L→R TL (common subgraph) :Shuttle v2 :Track :Track v1 v3 next Graph Grammars
Rule Application - Formally • The application of a rule r: L→R is defined by a relation appr = { (G, G‘‘)(GType xGType) | G‘‘ is defined by the three steps • Search (on host graph G) • Delete (result is G‘) • Create (result is G‘‘) or G = G‘‘ } • apprrepresents all possible applications of r GType An element is a possible (host) graph typed over GType Graph Grammars
Rule Application – Step 1 :Shuttle :Track :Track :Track :Track • Search: Given a host graph G, find a subgraph isomorphism match: L→GSub, GSub G Subgraph GSub isOn next next next isomorphism :Shuttle L (lhs) v2 ::= … isOn :Track :Track v1 v3 next Graph Grammars
Rule Application – Step 2 (1/3) :Shuttle :Track :Track :Track :Track • Delete:Form a new graph G‘ from G by deleting all nodes and edges in GSub which are in match(L) = GSubbut not in G‘Sub = match(TL) ( GSub) (Only the edge labeled “isOn” is removed) GSub isOn next next next TL (common subgraph) L (lhs) :Shuttle :Shuttle v2 v2 isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Rule Application – Step 2 (2/3) • Delete:Remove all edges leading to deleted nodes createAssignment created :Request :Customer queuedRequest :Request :Assignment :Broker queuedRequest assignment assigned respFor respFor <<delete>> <<create>> <<create>> respFor :Shuttle :Shuttle :Shuttle :Shuttle :Broker respFor L (lhs) :Request TL (common subgraph) queuedRequest :Shuttle :Broker :Shuttle :Broker respFor respFor Graph Grammars
Rule Application – Step 2 (2/3) • Delete:Remove all edges leading to deleted nodes createAssignment created :Customer :Request :Assignment :Broker queuedRequest assignment assigned respFor respFor <<delete>> <<create>> <<create>> respFor :Shuttle :Shuttle :Shuttle :Shuttle :Broker respFor L (lhs) :Request TL (common subgraph) queuedRequest :Shuttle :Broker :Shuttle :Broker respFor respFor Graph Grammars
Rule Application – Step 2 (3/3) • Delete:Remove all edges leading to deleted nodes createAssignment :Customer :Request :Assignment :Broker queuedRequest assignment assigned respFor respFor <<delete>> <<create>> <<create>> respFor :Shuttle :Shuttle :Shuttle :Shuttle :Broker respFor L (lhs) :Request TL (common subgraph) queuedRequest :Shuttle :Broker :Shuttle :Broker respFor respFor Graph Grammars
Rule Application – Step 3 (1/3) • Create:Add nodes and edges to G‘, forming an extended graph G‘‘, such that there is an isomorphism matchR: R → G‘‘Sub from R to a new subgraph G‘‘Sub G‘‘ such that(G‘Sub =) match(TL) matchR(R) (= G‘‘Sub) Graph Grammars
Rule Application – Step 3 (2/3) :Shuttle :Track :Track :Track :Track • Create:G‘Sub = match(TL) matchR(R) = G‘‘Sub G‘‘Sub isOn next next next TL (common subgraph) R (lhs) :Shuttle :Shuttle v2 v2 isOn :Track :Track :Track :Track v1 v3 v1 v3 next next Graph Grammars
Rule Application – Step 3 (3/3) • Create:G‘Sub = match(TL) matchR(R) = G‘‘Sub G‘Sub :Customer G‘‘Sub :Assignment assignment :Broker assigned respFor respFor respFor :Shuttle :Shuttle :Shuttle R (lhs) TL (common subgraph) :Assignment assignment assigned :Shuttle :Shuttle :Broker :Broker respFor respFor Graph Grammars
Rule Application - Formally • The application of a rule r: L→R is defined by a relation appr = { (G, G‘‘)(GType xGType) | G‘‘ is defined by the three steps • Search (on host graph G) • Delete (result is G‘) • Create (result is G‘‘) or G = G‘‘ } • A concrete rule application is repre-sented by a tuple (G, G‘‘) appr whereG is the host graph before applying the rule and G‘‘ is the host graph after rule application. GType Graph Grammars
What about Inheritance? • GType = (GL, I) • GL – Directed labeled Graph • I V x V – inheritance relation (cycle free) Example: V = {v1, v2, v3} E = {e1, e2, e3} s(e1) = v1 t(e1) = v2 I = {(v2, v3)} v3 e3 e2 v1 v2 e1 But now the formalization of the matching becomes more complicated… Graph Grammars
Summary :Shuttle :Track :Track • Graph transformation rules allow us: • To describe the behavior of systems (e.g. RailCab) in an abstract, formal way • To model and analyze them • To describe the semantics of OO-programs • To validate or verify them • To model the program behavior visually (Executable code can be generated from them) move <<create>> isOn isOn <<delete>> next Graph Grammars