Metaprogrammierung und Metalinguistische Abstraktion in … · 2008. 12. 16. · Einleitung Ruby...

Post on 13-Oct-2020

5 views 0 download

Transcript of Metaprogrammierung und Metalinguistische Abstraktion in … · 2008. 12. 16. · Einleitung Ruby...

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Metaprogrammierung und MetalinguistischeAbstraktion in modernenProgrammiersprachen

Fabian Bieker

23.10.2008 - Version 0.9.6

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

• Wenn ihr Verständnisfragen habt, bitte sofort melden

• Diskusion später

• Folien sind im SE Wiki verlinked

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Abstract

• Metaprogammierung == Code der Code schreibt /generiert

• Metalinguistische Abstraktion == Abstraktion auf(linguistischem) Sprachniveau

• Metaprogramierung ist ein Werkzeug für MetalinguistischeAbstraktion

• In Ruby weil sehr lesbar (oder will jemand Lisp lernen?)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Abstract

• Metaprogammierung == Code der Code schreibt /generiert

• Metalinguistische Abstraktion == Abstraktion auf(linguistischem) Sprachniveau

• Metaprogramierung ist ein Werkzeug für MetalinguistischeAbstraktion

• In Ruby weil sehr lesbar (oder will jemand Lisp lernen?)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Gliederung

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Inhalt

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

• Warum Abstraktion?

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

• “Tip 53: Abstractions Live Longer than Details”

• “Tip 38: Put Abstraction in Code, Details in Metadata”

• Quelle: The Pragrammtic Programmer (PragProg)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Abstraktion in Programmiersprachen 1

• Abstraktion durch Assembler (statt Maschinencode /Lochkarten)

• Abstraktion durch Funktionen / Prozeduren• Abstraktion durch Module

• Abstraktion durch APIs

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Abstraktion in Programmiersprachen 2

• Abstraktion durch OOP• Abstraktion durch Komponenten Frameworks

• Abstraktion durch Sprache (Metalinguistische Abstraktion)• Bsp.: “Das Ding mit einer Lampe, einem Stromanschluß,

einem Lüfter usw., das diese Präsentation projiziert” →“Beamer”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Abstraktion in Programmiersprachen 2

• Abstraktion durch OOP• Abstraktion durch Komponenten Frameworks

• Abstraktion durch Sprache (Metalinguistische Abstraktion)• Bsp.: “Das Ding mit einer Lampe, einem Stromanschluß,

einem Lüfter usw., das diese Präsentation projiziert” →“Beamer”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Anmerkung

• Compiler == Interpreter == Runtime Env.• Unterscheidung zwischen Compile- und Runtime ist

unwichtig in Ruby (und erst recht in Lisp)• nur für Laufzeitbetrachtungen relevant

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Anmerkung

• Compiler == Interpreter == Runtime Env.• Unterscheidung zwischen Compile- und Runtime ist

unwichtig in Ruby (und erst recht in Lisp)• nur für Laufzeitbetrachtungen relevant

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Inhalt

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby Grundlagen 1

• Syntax: Wie Java oder Perl . . .

• Konzepte: Smalltalk, Lisp und Perl

• Design-Ziel: “Programmers Happiness”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby Grundlagen 2

• Dynamic Typing / Duck Typing

• Alles ist ein Object ! (ausser Blocks . . . )

• Open Base Classes

• Mix-Ins

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby und Java

• JRuby (Beide Hauptentwickler von Sun eingestellt)

• Groovy

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Kommentare, Arrays und Strings

# This is a one-line comment

[1,2,3,5,8] # Array

"Ruby rocks!" # String

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Methoden

def foo(a,b,c)a + b + c # no return needed

end

> foo(1,2,3)=> 6

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Klassen

class A

attr_reader :name # def method name()def initialize(n) # constructor hook

@name = nend

def foo { "foo" } # def method foo()

# def class method bar()def self.bar { "bar" }

end

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Klassen - Methodenaufrufe

# A.new("MyName").foo()> A.new("MyName").foo # no ’()’ needed=> "foo"

> A.bar=> "bar"

> A.new("MyName").name=> "MyName"

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Einige Metaprogammierungs-Funktionen in Ruby

• attr_reader, attr_accessor, . . .

• instance_eval, class_eval, . . .

• send

• define_method

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Inhalt

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Metaprogrammierung?

• Code der Code schreibt

• Ursprung: Lisp makros - 1958 am MIT entwickelt• In Ruby Metaprog. u.a. über Interpreter Hooks

• z.B. method_missing, Class.inherited

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Metaprogrammierung?

