KET: Kermeta Emitter Template
Authors: Members of the Triskell team
Summary
The idea is similar to JET, Velocity or JSP. You put some Kermeta code directly in your template. It is a template engine for Kermeta, contrary to Sintaks, this one is only to generate code but it is "easier" to use than Sintaks.
Your template file should be *.ket. Next with a right click you can transform it as a kmt file. You can call generate method on the generated class. It returns a String that contains the generated code.
Characteristics of a KET template (*.ket).
Attribute | Value |
package | The package name of the Kermeta implementation class that the template is translated to. If this attribute is not present, the Kermeta implementation class is created in the default package |
class | The class name of the Kermeta implementation class that the
template is translated to. If not present, the Kermeta implementation
class is called CLASS |
require | A space-separated list of ecore/kmt/km to require in the Kermeta template class |
using | A space-separated list of package to alias in the Kermeta template class |
parameters | A comma-separated list of parameters to put in the Kermeta template operation |
isaspectclass | Indicate that the class must be an aspect. (accepted values : "true" or "false") Useful if you want to add the generator operation to an existing class. If not present, same as false) (available with ket 1.2.1 and above) |
operation | The operation name of the Kermeta implementation operation that the template is translated to. If not present, the Kermeta implementation class is called "generate" (available with ket 1.2.1 and above) |
ismethod | Indicate that the operation is a method. (accepted values : "true" or "false") Useful if you want to add the generator operation to an existing class that already inherits the same operation. If not present, same as false (available with ket 1.2.1 and above) |
Ket process.
- Include Directive
The include directive is used to substitute text and/or code at template
translation-time. The <%@ include file="urlSpec" %> directive inserts
the text of the specified resource into the ket template file. The
included file may have ket scripting elements which will also be processed.
This directive has one single attribute, /file/. The value of this
attribute is the URI of the location of the file to include. This URI
can be either an absolute path or a relative path. Relative URIs are
always interpreted as relative to the folder of the template that
contains the include directive.
Example:
The following example requests the inclusion, at translation time, of a
copyright file.
<%@ include file="copyright.ket" %>
- KET Scripting Elements
KET has two scripting language elements: scriptlets and expressions. A
scriptlet is a statement fragment, and an expression is a complete
Kermeta expression.
Each scripting element has a "<%"-based syntax as follows:
<% this is a scriptlet %>
<%= this is an expression %>
White space is optional after "<%", and "<%=", and before "%>".
If you want to use the %> character sequence as literal characters in a
scriptlet, rather than to end the scriptlet, you can escape them by
typing %\>. Similarly, the <% character sequence can be escaped by using
<\%.
- Scriptlets
Scriptlets can contain any valid Kermeta code fragment.
Scriptlets are executed at template invocation time. Whether or not they
produce any output into the result String depends on the actual code in
the scriptlet. Scriptlets can have side effects, modifying the objects
visible in them.
When all scriptlet fragments in a given translation unit are combined in
the order they appear in the KET template, they should yield a valid
Kermeta statement or sequence of statements.
Syntax: <% scriptlet %>
- Expressions
A KET expression element is a Kermeta expression that is evaluated and
the result is appended to the String object returned by the generate
method. Expressions are evaluated at template invocation time.
If the result of the expression cannot be appended to a String then a
translation time error occurs. The content of a KET expression must be a
complete Kermeta expression.
Side-effects in expressions are supported. They take effect when the KET
expression is evaluated. KET expressions are evaluated left-to-right in
the KET template.
Syntax: <%= expression %>
An example
It is a transformation from Ecore to the XML input of ATOM3 develop at
McGill. The first line contains metadata (require, using, package,
class, parameters) Use spaces if you have several using or require
("using foo bar bar1") and comma for parameters.Next, you have two kinds
of marker. Between <% %>, Kermeta code will be interpreted. Between <%=
%>; the result of the code should be a string and it will be printed.
<%@ ket package="hello" require="http://www.eclipse.org/emf/2002/Ecore"
using="ecore" class="XMLDemoTemplate" parameters="p:EPackage" %>
<!Template -->
<?xml version="1.0"?>
<!DOCTYPE agl SYSTEM "http://agl.dtd">
<!-- GraphElements -->
<agl>
<graph id="<%=p.name%>" type="EClass">
<% p.eClassifiers.each{e| %>
<graph id="<%=e.name%>" type="EClass"/> <% }
p.eClassifiers.select{e| var e1 : EClass e1 ?= e e!=void}.each{e2 |
var e3 : EClass
e3 ?= e2 e3.eReferences.each{e4| %>
<edge from="<%=e4.name%>" to="<%=e4.eType.name%>" fromOrd="0"
toOrd="0"/> <% }} %>
</graph>
</agl>
Documentation
Some slides about Ket [PDF]
Reference Manual is available hereKET Manual