<<

TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by . 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

                          -.                 ! .            / " #   $    , .#   &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 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  in an interpreter language (.g., )   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 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[] , 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 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 ) { 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 run-time systems for parallel 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 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 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 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Metalevel Architecture Introspective Architectures

Metalevel Metalevel Metaobjects Metaobjects Metaobjects Introspection Meta- program

Base Level

Base Level 19 20 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. Static Metaprogramming Architecture

Metaobjects Metalevel Programs in Programs in Source Form Target Form

Meta- Code Generation, program Dynamic Time Parsing, Pretty Analysing Printing

Base Level

Static Time AST ASG

Intermediate Representation 21 22

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. Compilers Are Static Metaprograms

3. Protocols (MOP) Compiler Meta- program

Programs in Programs in Source Form Target Form

Analysis, AST Transformations ASG !           

23 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. Metaobject Protocol A Very Simple MOP

[ISC] p.52 public class Class { public class Attribute { A metaobject protocol (MOP) Class(Attribute[] f, Method[] m) { public String name; public class Method { is an implementation of the methods of the metaclasses. fields = f; methods = m; public Object value; public String name; } Attribute (String n) { name = n; } public Statement[] statements; public void enterAttribute() { } It specifies an interpreter for the language, Attribute[] fields; public Method(String n) { name = n; } Method[] methods; public void leaveAttribute() { } public void enterMethod() { } describing the semantics, i.e., the behavior of the language objects } public void setAttribute(Object v) { public void leaveMethod() { } enterAttribute(); public Object execute { in terms of the language itself. this.value = v; Object returnValue; leaveAttribute(); enterMethod(); } By changing the MOP, the language semantics is changed for (int i = 0; public Object getAttribute() { i <= statements.length; i++) { or adapted to a context. Object returnValue; statements[i].execute(); enterAttribute(); } returnValue = value; leaveMethod(); If the language is object-oriented, default implementations of leaveAttribute(); return returnValue; metaclass methods can be overwritten by subclassing return returnValue; } } } thereby changing the semantics of the language } public class Statement { public void execute() { ... } } 25 26

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. An Adapted MOP Open Languages

public class TracingAttribute extends Attribute { Program with Language public void enterAttribute() { Open Java, Open C++ Extensions System.out.println("Here I am, accessing attribute " + name); } Employ static metaprogramming public void leaveAttribute() { System.out.println("I am leaving attribute " + name + Language Extensions ": value is " + value); } } Open Compiler Static Metaprograms Class Robot = new Class( new Attribute[]{ "WorkPiece piece1", "WorkPiece piece2" }, new Method[]{ "takeUp() { WorkPiece a = rotaryTable.place1; } "}); Metamodel Metaobject Protocol Class RotaryTable = new Class( new TracingAttribute[]{ "WorkPiece place1", "WorkPiece place2" }, new Method[]{}); Program in Standard Language

Here I am, accessing attribute place1 I am leaving attribute place1: value is WorkPiece #5 Standard Language 27 28 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. An Open Language

... offers its own metamodel for static metaprogramming Its schema (e.g., structure of AST) 4. Metaobject Facilities (MOF) is made accessible as an abstract Users can write static metaprograms to adapt the language Users can override default methods in the metamodel, changing the static language semantics or the behavior of the compiler

... can be used to adapt components at compile time During system generation Static adaptation of components

Metaprograms are removed during system generation, no runtime overhead Avoids the overhead of dynamic metaprogramming 29

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. Example: Generating IDL specifications Metaobject Facility (MOF)

