From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Β
Using DSL for generation of software connectors
1. Using DSL for Automatic
Generation of Software Connectors
TomΓ‘Ε‘ BureΕ‘, Michal Malohlava, Petr HnΔtynka
DISTRIBUTED SYSTEMS RESEARCH GROUP
http://dsrg.mff.cuni.cz/
CHARLES UNIVERSITY IN PRAGUE
Faculty of Mathematics and Physics
2. Outline
β Components and Connectors
β Connector generation
β Proposed connector code generator
β Template system
β Code generation engine
β Conclusion
28.2.2008, ICCBSS 2008, Madrid 2
3. Components
β Component
β Independent entity implementing some functionality
β Provides/requires interfaces
β Typical development cycle
β Design
β Implementation
β Deployment
β Execution
β SOFA component system (http://sofa.ow2.org/)
28.2.2008, ICCBSS 2008, Madrid 3
4. Connectors? Why?
β Why should be a component interested in communication?
=> Connectors
β Displace communication matters
from components
β Can connect different component
technologies
β Connectors
β Design time view
β Model component interaction (communication style, non-functional
properties)
β Represent only bindings and their properties
β Implementation view
β Implements interaction with help of some middleware (RMI, JMS,...)
β Implements additional services
(logging, benchmarking, ...)
28.2.2008, ICCBSS 2008, Madrid 4
5. Connector generation
β Automatically during deployment time
β Application components are distributed, connected
β All information about application are known...
β Distribution of application components
β Component interfaces' signatures
β Connection requirements, NFP (security, logging,...)
β ... and stored in a high-level connector specification
β Generated by a deployment tool connector {
unit βclient_unitβ {
in according to dock βnodeAβ
provided port βcallβ
a defined deployment plan signature βIserviceβ
nfp βcommunication_styleβ
β Input for a connector generator βmethod_invocationβ
}
...
}
28.2.2008, ICCBSS 2008, Madrid 5
6. Connector architecture
β Connector
β Connector units
β Top-level connector elements
β Remote bindings (between remote ports)
β Connector elements
β Element ports
β Primitive/composite
β Only local bindings
28.2.2008, ICCBSS 2008, Madrid 6
7. Architecture of connector generator
β Architecture resolver
β Find an architecture of a connector in according to described
requirements (H-LCS)
β Source code generation
β Driven by generation script
β Step-by-step process
β Generates source code from:
β Low-level connector
configuration
β Simple templates
β Compile generated code
β Package binaries
28.2.2008, ICCBSS 2008, Madrid 7
8. Architecture of connector generator
β Architecture resolver
β Find architecture of connector in according to described
requirements (H-LCS)
β Source code generation
β Driven by generation script
β Step-by-step process
β Generates source code from:
β Low-level connector
configuration
β Simple templates
β Compile generated code
β Package binaries
28.2.2008, ICCBSS 2008, Madrid 8
9. Low level connector configuration
β Low-level connector configuration
β Input for source code generator
β Description of resolved connector architecture:
β Describes ports
β Name
β Type (provided, required, remote)
β Resolved port signature
β List of sub-elements
β Bindings between sub-elements
β Selects template for implementation
28.2.2008, ICCBSS 2008, Madrid 9
10. Simple template system
β Simple code templates which are processed by a Java class
β Class just substitutes parts enclosed in % by Java code
β Sufficient for primitive connector elements, but for composite
elements Java class generates all code:
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
}
28.2.2008, ICCBSS 2008, Madrid 10
11. Simple template system
β Simple code templates which are processed by a Java class
β Class just substitutes parts enclosed in % by Java code
β Sufficient for primitive connector elements, but for composite
elements Java class generates all code:
package %PACKAGE%;
imports org...runtime.*;
public class %CLASS% implements This variable is unfolded
ElementLocalServer , into 200LOC by a Java
ElementLocalClient , class with 1200LOC!
ElementRemoteServer ,
ElementRemoteClient {
protected Element[] subElements ;
protected UnitReferenceBundle[] boundedToRRef;
public %CLASS%( ) {
}
%INIT METHODS% // this part processed by special
Java class
}
28.2.2008, ICCBSS 2008, Madrid 11
12. Issues of the simple template system
β Connector implementation defined at several
places
β Java class does not allow syntax checking of
generated code
β Error prone
β Bad maintainability of generator classes
β Large blocks of System.out.println(β...β);
β Solution β new Domain Specific Language
(DSL)
28.2.2008, ICCBSS 2008, Madrid 12
13. Proposed template system
β Template system based on a new DSL
β DSL describing connector elements
β Should support writing connector implementations in
different programming languages
β Should allows easy development (syntax checking,
debugging,...)
β Extensible
β Generation framework
β Processing DSL
β Generating target source code
β Code per connector element
β Independent on a chosen target language
28.2.2008, ICCBSS 2008, Madrid 13
14. DSL β ElLang basics
β Designed new DSL
β Mixture of the meta-language ElLang (element language) and a
target language (Java)
β Used MetaBorg method
β Allows embedding language into another language
β Connecting selected nonterminals of both languages
β Meta-language ElLang (target language independent)
β Meta-variables
β ${a}, ${a[index]}
β Meta-queries (querying input XML)
β ${a.b.c}
β Meta-statements
β $set, $if, $include, $foreach, $rforeach
28.2.2008, ICCBSS 2008, Madrid 14
15. DSL β ElLang basics
β Designed new Domain Specific Language
Mixture of meta-language
β Recursive foreach ElLang and target language (Java)
β MetaBorg method developed by Stratego/XT group
$rforeach(PORT in ${ports.port(type=PROVIDED)} )$
if (quot;${PORT.name}quot;.equals(portName)) { into another language
β Allows embedding language
Object Connecting selected neterminals of both languages
β result = ((ElementLocalServer) subElements[${el[PORT....]}]);
β Defined via SDF
if (isTopLevel) {
β Meta-language ElLang
dcm.reregisterConnectorUnitReference(parentUnit, portName, result);
}
β Meta-variables
return result;
} else $recpoint$
β ${a}, ${a[index]}
$final$
throw new ElementLinkException(quot;Invalid port 'quot;+portName+quot;'.quot;);
β Meta-queries
$end$
β ${a.b.c}
β Meta-statements
β $set, $if, $include, $foreach, $rforeach
28.2.2008, ICCBSS 2008, Madrid 15
16. DSL β ElLang advanced features
β Special meta-statements
β Simple templates inheritance (extends)
β Extension points ($extPoint$)
β Allow define points in a 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 !
β ElLang has to provides information about methods of an
interface
β ${method.name}, ${method.variables}, ...
28.2.2008, ICCBSS 2008, Madrid 16
17. DSL β ElLang advanced features
β Special meta-statements
element console_log extends quot;primitive_default.ellangquot; {
β Simplemethod interfaceinheritance (extends)
implements
templates ${ports.port(name=in).signature} {
template {
${method.declareReturnValue}
β Extension points ($extPoint$)
System.out.println(quot;method > ${method.name} < calledquot;);
β Allow define points in template which can be extended in
a child template
$if (method.returnVar) $
${method.returnVar}
β Method templates
= this.target.${method.name}(${method.variables});
$else$
β Important for implementing component interfaces
this.target.${method.name}(${method.variables});
$end$
β ! component iface is not known when templates is designing !
β Language shouldreturn statemene if it is needed
//generates provides information about iface
${method.returnStm}
methods
}
}
β ${method.name}, ${method.variables}, ...
}
28.2.2008, ICCBSS 2008, Madrid 17
18. DSL β element template structure
β ElLang-J = an instance of ElLang
β For generating Java code
β Connector element template:
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 { /* ... */ }
}
28.2.2008, ICCBSS 2008, Madrid 18
19. Generator architecture
β Java part
β Prepares low-level connector configuration (L-LCC)
β Description of connector element internals
β Calls source code generator implemented in
Stratego
β Stratego part
β Implemented in Stratego language
β In fact compiled C-source code
β Generates source code
β From template written in ElLang-J
β From L-LCC passed from Java part of generator
28.2.2008, ICCBSS 2008, Madrid 19
20. Stratego/XT
β Developed at Delft University of Technology,
Netherlands
β Tool set
β Grammar tools
β SDF β Syntax Definition Formalism
β Pretty printers
β Grammar definitions (Java, C/C++, XML, ...)
β Program transformation language Stratego
β Input represented as Abstract Syntax Tree (AST)
β Term representation of AST called ATerm
β Based on rewriting of AST via strategies
28.2.2008, ICCBSS 2008, Madrid 20
22. Generator - Java part
β Implementing action interface
β Therefore it can be used as a new action in connector
element build script
β Rewrites low-level connector configuration into XML and
passed it to Stratego part
β Bridge between Java and Stratego:
β JNI bridge
β Shell bridge (execute connector generator)
28.2.2008, ICCBSS 2008, Madrid 22
23. Generator β Stratego part
β Pipe line of several small transformation components
β Input XML preprocessor
β Template parser
β Template evaluation
β Target code generator
β Query component
β All of them transform and
produce AST
28.2.2008, ICCBSS 2008, Madrid 23
24. Stratego part β query component
β 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
28.2.2008, ICCBSS 2008, Madrid 24
25. Stratego part β evaluation component
β Pipe-line of evaluation modules
β Processing extends statements
β Processing imports statements
β 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
β Resulting AST is
transformed into
target code
28.2.2008, ICCBSS 2008, Madrid 25
26. Evaluation
β Integrated in our SOFA component model
β It works :-)
β But it is suitable for any component model
β And even for non-component based systems
28.2.2008, ICCBSS 2008, Madrid 26
27. Evaluation (pros and cons)
β Advantages
β Simple template system allowing easy writing of connector
implementations
βGeneric solution
β Allows syntax checking
β Extensible template language
βAllows defining new clones of ElLang in according to
specified requirements (e.g. ElLang-C#)
β Solutions is not invasive - proposed source code generator
can be used just by modifying a build script controlling a
connector generation
β Disadvantages
β Stratego/XT is C-based, rest of generator is in Java
28.2.2008, ICCBSS 2008, Madrid 27
28. Future work
β Byte code manipulation
β To avoid need of javac (~SDK) during deployment
process
β Pre-compiled templates into binary form (templated
bytecode)
β Fast template evaluation
β Simplifying connector implementation
β Merging generated Java classes
β Implementing ElLang for other target languages
(e.g. ElLang-C#)
β Simplifying method templates
28.2.2008, ICCBSS 2008, Madrid 28
29. Comments & Questions
Thank you for your attention!
28.2.2008, ICCBSS 2008, Madrid 29