Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to restrict validator to the active working file?
How to restrict validator to the active working file? [message #1865118] Fri, 26 April 2024 16:13 Go to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
Hi,
I have 4 different model files (of the same metamodel) and my validation checks are applied on all on them while I am working on one of them. All the model files need to be in the same project folder.

Is it possible to restrict validator to one active working file only?

Thanks,

Ehsan
Re: How to restrict validator to the active working file? [message #1865121 is a reply to message #1865118] Fri, 26 April 2024 16:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
Hi what you you mean by validator
In the editor? In the build ?
Both
Can you provide stackstrace?

Why is it a problem in the first place


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: How to restrict validator to the active working file? [message #1865124 is a reply to message #1865121] Fri, 26 April 2024 16:38 Go to previous messageGo to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
I mean after the build. I have four different model files my DSL. For example, first.mydsl, second.mydsl, third.mydsl, and four.mydsl. All in the same project in runtime Eclipse application.

I have implemented the following Check in Validator class:

@Check(CheckType.FAST)
public void checkInitialValueMatchesType(Variable var){
if(var.getBasic_type().equals("boolean")) {
error("boolean type", DevsPackage.Literals.VARIABLE__INITIAL_VALUE);
}

}

This check runs on all files (first, second, third, fourth) while I am working in second.mydsl. I want this check to run only for second.mydsl.


Thanks.
Re: How to restrict validator to the active working file? [message #1865127 is a reply to message #1865124] Fri, 26 April 2024 16:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
But how would the build know what the active file is
Maybe you want to to temporarily disable project-> build automatically


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: How to restrict validator to the active working file? [message #1865130 is a reply to message #1865127] Fri, 26 April 2024 17:34 Go to previous messageGo to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
Thank you for your replies Christian.

Temporarily disabling project-> build automatically does not work.

How does it apply the NamesAreUniqueValidator?

here is the part of my grammar for states

BehaviorStatesSection:
	'states' behavior_states+=BehaviorState+
	
;	

BehaviorState:
	name=ID ':' (isInitial='initial')? state_sigma = Sigma  ';'
	
;



It allows me to declare states with same names in different files but but not in the same file. For example, file second.mydsl cannot have two states with name "Start" but file first.mydsl and second.mydsl both can have one "Start" state.

Now, I am applying to the following Check to ensure that there is only one initial state in one model file(e.g, first.mydsl).


@Check(CheckType.NORMAL)
	public void checkOnlyOneInitialState(BehaviorState behaviorState) {
		int initialStateCount = 0;
		BehaviorStatesSection bss = (BehaviorStatesSection) behaviorState.eContainer();
		for (BehaviorState bs : bss.getBehavior_states()) {
			if(bs.getIsInitial().equals("initial"))
				initialStateCount = initialStateCount +1;
		}
		if (initialStateCount > 1)
			error(behaviorState.getName(), DevsPackage.Literals.BEHAVIOR_STATE__NAME);
			
		}



This Check must be applied to only the active file I am working on like NamesAreUniqueValidator.

Regards,
Ehsan
Re: How to restrict validator to the active working file? [message #1865133 is a reply to message #1865130] Fri, 26 April 2024 18:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
but a validation always will be local.
do you want to restrict scoping (org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getGlobalScope) or affected calc so that files cant see each other (org.eclipse.xtext.resource.IResourceDescription.Manager.isAffectedMethod),


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: How to restrict validator to the active working file? [message #1865136 is a reply to message #1865133] Fri, 26 April 2024 18:28 Go to previous messageGo to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
>> but a validation always will be local.

What do you mean by local? It will check on the currently working file only? This is not the case for me. It seems validation is check global (my understanding).

>> do you want to restrict scoping ....
I am using ImportUriValidator
composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
Can this make validation global?

Thanks
Re: How to restrict validator to the active working file? [message #1865139 is a reply to message #1865136] Fri, 26 April 2024 19:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
Why is it a problem all 4 files get validated by the build
They are all in the project

If you use imports then the files see each other and should see each other
So a change to one file can make another invalid


I am still struggling to understand your problems in the first case


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: How to restrict validator to the active working file? [message #1865148 is a reply to message #1865139] Fri, 26 April 2024 21:26 Go to previous messageGo to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
>> If you use imports then the files see each other and should see each other
So a change to one file can make another invalid

Yes. I agree the files should see each other.

>> I am still struggling to understand your problems in the first case.

Here is the problem statement. Each file has an atomic component and each atomic component has a states but only one state can be marked as initial state in each file/atomic component.

Below is states section of the file first.mydsl.

states
	Start:  initial 0.0;
	Chk_Mode: pd;
	Set_Vars: 0.0;



Here is the states section of the second.mydsl:


states
	Start:  initial 0.0;
	Chk_Status: pd;
	Print_Vars: 0.0;


Each file can have its own initial state but currently the validator checks initial states across both files and only allows initial state in one of the them.

I hope this describes the problem.

Thanks very much.

Ehsan
Re: How to restrict validator to the active working file? [message #1865154 is a reply to message #1865148] Sat, 27 April 2024 04:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
Then you have implemented something wrong in your validator

maybe you are looking for LocalUniqueNameContext
if you use the NameAreUniqueValidator


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com

[Updated on: Sat, 27 April 2024 05:55]

Report message to a moderator

Re: How to restrict validator to the active working file? [message #1865163 is a reply to message #1865154] Sat, 27 April 2024 13:42 Go to previous messageGo to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
This is my validator
public class DevsValidator extends AbstractDevsValidator {
 private int initialStateCount = 0; //for number of initial states

	@Check(CheckType.FAST)
	public void checkStateStartsWithCapital(BehaviorState behaviorState) {
		if (!Character.isUpperCase(behaviorState.getName().charAt(0))) {
			error("Name should start with a capital letter", DevsPackage.Literals.BEHAVIOR_STATE__NAME);
		}
	}


	@Check(CheckType.NORMAL)
	public void checkOnlyOneInitialState(BehaviorState behaviorState) {
		
		BehaviorStatesSection bss = (BehaviorStatesSection) behaviorState.eContainer();
		for (BehaviorState bs : bss.getBehavior_states()) {
			if(bs.getIsInitial().equals("initial"))
				initialStateCount = initialStateCount +1;
		}
		if (initialStateCount > 1)
			error(behaviorState.getName(), DevsPackage.Literals.BEHAVIOR_STATE__NAME);
			
		}
	
	
} // end DevsValidator



Being new to the validation stuff, I have no idea how to implement LocalUniqueNameContext in my validator.
Any Hint?

Re: How to restrict validator to the active working file? [message #1865178 is a reply to message #1865163] Sat, 27 April 2024 16:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
now i am even more confused. with this validator the validator will just validate one file.
=> can you provide a reproducing problem and an exact description what the problem is.

of course

private int initialStateCount = 0; //for number of initial state

is a terrible bad idea.

put the validator to the model or the parent of all states and count within the model
and not global

validator classes are a singleton


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com

[Updated on: Sat, 27 April 2024 16:31]

Report message to a moderator

Re: How to restrict validator to the active working file? [message #1865187 is a reply to message #1865178] Sat, 27 April 2024 19:41 Go to previous messageGo to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
Yes, the validator runs for one file now, as required.
	@Check(CheckType.NORMAL)
	public void checkOnlyOneInitialState(BehaviorState behaviorState) {
		int initialStateCount = 0;
		
		BehaviorStatesSection bss = (BehaviorStatesSection) behaviorState.eContainer();
		
		for (BehaviorState bs : bss.getBehavior_states()) {
			if(bs.getIsInitial().equals("initial"))
				initialStateCount = initialStateCount +1;
		}
		if (initialStateCount > 1)
			error(behaviorState.getName(), DevsPackage.Literals.BEHAVIOR_STATE__NAME);
			
	}




But regardless to the size of the bss.getBehavior_states(), it only runs twice. As a result it only checks isInitial for the first and the second state. So, for the following model, it only produces error if the state Test1 and Test2 have 'initial'.

states
	Test1: initial 0.0;
	Test2:  initial 0.0;
	Test3: 1.0;
	Test4:   2.0;
	Test5: initial 3.0;



But does not produce error if the second state Test2 in case does not have initial. For example, no error is produces for the following model



states
	Test1: initial 0.0;
	Test2: 0.0;
	Test3:  initial  1.0;
	Test4:   2.0;
	Test5: initial 3.0;



Global declaration private int initialStateCount = 0; //for number of initial state
was add to tackle this issue as the initialSateCount was reset after 2 iterations but is of no use and still the loop only runs for the first two states.

Regards,
Ehsan

[Updated on: Sat, 27 April 2024 19:43]

Report message to a moderator

Re: How to restrict validator to the active working file? [message #1865190 is a reply to message #1865187] Sat, 27 April 2024 21:44 Go to previous messageGo to next message
Ehsan Ahmad is currently offline Ehsan AhmadFriend
Messages: 40
Registered: April 2018
Member
Dear Christian,
There was an issue in my grammar that was throwing exception. Setting isInitial as boolean solved the issue as shown below:

BehaviorState:
	name=ID ':' (isInitial?='initial')? state_sigma = Sigma  ';' 
;


The following validator is working fine now.

	@Check(CheckType.FAST)
	public void checkOnlyOneInitialState(BehaviorState behaviorState) {
		int initialStateCount = 0;
		if(behaviorState.isIsInitial()) {
		BehaviorStatesSection bss = (BehaviorStatesSection) behaviorState.eContainer();
		for (BehaviorState bs : bss.getBehavior_states()) {
			if(bs.isIsInitial())
				initialStateCount = initialStateCount + 1;
		}
		if (initialStateCount > 1)
			error("Duplicate initial state " + '\''+ behaviorState.getName()+'\'', DevsPackage.Literals.BEHAVIOR_STATE__NAME);		
	 }
	}
	


Thanks very much for the discussion.

Regards,
Ehsan
Re: How to restrict validator to the active working file? [message #1865205 is a reply to message #1865190] Sun, 28 April 2024 10:16 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
you could for algo/performance reasons also define the validation on BehaviorStatesSection
and call the error overload that accepts an eobject as param


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Previous Topic:Validation Test Fail after update to Xtext 2.34 and MWE 2.17
Next Topic:Context-specific scopeFor method dispatched without entering IScopeProvider.getScope
Goto Forum:
  


Current Time: Fri Nov 08 23:31:22 GMT 2024

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

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

Back to the top