| <?xml version='1.0' encoding='utf-8' ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body><h1 id="AcceleoQueryLanguageAQL">Acceleo Query Language (AQL)</h1><h2 id="Introduction">Introduction</h2><p>The Acceleo Query Language, AQL, is a new query interpreter created to replace the Acceleo MTL engine when used as a simple interpreter. AQL is small, simple, fast, extensible and it brings a richer validation than the MTL interpreter.</p><p>For those looking for a simple and fast interpreters for your EMF models, AQL can provide you with a lot of features, including:</p><ul><li>Support for static and dynamic Ecore models, no query compilation phase required.</li><li>The least possible overhead at evaluation time. During this phase, the evaluation goes forward and will not even try to validate or compile your expressions. Errors are tracked and captured along the way.</li><li>Strong validation: types are checked at validation time and the metamodels used are analyzed to do some basic type inference and avoid false positive errors.</li><li>Union types: In some context, a variable in a given query can have N potential types with N being often greater than one. AQL can embrace this fact without having to fall back to EObject as soon as there is more than one type.</li><li>A simple and straightforward implementation easily extensible with Java classes providing extension methods.</li><li>A very narrow dependency surface: AQL uses the very central parts of EMF, Guava and Antlr so that we could easily deploy AQL outside of Eclipse in a server or a standalone scenario.</li></ul><p>The AQL interpreter is used in Sirius with the prefix «aql:».</p><h2 id="LanguageReference">Language Reference</h2><p>These sections are listing all the services of the standard library of AQL.</p><ul><li><a href="./reference/aql_service_anyservices.html">Services available for all types</a></li><li><a href="./reference/aql_service_booleanservices.html">Services available for Booleans</a></li><li><a href="./reference/aql_service_collectionservices.html">Services available for Collections</a></li><li><a href="./reference/aql_service_comparableservices.html">Services available for Comparables</a></li><li><a href="./reference/aql_service_eobjectservices.html">Services available for EObjects</a></li><li><a href="./reference/aql_service_resourceservices.html">Services available for Resources and URIs</a></li><li><a href="./reference/aql_service_stringservices.html">Services available for Strings</a></li></ul><h2 id="UsingAQLprogrammatically">Using AQL programmatically</h2><p>This section provide information and code snippet. It will help you to integrate AQL in your own tool.</p><p>Simple overview of AQL:</p><p><img border="0" src="../images/aql/AQL_overview.png"/> </p><h3 id="Typevalidation">Type validation</h3><p>For each node of the AST we create a set of possible types as follow:</p><ul><li>for a VarRef we ask the environment for its possible types</li><li>for a FeatureAccess we look up the type of the feature in the registered metamodels</li><li>for a Call we look up the the service in the service registry according to the possible types of its parameters (receiver is the first parameter). At this point there is a conversion from EMF to Java. The return type of the service is given by the IService.getType() method. At this point the there is a conversion form Java to EMF, one Java type can correspond to more than one EMF EClassifier. If no service can be found we try to find a corresponding EOperation if the receiver is an EObject.</li></ul><p>A special type NothingType is used to mark a problem on a given node of the AST. Those NothingTypes are then used to create validation messages. If an AST node has only NothingTypes validation messages will be set as errors for this node, otherwise they are set as warnings.</p><h3 id="Completion">Completion</h3><p>The completion rely on the AST production and the type validation.<br/>The identifier fragments preceding (prefix) and following (remaining) the cursor position are removed from the expression to parse. The prefix and remaining are used later to filter the proposals. Many filters can be implemented: filter only on prefix, filter on prefix and remaining, same strategies with support for camel case, ...</p><p>Completion on the AST:</p><ul><li>if there is no error node in the AST the completion provide any symbols that can follow an expression («+», «-», ...).</li><li>if there is an ErrorExpression node in the AST the completion provides anything that can prefix an expression («not», «-», variable name, type name, ...).</li><li>if there is an ErrorFeatureAccesOrCall node in the AST the completion provides feature and service names corresponding to the receiver possible types. It also possible to add symbols that follow an expression if the prefix and remaining are already a valid feature or service name for the receiver possible types.</li><li>if there is an ErrorCollectionCall node in the AST the completion provides collection service names. It also possible to add symbols that follow an expression if the prefix and remaining are already a valid service name.</li><li>if there is an ErrorTypeLiteral node in the AST the completion provides EClassifier, EEnumLiteral names according to the state of the type description.</li></ul><h3 id="Creatingandsettingtheenvironment">Creating and setting the environment</h3><p>To get a fresh environment you can use one of the following snippet:</p><pre><code>IQueryEnvironment queryEnvironment = Query.newEnvironmentWithDefaultServices(null); |
| </code></pre><p>To get an environment with predefined services.</p><p>or</p><pre><code>IQueryEnvironment queryEnvironment = Query.newEnvironment(null); |
| </code></pre><p>To get an environment with no predefined services. It can be useful to create your own language primitives.</p><p>Note that you can also provide a CrossReferenceProvider to define the scope of cross references in your environment. See CrossReferencerToAQL for more details.</p><p>You can register new services Class as follow:</p><pre><code>ServiceRegistrationResult registrationResult = queryEnvironment.registerServicePackage(MyServices.class); |
| </code></pre><p>The registration result contains information about services overrides.</p><p>You can also register your EPackages. Only registered EPackages are used to validate and evaluate AQL expression.</p><pre><code>queryEnvironment.registerEPackage(MyEPackage.eINSTANCE); |
| </code></pre><p>In some cases you might also want to create custom mappings between an EClass and its Class. A basic case is the use of EMap:</p><pre><code>queryEnvironment.registerCustomClassMapping(EcorePackage.eINSTANCE.getEStringToStringMapEntry(), EStringToStringMapEntryImpl.class); |
| </code></pre><p>By default the EClass is mapped to Map.Entry which is not an EObject. This prevents using services on EObject.</p><h3 id="BuildinganAQLexpression">Building an AQL expression</h3><p>The first step is building your expression from a String to an AST:</p><pre><code>QueryBuilderEngine builder = new QueryBuilderEngine(queryEnvironment); |
| AstResult astResult = builder.build("self.name"); |
| </code></pre><h3 id="EvaluatinganAQLexpression">Evaluating an AQL expression</h3><p>To evaluate an AQL expression you can use the QueryEvaluationEngine</p><pre><code>QueryEvaluationEngine engine = new QueryEvaluationEngine(queryEnvironment); |
| Map<String, Object> variables = Maps.newHashMap(); |
| variables.put("self", EcorePackage.eINSTANCE); |
| EvaluationResult evaluationResult = engine.eval(astResult, variables); |
| </code></pre><p>Here we only use one variable for demonstration purpose.</p><h3 id="ValidatinganAQLexpressionoptional">Validating an AQL expression (optional)</h3><p>This step is optional for evaluation. You can evaluate an AQL expression without validating it in the first place.</p><pre><code>Map<String, Set<IType>> variableTypes = new LinkedHashMap<String, Set<IType>>(); |
| Set<IType> selfTypes = new LinkedHashSet<IType>(); |
| selfTypes.add(new EClassifierType(queryEnvironment, EcorePackage.eINSTANCE.getEPackage())); |
| variableTypes.put("self", selfTypes); |
| AstValidator validator = new AstValidator(queryEnvironment, variableTypes); |
| IValidationResult validationResult = validator.validate(astResult); |
| </code></pre><h3 id="CompletinganAQLexpression">Completing an AQL expression</h3><p>To do this use the QueryCompletionEngine, it will build the query and validate it for you. It will also compute needed prefix and suffix if any:</p><pre><code>Map<String, Set<IType>> variableTypes = new LinkedHashMap<String, Set<IType>>(); |
| Set<IType> selfTypes = new LinkedHashSet<IType>(); |
| selfTypes.add(new EClassifierType(queryEnvironment, EcorePackage.eINSTANCE.getEPackage())); |
| variableTypes.put("self", selfTypes); |
| QueryCompletionEngine engine = new QueryCompletionEngine(queryEnvironment); |
| ICompletionResult completionResult = engine.getCompletion("self.", 5, variableTypes); |
| List<ICompletionProposal> proposals = completionResult.getProposals(new BasicFilter(completionResult)); |
| </code></pre><p>Here 5 is the offset where the completion should be computed in the given expression.</p><h2 id="MigratingfromMTLqueries">Migrating from MTL queries</h2><p>As languages, AQL and MTL are very close yet there are some notable differences:</p><h3 id="Implicitvariablereferences">Implicit variable references</h3><p>There is no implicit variable reference. With this change, you can easily find out if you are using a feature of an object or a string representation of said object. As a result, instead of using «[something/]», you must use «self.something» if you want to access the feature named «something» of the current object or «something» if you want to retrieve the object named something.</p><p>In a lambda expression, you must now define the name of the variable used for the iteration in order to easily identify which variable is used by an expression. In Acceleo MTL, you can write «Sequence{self}->collect(eAllContents(uml::Property))» and Acceleo will use the implicit iterator as a source of the operation eAllContents.</p><p>The problem comes when using a lambda like «Sequence{self}->collect(something)», we can’t know if «something» is a feature of «self» or if it is another variable.</p><p>Using AQL, you will now have to write either «collect(m | m.eAllContents(uml::Property))» or «collect(m: uml::Model | eAllContents(uml::Property))».</p><h3 id="Collectandflatten">Collect and flatten</h3><p>When a call or a feature acces is done on a collection the result is flattened for the first level. For instance a service returning a collection called on a collection will return a collection of elements and not a collection of collection of elements.</p><h3 id="TypeliteralschildrenEPackages">Type literals & children EPackages</h3><p>Type literals can’t be in the form someEPackage::someSubEPackage::SomeEClass but instead someSubEPackage::SomeEClass should be directly used. Note that the name of the EPackage is now mandatory.</p><h3 id="TypeliteralschildrenEPackages2">Type literals & children EPackages</h3><p>Enumeration literal should be prefixed with the name of the containing EPacakge for instance «myPacakge::myEnum::value».</p><h3 id="Collections">Collections</h3><p>You can only have Sequences or OrderedSets as collections and as such the order of their elements is always deterministic. In Acceleo MTL, you had access to Sets, which are now OrderedSets and Bags, which are now Sequences. Those four kinds of collections were motivated by the fact that Sequence and OrderedSet were ordered contrary to Sets and Bags. On another side, OrderedSets and Sets did not accept any duplicate contrary to Bags and Sequences.</p><p>By careful reviewing the use of those collections in various Acceleo generators and Sirius Designers we have quickly found out that the lack of determinism in the order of the collections Sets and Bags was a major issue for our users. As a result, only two collections remain, the Sequence which can contain any kind of element and the OrderedSet which has a similar behavior except that it does not accept duplicates.</p><p>Previously in Acceleo MTL, you could transform a literal into a collection by using the operator «->» on the literal directly. In Acceleo MTL, the collection created was a Bag which is not available anymore. It is recommended to use the extension notation like «Sequence{self}» or «OrderedSet{self}». By default in AQL the created collection is an OrderedSet.</p><h3 id="Renamedoperations">Renamed operations</h3><p>Some operations have been renamed. As such «addAll» and «removeAll» have been renamed «add» and «sub» because those two names are used by AQL in order to provide access to the operator «+» and «-». As a result we can now write in AQL «firstSequence + secondSequence» or «firstSet - secondSet».</p><h3 id="Typing">Typing</h3><p>AQL is way smarter than MTL regarding to the types of your expressions. As a result, you can combine expressions using multiple types quite easily. For example, this is a valid AQL expression «self.eContents(uml::Class).add(self.eContents(ecore::EClass)).name». In Acceleo MTL, we could not use this behavior because Acceleo MTL had to fall back to the concept EObject which does not have a feature «name» while AQL knows that the collection contains objects that are either «uml::Class» or «ecore::EClass» and both of those types have a feature named «name».</p><h3 id="nullhandling">null handling</h3><p>AQL handles null (OclVoid) differently from ocl, so «oclIsUndefined» should be mostly useless for AQL expressions. For example, «null.oclIsKindOf(ecore::EClass)» would have returned true for MTL/OCL, forcing users to use «not self.oclIsUndefined() and self.oclIsKindOf(ecore::EClass)» instead. This is no longer true in AQL, where «null» doesn't conform to any type, so «null.oclIsKindOf(ecore::EClass)» will return false. Note that it's still possible to cast null into any given classifier. «null.oclAsType(ecore::EClass)» will not fail at runtime.</p><h2 id="MigratingfromAcceleo2queries">Migrating from Acceleo2 queries</h2><h3 id="EClassifierreferences">EClassifier references</h3><p>All operations referencing a type are now using a type literal with the name of the EPackage and the name of the type instead of a string with the name of the type. As a result, «eObject.eAllContents('EClass')» would be translated using "eObject.eAllContents(‹ecore::EClass›). This allows AQL to now in which EPackage to look for the type and as such, it improves the quality of the validation.</p><h3 id="Typesandcast">Types and cast</h3><p>In order to test the type of an EObject, a common pattern in Acceleo 2 was to treat the EObject as a collection and filter said collection on the type desired to see if the size of the collection changed. In AQL, you have access to the operations oclIsTypeOf and oclIsKindOf. You can thus test the type of an EObject with the expression «eObject.oclIsKindOf(ecore::EStructuralFeature)» or «eObject.oclIsTypeOf(ecore::EAttribute)». You can use the operation oclIsKindOf to test if an object has the type of the given parameter or one of its subtype. On the other hand, you can use the operation oclIsTypeOf to test if an object has exactly the type of the given parameter.</p><p>Casting in AQL is useless, since AQL is very understandable when it comes to types, it will always tries its best to evaluate your expression.</p><p>Since AQL is very close to Acceleo MTL, you can find some additional documentation using the Acceleo equivalence documentation in the <a href="http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.acceleo.doc%2Fdoc%2Fhtml%2Facceleo_equivalence.html">Acceleo documentation</a>.</p></body></html> |