Skip to main content



      Home
Home » Modeling » TMF (Xtext) » how to get access to global scope inside a AbstractDeclarativeScopeProvider method?
how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895423] Thu, 12 July 2012 16:00 Go to next message
Eclipse UserFriend
Hi,

I'm learn to deal with scope using this model:

Organization:
	'Organization:'
	'{'
 	'code' name=ID
 	'name' longname=STRING
	('description' description=STRING)?
	('Partnerships:' '{' partnerships+=Partnership* '}')?
	'}';
Partnership:
	'Partnership:'
	'{'
	'partner' company=[Organization|Fqn]
	('agreement' agreement=STRING)?
	'}'
;


I could try the ArithmeticsScopeProvider example:
@Override
	public IScope getScope(EObject context, EReference reference) {
		IScope scope = super.getScope(context, reference);
		return new FilteringScope(scope,
				new Predicate<IEObjectDescription>() {
					public boolean apply(IEObjectDescription input) {
						return input!=null && input.getName()!=null && input.getName().getSegmentCount()==1;
					};
				});
	}


But what I want to know is how could I get access to global scope values inside a AbstractDeclarativeScopeProvider method like the one below. I can't call super.getscope() as in ArithmeticsScopeProvider because I will get a Stack exception. So how to know all Organizations discovered by xtext inside this method?:
IScope scope_Partnership_company(Organization context, EReference ref) {}


thanks for any tip.

regards,

Cristiano
Re: how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895425 is a reply to message #895423] Thu, 12 July 2012 16:07 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

you can call the scopeprovider-chain (ImportedNamespaceAwareScopeProvider->GlobalScopeProvider)
by calling delegateGetScope(context,reference)

~Christian
Re: how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895431 is a reply to message #895425] Thu, 12 July 2012 17:25 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I will have a try.. thanks !

regards,

Cristiano
Re: how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895602 is a reply to message #895425] Fri, 13 July 2012 14:10 Go to previous messageGo to next message
Eclipse UserFriend
Hey Christian,

I could make it work, thank you. But I need to create a test for it.

Could you give some tips about the best way to test this kind of scope filtering behavior?

I've created a test file using xtend as stated in docs.

I've loaded one resource using the ParseHelper.parse()... I've create a method to ensure that values was correctly set to entities.

Now I don't know how to trigger the proposal and how ensure that the scope was rightly calculated when I load a second resource.

any example or link will be appreciated.

regards,

Cristiano
Re: how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895604 is a reply to message #895602] Fri, 13 July 2012 14:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi

do you want to test scoping or content assist?
so what about calling org.eclipse.xtext.junit4.util.ParseHelper.parse(CharSequence, URI, ResourceSet)
with the same rsourceset multiple times
(and thus create a bunch of resources)
and then call the scopeprovider on a specific model element
and the analyze the resulting scope?

~Christian
Re: how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895692 is a reply to message #895604] Sat, 14 July 2012 23:43 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

I'm trying to do what you have suggested. I could load two resources using the same resourceSet.

The org1.organization has a dependency to org2.organization, but the link resolution is not working. I did some debugging and note that the value of company attribute is null, so the global scope seems not to be working in the test.

Should I activate something in the test? Could you point what I'm missing?

thanks

@InjectWith(typeof(OrganizationDslInjectorProvider))
@RunWith(typeof(XtextRunner))
/**
 * Test 
 */
class TestCompleteOrganizationModel {

	@Inject
	private ParseHelper<Organization> parser
	
	@Inject
    private Provider<XtextResourceSet> resourceSetProvider
    
    @Inject 
    private IScopeProvider scopeProvider
    
    private XtextResourceSet resourceSet
	
	private Organization orgModel1
	private Organization orgModel2
	
	def parseTestFile (String name) {
    	val cl = Thread::currentThread().getContextClassLoader()
    	val in = cl.getResourceAsStream(name)
 		
 		assertNotNull("Couldn't find the resource file in classpath.", in)
 		
    	val uri = URI::createFileURI(name)
    	
    	return parser.parse(in, uri, null, resourceSet);
}
	
	@Before
	def void before(){
		
		resourceSet = resourceSetProvider.get
		
		orgModel2 = parseTestFile("tests/data/org2.organization")
		assertNotNull(orgModel2)
		orgModel1 = parseTestFile("tests/data/org1.organization")
		assertNotNull(orgModel1)
	}

	@Test
	def void ensureOnlyExternalCompaniesProposalsForPartnerCompany(){
		

		val partner = orgModel1.partnerships.head
		assertNotNull(partner)
		
		val reference = OrganizationDslPackage::eINSTANCE.partnership_Company
		
		var scope = scopeProvider.getScope(partner, reference)
		
		var list = scope.allElements
				
		var actual = scope.allElements.map[name.toString].join(", ")
		
		assertEquals("Org2Company", actual)		
	}
Re: how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895710 is a reply to message #895692] Sun, 15 July 2012 04:41 Go to previous messageGo to next message
Eclipse UserFriend
Habe a look at https://bugs.eclipse.org/bugs/show_bug.cgi?id=382555

--
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: how to get access to global scope inside a AbstractDeclarativeScopeProvider method? [message #895745 is a reply to message #895710] Sun, 15 July 2012 12:22 Go to previous message
Eclipse UserFriend
Hi Christian,

I could run the tests properly using the workaround stated in the latest bug comment, thanks. Smile

Now that my tests are running I would like to ask one more question about a strange behavior that I noted in the scope computing. I'm getting duplicated items when they come from the same current resource.

For example, this is the result for the test variable in the method below:
[systemAnalyst, javaProgrammer, Org1.systemAnalyst, Org1.javaProgrammer, Org2.systemAnalyst, Org2.javaProgrammer]


Repair the occurrence of systemAnalyst and javaProgrammer, with and without parent, that comes from the same resource.

why this happen? The Content Assist handle this properly, but it is curious.


public IScope scope_Worker_personRoles(Organization context,
			EReference reference) {
IScope scope = delegateGetScope(context, reference);
final String orgNameToFix = context.getName();

String test = scope.getAllElements().toString();

return new FilteringScope(scope, new Predicate<IEObjectDescription>() {
	public boolean apply(IEObjectDescription input) {
		if (input != null
		&& input.getEObjectOrProxy() instanceof BusinessRole
		&& input.getEObjectOrProxy().eContainer() != null) {
		return input.getEObjectOrProxy().eContainer().eGet(OrganizationDslPackage.eINSTANCE	.getOrganization_Name()).equals(orgNameToFix);
				}
	return false;
			};
		});
	}

BusinessRole:
	'Role:'
	'{'
	'code' name=ID
	'name' longname=STRING
	'}';
Worker:
	'Worker:'
	'{'
	'person' person=[Person]
	('roles' '(' personRoles+=[BusinessRole] (',' personRoles+=[BusinessRole])* ')')?
	'}';
Previous Topic:Completion from POJO
Next Topic:[xtext] import model from xcore
Goto Forum:
  


Current Time: Mon Jul 07 09:25:49 EDT 2025

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

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

Back to the top