29
Michal Malohlava Using Stratego/XT for Generation of Software Connectors DISTRIBUTED SYSTEMS RESEARCH GROUP http://dsrg.mff.cuni.cz/ CHARLES UNIVERSITY IN PRAGUE Faculty of Mathematics and Physics Supervisor: RNDr. Tomáš Bureš, Ph. D.

Using Stratego/XT for generation of software connectors

Embed Size (px)

DESCRIPTION

The talk presents a master thesis describing generation of software connectors code in accordance with a high-level connector description and a domain specific language.

Citation preview

Page 1: Using Stratego/XT for generation of software connectors

Michal Malohlava

Using Stratego/XT for Generation of Software

Connectors

DISTRIBUTED SYSTEMS RESEARCH GROUPhttp://dsrg.mff.cuni.cz/

CHARLES UNIVERSITY IN PRAGUEFaculty of Mathematics and Physics

Supervisor: RNDr. Tomáš Bureš, Ph. D.

Page 2: Using Stratego/XT for generation of software connectors

2

● Connector generation● Existing connector generator overview● Stratego/XT overview● Proposed solution● Conclusion

Outline

Page 3: Using Stratego/XT for generation of software connectors

3

● Why should be component interested in communication?

– It should just implement business logic

● => Connectors

– Displace communication matters from components

– Design time view● Model component interaction (communication style, NFP)

– Run-time view● Implements interaction with help of some middleware● Additional services (e.g. Logging)

● Preparation of connectors

– During deployment time ● complete info about application and its distribution● Automatic generation (component interface, depl. docks,

requirements, code template)

Connectors? Generation? Why?

Page 4: Using Stratego/XT for generation of software connectors

4

● Define and implement simple method of defining connectors implementations– Template based system

– Source code generation

– Integrate new solution with existing solution● Non-invasively (preserve original functionality)

General goal

Page 5: Using Stratego/XT for generation of software connectors

5

● Connector– Connector units

● Connector elements

Connector architecture

Page 6: Using Stratego/XT for generation of software connectors

6

● Architecture resolver

– Find architecture of connector in according to described requirements (HLCS)

● Source code generation

– Driven by generation script

– Generates Java code

● Per conn. element● Simple templates

– Compile java code

– Package binaries

Existing connector generator

Page 7: Using Stratego/XT for generation of software connectors

7

Existing connector generator● Simple code templates which are processed by Java class

– Class just substitutes parts enclosed in % by Java code● Sufficient for primitive connector elements, but composite