• Code der Code schreibt

• Ursprung: Lisp makros - 1958 am MIT entwickelt• In Ruby Metaprog. u.a. über Interpreter Hooks

• z.B. method_missing, Class.inherited

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

map_send - 1

• Haskell like map (+1) xs (u.a.)

• Abstraktion durch neue Sprachkonstrukte

• Ja . . . nur mittel sinnvoll, aber einfaches Bsp.

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

map_send - 1

• Haskell like map (+1) xs (u.a.)

• Abstraktion durch neue Sprachkonstrukte

• Ja . . . nur mittel sinnvoll, aber einfaches Bsp.

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

map_send - 1

• Haskell like map (+1) xs (u.a.)

• Abstraktion durch neue Sprachkonstrukte

• Ja . . . nur mittel sinnvoll, aber einfaches Bsp.

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

map_send - 2

# reopen the Enumerable modulemodule Enumerable

# define a public method# usage: [1,2,3].map_send1 :+, 1def map_send1(method, * args) # ruby varargs

map { |x| x.send method, * args }end

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

map_send - 3

# define a public method (using define_method)# usage: [1,2,3].map_send2 :+, 1define_method(’map_send2’) do | method, * args |

map { |x| x.send method, * args}end

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

map_send - 4

# define a bunch of methods - called map_send,# each_send ...# usage: [1,2,3].map_send :+, 1%w[select map each collect inject].each do |method|

self.send(:define_method,"#{method}_send") do |m, * args|

send(method) { |x| x.send(m, * args) }end

end

end # module Enumerable

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Bsp. map_send - 5

> [1,2,3].map_send :+, 1=> [2,3,4]

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Optional: Java synchronized in Ruby

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Warum? Wieso? Weshalb?

• Kritik: “Das spart doch nur Zeichen!”

• Nein - Abstraktion durch Einführung neuerSprachkonstrukte

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Warum? Wieso? Weshalb?

• Kritik: “Das spart doch nur Zeichen!”

• Nein - Abstraktion durch Einführung neuerSprachkonstrukte

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Inhalt

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

SICP

• Quelle: Structure and Interpretation of Computer Programs- 2nd Edition (SICP)

• s.http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-25.html

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

SICP über Metalingustische Abstraktion

“Metalinguistic abstraction – establishing new languages –plays an important role in all branches of engineering design. Itis particularly important to computer programming, because inprogramming not only can we formulate new languages but wecan also implement these languages by constructingevaluators.An evaluator (or interpreter) for a programming language is aprocedure that, when applied to an expression of the language,performs the actions required to evaluate that expression.”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

SICP über Metalingustische Abstraktion

“It is no exaggeration to regard this as the most fundamentalidea in programming:The evaluator, which determines the meaning ofexpressions in a programming language, is just anotherprogram.In fact, we can regard almost any program as the evaluatorfor some language.”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

(Sehr optimistisches) Ziel von MetalinguistischerAbstraktion

• eval(SPEC)

• deploy(eval(SPEC))

• (if (testAndVerify(eval(SPEC)), SPEC)(deploy(eval(SPEC)))) → everyone is happy

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

(Sehr optimistisches) Ziel von MetalinguistischerAbstraktion

• eval(SPEC)

• deploy(eval(SPEC))

• (if (testAndVerify(eval(SPEC)), SPEC)(deploy(eval(SPEC)))) → everyone is happy

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

(Sehr optimistisches) Ziel von MetalinguistischerAbstraktion

• eval(SPEC)

• deploy(eval(SPEC))

• (if (testAndVerify(eval(SPEC)), SPEC)(deploy(eval(SPEC)))) → everyone is happy

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ziel von Metalinguistischer Abstraktion

• So was geht nicht?

• Was ist mit eval(RFC) → Protokol En-/Decoder ?

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ziel von Metalinguistischer Abstraktion

• So was geht nicht?

• Was ist mit eval(RFC) → Protokol En-/Decoder ?

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Sprache vs. Framework

• Framework:new CoffeeShop(”Foo”, ...).getCoffee(new Customer(”Fabian”, ...));

• Spache:Fabian getsCoffee @ CoffeeShop named Foo

• Vorteil: Abstraktionsgewinn• z.B.: “Fabian ist ein Customer Objekt” ist nicht relevant

• Nachteil: Evtl. höherer Aufwand

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Sprache vs. Framework

• Framework:new CoffeeShop(”Foo”, ...).getCoffee(new Customer(”Fabian”, ...));

• Spache:Fabian getsCoffee @ CoffeeShop named Foo

