Home » Modeling » TMF (Xtext) » Check Type of Element 
| Check Type of Element [message #1020565] | 
Mon, 18 March 2013 09:33   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 #1021333 is a reply to message #1020779] | 
Tue, 19 March 2013 18:06    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 #1021533 is a reply to message #1021423] | 
Wed, 20 March 2013 05:26    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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     
@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 #1021708 is a reply to message #1021696] | 
Wed, 20 March 2013 10:46    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
Hi Christian, 
 
sory I can't understand . I want to bring  Drive Name at the quick fix for example  ,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 #1021843 is a reply to message #1021836] | 
Wed, 20 March 2013 15:27    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 #1021861 is a reply to message #1021845] | 
Wed, 20 March 2013 16:20    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
 
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 #1021879 is a reply to message #1021876] | 
Wed, 20 March 2013 17:06    | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 #1022376 is a reply to message #1022042] | 
Thu, 21 March 2013 15:15   | 
 
Eclipse User  | 
 | 
 | 
   | 
 
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 
 
 |  
 |  
  |   
Goto Forum:
 
 Current Time: Tue Nov 04 07:50:13 EST 2025 
 Powered by  FUDForum. Page generated in 0.51259 seconds  
 |