Upload
michal-malohlava
View
1.508
Download
0
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
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.
2
● Connector generation● Existing connector generator overview● Stratego/XT overview● Proposed solution● Conclusion
Outline
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?
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
5
● Connector– Connector units
● Connector elements
Connector architecture
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
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}
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!
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
10
● How does it work?
Stratego/XT - architecture
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
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
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
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
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
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
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
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
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}
} }}
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 { } }
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
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
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
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
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
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
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)
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
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