Intermodeling, Queries and Kleisli categories Zinovy Diskin, Tom Maibaum, Krzysztof Czarnecki McMaster University, University of Waterloo NECSIS and MDE • Network for Engineering of Complex SoftwareIntensive Systems for Automotive Systems – Eight Canadian research institutions – Three industrial partners (GM Canada, IBM Canada, and Malina Software) WADT'12 Query languages are Cartesian monads 2 Intermodeling (termed coined by Guerra et al, at MoDELS’10): • What: Specification and maintenance of intermodel relations (techniques and tools) • Why: Modeling is multimodeling R12 M2 M1 R123 R13 WADT'12 R23 M3 Query languages are Cartesian monads 3 Why intermodeling, cont’d Scenarios: • Consistency • Sync/change propagation • Merge R12 M2 M1 R123 R13 R23 M3 There is much more to R’s than name correspondence! WADT'12 Query languages are Cartesian monads 4 Contents • • • • • Specifying intermodel relations is difficult Main features of our framework by examples Q-mappings at work: Model merge Formalization via Cartesian monads Summary: model management via algebra (or, colors of model management) WADT'12 Query languages are Cartesian monads 5 Specifying intermodel relations is not easy The Domain: Ms. Ann Lee, 01/01/1990, a student and an actor = Model S s:Student name=Mr. Lee ? ? bdate=01/01/1990 Model A a: Woman -Actor name=Ann age = 21 ? WADT'12 Query languages are Cartesian monads The indirect link problem 6 Contents • • • • • Specifying intermodel relations is difficult Main features of our framework by examples Q-mappings at work: Model merge Formalization via Cartesian monads Summary: model management via algebra (or, colors of model management) WADT'12 Query languages are Cartesian monads 7 Indirect links via direct Q-links (Kleisli’s idea) • Replace indirect links by direct links into derived elements (computed by queries, Q-links) Model S + s: Student name = Ms.Lee bdate = 01/01/90 /age = 2012-1990 = 22 WADT'12 = ? = Model A a: Actor name = Ann age = 22 Query languages are Cartesian monads 8 But first relate metamodels Metamodel S+ f Student name: {Ms,Mr} x Str bdate: Int3 /age = Q(bdate) : Int Model S+: S+ S: Student name = Ms.Lee bdate = 01/01/90 /age = exec Q(01/01/90) = 22: Int WADT'12 Metamodel, A Actor name = Str age = Int f: f Model, A: A a: Actor name = Ann age = 22 Query languages are Cartesian monads 13 Q-links vs. semantic relativism, 1 Metamodel S Student name: {Mr, Ms} x Str ? Metamodel A Actor name: Str Woman Man Common intermodeling jargon: 1-to-M(any) link We say indirect 1-M link WADT'12 Query languages are Cartesian monads 10 Q-links vs. semantic relativism, 2 Metamodel S Student name: {Mr, Ms} x Str Metamodel A+ Actor name: Str /title = Q1(W-M typing) Woman WADT'12 Query languages are Cartesian monads Man 11 Q-links vs. semantic relativism, 3 Metamodel S Student name: {Mr, Ms} x Str Metamodel A+ Actor name: Str /title = Q1 (W-M typing) /tname = Q2 (name, /title) Woman Man An indirect 1-M link x <---> {y1,…, ym } is replaced by a direct 1-1 q-link x <---> Q(y1,…, ym) WADT'12 Query languages are Cartesian monads 12 Q-links and asymmetry of mappings m Metamodel S Student name: {Mr, Ms} x Str Metamodel A+ Actor name: Str /title = Q1 (W-M typing) /tname = Q2 (name, title) S is a view of A S Woman A m Man i A+ = Q(A) m is an ordinary total mapping consisting of pairs of elements. WADT'12 Query languages are Cartesian monads 13 Derived-derived case. Partial mappings. Metamodel S+ Student name: {Mr, Ms} x Str m Metamodel A+ Actor x /title = Q2 (W-M typing) /title = Q1(name) Woman x S i1 Man A m2 S+ = Q1(S) i2 A+ = Q2(S) m1 WADT'12 Query languages are Cartesian monads 14/36 From partial mappings to spans (Reifying intermodel links) Metamodel S+ Student name: {Mr, Ms} x Str Metamodel A+ Actor Metamodel R Person title: {Mr, Ms} /title = Q1 (W-M typing) /title = Q1(name) Woman S m1 i1 A m2 i2 S+ = Q1(S) WADT'12 R Man A+ = Q2(S) Query languages are Cartesian monads 15/36 Contents • • • • • Specifying intermodel relations is difficult Main features of our framework by examples Q-mappings at work: Model merge Towards formalization via Cartesian monads Summary: model management via algebra (or, colors of model management) WADT'12 Query languages are Cartesian monads 16 General pattern for merge B R1 A ... … r1 R2 C r2 . . . M D + Color Legend: Black -- given Green --- discovery/heuristics/AI Blue --- pure algebra! FASE'12 Intermodeling, Queries, and Kleisli categories 28/36 Merge via colimit: Example Metamodel S+ Student name: {Mr, Ms} x Str R2S Metamodel R Person title: {Mr, Ms} R2A /title = Q2 (W-M typing) /title = Q1(name) R2M S2M Metamodel A+ Actor Woman Metamodel M+ ? (Person) name: {Mr, Ms} x Str Man A2M /title = Q1 (name) = Q2 (W-M typing) Woman FASE'12 Man Intermodeling, Queries, and Kleisli categories 23/36 Merge via colimit cont’d Metamodel S+ Student name: {Mr, Ms} x Str R2S Metamodel R Person title: {Mr, Ms} R2A /title = Q2 (W-M typing) **** /title = Q1(name) S2M Metamodel A+ Actor R2M Woman Metamodel M ? (Person) name: {Mr, Ms} x Str Woman Man A2M Man Q1 (name) = Q2 (W-M typing) FASE'12 Intermodeling, Queries, and Kleisli categories 23/36 Another version of intermodel span Metamodel S+ R2S Metamodel R Student Person name: {Mr, Ms} x Str title: {Mr, Ms} x Str /title = Q1(name) Q2(title) /MrPerson R2A Metamodel A Actor Woman Man /MsPerson Metamodel M+ ? (Person) name: {Mr, Ms} x Str /title = Q1 (name) Q2(/title) /MrPerson FASE'12 /MsPerson 20 Normalized merge R2S Metamodel R Metamodel S+ Student Person name: {Mr, Ms} x Str title: {Mr, Ms} x Str /title = Q1(name) Q2(title) /MrPerson R2A Metamodel A Actor Woman Man /MsPerson Metamodel M ? (Person) name: {Mr, Ms} x Str FASE'12 FASE'12 Intermodeling, Queries, and Kleisli categories 21/36 General pattern for merge B R1 A ... … r1 R2 C r2 . . . M D + Color Legend: Black -- given Green --- discovery/heuristics/AI Blue --- pure algebra! FASE'12 Intermodeling, Queries, and Kleisli categories 28/36 Three colors of model management Discovering (heuristics + AI): Model diff/matching/alignment Execution (pure algebra): Model merge/update propagation Mixture: Weaving Green and Blue in one algorithm/module often results in Red FASE'12 Intermodeling, Queries, and Kleisli categories But… • Algebraic techniques work well if the universe of models and Q-mappings between them is a category • We need to define composition of Q-mappings • It is not trivial FASE'12 Intermodeling, Queries, and Kleisli categories 24/36 Q-mappings as Kleisli mappings Thick (extensional) notation A Ordinary mappings A B f Thin (formula) notation B f iQ Q(B) Q(B) m = (Q, f) Q-mappings WADT'12 A ******* B iQ A Query languages are Cartesian monads m B 27/36 Problem of Q-mapping composition Models and their maps A f AD Instances (data and query execs) Subindex D stands for “the Data part” Typing mapping (t is “the typing part”) fD Qf(AD) At fS Qf(AS ) Schemas (data & query/view definitions) S stands for “the schema part” Metamodels (or schemas) WADT'12 AS BD fS C gD CD Qg(BD) ? Qf(At) AS g B Ct Bt Qg(Bt) BS ? gS CS Qg(BS ) BS Query languages are Cartesian monads gS CS 26/36 Solution: first, we manage syntax (the easy part) f A B AD BD Qf(AD) Instances (data and query execs) Subindex D stands for “the Data part” typing AS Schemas (data & query/view definitions) S stands for “the schema part” WADT'12 fD C gD ? fS g Qg(BD) BS Qf (AS) Qg#( Qf (AS)) CD gS Qg (BS) f S# Query languages are Cartesian monads CS Solution: second, we manage semantics (the difficult part) Fortunately, many useful queries are monotonic (e.g., SPJ) f A B AD BD Schemas (data & query/view definitions) S stands for “the schema part” WADT'12 CD Qg(BD) Qg#(Qf(AD)) AS C gD Qf(AD) Instances (data and query execs) Subindex D stands for “the Data part” typing fD g fS f D# BS Qf (AS) Qg#( Qf (AS)) gS Qg (BS) f S# Query languages are Cartesian monads CS Some formal details 1) Simple (no-queries) model translation from one metamodel to another (retyping) can be specified by a categorical construct called fibration, p: Mod -->MMod (by def). 2) A monotonic query language is specified by a monad over the fibration 1), Q: Mod-->Mod, Qdef: MMod-->MMod, p:Q-->Qdef. This monad is Cartesian! (by def) 3) Models and Q-mappings form the Kleisli category of the monad in 2) • Corollary. If the cat of models and simple mappings is closed wrt. merge, then the cat of models and Qmappings is closed wrt. merge as well. 4) Theorem. Model translations determined by Q-mappings (= view mechanism) again form a fibration. That is, querying followed by retyping is compositional. FASE'12 Intermodeling, Queries, and Kleisli categories 33/36 Summary • A (vast?) majority of intermodel relationships can be specified by Q-mappings • With Kleisli construction, models and Qmappings between them form a category, i.e., a universe of well-defined algebraic operations • Model management scenarios include two basic stages: – Discovery of Q-mappings between models; – Execution of an algebraic operation over models and mappings • Weaving green into blue often results in red WADT'12 Query languages are Cartesian monads THE END WADT'12 Query languages are Cartesian monads /30 Solution 1: Annotate indirect links with correspondence rules (= expressions attached to links) Model S s:Student name=Mr. Lee = Conflict! Model A a: Woman -Actor name=Ann If s = a, then if s.name.title = Mr, then a.class = Man, else a.class = Woman WADT'12 Query languages are Cartesian monads 9 Solution 1: another sample Model S s:Student name=Mr. Lee bdate=01/01/1990 = Conflict! Model A a: Woman -Actor name=Ann age = 21 if s = a, then if s.bdate.month&day current.month&day then a.age = 2012 s.bdate.year - 1, else a.age = 2012 – p.bdate.year WADT'12 Query languages are Cartesian monads 10 Problems of Solution 1 R12 M1 … R23 M2 … … M3 … … R13 ? • Partially ad hoc • Or, use something universal, say, OCL or FOL, but • Network structure is collapsed into one big model • Mapping composition problem (well recognized in the DB literature, Bernstein et al.) WADT'12 Query languages are Cartesian monads 34 Grand statement • Many (all?) 1-M indirect links are nothing but hidden 1-1 Q-links: A basic element of one model can be derived in another model and vv. • Semantic relativism = Basic-Derived correspondence (= Q-links) WADT'12 Query languages are Cartesian monads /30 Grand indeed? • What’s about M-M links, or the derived-derive case? WADT'12 Query languages are Cartesian monads /30