• Vorteil: Abstraktionsgewinn• z.B.: “Fabian ist ein Customer Objekt” ist nicht relevant

• Nachteil: Evtl. höherer Aufwand

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Was ist eine DSL?

• DSL == Sprache um domänen-spezifische Probleme zulösen

• z.B.: Make oder ANT

• Zwei Arten von DSLs: eingebettet und nicht-eingebettet

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Was ist eine DSL?

• DSL == Sprache um domänen-spezifische Probleme zulösen

• z.B.: Make oder ANT

• Zwei Arten von DSLs: eingebettet und nicht-eingebettet

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

nicht eingebettete DSLs

• wird mit externem Parser-Generatoren (Yacc, . . . ) erzeugt

• potentziell flexibler

• mehr Aufwand bei der Impl.

• Problem: “Wir brauchen ein if - Ach, und einefor -Schleife wäre auch gut . . . ”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

nicht eingebettete DSLs

• wird mit externem Parser-Generatoren (Yacc, . . . ) erzeugt

• potentziell flexibler

• mehr Aufwand bei der Impl.

• Problem: “Wir brauchen ein if - Ach, und einefor -Schleife wäre auch gut . . . ”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

eingebettete DSLs (eDSL)

• wenig Aufwand bei der Impl.

• Rückgriff auf bereits vorhandene Sprachfeatures

• Abhängigkeit von Sprachsyntax der umgebenden Sprache

• verbreited in Ruby, Lisp, Smalltalk, Python, . . .

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Beispiele für DSLs

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Rake - Ruby Makefile DSL - 1

# [...] by Sandor Szuecs - FU Berlin# c compiler flags CFLAGSCFLAGS = "-Wall -pedantic [...]"

if (‘uname‘).strip.chomp=="Darwin"ARCH = "-DMACOSX"# linking OpenGLGL_LIBS = "-framework OpenGL [...]"

# [...]else

puts("unknown system, maybe windows?")exit!

end

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Rake - Ruby Makefile DSL - 2

## rake taskstask :default => []

task ’build:mathgl’ => [’math_opengl.o’]

desc "build all"task "build:all" => [’build:mathgl’, [...]]

# [...]

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby OCL eDSL - Quelle

• Quelle: “Building Domain-Specific Languages forModel-Driven Development” (MDADSL)

http://www.computer.org/portal/web/csdl/doi/10.1109/MS.2007.135

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

OCL

context UML::Classinv: self.attributes->forAll(attr |

attr.name == ’id’ impliesattr.type.oclKindOf(UML::Integer))

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby DSL

context UML::Class doinv(’integer-id’) do

self.attributes.forAll { |attr|(attr.name == ’id’).implies(

attr.type.oclKindOf(UML::Integer))}

endend

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Inhalt

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

One-Time Code Generators

• z.B.: Eclipse - Source - Generate Getters / Setters• suboptimal

• redundanter Code• Metainformationen gehen verloren

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

One-Time Code Generators

• z.B.: Eclipse - Source - Generate Getters / Setters• suboptimal

• redundanter Code• Metainformationen gehen verloren

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Java Features für Metaprogammierung

• Anotations

• Java Instrumentierung und Reflection

• Beans (?)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Optional: Metaprogammierung / MetalinguistischeAbstraktion und MDA / MDSD

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Werkzeuge für Java

• ANTLR (ANother Tool for Language Recognition)• OAW (OpenArchitectureWare)

• MDA / MDSD Toolkit• Sexy!

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Werkzeuge für Java - Google Code

• http://code.google.com/p/java-macros/

• “An enhancement to the JDK7 compiler (the GPL’d version)that adds support for compile-time macros.”

• Sprich: Lisp-artige Makros für Java

• Sehr sexy!

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Optional: Metaprogammierung und MetalinguistischeAbstraktion in Unternehmen

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Inhalt

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Probleme und Lösungen

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Komplexität

“While I admire the cleverness and skill that hides behind C++libraries (...), the fact remains that writing advanced templatecode is devilishly hard, (...)” (RatMeta)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Komplexität - Lösungsansätze

• Verwendung bekannter Metaprog. Paradigmen / Patterns

• Lisp hat etliche zu bieten!

• In der Funktionalen Programmierung gibt es viele Patterns(z.B. aus ALP1: higher-order functions und currying)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Abgeschlossenheit

• Metaprog. soll den selben Code erzeugen können, denman von Hand schreiben kann (und umgekehrt)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Debuging

• Komplexe DSLs erzeugen teilweise schwer Debug-barenCode