Problem: How to generate IDL from a Java application ? IDL = Description Language You would like to say (here comes the introspection:) The type system of CORBA for all c in classes do generate_class_start(c); for all a in c.attributes do Maps to many other language type systems (Java, C++, C#, etc.) generate_attribute(a); done; generate_class_end(c); Is a of “mediating type system”, least common denominator... done;

For interoperability to components written in other languages, Need a type system that describes the Java type system an interface description in IDL is required With classes and attributes, methods

Some other problems: How to generate code for exchange between C++ and Java? How to bind other type systems than IDL into Corba (UML, ..)?

31 32 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. Metaobject Facility (MOF) The MOF Generator

Programming LanguageLanguage Concept Language Metadata can be used to Level 4- Meta-Concepts in the Description 1 metameta model, the metalanguage Description 2 Get knowledge about unknown data formats, types (language description) Navigate in unknown data Level 3- Generate unknown data Language concepts Method Generate type systems (e.g., IDL from programming languages) (Metaclasses in the metamodel) Class Attribute Language 1 Generate languages from metalanguage specifications Language 2

Level 2- Software Classes void drive(){} int color (meta-objects) A metaobject facility (MOF) is a generative mapping (Model) Person (transformer, generator) from the metalanguage level (Level 4) Level 1- Software Objects car1.color to the language level (Level 3) car 1 car1.drive()

Level 0 – Real World car driving car color 33 34

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. MOF: Example Metaobject Facility (MOF)

The MOF for the CORBA meta-metamodel From different language descriptions, contains a type system for type systems: different (parts of) languages are generated Entities Type systems Relationships Modelling languages (such as UML) Packages Component models Exceptions Workflow languages

Can describe every type system A MOF cannot generate a full-fledged language of a programming or modeling language A MOF is not a MOP MOF concepts must be mapped to types of a specific type system The MOF is generative The MOP is interpretative From these mappings, code can be generated that provides services for that type system, e.g. code that navigates in object graphs. 35 36 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. Meta Levels in Corba Type Systems Metaobject Facility MOF in CORBA

Concept Level 4- Meta-Concepts Language Language (Meta-meta model) Description Description The OMG-MOF (metaobject facility) is a MOF, i.e., a metalanguage, Meta-object facility MOF describing type systems Level 3- Software concepts Describing IDL, the CORBA type system (Meta-model, meta-classes) UML Describing the UML metamodel IDL Method Attribute Type Systems such as Class Describing XML schema IDL, UML, C++, C, Cobol C# Java Standardized Nov. 1997

Level 2- Software Classes It is not a full metalanguage, but only contains (Model) void Color Classes, relations, attributes Types Person OCL specifications to express constraints on the classes and their relations Level 1- Software Objects A MOP cannot be specified in the MOF object1 object1.print() object1.color (methods are lacking in the MOF)

37          38

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. Automatic Data Transformation with Meta Levels in Corba Type Systems the Metaobject Facility (MOF) Concept Level 4- Meta-Concepts Language Language (Meta-meta model) Description Description Given:

Meta-object facility MOF 2 different language descriptions Level 3- Software concepts Class An isomorphic mapping between them XSchema (Meta-model, meta-classes) Class Method IDL Class Method Attribute UML Produced: Type Systems such as Attribute Attribute IDL, C++, C, Cobol A transformer that transforms data in the languages Class C#Method Attribute Level 2- Software Classes Data fitting to MOF-described type systems can automatically be (Model, meta-objects) Color transformed into each other void Types Person The mapping is only an isomorphic function in the metametamodel Exchange data between tools possible Level 1- Software Objects object1 object1.color object1.print() 39          40 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. Isomorphic Language Mappings Reason: Similarities of Type Systems

Metalevel hierarchies are similar for programming, specification, Concept Language and modeling level Description Since the MOF can be used to describe type systems there is hope to describe them all in a similar way

Class Class Method IDL Method UML These descriptions can be used to generate Attribute Attribute Conversions Mappings (transformations) of interfaces and data Color Color void Person Person void

Transformer

41 42

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. Summary MOF

The MOF describes general type systems 5. Component Markup New type systems can be added, composed and extended from old ones

Relations between type systems are supported

For interoperability between type systems and -repositories

Automatic generation of IDL Language extensions, e.g. for extending UML  .          Reflection/introspection supported     Application to workflows data bases, groupware, business processes, data warehouses (Common Warehouse Model, CWM) 43 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. Markup Languages

Convey more semantics for the artefact they markup Hungarian notation is a markup method that defines naming conventions for identifiers in languages HTML, XML, SGML are markup languages to convey more semantics for composition in a component system but still, to be compatible with the syntax of the component language Remember: a component is a container so that standard tools can be used A markup can offer contents of the component for the external world, i.e., for composition The composition environment can ask about the names in the interfaces of a component (introspection) It can offer the content for introspection and can deduce more semantics Or even introcession

45 46

TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. TDDC18 Component-based software. IDA, Linköpings universitet. Slides by courtesy of Uwe Assmann, IDA / TU Dresden. Revised by C. Kessler, 2005. Generic Types Java Beans Naming Schemes

Property access << ClassBox >> << ClassBox >> setField(Object value); Object getField();

Event firing class SimpleList { class SimpleList { genericTType elem; WorkPiece elem; fire SimpleList next; SimpleList next; registerListener genericTType getNext() { WorkPiece getNext() { return next.elem; return next.elem; unregisterListener } } } } Metainformation for JavaBeans is identified by markup in the form of Hungarian Notation. 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. 47 48 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. Markup is Essential Markup by Comments for Component Composition

Javadoc tags C# attributes because it identifies metadata, @author //@author which in turn supports introspection and introcession @date //@date Components that are not marked-up cannot be composed @obsolete //selfDefinedData

Every component model has to introduce Java 1.5 attributes a strategy for component markup C# /.NET attributes

[author(Uwe Assmann)] Insight: [date Feb 24] A component system that supports composition techniques [selfDefinedData(...)] must be a reflective architecture!

49 50

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. What Have We Learned? What Have We Learned?

Reflection is reasoning and modification of oneself or others Metamodeling, e.g. MOF for UML / Corba IDL / ...

Introspection is thinking about oneself, but not modifying Some well-known examples of metaprogramming: Metaprogramming is programming with meta-objects Static metaprogramming at base level System has reflective architecture if meta- and base level use the e.g. C++ templates, AOP same specification or Static metaprogramming at meta level

System has metalevel architecture if it only supports e.g. Compiler analysis / transformations metaprogramming at meta-level (not at the base level) Dynamic metaprogramming at base level

There are several general types of reflective architectures e.g. Java Reflection

A MOP can describe an interpreter for a language; Component and composition systems are reflective architectures the language is modified if the MOP is changed Markup marks the variation and extension points of components A MOF is a generator for (part of) a language Composition introspects the markup The CORBA MOF is a MOF for type systems mainly

51 52