Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Limit scoping to a referenced declaration element
Limit scoping to a referenced declaration element [message #559283] Thu, 16 September 2010 09:27 Go to next message
awidegreen  is currently offline awidegreen Friend
Messages: 14
Registered: July 2010
Junior Member
Hej everyone!

I want to define a construct which is similar to the following grammar definition. Therefore I want to limit the scope of available elements to a declaration which is referenced through a partOf-element.

Model:
   element=(Manifest|Definition);
	
Manifest:
   'manifest' '{'
      (declarations+=Declaration)+
   '}';
Declaration:
   name=QualifiedName;
	
Definition:
   'partOf' partOf=[Declaration|QualifiedName]
   (elements+=Element)+;
	
Element:	
   'element' name=QualifiedName '{'
      'desc' '=' string=STRING
      ('linkTo' '=' linkTo=[Element|QualifiedName])?
   '}';
	
QualifiedName:
   ID ('.' ID)*;


The following listings illustrate the given grammar:

Define a single manifest-file:
manifest {
   movie
   actor
}


After that two definitions with a linkTo Element.
partOf movie

element aFilm {
   desc = "great film"
}

partOf actor

element anActor {
   desc = "great Actor"
   linkTo = aFilm
   // i want to do something like: film.aFilm
}


As mentioned in the comment, I want to link the element through the declaration like: film.aFilm. In fact, I want to stick to the default scoping behavior, which means I don't want to specify the declaration every time e.g. when I define another element in the same part.
partOf film

element anotherFilm {
   desc = "another film"
   linkTo = aFilm
}

... well the last code doesn't make sense in that context, I know! Wink

So is there a best practice for this feature? Can you give me a hint which Scoping-Class I maybe have to extend?

Thanks,
awidegreen


Update: thanks for the clarification Meinte

[Updated on: Thu, 16 September 2010 13:26]

Report message to a moderator

Re: Scoping over a 'partOf' Element [message #559351 is a reply to message #559283] Thu, 16 September 2010 13:09 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
I don't quite understand the verb "to link over". I'm guessing that you want to limit the scope of linkTo-references to Element's of the Declaration referenced through the partOf-element.

In that case, you'll have to implement an scope_Element_linkTo(Definition def, ...) scoping method which inspects the current Definition object and returns all Elements which are allowed to be linked through the partOf-element.


Re: Scoping over a 'partOf' Element [message #559374 is a reply to message #559351] Thu, 16 September 2010 13:39 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14667
Registered: July 2009
Senior Member
Hi,

i guess what you want to do is:

the qualified name of an element is:

(the name of the Declaration the Definition the Element is defined within references to using the partOf relation) + "." + the name of the Element

public class MyQNP extends DefaultDeclarativeQualifiedNameProvider{
	
	String qualifiedName(Element e) {
		return ((Definition)e.eContainer()).getPartOf().getName()+"."+e.getName();
	}
	
}


but this will lead to the cyclic resolution of lazy links error and i have no idea how to get rid of this.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 16 September 2010 13:39]

Report message to a moderator

Re: Scoping over a 'partOf' Element [message #559384 is a reply to message #559374] Thu, 16 September 2010 13:54 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14667
Registered: July 2009
Senior Member
A ugly but working solution is:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
   element=(Manifest|Definition);
	
Manifest:
   'manifest' '{'
      (declarations+=Declaration)+
   '}';
Declaration:
   name=ID;
	
Definition:
   'partOf' partOf=[Declaration]
   (elements+=Element)+;
	
Element:	
   'element' name=ID '{'
      'desc' '=' string=STRING
      ('linkTo' '=' linkToDeclaration=[Declaration] "." linkToElement=[Element])?
   '}';


public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	IScope scope_Element_linkToElement(final Element e, EReference r) {

		final String dname = e.getLinkToDeclaration().getName();
		IScope delegateGetScope = delegateGetScope(e, r);
		System.out.println(delegateGetScope);
		return new FilteringScope(delegateGetScope,new Predicate<IEObjectDescription>() {

			@Override
			public boolean apply(IEObjectDescription input) {

				if (input != null) {
				Element eObjectOrProxy = (Element)input.getEObjectOrProxy();
				if (eObjectOrProxy.eIsProxy()) {
					eObjectOrProxy = (Element) EcoreUtil.resolve(eObjectOrProxy, e.eResource().getResourceSet());
				}
				Definition definition = (Definition)eObjectOrProxy.eContainer();
				Declaration partOf = definition.getPartOf();
				if (partOf.eIsProxy()) {
					partOf = (Declaration) EcoreUtil.resolve(partOf, e.eResource().getResourceSet());
				}
				String declarationName = partOf.getName();
				return declarationName.equals(dname);
				}
				return false;
			}
		});
	}
	
}


public class MyQNP extends DefaultDeclarativeQualifiedNameProvider{
	
	String qualifiedName(Element e) {
		return e.getName();
	}
	
}


public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

	@Override
	public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
		return MyQNP.class;
	}
		
}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Scoping over a 'partOf' Element [message #559385 is a reply to message #559384] Thu, 16 September 2010 13:55 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14667
Registered: July 2009
Senior Member
Hi,

but i'd although be interested in a solution for the cyclic resolution of lazy links error problem.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Scoping over a 'partOf' Element [message #559410 is a reply to message #559384] Thu, 16 September 2010 14:44 Go to previous messageGo to next message
awidegreen  is currently offline awidegreen Friend
Messages: 14
Registered: July 2010
Junior Member
Hej Christian,

thanks for your reply and your ugly but working solution. Wink

You can imagine that my actual grammar is much more complex..., which means that I have to reference to a depth of 4:
linkTo = declaration.element.subselement.subsubelement

In general, I want to implement something like a java package (in this example the declaration). Therefore I want to split the elements of the 'package' in different files. Compared to java packages, I don't want to rely on a given filesystem structure. That's why I thought a manifest-file would be a good solution. Wink But that only makes sense when I can distinguish between both declarations - because two packages can have an element with the same name.

tack,
awidegreen
Re: Scoping over a 'partOf' Element [message #559450 is a reply to message #559410] Thu, 16 September 2010 16:31 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14667
Registered: July 2009
Senior Member
Hi,

I would kick out the Module and the Declaration - then it should work out of the Box.

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Scoping over a 'partOf' Element [message #559560 is a reply to message #559450] Fri, 17 September 2010 08:06 Go to previous message
awidegreen  is currently offline awidegreen Friend
Messages: 14
Registered: July 2010
Junior Member
Hej!

Quote:
I would kick out the Module and the Declaration - then it should work out of the Box.

you are right. And for future reference, here is the grammar I used:
Model:
	element=Definition;
	
Definition:
	'definition' name=QualifiedName 
	(elements+=Element)+;
	
Element:	
	'element' name=QualifiedName '{'
		'desc' '=' string=STRING
		('linkTo' '=' linkTo=[Element|QualifiedName])?
	'}';

QualifiedName:
  ID ('.' ID)*;

example:
definition film     

element aFilm {
    desc = "great film"
}

Thanks for your help, Christian!

tack,
awidegreen
Previous Topic:XtextEditor inside a TitleAreaDialog is read-only ;-(
Next Topic:[Resolved] Generation for only one file
Goto Forum:
  


Current Time: Fri Apr 26 17:48:54 GMT 2024

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

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

Back to the top