Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Check Type of Element
Check Type of Element [message #1020565] Mon, 18 March 2013 13:33 Go to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi all,

I want to check type of two object with custom validation.for example:
sync sync1 source =file9 target = D; ->if Source is a file ,target must be file .now my language source is a file ,target is a drive ->I want to check this case.

My grammar:
Filesystem returns Filesystem:
'Filesystem' name=ID'{'
(drives+=Drive (drives+=Drive)* )?
(syncs += Sync (syncs +=Sync)* )?

'}';
Sync returns Sync:
'sync' name=ID 'source' '=' source =[SyncElement|ID] 'target' '=' target =[SyncElement|ID] ';'
;
SyncElement :
Drive|File|Folder
;


Drive returns Drive:
{Drive}
'Drive' name=ID '{'
(contents+=Object ( contents+=Object)* )?
'}' ;
Object:
File|Folder
;
File returns File:
'File' name=ID (('{'
('ext' '=' ext = EXT )
&('content' '=' content = STRING )?
&('hidden' '=' Hidden = Boolean)?
&('readonly' '=' readonly = Boolean)?
&('system' '=' system = Boolean)?
&('archive' '=' archive = Boolean)?
&('location' '=' location=STRING )?
&('date' '=' date=STRING )?
'}')|';' ) | Shortcut
;

enum Boolean:
true |false
;
enum EXT:
PNG |TXT |JPG |JAVA | C | BAT |BMP |EXE |COM |DOC |PDF|DOCX |XLS|XLSX
;
Folder returns Folder:

'Folder' name=ID'{'
( contents+=Object(contents+=Object)* )?
'}';

Shortcut returns Shortcut:
'Shortcut' name=ID ('target' '=' target=[Object|ID]) ';'
;


I want to check this section:SyncElement may be file folder drive , but must be file sync file ,folder sync folder ,drive sync drive .How I can this validation ?Please help me ?
Sync returns Sync:
'sync' name=ID 'source' '=' source =[SyncElement|ID] 'target' '=' target =[SyncElement|ID] ';'

Correct Language(I wanted) :sync sync1 source =file9 target = file10; or sync sync1 source =Folder1 target = Folder2;

Best Regards
Re: Check Type of Element [message #1020779 is a reply to message #1020565] Mon, 18 March 2013 22:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Hi,

if you write a javavalidation you can do a simple instanceof check.
Re: Check Type of Element [message #1021068 is a reply to message #1020779] Tue, 19 March 2013 13:18 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian I solved my problem,Thanks for a lot
Best Regards
Re: Check Type of Element [message #1021333 is a reply to message #1020779] Tue, 19 March 2013 22:06 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian,
My question is scoping,I write custom validation for drive name (drive name starting with upper case and one character ).I written a validation with above grammar ,but my warning shows all line I want to my warning shows only Drive name.I do not solving this problem :
My validation code :
@Check
public void CheckDriveName(Drive d){
if(d.getName().length()!=1){
error(" 'Drive' name must be one character " ,null,FilesystemPackage.DRIVE__NAME,
NVALID_DRIVENAME, d.getName());
}else if (Character.isLowerCase(d.getName().charAt(0)) ){
warning(" Drive name should start with uppercase " ,null,FilesystemPackage.DRIVE__NAME,INVALID_DRIVENAME,d.getName());
}
}

I upload my language screenshot
Re: Check Type of Element [message #1021351 is a reply to message #1021333] Tue, 19 March 2013 22:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Hi still hard to understand your problem but you can pass the
estructuralfeature to be marked as param to the method. E.g.
MyDslPackage.Literals.MY_TYPE__MY_FEATURE

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Check Type of Element [message #1021353 is a reply to message #1021333] Tue, 19 March 2013 22:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Thus you may call warning(message, object, feature, index) if I have
it right in mind

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Check Type of Element [message #1021362 is a reply to message #1021353] Tue, 19 March 2013 23:32 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian ,

I write this line for scope :
FilesystemPackage.DRIVE__NAME.but warning shows all line ,like the screenshot.because I call file or folder in the drive scope
I do not solved this problem.
Re: Check Type of Element [message #1021372 is a reply to message #1021353] Wed, 20 March 2013 00:06 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian,
I use this format for error but marked error line
Filesystem filesystem1 {
Drive De{
File file1 ;
File file2;

error("message", d, FilesystemPackage.Literals.DRIVE__CONTENTS, INVALID_DRIVENAME);

DRIVE_NAME is not EstructureFeature so I used FilesystemPackage.DRIVE_NAME previously code Sad

Best Regards
Re: Check Type of Element [message #1021423 is a reply to message #1021372] Wed, 20 March 2013 03:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Hi,

since you use a derived metamodel (at least i think so) the featrue may be in a parent class (so have a look where the getName() is in the Java Classes

so in your case you may have to call org.eclipse.xtext.validation.AbstractDeclarativeValidator.error(String, EObject, EStructuralFeature, int)
like

error("message", drive, YourPackage.Literals. Sync_Element__NAME, -1) or something like that
Re: Check Type of Element [message #1021472 is a reply to message #1021423] Wed, 20 March 2013 07:10 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian,

My problem is solved with warning(" 'Drive' ddddddddddddddr ",d,FilesystemPackage.Literals.SYNC_ELEMENT__NAME, -1 , INVALID_DRIVENAME,d.getName());

thank you so much for the quick reply and explanation.

Best Regars

Re: Check Type of Element [message #1021533 is a reply to message #1021423] Wed, 20 March 2013 09:26 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi christian ,
I write validation for Sync .if source is a file type Target must be file.I cehck this case but I want to write quick fix if source is a file, for targer comes files types or just the opposite.I hope understand my problem.I solved this problem How solved Sad
@Check
public void Checkscyn(Sync s){
SyncElement synsource = s.getSource();
SyncElement syntarget = s.getTarget();
if(synsource.eClass() != syntarget.eClass()){
error(" synchronization is incorrect ,use same type element",null, FilesystemPackage.SYNC__NAME,
INVALID_SYCN,s.getName());

}
}

Ouick fix:

@Fix(FilesystemJavaValidator.INVALID_SYCN)
public void fixSourceTarger(final Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Choose" , " '" + issue.getData()[0]+ "'", "gggg", new ISemanticModification() {
public void apply(EObject element , IModificationContext context){
((Sync)element).setName(issue.getData()[0]);
System.out.println( ((Sync)element).getSource().getName());


System.out.println(issue.getData()[0]);
}
}
);
}
Re: Check Type of Element [message #1021636 is a reply to message #1021533] Wed, 20 March 2013 12:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
I i dont get your question.

what does work and what does not?
Re: Check Type of Element [message #1021652 is a reply to message #1021636] Wed, 20 March 2013 13:10 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian,

I load screenshot.This validation screenshot below:
@Check
public void Checkscyn(Sync s){
SyncElement synsource = s.getSource();
SyncElement syntarget = s.getTarget();
if(synsource.eClass() != syntarget.eClass()){
error(" synchronization is incorrect ,use same type element",null, FilesystemPackage.SYNC__NAME,
INVALID_SYCN,s.getName());

}
}

I want to write quick fix if source is a drive (D) target is a Drive type for example :
D,H comes at the Quick Fix screen (I load a screenshot for this case)

I hope ,I can tell my problem Sad How comes related cross reference type for target with quick fix
Re: Check Type of Element [message #1021696 is a reply to message #1021652] Wed, 20 March 2013 14:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Hi,

Data is an array. you can pass more as the name.
Re: Check Type of Element [message #1021708 is a reply to message #1021696] Wed, 20 March 2013 14:46 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian,

sory I can't understand . I want to bring Drive Name at the quick fix for example Very Happy,C
.User select these one.I put Drive name in a list but ı can t call this function.

I try this code but I do not bring related type.
@Fix( FilesystemJavaValidator.INVALID_SYCN)
public void createsycn(final Issue issue,IssueResolutionAcceptor acceptor){
final String linkText = issue.getData()[0];
acceptor.accept(issue, "Create entity '" + linkText+ "'", "Create entity '" + linkText+ "'", null,
new ISemanticModification() {
public void apply(EObject element, IModificationContext context)
{ SyncElement a = null;
Checkscyn(a );

// TODO Auto-generated method stub
}

public void Checkscyn(SyncElement s){
Drive d = EcoreUtil2.getContainerOfType(s, Drive.class);
List<Drive> drivelist = EcoreUtil2.getAllContentsOfType(d,Drive.class);

System.out.println(drivelist);
}
});
createLinkingIssueResolutions(issue, acceptor);
}
Re: Check Type of Element [message #1021714 is a reply to message #1021708] Wed, 20 March 2013 15:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
What is yopur problem

- Do you want to open a dialog? why dont you simple add a fix for every possible value
- is your problem that you cannot get possible values?
- is your problem that you cannot get the object?

....

~Christian
Re: Check Type of Element [message #1021730 is a reply to message #1021714] Wed, 20 March 2013 15:36 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian ,
I can open a dialog page I load a screenshot ,but I can not get possible object.for example :I want to get sorce is a drive name, target must be drive name
so I must get all drive name at the dialog page.OR source is a file ,target must be a file name hence must bring all file name.Sad I try add a fix but wanted objects can not bring.

[Updated on: Wed, 20 March 2013 15:47]

Report message to a moderator

Re: Check Type of Element [message #1021747 is a reply to message #1021730] Wed, 20 March 2013 16:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Hi, how does your scoping work? local on the actual file, if so traverse it
if not inject the IGlobalScopeProvider and ask it for the scope, and traverse it
Re: Check Type of Element [message #1021836 is a reply to message #1021747] Wed, 20 March 2013 19:06 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian,
no local on the actual file.I'm sorry I do not understand exactly Sad .Why use IGlobalScopeProvider ? how I use it with the quick fix ?

My grammar : post it previously

Filesystem returns Filesystem:
'Filesystem' name=ID'{'
(drives+=Drive (drives+=Drive)* )?
(syncs += Sync (syncs +=Sync)* )?

'}';


Sync returns Sync:
'sync' name=ID 'source' '=' source =[SyncElement|ID] 'target' '=' target =[SyncElement|ID] ';'
;
SyncElement :
Drive|File|Folder
;


Drive returns Drive:
{Drive}
'Drive' name=ID '{'
(contents+=Object ( contents+=Object)* )?
'}' ;



Object:
File|Folder
;

File returns File:
'File' name=ID (('{'
('ext' '=' ext = EXT ';')
&('content' '=' content = STRING ';')?
&('hidden' '=' Hidden = Boolean ';')?
&('readonly' '=' readonly = Boolean ';')?
&('system' '=' system = Boolean ';')?
&('archive' '=' archive = Boolean ';')?
&('location' '=' location=STRING ';')?
&('date' '=' date=STRING )?
'}')|';' ) | Shortcut
;



enum Boolean:
true |false
;


enum EXT:
PNG |TXT |JPG |JAVA | C | BAT |BMP |EXE |COM |DOC |PDF|DOCX |XLS|XLSX
;


Folder returns Folder:

'Folder' name=ID'{'
( contents+=Object(contents+=Object)* )?
'}';

Shortcut returns Shortcut:
'Shortcut' name=ID ('target' '=' target=[Object|ID]) ';'
;


Re: Check Type of Element [message #1021843 is a reply to message #1021836] Wed, 20 March 2013 19:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
public class MyDslQuickfixProvider extends DefaultQuickfixProvider {

	@Fix("code")
	public void createsycn(final Issue issue,IssueResolutionAcceptor acceptor){
	final String linkText = issue.getData()[0];
	acceptor.accept(issue, "Create entity '" + linkText+ "'", "Create entity '" + linkText+ "'", null,
	new ISemanticModification() {
	public void apply(EObject element, IModificationContext context)
	{ 
		Sync d = (Sync)element;
		Filesystem fs = EcoreUtil2.getContainerOfType(d, Filesystem.class);
		for (File fx : EcoreUtil2.getAllContentsOfType(fs, File.class)) {
			System.out.println(fx.getName());
		}
	}
	});}

}


public class MyDslJavaValidator extends AbstractMyDslJavaValidator {

	@Check
	public void Checkscyn(Sync s){
	SyncElement synsource = s.getSource();
	SyncElement syntarget = s.getTarget();
	if(synsource.eClass() != syntarget.eClass()){
	error(" synchronization is incorrect ,use same type element",s, MyDslPackage.Literals.SYNC__NAME,"code",s.getName());

	}
	}
Re: Check Type of Element [message #1021845 is a reply to message #1021843] Wed, 20 March 2013 19:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Btw you may have a look at org.eclipse.xtext.ui.editor.quickfix.DefaultQuickfixProvider.createLinkingIssueResolutions(Issue, IssueResolutionAcceptor) too
(to get some ideas)
Re: Check Type of Element [message #1021861 is a reply to message #1021845] Wed, 20 March 2013 20:20 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member

Thank you very much for your interest.I'm sorry because I'm new to these issues and having trouble understanding.I try these code but fx.getName() not get in the quick fix dialog page.I want to choose fx.getName for target.in addition I try createLinkingIssueResolutions(Issue, IssueResolutionAcceptor) but parameters wrong ,I am not define correct parameters for createLinkingIssueResolutions.

@Fix( FilesystemJavaValidator.INVALID_SYCN)
public void createsycn(final Issue issue,IssueResolutionAcceptor acceptor){
final String linkText = issue.getData()[0];
acceptor.accept(issue, "choose same type '" + linkText+ "'", "null '" + linkText+ "'", null,
new ISemanticModification() {
public void apply(EObject element, IModificationContext context)
{
Sync d = (Sync)element;
Filesystem fs = EcoreUtil2.getContainerOfType(d, Filesystem.class);
for (File fx : EcoreUtil2.getAllContentsOfType(fs, File.class)) {
System.out.println(fx.getName());
}
}
});
}
Re: Check Type of Element [message #1021876 is a reply to message #1021861] Wed, 20 March 2013 20:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Hi,

you have no linking issue so you cannot use it. and it wont solve your problem since the cross ref is valid by semantics of scoping/linking
i just told you to get an idea how a similar not the same problem is solved.

the link text problem i cannot reproduce
the quickfix has the text. Create entity 'xxxx'

sooooooo i have still no clue at all what your problem is. i do not want to do your work.
and a am distrubed by your context switches.

Re: Check Type of Element [message #1021879 is a reply to message #1021876] Wed, 20 March 2013 21:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
So i would have expected that you would build something like

public class MyDslQuickfixProvider extends DefaultQuickfixProvider {
	
	@Inject
	private IssueModificationContext.Factory modificationContextFactory;
	

	@Fix("code")
	public void createsycn(final Issue issue, final IssueResolutionAcceptor acceptor){
		
		final IModificationContext modificationContext = modificationContextFactory.createModificationContext(issue);
		final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
		if (xtextDocument == null)
			return;
		xtextDocument.readOnly(new IUnitOfWork.Void<XtextResource>() {

			@Override
			public void process(XtextResource state) throws Exception {
				Sync sync = (Sync) state.getEObject(issue.getUriToProblem().fragment());
				Filesystem fs = EcoreUtil2.getContainerOfType(sync, Filesystem.class);
				if (sync.getSource() instanceof Folder) {
					for (final Folder fx : EcoreUtil2.getAllContentsOfType(fs, Folder.class)) {
						acceptor.accept(issue, fx.getName(), "Folder " + fx.getName(), null, new ISemanticModification() {
							
							@Override
							public void apply(EObject element, IModificationContext context)
									throws Exception {
								((Sync)element).setTarget(fx);
								
							}
						});
					}
				} else if (sync.getSource() instanceof File) {
					for (final File fx : EcoreUtil2.getAllContentsOfType(fs, File.class)) {
						acceptor.accept(issue, fx.getName(), "File " + fx.getName(), null, new ISemanticModification() {
							
							@Override
							public void apply(EObject element, IModificationContext context)
									throws Exception {
								((Sync)element).setTarget(fx);
								
							}
						});
					}
				}
				
			}
			
		});
		
		
	}
	
	
}


yourself
Re: Check Type of Element [message #1022040 is a reply to message #1021879] Thu, 21 March 2013 07:11 Go to previous messageGo to next message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian ,

these lines are incomplete .I could not use the appropriate parameters.
if (sync.getSource() instanceof Folder) {
for (final Folder fx : EcoreUtil2.getAllContentsOfType(fs, Folder.class)) {
acceptor.accept(issue, fx.getName(), "Folder " + fx.getName(), null, new ISemanticModification()

Thank you very much for your help.I understand the subject better.acceptor.accept () method parameters is oke anymore.

Best Regards
Re: Check Type of Element [message #1022042 is a reply to message #1022040] Thu, 21 March 2013 07:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6313
Registered: July 2009
Senior Member
Hi the only thing I understand is "Bahnhof" I tested the code with
your grammar and it works fine. From you post I never understand what
your problem is.

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de
Re: Check Type of Element [message #1022376 is a reply to message #1022042] Thu, 21 March 2013 19:15 Go to previous message
junior developer is currently offline junior developer
Messages: 335
Registered: January 2013
Senior Member
Hi Christian,

Your code works fine .I want to do this exactly.but I could not do previously work ,I used process(XtextResource state) method put I could not succesful.now solved my problem.thanks so much for your help.
Sory, I could not tell my problem from my post .My problem solved for your help.
again,thank you

Previous Topic:XText for existing language
Next Topic:JvmModelInferrer and InferredTypes
Goto Forum:
  


Current Time: Sun Sep 21 14:29:50 GMT 2014

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

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