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 19:49  |
Eclipse User |
|
|
|
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 09:59] by 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 06:20   |
Eclipse User |
|
|
|
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 07:05] by 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 08:14   |
Eclipse User |
|
|
|
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 08:44] by Moderator
|
|
|
Difference between Editing and Saving in resource [message #1075125 is a reply to message #1074277] |
Sun, 28 July 2013 16:43  |
Eclipse User |
|
|
|
Say I have a validator
@Check
public void checkSomething(MyModelRoot root){
Resource r = root.eResource();
System.out.println(r);
}
I use it to play with the basic example as shown in "XText Tutorial" http://www.eclipse.org/Xtext/documentation.html#DomainModelWalkThrough.
Here is the behavior if I try to edit and then save the following files:
1- Inside datatypes.dmodel
OnEdit = Only datatypes.dmodel
OnSave = First commons.dmodel and then blogs.dmodel
2- Inside commons.dmodel
OnEdit = Only commons.dmodel
OnSave = Only blogs.dmodel
3- Inside blogs.dmodel
OnEdit = Only blogs.dmodel
OnSave = Nothing
Note that the behavior of OnSave is only happening if it goes immediately after an OnEdit.
|
|
|
Powered by
FUDForum. Page generated in 0.04291 seconds