Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1073499] |
Wed, 24 July 2013 23:49 |
J A Messages: 31 Registered: July 2013 |
Member |
|
|
Dear all,
While trying to validate that an element in my AST tree must not have the same name as any other element of some specific types inside the tree, I hit two difficult problems:
1) What happens before any validation methods is executed and after a) The Editor notices that there's a change in the model source, (b) The user hit the Ctrl + S
Could you please show me which method of which class handles the onChange() or onSave() of the editor ?
2) How to get to the root of the imported AST tree ?
If I use ECoreUtil2.getContainerOfType(@Nullable EObject ele, @NonNull Class<T> type) I could get to the root of the tree that contains an element ele, but how can I get to the root of the second tree that is imported into this tree ?
Let's take a simple grammar as described in the "DomainModelWalkThrough" of our series "Xtext - Language Development made easy"
Grammar:
grammar org.example.domainmodel.Domainmodel with
org.eclipse.xtext.common.Terminals
generate domainmodel "The full URL goes here"
Domainmodel:
(elements += AbstractElement)*
;
PackageDeclaration:
'package' name = QualifiedName '{'
(elements += AbstractElement)*
'}'
;
AbstractElement:
PackageDeclaration | Type | Import
;
QualifiedName:
ID ('.' ID)*
;
Import:
'import' importedNamespace = QualifiedNameWithWildcard
;
QualifiedNameWithWildcard:
QualifiedName '.*'?
;
Type:
DataType | Entity
;
DataType:
'datatype' name=ID
;
Entity:
'entity' name = ID
('extends' superType = [Entity | QualifiedName])?
'{'
(features += Feature)*
'}'
;
Feature:
(many ?= 'many')? name = ID ':' type = [Type | QualifiedName]
;
// datatypes.dmodel
datatype String
// commons.dmodel
package my.company.common {
entity HasAuthor {
author: String
}
}
// blogs.dmodel
package my.company.blog {
import my.company.common.*
entity Blog {
title: String
many posts: Post
}
entity Post extends my.company.common.HasAuthor {
title: String
content: String
many comments: Comment
}
entity Comment extends HasAuthor {
content: String
}
}
[Updated on: Thu, 25 July 2013 13:59] Report message to a moderator
|
|
|
Re: Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1073799 is a reply to message #1073499] |
Thu, 25 July 2013 14:02 |
J A Messages: 31 Registered: July 2013 |
Member |
|
|
While the user is editing the file blogs.dmodel, I want to grab any entity inside this file using
@Check
public void checkThisNameMustNotExistElseWhere(Entity thisEntity)
{
// Can reach to the root of thisEntity, and search for all entity names inside this root's tree
// But how can I grab also all elements' names inside the imported file commons.dmodel ?
}
I can reach to the root of thisEntity, and search for all entity names inside this root's tree. But how can I grab also all elements' names inside the imported file commons.dmodel
[Updated on: Thu, 25 July 2013 14:02] Report message to a moderator
|
|
|
|
|
|
Re: Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1074110 is a reply to message #1073845] |
Fri, 26 July 2013 06:16 |
J A Messages: 31 Registered: July 2013 |
Member |
|
|
Hi Christian,
Christian Dietrich wrote on Thu, 25 July 2013 11:59Btw you can configure when a check is started through
@Check(CheckType.XXX)
with XXX
FAST: xms
NORMAL: on save
EXPENSIVE: (explicit ask for vaidation)
- What is "xms" in the case of FAST ?
- For EXPENSIVE: How to "explicitly ask for validation" ?
- In the case of NORMAL, is it the default check if no CheckType is specified ?
Furthermore, above all else I notice that there is already some validation when I hit the enter. What CheckType is it ? I know it's different from when I press Ctrl + S.
[Updated on: Fri, 26 July 2013 06:42] Report message to a moderator
|
|
|
|
Re: Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1074228 is a reply to message #1074140] |
Fri, 26 July 2013 10:20 |
J A Messages: 31 Registered: July 2013 |
Member |
|
|
Different validation hooks (XText 2.4.2)
In UI there are now (since which version) three points where validation is triggered. Each points passes different user data into the validation, so that validators can decide what constraints should be evaluated. Those three different modes are defined in org.eclipse.xtext.validator.CheckType
// XText source code
public enum CheckType {
FAST,
NORMAL,
EXPENSIVE;
}
// Client code
@Check(CheckType.XXX) // XXX = FAST / NORMAL / EXPENSIVE
public void checkYourDSL(){
}
- FAST: is executed after a delay of 500ms after ANY editing action (type, enter, delete)
- NORMAL: is executed after a build (Project -> Build project)
- EXPENSIVE: is executed by right clicking ANYWHERE in the editor window and chooseing "Validate"
Note that:
* If you don't specify any CheckType, then FAST is used (see org.eclipse.xtext.ui.MarkerType.toCheckType())
* If you trigger a stronger check, then all its weaker checks are executed. We have EXPENSIVE > NORMAL > FAST. If you want to override this behavior, use the implementation (or override it) of org.eclipse.xtext.validation.CheckMode
[Updated on: Fri, 26 July 2013 11:05] Report message to a moderator
|
|
|
|
|
|
|
Re: Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1074277 is a reply to message #1074270] |
Fri, 26 July 2013 12:14 |
J A Messages: 31 Registered: July 2013 |
Member |
|
|
Thank you Christian,
I have also found, while playing with the class
public class org.eclipse.xtext.validation.NamesAreUniqueValidator extends AbstractDeclarativeValidator {}
that there IS a difference between editing and saving, even if in the runtime of the model I don't use "Build automatically".
// In a validator that extends org.eclipse.xtext.validation.AbstractDeclarativeValidator
@Check(CheckType.FAST)
public void doSimpleCheck(){
// Call org.eclipse.xtext.validation.NamesAreUniqueValidator.checkDescriptionForDuplicatedName(...)
}
// In another class:
// public class org.eclipse.xtext.validation.NamesAreUniqueValidator extends AbstractDeclarativeValidator
protected void checkDescriptionForDuplicatedName(IEObjectDescription description, Map<EClass, Map<QualifiedName, IEObjectDescription>> clusterTypeToName, ValidationMessageAcceptor acceptor)
{
EObject object = description.getEObjectOrProxy();
EClass eClass = object.eClass();
QualifiedName qualifiedName = description.getName();
System.out.println(qualifiedName);
}
The printing out gives me a different set of names when I save, than when I edit.
[Updated on: Fri, 26 July 2013 12:44] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.06121 seconds