Metamodelling & Metaprogramming Lena Buffoni lena.buffoni@liu.se
What is a model? A representation of a concept, phenomenon, relationship, structure, system from the real world Used to communicate, test scenarios, predict behavior Is an abstraction Always simplified to some degree Maps, toy- cars, stochastic economic models are all examples of models
A building model Renters view Tax collectors view Electricians view Builders view Realtors view Designers view Each view concentrates on modeling specific aspects
From model to meta-model - vehicles How can we generalize the concepts? Car Model (in Modelica) Truck model
Generalizing through inheritance Super-class vehicle X wheels a steering wheel a clutch
What if we throw an airplane in the mix? Super-class vehicle X wheels a steering wheel a clutch
GeneralizaOon through metamodelling Aerodynamic Model 1 Vehicle Engine 1..n Actuator 1..n Sensor 1..n Reason in terms of categories on a higher abstracoon level
Another example : Modeling Methodologies Classical model V model Can we deduce a general modeling methodology? Agile model
4 layers of meta-modeling Conforms to Meta-meta model (language specificaoon) Conforms to Meta-model (modeling language) Not the only way to view things! Model (car class) Conforms to Represented by System (car instance) Jean Bézivin, On the unificaoon power of models, 2005.
UML and Meta-modeling MOF (Meta Object Faculty) a standard for specifying UML meta-models Provides language constructs for specifying a DSL meta-model Mainly based on Object-Oriented constructs: package, classes, properties (attribute and reference), and (multiple) inheritance. Specificities: composition, opposite...
Meta-programming Operations on the program itself We have a program model abstract syntax tree Simplified example: A := B + 3; assignement Led-side assignment Right-Side expression Variable Expression A Binary Operator +
When? At compile time : preprocessing Macros in C optimization annotations templates in C++ Compilers can be seen as metaprograms
When? At runtime : interpreters Dynamic component configuration and reconfiguration Java reflection API
To what extent? Introspection - look at the structure of the program Generate documentation Verify composability Reflection - manipulate the structure of the program Dynamically configure components Adapt component behavior over time
How? At a meta-level: A separate language for processing the program At the same level as the program itself (reification) Concepts of a meta-level can be represented at the base level. Ex: Reflection API in Java Class c = A.getClass();
Java reflecoon API Dynamically select which method to call public class ConfigurablePrint { void printonlinux(){} void printonmac() {} void printonwindows(){} }
Java reflecoon API public sta?c void main(string[] args) { try{ //load the AppTest at runome Class cls = Class.forName("ConfigurablePrint"); Object obj = cls.newinstance(); Class [] noparams = {}; //call the printit method String methodname = ""; if(getsystem() == SystemType.WINDOWS) methodname = "printonwindows"; else if(getsystem() == SystemType.MAC) methodname = "printonmac"; else methodname = "printonlinux"; Method method = cls.getdeclaredmethod(methodname, noparams); method.invoke(obj, (Object[])null); }catch(excep?on ex){ ex.printstacktrace(); }}
Meta-programming one step further A hack to language extension? add a backwards_for to a language change how inheritance works Meta-meta-programming? compilers of compilers
Component Markup languages Markup languages distinguish between the basic programming directives and meta instructions LaTeX mixes text and typesetting directives HTML, XML Annotations, Javadoc tags is Java Attributes in C++
AnnotaOons in Java @Override public String tostring() { Predefined annotaoons return "This is String Representation of current object."; } Javadoc annotaoons /** * The HelloWorld program. * * @author Lena * @version 1.0 */ public class HelloWorld { } }
AnnotaOons in composioon @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) //on class level public @interface PreferencePolicy { public enum Policy { WEAK, MEDIUM, STRONG } Policy priority() default Policy.MEDIUM; }
Component composioon Markup is important in component composition Mark methods that will be exposed to the client Version compatibility verification Define configuration and initialization parameters
Summary Metamodelling : from specific models towards models of models Metaprogramming : operating on the structure of the code itself Meta-information is essential for component composition