Metadata TDDD05 / DF14900 Component-Based Software Metamodeling and Metaprogramming Christoph Kessler, IDA, Linköpings universitet, 2010 Some slides by courtesy of U. Assmann, IDA / TU Dresden Meta: means “describing” Metadata: describing data (sometimes: self-describing data). z The language (esp., type system) for specifying metadata is called metamodel. Metalevel: the elements of the meta-level (the meta-objects) describe the objects on the base level Metamodeling: description of the model elements/concepts in the metamodel 1. Introduction to metalevels 2. Different Ways of Metaprogramming Metadata 3. UML Metamodel and MOF 4. Component markup U. Assmann: Invasive Software Composition, Sect.Christoph 2.2.5Kessler, Metamodeling; IDA, Linköpings universitet, 2009. C. Szyperski: Component Software, Sect. 10.7, 14.4.1 Java Reflection Metalevels in Programming Languages Level 3 - Meta-Concepts in the metameta model, the metalanguage (language description) Level 2 - Language concepts (Metaclasses in the metamodel) Method Class Level 1 - Software Classes (meta-objects) (Model) Car Level 0 - Software Objects car 1 “Real World” Entities car 3 Programming Language Concept void drive() {} car1.drive() C. Kessler, IDA, Linköpings universitet. driving 2 C. Kessler, IDA, Linköpings universitet. Level 3 - Meta-Concepts in the metameta model, the metalanguage (language description) Attribute int color car1.color car color TDDD05 Component-Based Software Classes in a software system Level 2 - Language concepts (Metaclasses in the metamodel) Car Level 0 - Software Objects car 1 “Real World” Entities car 4 Method Attribute void drive() {} car1.drive() driving C. Kessler, IDA, Linköpings universitet. int color car1.color car color TDDD05 Component-Based Software Reflection is computation about the metamodel in the base model. The application can look at its own skeleton (metadata) and may even change it z Metaclasses C. Kessler, IDA, Linköpings universitet. Programming Language Concept Class Level 1 - Software Classes (meta-objects) (Model) z 5 TDDD05 Component-Based Software Reflection (Self-Modification, Metaprogramming) class WorkPiece { Object belongsTo; } class RotaryTable { WorkPiece place1, place2; } class Robot { WorkPiece piece1, piece2; } class ConveyorBelt { WorkPiece pieces[]; } Examples: • Java Reflection API [Szyperski 14.4.1] • UML metamodel (MOF) Base level Metalevels in Programming Languages Classes and Metaclasses Concepts of a metalevel can be represented at the base level. This is called reification. Data, Code, Information Meta level Concepts level public class Class { Attribute[] fields; Method[] methods; Class ( Attribute[] f, Method[] m) { fields = f; methods = m; } } public class Attribute {..} public class Method {..} TDDD05 Component-Based Software Allocating new classes, methods, fields Removing classes, methods, fields Enabled by reification of meta-objects at base level (e.g., as API) Remark: In the literature, “reflection” was originally introduced to denote “computation about the own program” [Maes'87] but has also been used in the sense of “computing about other programs” (e.g., components). 6 C. Kessler, IDA, Linköpings universitet. Metadata Meta level Data, Data, Code, Code, Information Information Base level TDDD05 Component-Based Software Example: Creating a Class from a Metaclass class WorkPiece { Object belongsTo; } class RotaryTable { WorkPiece place1, place2; } class Robot { WorkPiece piece1, piece2; } class ConveyorBelt { WorkPiece pieces[]; } Create a new class at runtime Introspection public class Class { Attribute[] fields; Method[] methods; Class ( Attribute[] f, Method[] m) { fields = f; methods = m; } } public class Attribute {..} public class Method {..} Read-only reflection is called introspection z The component can look up the metadata of itself or another component and learn from it (but not change it!) Typical application: find out features of components by instantiating the metaclass: z Classes, methods, attributes, types z Very important for late (run-time) binding Metadata Class WorkPiece = new Class( new Attribute[]{ "Object belongsTo" }, new Method[]{}); Class RotaryTable = new Class( new Attribute[]{ "WorkPiece place1", "WorkPiece place2" }, new Method[]{}); Class Robot Data, Code, Information = new Class( new Attribute[]{ "WorkPiece piece1", "WorkPiece piece2" }, new Method[]{}); Data, Code, Information Class ConveyorBelt = new Class( new Attribute[]{ "WorkPiece[] pieces" }, new Method[]{}); 7 C. Kessler, IDA, Linköpings universitet. Metaprogram at base level TDDD05 Component-Based Software 8 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Reflection Example Introcession Read and Write reflection is called introcession z Reading Reflection The component can look up the metadata of itself or another component and may change it (Introspection): for all c in self.classes do generate_class_start(c); for all a in c.attributes do generate_attribute(a); done; generate_class_end(c); done; Typical application: dynamic adaptation of parts of own program z Classes, methods, attributes, types Full Reflection (Introcession): for all c in self.classes do helpClass = makeClass( c.name + "help“ ); for all a in c.attributes do helpClass.addAttribute(copyAttribute(a)); done; self.addClass(helpClass); done; Metadata AAreflective reflectivesystem systemisisaasystem systemthat thatuses usesthis thisinformation informationabout aboutitself itself ininits itsnormal normalcourse courseofofexecution. execution. Data, Code, Information 9 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software 10 Metaprogramming on the Language Level C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Made It Simple Level 0: objects enum { Singleton, Parameterizable } BaseFeature; Metalanguage concepts Language description concepts public class LanguageConcept { (Metametamodel) String name; BaseFeature singularity; LanguageConcept ( String n, BaseFeature s ) { name = n; Good for language singularity = s; extension / customization, } e.g. with UML MOF, or for } compiler generation Language concepts (Metamodel) Level 1: classes, types Level 2: language elements Level 3: metalanguage, language description language LanguageConcept Class = new LanguageConcept("Class", Singleton); LanguageConcept Attribute = new LanguageConcept("Attribute", Singleton); LanguageConcept Method = new LanguageConcept("Method", Parameterizable); 11 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software 12 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Use of Metamodels and Metaprogramming TDDD05 / DF14900 Component-Based Software 2. Different Ways of Metaprogramming To model, describe, introspect, and manipulate Programming languages, such as Java Reflection API Modeling languages, such as UML or Modelica XML - meta-level vs. base level - static vs. dynamic Compilers Debuggers Component systems, such as JavaBeans or CORBA DII Metaprograms Metaprograms are areprograms programs that that compute computeabout about programs programs Composition systems, such as Invasive Software Composition Databases Metaprogram ... many other systems ... Program Program’ run-time output run-time input 13 Christoph Kessler, IDA, Linköpings universitet, 2009. C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Metaprograms can run at base level or at meta level Base-Level Metaprogram Metaprogram execution at the metalevel: Metaprogram is separate from base-level program Direct control of the metadata as metaprogram data structures Expression operators are defined directly on the metaobjects Metalevel Metaobjects Repository with Concepts/ Types/Descriptions as Artefacts Example: Compiler, program analyzer, program transformer z Program metadata = the internal program representation Reflection has classes to create objects describing base program classes, functions, statements, variables, constants, types etc. Metaprogram execution at the base level: Metaprogram/-code embedded into the base-level program All expressions etc. evaluated at base level Base-level program data memory: Repository with Objects as Artefacts Access to metadata only via special API, e.g. Java Reflection 15 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Meta-level Metaprogram Metaobjects 16 Metaprogram Base Level Class Classsomeclass someclass== foo.getClass(); foo.getClass(); C. Kessler, IDA, Linköpings universitet. Static vs. Dynamic Metaprogramming Metalevel Recall: Metaprograms are programs that compute about programs. Static metaprograms Metaprogram for foreach eachclass classcc add addaanew newmethod method int intbar() bar(){...} {...} Base Level z Execute before runtime z Metainformation removed before execution – no runtime overhead z Examples: Program generators, compilers, static analyzers Dynamic metaprograms z Execute at runtime z Metadata stored and accessible during runtime z Examples: Programs using reflection (Introspection, Introcession); Interpreters, 17 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software TDDD05 Component-Based Software 18 C. Kessler, IDA, Linköpings universitet. debuggers TDDD05 Component-Based Software Static Metaprogramming Static Time Metaprogram Example: Static Metaprogramming (1) Run Time Static Time Metalevel Metaobjects Base Level Metaprogram and metaobjects exist only at compile time. No run-time overhead. 19 Metalevel Metaobjects Metaprogram Compiler Integer 4 myType 8 type table ...malloc( N * sizeof(myType)); TDDD05 Component-Based Software 20 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Compilers Are Static Metaprograms Example: Static Metaprogramming (2) Metaclasses: The compiler’s own data types to represent IR structures C++ templates Example: generic type definition (Meta)Information about generic type removed after compiling! class Vector_int { template <class E> class Vector { E *pelem; int size; E get( int index ) {...} ... } ... Vector<int> v1; Vector<float> v2; 21 C. Kessler, IDA, Linköpings universitet. Base Level ...malloc( N * 8 ); Metaprogram and metaobjects exist only at compile time. No run-time overhead. C. Kessler, IDA, Linköpings universitet. z Run Time expanded at compile time to equivalent of: int *pelem; int size; int get( int index ) {...} ... } class Vector_float { float *pelem; int size; float get( int index ) {...} ... } ... Vector_int v1; Vector_floatTDDD05 v2; Component-Based Software Metaprogram (Level 2) Programs Programs ininSource Source Form Parsing, Form IR’ Analysing IR Analysis, Transformations Programs Programsinin Target TargetForm Form Code Generation / Pretty Printing Classes etc. represented by IR objects = metaobjects = instances of IR classes (metaclasses) Run time objects (Level 0) 22 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Dynamic Metaprogramming Compilers are Static Metaprograms char x[7]; /* array - construct the type `array 0..n-1 of ty' with alignment a or ty's */ int a[13]; Type array( Type ty, int n, int a ) { ... if (ty && isfunc(ty)) { chartype 1 error( "illegal type `array of %t'\n", ty ); return array ( inttype, n, 0 ); inttype 4 } voidtype 0 if (a == 0) a = ty->align; ARRAY(7,chartype) 7 if (level > GLOBAL && isarray(ty) && ty->size == 0) ARRAY(13,inttype) 52 error( "missing array size\n“ ); if (ty->size == 0) { type table excerpt if (unqual(ty) == voidtype) error( "illegal type `array of %t'\n", ty ); else if (Aflag >= 2) warning( "declaring type `array of %t' is undefined\n", ty ); } else if (n > INT_MAX / ty->size) { error( "size of `array of %t' exceeds %d bytes\n", ty, INT_MAX ); n = 1; Source: lcc C compiler, } excerpt of file ”types.c” return tynode ( ARRAY, ty, n * ty->size, a, (Generic)0 ); 23 } C. Kessler, IDA, Linköpings universitet. (typeTDDD05 tableComponent-Based management) Software Repository with Concepts/ Types/Descriptions as Artefacts Metaobjects Metalevel Reflection Base-level program data memory: Repository with Objects Metaas Artefacts program 24 Class Classsomeclass someclass== foo.getClass(); foo.getClass(); C. Kessler, IDA, Linköpings universitet. Base Level TDDD05 Component-Based Software Summary: Ways of Metaprogramming Reflective Architecture A system with a reflective architecture maintains metadata and a causal connection between meta- and base level. Metaprogram runs at: Base level Compile/Deployment time (static metaprogramming) C++ template programs Compiler C sizeof(...) operator transformations; C preprocessor COMPOST Run time Java Reflection (dynamic metaprogramming) JavaBeans introspection z The metaobjects describe structure, features, semantics of domain objects z This connection is kept consistent Meta level Reflection is thinking about oneself (or others) at the base level with the help of metadata Debugger Metaprogramming is programming with metaobjects, either at base level or meta level Reflection 25 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software 26 TDDD05 / DF14900 Component-Based Software C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software UML Metamodel and MOF UML metamodel specifies UML semantics in the form of a (UML) class model (= reification) specified in UML Superstructure document (OMG 2006) using only elements provided in MOF 3. UML Metamodel and MOF UML metametamodel: MOF (”Meta-Object Facility”) self-describing subset of UML (= reification) for bootstrapping the UML specification Christoph Kessler, IDA, Linköpings universitet, 2009. UML metamodel hierarchy UML Extension possibility 1: Stereotypes e.g., <<metaclass>> is a stereotype (specialization) of a class z by subclassing metaclass ”Class” of the UML metamodel 28 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software UML Metamodel (Simplified Excerpt) ModelElement Metametalevel (L3) UML metametamodel: MOF name: Name ModelElement : ModelElement name: Name UML metamodel 1 subtype 1 supertype Generalizable Element is instance of Metalevel (L2) Class : ModelElement isRoot: Boolean isLeaf: Boolean isAbstract: Boolean isActive: Boolean * * is instance of Base level (L1) UML base-level class diagrams owner Cat : Class Classifier name: String color: Color age: Integer 1 type * is instance of Object level (L0) 29 name: ”Tom” color: White age: 7 C. Kessler, IDA, Linköpings universitet. discriminator: Name BehavioralFeature isQuery: Boolean Operation <<metaclass>> Class Interface C. Kessler, Boolean IDA, Linköpings universitet. 30 isActive: ownerScope: ScopeKind visibility: VisibilityKind multiplicity: Multiplicity changeable: ChangeableKind targetScope: ScopeKind initialValue: Expression TDDD05 Component-Based Software * StructuralFeature Attribute tom : Cat Feature Generalization isAbstract: Boolean concurrency: CallConcurKind Datatype TDDD05 Component-Based Software Example: Reading the UML Metamodel UML vs. programming language metamodel hierarchies Some semantics rules expressed in the UML metamodel above: Metametalevel (L3) Each model element must have a name. A class can be a root, leaf, or abstract z Metalevel (L2) UML metamodel Class : ModelElement Base level (L1) UML base-level class diagrams (via Classifier) Cat : Class C. Kessler, IDA, Linköpings universitet. is instance of class Cat { String name; Color color; Integer age; ... } models is instance of Object level (L0) (1:N relation to Classifier), e.g. classes, interfaces, datatypes 31 java.lang.Class name: String color: Color age: Integer Each attribute has a type z models isActive: Boolean is instance of (1:N relations to class ”Generalization”) A class can have many features, e.g. attributes, operations z -- name: Name is instance of (inherited from GenerizableElement) A class can have many subclasses and many superclasses z ModelElement : ModelElement UML metametamodel: MOF TDDD05 Component-Based Software 32 tom : Cat is instance of models name: ”Tom” color: White age: 7 C. Kessler, IDA, Linköpings universitet. Cat tom = new Cat(”Tom”, White, 7); TDDD05 Component-Based Software TDDD05 / DF14900 Component-Based Software Caution A metamodel is not a model of a model but a model of a modeling language of models. 4. Component Markup A model (e.g. in UML) describes a language-specific software item at the same level of the metalevel hierarchy. z In contrast, metadata describes it from the next higher level, from which it can be instantiated. MOF is a subset of UML able to describe itself – no higher metalevels required for UML. ... A simple aid for introspection and reflection... 33 Christoph Kessler, IDA, Linköpings universitet, 2009. C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Markup Languages Hungarian Notation Hungarian notation is a markup method that defines Convey more semantics for the artifact they markup naming conventions for identifiers in languages HTML, XML, SGML are markup languages Remember: a component is a container Markup can make contents of the component accessible for the external world, i.e., for composition z It can offer the content for introspection z Or even introcession z to convey more semantics for composition in a component system z but still, to be compatible with the syntax of the component language z so that standard tools can still be used The composition environment can ask about the names in the interfaces of a component (introspection) z 35 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software 36 and can deduce more semantics from naming conventions C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Generic Types in COMPOST Java Beans Naming Schemes Metainformation for JavaBeans is identified by markup in the form of Hungarian Notation. z << ClassBox >> << ClassBox >> T Property access class SimpleList { genericTType elem; SimpleList next; genericTType getNext() { return next.elem; } } 37 This metainformation is needed, e.g., by the JavaBeans Assembly tools to find out which classes are beans and what properties and events they have. class SimpleList { WorkPiece elem; SimpleList next; WorkPiece getNext() { return next.elem; } } C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software setField(Object value); z Object getField(); Event firing 38 z fire<Event> z register<Event>Listener z unregister<Event>Listener C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software Markup is Essential for Component Composition Markup by Comments Javadoc tags, XDoclet z C# attributes z @author z //@author z @date z //@date z @deprecated z //selfDefinedData because it identifies metadata, which in turn supports introspection and introcession Components that are not marked-up cannot be composed Java 1.5 attributes z Can annotate any declaration e.g. class, method, interface, field, enum, parameter, ... C# /.NET attributes z [author(Uwe Assmann)] z predefined and user-defined z [date Feb 24] z class C extends B { @Overrides public int foo() { ... } z [selfDefinedData(...)] Every component model has to introduce a strategy for component markup Insight: A component system that supports composition techniques must be a reflective architecture! ... } 39 C. Kessler, IDA, Linköpings universitet. What Have We Learned? TDDD05 Component-Based Software (1) 40 C. Kessler, IDA, Linköpings universitet. What Have We Learned? TDDD05 Component-Based Software (2) Reflection is a program’s ability to reason about and possibly modify itself or other programs with the help of metadata. z Reflection is enabled by reification of the metamodel. z Introspection is thinking about a program, but not modifying. The UML metamodel is a description of UML specified in terms of the UML metametamodel, MOF z UML models describe program objects on the same level of the meta-hierarchy level. A metaprogram is a program that computes about programs z Metaprograms can execute at the base level or at the metalevel. z Metacode can execute statically or at run time. Component and composition systems are reflective architectures z Markup marks the variation and extension points of components e.g., using Hungarian notation, Comments/Annotations, external markup (separate files referencing the contents) z Composition introspects the markup z Look up type information, interface information, property information z or full reflection Static metaprogramming at base level e.g. C++ templates, AOP Static metaprogramming at meta level e.g. Compiler analysis / transformations Dynamic metaprogramming at base level e.g. Java Reflection 41 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software 42 C. Kessler, IDA, Linköpings universitet. TDDD05 Component-Based Software