• Komplexe Frameworks erzeugen teilweise schwerDebug-baren Code

• Hat jemand eine Idee für eine Lösung?

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Debuging

• Komplexe DSLs erzeugen teilweise schwer Debug-barenCode

• Komplexe Frameworks erzeugen teilweise schwerDebug-baren Code

• Hat jemand eine Idee für eine Lösung?

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Code als ASCII-String etc.

• Compiler kann Code nicht prüfen

• besser: Programmiersprachen-Konstrukte für Metaprog.verwenden

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Sprachkonsistens

• Programmiersprachen-Design ist schwierig

• Das Erlernen neuer Sprachen ist aufwändig

• Der Support für Sprachen ist resourcen-intenssiv

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Sprachkonsistens - Lösungsansatz

• Also sollten die verwendeten Sprachen möglichstkonsistent sein und sich optimal in vorhandeneSprachumgebung einpassen

• Eingebettete DSLs helfen hierbei

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

• Danke für Ihre Aufmerksamkeit!

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Diskussion

• Anregungen? Kritik?

• Was haltet Ihr von Meta* ? Benutzt Ihr das? Wofür?• Benutzt Ihr MDA / MDSD? Wofür?

• Kennt Ihr gute Papers, Bücher oder Links?

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Quellenverzeichnis

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Quellenverzeichnis - Bücher

(SICP) “Structure and Interpretation of Computer Programs” -2nd Edition,MIT Electrical Engineering and Computer Science,ISBN-13: 978-0262011532(PragProc) “The Pragrammtic Programmer”,Addison-Wesley Longman, Amsterdam (24. November 1999),ISBN-13: 978-0201616224

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Quellenverzeichnis - Bücher

(MatzRuby) “The Ruby Programming Language”,David Flanagan and Yukihiro ’Matz’ Matsumoto,O’Reilly Media, Inc. (January 25, 2008),ISBN-13: 978-0596516178

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Quellenverzeichnis - IEEE Papers

(RatMeta) “Rational Metaprogramming”, Diomidis Spinellis,2008, IEEE Software,http://www.computer.org/portal/web/csdl/doi/10.1109/MS.2008.15(MDADSL) “Building Domain-Specific Languages forModel-Driven Development”, Jesús Sánchez Cuadrado andJesús García Molina, University of Murcia, September/October2007 IEEE SOFTWARE,http://www.computer.org/portal/web/csdl/doi/10.1109/MS.2007.135

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Quellenverzeichnis - IBM Papers

(IBM-eMetaProg) “The Art of Enterprise Metaprogramming”,MDA, MDSD und Metaprogramminghttp://www.ibm.com/developerworks/linux/library/l-metaprog3/(IBM-MetaProg) “The Art of Metaprogramming”http://www.ibm.com/developerworks/linux/library/l-metaprog1.html

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

• Slides:https://page.mi.fu-berlin.de/bieker/metaprog.pdf

• Source: page.mi.fu-berlin.de -slash- bieker/metaprog(punkd) tex

• License: CC - by-nc-sahttp://creativecommons.org/licenses/by-nc-sa/3.0/de /

• Contact: Fabian Bieker - bieker -at- zedat . . .

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Inhalt

EinleitungAbstraktion

Ruby

Metaprog.Beispiele

Metal. Abs.Domain Specific Language (DSL)

Java

Probleme

OptionalUnternehmenMDA / MDSD und Meta*

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby - Alles ist ein Objekt

> 5.class=> Fixnum

> 5.methods=> ["%", "inspect", "<<", ... ]

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby - Klassen, Methoden etc. sind Objekte

> 5.class=> Fixnum

> Fixnum.class=> Class

> Fixnum.methods=> ["inspect", "private_class_method",

"const_missing", ... ]

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby - Open Base Classes 1

class Object # reopen class Objectdef foo

"foo"end

end

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Ruby - Open Base Classes 2

> Object.new.foo=> "foo"> Object.foo=> "foo"> 5.foo=> "foo"> Fixnum.foo=> "foo"

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Bsp. synchronized - 1

require ’thread’ # ruby 1.8 (Mutex in this lib)

def synchronized(o)o.mutex.synchronize

end

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Bsp. synchronized - 2

class Object # opening class Objectdef mutex

return @__mutex if @__mutex# get mutex on class obj.synchronized(self.class) {

@__mutex = Mutex.new # ret-val is @__mutex}

endend

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Bsp. synchronized - 3

# Prevent endless recursion# Class is an object after all :)Class.instance_eval { @__mutex = Mutex.new }

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

