M E Ta Mo De Lin G a N D Meta P Rog Ra Mming 1. in Trod U Ctio N to M E Ta Le
Total Page:16
File Type:pdf, Size:1020Kb
TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Metamodeling and 1. Introduction to Metalevels Metaprogramming -. ! . / " # $ , .# &0. 12+ %&#' " () %#&' *+, TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-basedExample: software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Metadata Different Types of Program Semantics and their Metalanguages (Description Languages) Meta: means “describing” Syntactic structure Metadata: describing data (sometimes: self-describing data). Described by a context free grammar The type system is called metamodel Does not consider context Metalevel: the elements of the meta-level (the meta-objects) Static Semantics describe the objects on the base level Described by context sensitive grammar Metamodeling: description of the model elements/concepts in the (or attribute grammar, denotational semantics, logic constraints) metamodel Describes context constraints, context conditions Can describe consistency conditions on the specifications “If I use a variable here, it must be defined elsewhere” “If I use a component here, it must be alive” Dynamic Semantics Interpreter in an interpreter language (e.g., lambda calculus) Sets of runtime states or terms 3 4 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Metalevels Classes and Metaclasses in Programming Languages Programming Language Concept Level 4- Meta-Concepts in the class WorkPiece { Object belongsTo; } Classes in a software system metameta model, the metalanguage class RotaryTable { WorkPiece place1, place2; } (language description) class Robot { WorkPiece piece1, piece2; } class Press { WorkPiece place; } Level 3- Language concepts class ConveyorBelt { WorkPiece pieces[]; } (Metaclasses in the metamodel) Method Class Attribute Metaclasses public class Class { Concepts of a metalevel can be Level 2- Software Classes Attribute[] fields; void drive() {} int color (meta-objects) represented at the base level. Method[] methods; (Model) Car This is called reification. Class ( Attribute[] f, Method[] m) { fields = f; methods = m; Level 1- Software Objects car1.color Examples: car 1 car1.drive() • Java Reflection API [Szyperski 14.4.1] } } Level 0 – Real World • UML metamodel (MOF) car driving car color public class Attribute {..} public class Method {..} 5 6 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Reflection Creating a Class from a Metaclass public class Class { (Self-Modification, Metaprogramming) Attribute[] fields; class WorkPiece { Object belongsTo; } Method[] methods; class RotaryTable { WorkPiece place1, place2; } Class ( Attribute[] f, Method[] m) { Computation about the metamodel in the model is reflection. class Robot { WorkPiece piece1, piece2; } fields = f; class Press { WorkPiece place; } methods = m; The application can look at its own skeleton (metadata) class ConveyorBelt { WorkPiece pieces[]; } } and may even change it } Example: Create a class public class Attribute {..} Allocating new classes, methods, fields by instantiating the metaclass: public class Method {..} Removing classes, methods, fields Metadata Class WorkPiece = new Class( new Attribute[]{ "Object belongsTo" }, new Method[]{}); Meta level Remark: In the literature, Class RotaryTable = new Class( new Attribute[]{ "WorkPiece place1", "WorkPiece place2" }, “reflection” was originally new Method[]{}); introduced to denote “computation about the Class Robot = new Class( new Attribute[]{ "WorkPiece piece1", "WorkPiece piece2" }, own program” [Maes'87] Data, new Method[]{}); but has also been used in Code, Base level Class Press = new Class( new Attribute[]{ "WorkPiece place" }, new Method[]{}); the sense of “computing Class ConveyorBelt = new Class( new Attribute[]{ "WorkPiece[] pieces" }, new Method[]{}); about other programs” Information (e.g., components). 7 Metaprogram at base level 8 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Introspection Introcession Read-only reflection is called introspection Read and Write reflection is called introcession The component can look up the metadata of itself or another The component can look up the metadata of itself or another component and learn from it (but not change it!) component and may change it Typical application: find out features of components Typical application: dynamic adaptation of parts of own program Classes, methods, attributes, types Classes, methods, attributes, types Very important in component supermarkets Metadata Metadata Data, Data, Data, Code, Code, Code, Information Information Information 9 10 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Metaprogramming Reflection Example on the Language Level enum { Singleton, Parameterizable } BaseFeature; Metalanguage concepts Reading Reflection (Introspection) Full Reflection: (Introcession) public class LanguageConcept { Language description concepts (Metametamodel) for all c in self.classes do for all c in self.classes do String name; generate_class_start(c); helpClass = makeClass(c.name+"help"); BaseFeature singularity; for all a in c.attributes do for all a in c.attributes do LanguageConcept ( String n, BaseFeature s ) { generate_attribute(a); helpClass.addAttribute(copyAttribute(a)); name = n; done; done; singularity = s; generate_class_end(c); self.addClass(helpClass); } done; done; } Language concepts (Metamodel) LanguageConcept Class = new LanguageConcept("Class", Singleton); A reflective system is a system that uses this information about itself LanguageConcept Attribute = in its normal course of execution. new LanguageConcept("Attribute", Singleton); LanguageConcept Method = new LanguageConcept("Method", Parameterizable); 11 12 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Use of Metamodels and Made It Simple Metaprogramming Level 1: objects To model, describe, introspect, and manipulate Level 2: classes, types Workflow systems Level 3: language Databases Level 4: metalanguage, language description language Programming languages Compiler run-time systems for parallel computers Component systems, such as CORBA Composition systems, such as Invasive Software Composition Modeling systems, such as UML or Modelica ... probably all systems ... 13 14 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Reflective Architecture A system with a reflective architecture maintains metadata and a 2. Metalevel Architectures causal connection between meta- and base level. The metaobjects describe structure, features, semantics of domain objects This connection is kept consistent Reflection is thinking about oneself with the help of metadata Metaprogramming is programming with metaobjects 16 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Reflective Architecture Metalevel Architecture Metalevel Metaobjects Repository In a metalevel architecture, with Concepts/ the metamodel is used for computations, Types/Descriptions as Artefacts but the metaprograms execute either on the metalevel or on the base level. Special variants: Reflection Introspective architecture Base Level (no self-modification) Example: Java Reflection API Staged metalevel architecture (metaprogram evaluation time is different from system runtime) Repository Meta- Example: C++ templates (generics), expanded at compile time with Objects program as Artefacts 17 18 TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. TDDC18