Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » trying to understand jvmInferer and JvmModelAssociator.getLogicalContainer(fixed error... still trying to understand best practice)
trying to understand jvmInferer and JvmModelAssociator.getLogicalContainer [message #860958] Sat, 28 April 2012 09:40 Go to next message
John Blue is currently offline John BlueFriend
Messages: 7
Registered: April 2012
Location: San Francisco
Junior Member

On a simple language which extends from XBase.

Module:
  {Module}
  'module' canonicalName=QualifiedName
  (elements += XExpressionInsideBlock|elements+=Import)*

XPrimaryExpression returns xbase::XExpression:
        ...
	XParenthesizedExpression | 
	Bean 
;

Bean returns xbase::XExpression:
  {Bean} 'bean'  name=ValidID type=JvmTypeReference 
	assignments = AssignmentBlock
;

AssignmentBlock returns xbase::XBlockExpression:
	{AssignmentBlock} '{'
    	(expressions+=XAssignment ';'?)* '}' 
;


With that, and the customizations in my Scope Provider, I am able to define bean as follows, without problems:

    
   module test
   bean book com.acme.Book {
      author  = "John"
   }


I am really trying to understand the best practice in order to minimize the code in the ScopeProvider (I over overridden ScopeProvider.createLocalVarScope to handle AssignmentBlock so it return SimpleScope for JVMType that corresponds to the actual Jvm class the bean is mapped to, bound to THIS variable.
I have also overridden the getContextType so it returns the JvmType that is referenced by the bean if the context is AssignmentBlock)


Currently JvmModelAssociator.getLogicalContainer is returnning null for XAssignment which are the elements inside the AssignmentBlock

I am trying to understand the following:

- Should I be using JVMModelInferrer to map the 'bean' to the JVmType?
- Or perhaps map the AssignmentBlock to to the JVmType of the bean?
- What should the JvmModelAssociator.getLogicalContainer return for XAssignment, XAssignmentBlock, or Bean itself?

Thanks in advance for your guidance

[Updated on: Sat, 28 April 2012 10:57]

Report message to a moderator

Re: trying to understand jvmInferer and JvmModelAssociator.getLogicalContainer [message #863111 is a reply to message #860958] Sun, 29 April 2012 09:14 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1770
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 04/28/2012 11:40 AM, John Blue wrote:
> On a simple language which extends from XBase.
>
> Module:
> {Module}
> 'module' canonicalName=QualifiedName
> (elements += XExpressionInsideBlock|elements+=Import)*
>
> XPrimaryExpression returns xbase::XExpression:
> ...
> XParenthesizedExpression |
> Bean
> ;
>
> Bean returns xbase::XExpression:
> {Bean} 'bean' name=ValidID type=JvmTypeReference
> assignments = AssignmentBlock
> ;
>
> AssignmentBlock returns xbase::XBlockExpression:
> {AssignmentBlock} '{'
> (expressions+=XAssignment ';'?)* '}'
> ;
>
>
> I want to be able to define a bean as follows:
>
>
> module test
> bean book com.acme.Book {
> author = "John"
> }
>
>
> I am getting failure as it does not recognize "author" as a field of Book.
>
> I over overridden ScopeProvider.createLocalVarScope to handle AssignmentBlock
> so it return SimpleScope for JVMType that corresponds to the actual Jvm class the bean is mapped to, bound to THIS variable. (See attached
>
> I have also overridden the getContextType so it returns the JvmType that is referenced by the bean if the context is AssignmentBlock
>
>
> Currently JvmModelAssociator.getLogicalContainer is returnning null for XAssignment which are the elements inside the AssignmentBlock
>
> I am trying to understand the following:
>
> - Should I be using JVMModelInferrer to map the 'bean' to the JVmType?
> - Or perhaps map the AssignmentBlock to to the JVmType of the bean?
> - What should the JvmModelAssociator.getLogicalMapping return for XAssignment, XAssignmentBlock, or Bean itself?
>
>
> I am really trying to understand the best practice so as not to write too much code in the ScopeProvider for some other more complex cases.
> Thanks in advance

Hi John

I've just started to port my DSL which uses Xbase
(http://xsemantics.sourceforge.net/ ) to the inferrer mechanism (for the
moment I manually generate into Java, and have some custom scoping).
Thus I'm not very expert about the inferrer mechanism and
associations... but I think I understood a few things:

- if you implement the inferrer to map your EObject to a JvmType (say
you map bean to a JvmType) then the scoping mechanism will use the
mapped JvmType when computing the scoping (and it will also bind
automatically many things for you, like this, and thus possible fields
and methods)

- this also means that if you intercept (i.e., override :)

override IScope createLocalVarScope(IScope parentScope,
LocalVariableScopeContext scopeContext) {
val EObject context = scopeContext.getContext();
switch (context) {

and you mapped Bean to a JvmType in the inferrer, then when it comes to
scoping for bean you will actually have the mapped JvmType as the
context and not the Bean (though you might get the original Bean using
an injected associator, which is a singleton by the way).

- when overriding createLocalVarScope, I think that the right way to do
it is to first get the parent scope from a super invocation, and then to
possibly create a custom scope using the parent scope (this way you're
sure to benefit all the automatic scoping of xbase, especially if you
used the inferrer to provide JvmType containers...), for instance I did
like this in my DSL

@Override
protected IScope createLocalVarScope(IScope parentScope,
LocalVariableScopeContext scopeContext) {
parentScope = super.createLocalVarScope(parentScope, scopeContext);

if (scopeContext != null && scopeContext.getContext() != null) {
EObject context = scopeContext.getContext();
if (context instanceof Rule) {
parentScope = Scopes.scopeFor(
utils.getJvmParameters((Rule) context), parentScope);
} else if ... other cases
}

return parentScope;
}

I don't know whether this might help... I'm still investigating about
the inferrer interacting with scoping and generation...

I hope I didn't say anything completely wrong in this message ;)

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


how to initialize a JvmType instance with XAssignments using XBaseInterpreter [message #869091 is a reply to message #863111] Thu, 03 May 2012 00:03 Go to previous message
John Blue is currently offline John BlueFriend
Messages: 7
Registered: April 2012
Location: San Francisco
Junior Member

Thanks Lorenzo for your response. As you suggested, I ended up infering the Bean to its corresponding JvmType.

I am trying to run this test and getting an error in initializing the bean by the interpreter:

@Test
def void testSetupBeanWithString() {
	val dsl = "
		module test
		import com.brane.cloud.flow.Book
		bean book Book {
			name='Cloud'
	}
   " 
   parser.parse(dsl).assertNoErrors
        
   val module = parser.parse(dsl)
   val bean = module.elements.get(1) as Bean
        
   val evaluationContext = contextProvider.get()
   evaluationContext.newValue(XbaseScopeProvider::THIS, bean)
       
   interpreter.evaluate(bean, evaluationContext, CancelIndicator::NullImpl)

   val beanVal = evaluationContext.getValue(QualifiedName::create(bean.name)) as Book

   Assert::assertNotNull(beanVal)
   Assert::assertEquals(beanVal.name, "Cloud")
}



I override XbaseInterpreter.getReceiver method as follows:


@Override
protected Object getReceiver(XAssignment assignment, IEvaluationContext context, CancelIndicator indicator) {
   Object receiver = super.getReceiver(assignment, context, indicator);
   if (receiver instanceof Bean) {
	JvmType jvmType = this.getAssociatedJvmType((Bean)receiver);
	return jvmType;
   }
   return receiver;
}


Apparently this is not the correct return type, as it is trying to call the .setName() method on the JvmType.

Any idea on how to approach this?
Previous Topic:How accessing file complete location ?
Next Topic:content assist problem
Goto Forum:
  


Current Time: Fri Dec 04 15:47:00 GMT 2020

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

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

Back to the top