Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree
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 Go to next message
J A is currently offline J AFriend
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 Go to previous messageGo to next message
J A is currently offline J AFriend
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 #1073801 is a reply to message #1073799] Thu, 25 July 2013 14:10 Go to previous messageGo to next message
J A is currently offline J AFriend
Messages: 31
Registered: July 2013
Member
I also know that there's an implementation of checking that all ID must be unique

 * @author Sebastian Zarnekow - Initial contribution and API
 */
public class org.eclipse.xtext.validation.NamesAreUniqueValidator extends AbstractDeclarativeValidator {}


How ever I also hit another problem while using that class: I cannot easily get all ID of imported resource.

Say I have the following 3 files:
//A.dmodel
import B.*;
import C.*;


//B.dmodel
import C.*;


I can only reach all IDs of C if I am editing B, the same thing I cannot do while editing A. What is the reason ?
Re: Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1073811 is a reply to message #1073801] Thu, 25 July 2013 14:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
Hi,

hi imports is just: make name available.
what do you exactly want todo?
Re: Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1073845 is a reply to message #1073811] Thu, 25 July 2013 15:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
Btw you can configure when a check is started through

@Check(CheckType.XXX)

with XXX
FAST: xms
NORMAL: on save
EXPENSIVE: (explicit ask for vaidation)
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 Go to previous messageGo to next message
J A is currently offline J AFriend
Messages: 31
Registered: July 2013
Member
Hi Christian,

Christian Dietrich wrote on Thu, 25 July 2013 11:59
Btw 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 #1074140 is a reply to message #1074110] Fri, 26 July 2013 07:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
Hmm

i fear i dont know exact numbers

(1) time is 150ms as far as i remember
(2) expensive checks are (i think so) is trigged by right click validate
(please note there may be more that one validate vbuttons so you have to choose the right one
(3) the default is FAST
(4) of course syntactic validation is done as you type
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 Go to previous messageGo to next message
J A is currently offline J AFriend
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 #1074238 is a reply to message #1074228] Fri, 26 July 2013 10:50 Go to previous messageGo to next message
J A is currently offline J AFriend
Messages: 31
Registered: July 2013
Member
Thanks Christian,
Basing on your answer and my own experiment, I made the note above for others to find.

Note that NORMAL is not for on-save, but for on-build. So the information in http://wiki.eclipse.org/Xtext/Documentation/Xtext_New_and_Noteworthy#Different_validation_hooks is not correct

[Updated on: Fri, 26 July 2013 11:04]

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 #1074244 is a reply to message #1074238] Fri, 26 July 2013 11:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
Hi,

i which "save" i mean "save with build automatically on" which you should have when you work with Xtext
Re: Xtext 2.4.2: (1) When does validation start and (2) how to get resource of the imported tree [message #1074259 is a reply to message #1074244] Fri, 26 July 2013 11:40 Go to previous messageGo to next message
J A is currently offline J AFriend
Messages: 31
Registered: July 2013
Member
Christian,
My Model is huge (13K lines, with thousands of elements at level 2 from the root, and up to 10-20K of all elements), thus "Build automatically" on save is not good.

Question: Do you know if the 500ms for FAST is configurable ? I search inside the source code of xtext, but cannot find the 500

[Updated on: Fri, 26 July 2013 11:41]

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 #1074270 is a reply to message #1074259] Fri, 26 July 2013 12:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 6499
Registered: July 2009
Senior Member
have a look org.eclipse.xtext.ui.editor.reconciler.XtextReconciler.XtextReconciler(XtextDocumentReconcileStrategy)
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 Go to previous messageGo to next message
J A is currently offline J AFriend
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

Difference between Editing and Saving in resource [message #1075125 is a reply to message #1074277] Sun, 28 July 2013 20:43 Go to previous message
J A is currently offline J AFriend
Messages: 31
Registered: July 2013
Member
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.
Previous Topic:Using multi line comments as elements of a DSL
Next Topic:Xtext architecture
Goto Forum:
  


Current Time: Thu Nov 27 19:52:21 GMT 2014

Powered by FUDForum. Page generated in 0.02005 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software