synchronized - 4

• Quelle: “The Ruby Programming Language” S. 282 - Matz

• (Und? Habt Ihr die (kleine) Racecondition gesehen?)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Historie 1

• ???? Selbstmodifizerender (Assembler) Code• 1958 Lisp (Designed von John McCarthy, Impl. von Steve

Russel - MIT)• Basiert auf lambda-calulus• CLOS (OO-System implemeniert durch Makros)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Historie 2

• 1969 Smalltalk (Alan Key, Xerox Park)• Metaprogrammierung für OOP

• seit 1980 MIT Lisp Courses baseierend auf dem SICP• 1982 Begriff DSL (definiert von James Martin in seinem

Buch “Applications Development Without Programmers”)• Martin, James. Application Development Without

Programmers. Prentice-Hall, 1981. ISBN 0-13-038943-9.

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Historie 3

• 1990 ANTLR 1.00B released

• 1995 Ruby (Yukihiro ’Matz’ Matsumoto)

• Juli 2004 - Ruby on Rails released

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Metaprogammierung und MetalinguistischeAbstraktion in Unternehmen

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

IBM

• “The Art of Enterprise Metaprogramming”

• MDA und Metaprogramming

• http://www-128.ibm.com/developerworks/linux/library/l-metaprog3/

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Sun

• JavaOne 2007 - Zwei Vorträge die sich u.a. mit DSLsbeschäftigen

• s. http://java.sun.com/javaone/sf/2007/javauniversity.jsp

• JRuby, Scalar ...

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Paul Graham - Viaweb

• “In 1995 he developed with Robert Morris the firstweb-based application, Viaweb, which was acquired byYahoo in 1998.”

• http://www.paulgraham.com/bio.html

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Netzwert -> Apertio -> Nokia Siemens Networks(NSN)

• Mobile IP AAA Server

• 3G / 3.5G / 4G / IMS Infastruktur

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

NSN - AAA Server

• Hochverfügbar und Geo-Redundant

• Massiv Parallel

• Multi-Tier

• DSL u.a. zum Konfigurieren der Buisness Logic

• Disclaimer: Ich habe zwei Jahre da gearbeited

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

BeagleSoft

• Viel Meta* / MDA / MDSD Know-How

• Kundenprojekte u.a. im BPM Bereich

• Disclaimer: Ich arbeite für BeagleSoft

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Naughty Dog (one Lisp game project failed!)

• “Crash Bandicoot” teilweise in Lisp

• GOAL (Game Object Assembly Lisp) für Naughty Dog’sJax and Daxter

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Naughty Dog - Naughty Dog’s Jax and Daxter

“GOAL rocks! (...)GOAL sucks! While it’s true that GOAL gave us manyadvantages, GOAL caused us a lot of grief. A singleprogrammer (who could easily be one of the top ten Lispprogrammers in the world) wrote GOAL. ...”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Naughty Dog - Naughty Dog’s Jax and Daxter

“... While he called his Lisp techniques and programmingpractices ’revolutionary’, others referred to them as ’codeencryption’, since only he could understand them. Because ofthis, all of the support, bug fixes, feature enhancements, andoptimizations had to come from one person, creating quite abottleneck. Also, it took over a year to develop the compiler,during which time the other programmers had to make do withmissing features, odd quirks, and numerous bugs.”

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Naughty Dog - Naughty Dog’s Jax and Daxter

• Quelle: http://ynniv.com/blog/2005/12/lisp-in-games-naughty-dogs-jax-and.html

http://www.gamasutra.com/features/20020710/white_02.htm

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Metaprog. / Metaling. Abstraktion und MDA / MDSD - 1

• Ähnliche Problemstellung - Ähnliche Lösungsansätze• Graphische Rep. (UML etc.) und Textuelle Rep. (DSL)

• Graphische Rep. muss formale Sprache sein, weilcompilierbar

• Gleiche Komplexität (s. nächste Folie)

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

Metaprog. / Metaling. Abstraktion und MDA / MDSD - 2

• Komplexität bzw. Abstraktionsniveau abhängig von derSprache

• Graphische Rep. kann man aber besser mit zusätzlichenInformationen anreichern

• und diese z.B. je nach View ein- und ausblenden

Einleitung Ruby Metaprog. Metal. Abs. Java Probleme Optional

DSLs für Domänenexperten

• Domänenexperten werden DSLs lieben• Aber Vorsicht mit den “Edge Cases”

• Dafür braucht man “richtige” Programmier

• Quelle: (IMB-eMetaProg)