element looks like this:package %PACKAGE%;imports org...runtime.*;public class %CLASS% implements ElementLocalServer , ElementLocalClient , ElementRemoteServer , ElementRemoteClient { protected Element[] subElements ; protected UnitReferenceBundle[] boundedToRRef; public %CLASS%( ) { } %INIT METHODS% // this part processed by special Java class}

Page 8: Using Stratego/XT for generation of software connectors

8

Existing connector generator● Simple code templates which are processed by Java class

– Class just substitutes parts enclosed in % by Java code● Sufficient for primitive connector elements, but composite

element looks like this:package %PACKAGE%;imports org...runtime.*;public class %CLASS% implements ElementLocalServer , ElementLocalClient , ElementRemoteServer , ElementRemoteClient { protected Element[] subElements ; protected UnitReferenceBundle[] boundedToRRef; public %CLASS%( ) { } %INIT METHODS% // this part processed by special Java class}

This variable is unfolded into into 200LOC by

Java class with 1200LOC!

Page 9: Using Stratego/XT for generation of software connectors

9

● Developed at Delft University of Technology, Netherlands (Eelco Visser, Martin Bravenboer)

● Tool set – Grammar tools

● SDF – Syntax Definition Formalism● Pretty printers● Grammar definitions (Java, C/C++, XML, ...)

– Program transformation language Stratego● Based on AST rewriting via strategies

Stratego/XT

Page 10: Using Stratego/XT for generation of software connectors

10

● How does it work?

Stratego/XT - architecture

Page 11: Using Stratego/XT for generation of software connectors

11

● Modular, one grammar defines:

– Lexical tokens

– Context-free rules● Generation of parser (scannerless generalized LR parser)

– Generates “a forest of Abstract Syntax Trees (AST)”● ambiguous parts are explicitly marked

– AST represented by ATerm

Stratego/XT - SDF

Page 12: Using Stratego/XT for generation of software connectors

12

module Expr−literals

exports

sorts Int

lexical syntax

”0” −> Int

[1−9][0−9]* −> Int

lexical restrictions

Int −/− [0−9]

Stratego/XT – SDF example

module Expr−expressions

imports Expr−literals

exports

sorts Exp

context−free syntax

I n t −> Exp { cons ( ” I n t ” )}

Exp ”+” Exp −> Exp { cons ( ”Add” ) , assoc}

Exp ”−” Exp −> Exp { cons ( ”Sub” ) , left }

Exp ” *” Exp −> Exp { cons ( ”Mul ” ) , assoc}

Exp ” / ” Exp −> Exp { cons ( ” Div ” ) , assoc}

Exp ” ˆ ” Exp −> Exp { cons ( ”Pow” ) , right }

” ( ” Exp ” ) ” −> Exp { bracket }

●SDF grammar for simple language describing numerical expressions

Page 13: Using Stratego/XT for generation of software connectors

13

● Based on strategies manipulating with AST

– Input/Output: AST represented by ATerms

● Rewriting strategies and rules

– Strategy ● Describes how is AST traversed and rewrited

– Rule

● simple rewrite strategy ( Rule: A -> B <=> ?A; !B)● Many predefined strategies and rules for AST traversing (id, fail,

bottomup, topdown, try,...), system access strategies (e.g. ls, chmod, open_file,...)

– Dynamic rules

● Allows creating rewrite rules on the fly in according to context● Program in Stratego language is translated into C and compiled

Stratego/XT – Stratego language

Page 14: Using Stratego/XT for generation of software connectors

14

module EExpr

imports liblib Expr−eval

strategies

io−EExpr = io−wrap ( expr−eval )

Stratego/XT – Stratego example

imports Exprstrategies expr-eval = innermost(EvalAdd <+ EvalMul <+ EvalSub <+ EvalDiv <+ EvalPow)rules EvalAdd: Add(Int(i), Int(j)) -> Int(<addS>(i,j)) EvalSub: Sub(Int(i), Int(j)) -> Int(<subtS>(i,j)) EvalMul: Mul(Int(i), Int(j)) -> Int(<mulS>(i,j)) EvalDiv: Div(Int(i), Int(j)) -> Int(<divS>(i,j)) EvalPow: Pow(Int(i), Int(j)) -> Int(<powS>(i,j))strategies powS = (string-to-int, string-to-int); pow; int-to-string pow = ?(A,B); <copy>(B, A); foldr(!1, mul) // à la Haskell

Page 15: Using Stratego/XT for generation of software connectors

15

● Rebuild source code generator – Design more sophisticated templates based on DSL

– Implement source code generator

– Test eligibility of Stratego/XT for this purposes

– Incorporate the developed connector element code generator into the existing solution

Goals revisited

Page 16: Using Stratego/XT for generation of software connectors

16

● Designed new Domain Specific Language

– Mixture of meta-language ElLang and target language (Java)

● MetaBorg method developed by Stratego/XT group– Allows embedding language into another language– Connecting selected nonterminals of both languages

● Defined via SDF● Meta-language ElLang

– Meta-variables

● ${a}, ${a[index]}

– Meta-queries● ${a.b.c}

– Meta-statements● $set, $if, $include, $foreach, $rforeach

Solution – DSL

Page 17: Using Stratego/XT for generation of software connectors

17

● Designed new Domain Specific Language

– Mixture of meta-language ElLang and target language (Java)

● MetaBorg method developed by Stratego/XT group– Allows embedding language into another language– Connecting selected neterminals of both languages

● Defined via SDF● Meta-language ElLang

– Meta-variables

● ${a}, ${a[index]}

– Meta-queries● ${a.b.c}

– Meta-statements● $set, $if, $include, $foreach, $rforeach

Solution – DSL

$rforeach(PORT in ${ports.port(type=PROVIDED)} )$ if ("${PORT.name}".equals(portName)) { Object result = ((ElementLocalServer) subElements[${el[PORT....]}]);

if (isTopLevel) { dcm.reregisterConnectorUnitReference(parentUnit, portName, result); } return result; } else $recpoint$ $final$ throw new ElementLinkException("Invalid port '"+portName+"'."); $end$

Recursive foreach – designed because of Java grammar constraints

Page 18: Using Stratego/XT for generation of software connectors

18

● Special meta-statements– Simple templates inheritance (extends)

– Extension points ($extPoint$)● Allow define points in template which can be extended in a

child template

– Method templates● Important for implementing component interfaces

– ! component iface is not known when template is designing !● Language should provides information about iface

methods– ${method.name}, ${method.variables}, ...

Solution - DSL

Page 19: Using Stratego/XT for generation of software connectors

19

● Special meta-statements– Simple templates inheritance (extends)

– Extension points ($extPoint$)● Allow define points in template which can be extended in a

child template

– Method templates● Important for implementing component interfaces

– ! component iface is not known when templates is designing !● Language should provides information about iface

methods– ${method.name}, ${method.variables}, ...

Solution - DSL

element console_log extends "primitive_default.ellang" {implements interface ${ports.port(name=in).signature} {

method template {${method.declareReturnValue}

System.out.println("method > ${method.name} < called");

$if (method.returnVar) $${method.returnVar} = this.target.${method.name}(${method.variables});

$else$this.target.${method.name}(${method.variables});

$end$

//generates return statemene if it is needed${method.returnStm}

} }}

Page 20: Using Stratego/XT for generation of software connectors

20

● ElLang-J = mixture of ElLang and Java

Solution – template structure

package ${package};import org . . . runtime .* ;

element console_log extends “primitive_default.ellang” { public ${classname} { / / constructor }

implements interface ElementLocalClient { public void bindElPort(String portName , Object target ) { /* ... */ } } implements interface ${ ports.port (name=line).signature} { method template { } }

Page 21: Using Stratego/XT for generation of software connectors

21

● Java part– Prepares low-level connector configuration

● Description of connector element internals

● Stratego part– Generates source code

● From template written in ElLang-J● From L-LCC passed from Java part of generator

Solution – generator architecture

Page 22: Using Stratego/XT for generation of software connectors

22

● Implementing action interface JimplGeneratorInterface

– rewrites Low-Level Connector configuration into XML and passed it to Stratego part

– Just defines new action for script controlling generation

● Bridge between Java and Stratego:

– JNI

– Shell (execute connector generator)

Solution - Java part

Page 23: Using Stratego/XT for generation of software connectors

23

● low-level connector configuration

● Describes ports– Name– Type (provided, required,

remote)– Resolved port signature

● List of subelements– Name– Implementing class

● Bindings between subelements

● Selects template for implementation

Solution – Java v. Stratego part

Page 24: Using Stratego/XT for generation of software connectors

24

● Pipe line of several small transformation components

– Input XML preprocessor

– Template parser

– Template evaluation

– Target code gen.

– Query component● All of them transform and

produce AST (in ATerms)

Solution – Stratego part

Page 25: Using Stratego/XT for generation of software connectors

25

● Provides access to input XML (contains L-LCC)– Simple queries à la XPath

● Traversing XML– ${ports.port.name}

● conditions– ${ports.port(name=call).signature}

● Returns signature of port called “call”● Count operator

– ${elements.element#count}● Returns number of sub-elements

Solution – Query module

Page 26: Using Stratego/XT for generation of software connectors

26

● Pipe-line of evaluation modules– Processing extends

– Processing imports

– Template adjustment● Normalization of statements with different notations

– e.g. If -> If-Else ( in ATerms: If(cond,body) -> If(cond, body, []))

– Queries evaluation

– Meta-statements evaluation

Solution – evaluation module

Page 27: Using Stratego/XT for generation of software connectors

27

● Advantages

– Simple template which has at least the same power as the previous solution

● Shown by implementing all connectors elements into new templates

– Extensible template language (e.g. ElLang-C#)

– New generator can be used just by modifying script controlling connector generation

● Disadvantages

– Stratego/XT is C-based, rest of generator is in Java● ? Java implementation of Stratego ?

– Annoying long-time compilation of longer Stratego programs● Should be fixed in new release of Stratego

Evaluation (pro-and-con)

Page 28: Using Stratego/XT for generation of software connectors

28

● Byte code manipulation– To avoid need of javac (~SDK) during deployment

process

– Templates are precompiled into binary form

– Presented and implemented in master thesis “Optimizing performance of software connectors code generator”, Pavel Petřek

● Simplifying connectors– Merging generated Java classes

● Implements ElLang-C#● Improve method templates

Future work

Page 29: Using Stratego/XT for generation of software connectors

29

● T. Bureš, M. Malohlava, P. Hnětynka “Using DSL for Automatic Generation of Software Connector” – Accepted at 7th IEEE International Conference on

Composition Based Software Systems (ICCBSS), Madrid, February 2008

Results