Meta Lena Buffoni lena.buff

advertisement
Metamodelling&
Metaprogramming
LenaBuffoni
lena.buffoni@liu.se
Whatisamodel?
•  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
Abuildingmodel
Renters
view
Builders
view
Designers
view
Tax
collectors
view
Electricians
view
Realtors
view
Eachviewconcentratesonmodeling
specificaspects
Frommodeltometa-model-vehicles
Howcanwegeneralizethe
concepts?
CarModel(inModelica)
Truckmodel
Generalizingthroughinheritance
•  Super-class vehicle
–  X wheels
–  a steering wheel
–  a clutch
Whatifwethrowanairplaneinthemix?
•  Super-class vehicle
–  X wheels
–  a steering wheel
–  a clutch
GeneralizaOonthroughmetamodelling
Aerodynamic
Model
1
Engine
1..n
Vehicle
Actuator
1..n
Sensor
1..n
Reasonintermsof
categoriesonahigher
abstracOonlevel
Anotherexample:ModelingMethodologies
Classicalmodel
Canwededuce
ageneral
modeling
methodology?
Vmodel
Agilemodel
4layersofmeta-modeling
Conformsto
Meta-metamodel(languagespecificaOon)
Conformsto
Meta-model(modelinglanguage)
Nottheonlywayto
viewthings!
Conformsto
Model(carclass)
Representedby
System(carinstance)
JeanBézivin,OntheunificaOonpowerof
models,2005.
UMLandMeta-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:
assignment
assignement
A:=B+3;
Led-side
expression
Variable
A
Right-Side
Expression
Binary
Operator
+
When?
–  At compile time :
•  preprocessing Macros in C
•  optimization annotations
•  templates in C++
Compilerscanbeseenasmetaprograms
When?
–  At runtime :
•  interpreters
•  Dynamic component configuration and
reconfiguration
•  Java reflection API
Towhatextent?
•  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();
JavareflecOonAPI
•  Dynamically select which method to call
publicclassConfigurablePrint{
voidprintOnLinux(){}
voidprintOnMac(){}
voidprintOnWindows(){}
}
JavareflecOonAPI
publicsta?cvoidmain(String[]args){
try{
//loadtheAppTestatrunOme
Classcls=Class.forName("ConfigurablePrint");
Objectobj=cls.newInstance();
Class[]noparams={};
//calltheprintItmethod
StringmethodName="";
if(getSystem()==SystemType.WINDOWS)methodName="printOnWindows";
elseif(getSystem()==SystemType.MAC)methodName="printOnMAC";
elsemethodName="printOnLinux";
Methodmethod=cls.getDeclaredMethod(methodName,noparams);
method.invoke(obj,(Object[])null);
}catch(Excep?onex){
ex.printStackTrace();
}}
Meta-programmingonestepfurther
A hack to language extension?
–  add a backwards_for to a language
–  change how inheritance works
Meta-meta-programming?
–  compilers of compilers
ComponentMarkuplanguages
•  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++
AnnotaOonsinJava
Predefined
annotaOons
@Override
public String toString() {
return "This is String Representation of
current object.";
}
Javadoc
annotaOons
/**
* The HelloWorld program.
*
* @author Lena
* @version 1.0
*/
public class HelloWorld {
…
}
}
AnnotaOonsincomposiOon
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE) //on class level
public @interface PreferencePolicy {
public enum Policy {
WEAK, MEDIUM, STRONG
}
Policy priority() default
Policy.MEDIUM;
}
ComponentcomposiOon
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
Download