Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Deriving from XBase(How to augment an existing grammar)
Deriving from XBase [message #1689918] Tue, 24 March 2015 16:24 Go to next message
Fenris Wolf is currently offline Fenris WolfFriend
Messages: 10
Registered: March 2015
Junior Member
Hy

Let's say i want a language like XBase, but with some more literals. The approach i found to achive that can be found here: [http ://koehnlein.blogspot.de/2011/07/extending-xbase.html]. According to it i create a grammar-rule, that has the name of an XBase-grammar-rule (XLiteral in that case), define it similarly and add some alternatives i like. The thing i don't get from the blog-entry: Where to place the code that describes how my additional alternatives are handled by the XBase-Compiler used in my model-inferrer? The blog-entry doesn't reveal where the code-snippets belong to.

A related question: In my model-inferrer i want to wrap an XExpression (let's call it foo) into an if-statement. I tried the following: I have created a subclass of XIfExpressionImpl (no modified or additional functionality -- only for creating an instance with the correct attributes, since the constructor of XIfExpressionImpl is protected) and simply used it as the body of a method in my inferrer; just like "body = new MyXIfExpressionImpl(if_expression, foo, else_expression);". But unfortunately all i get from this is a missing body in the output (and no error or warning telling me what the problem might be). What is the correct way to do it?

Greetings,
Fenris

PS: Is there a documentation for the model-inferrer-language?
Re: Deriving from XBase [message #1690150 is a reply to message #1689918] Wed, 25 March 2015 13:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
No there is no Dokumentation. Have a look at xbase type Computer and jvmmodelgenerator.
For your if else pattern what about delegating in the if part to another ( private) method


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Deriving from XBase [message #1690233 is a reply to message #1690150] Wed, 25 March 2015 22:38 Go to previous messageGo to next message
Fenris Wolf is currently offline Fenris WolfFriend
Messages: 10
Registered: March 2015
Junior Member
Christian Dietrich wrote on Wed, 25 March 2015 13:36
No there is no Dokumentation. Have a look at xbase type Computer and jvmmodelgenerator.

So download.eclipse.org/modeling/tmf/xtext/javadoc/2.4/org/eclipse/xtext/xbase/typesystem/computation/XbaseTypeComputer.html and download.eclipse.org/modeling/tmf/xtext/javadoc/2.3/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.html? I don't see how these help to solve my problem.

Christian Dietrich wrote on Wed, 25 March 2015 13:36
For your if else pattern what about delegating in the if part to another ( private) method

Hmm, can you explain this a little closer please?
Re: Deriving from XBase [message #1690248 is a reply to message #1690233] Thu, 26 March 2015 06:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
for your first problem please be a bit more specific. Having a look what is done with other literals should help you to introduce new ones.

for the second:

inferr this part:


public void someMethod() {
    if(....) {
          someMethodIfPart();
    }
}

private void someMethodIfPart() {

}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Deriving from XBase [message #1690372 is a reply to message #1690248] Thu, 26 March 2015 23:52 Go to previous messageGo to next message
Fenris Wolf is currently offline Fenris WolfFriend
Messages: 10
Registered: March 2015
Junior Member
If i understand you correctly, you suggest to not create the if-statement explicitly (by manual instanciation), but implicitely by writing its textual representation. Though this sounds a little circuitous, i want to give it a try. But where to place the code? Let me provide a more concrete example:

I have the following grammar-rule
Notification:
    "notification" (name = ID) "~" (predicate = XExpression) ":" (code = XBlockExpression)
;


What i want from that is a method like
void notification_[notification.name]()
{
    if (compilation([notification.predicate]))
    {
        compilation([notification.code])
    }
}


Where and how would i implement that?
Re: Deriving from XBase [message #1690395 is a reply to message #1690372] Fri, 27 March 2015 06:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,
have a look at the jvmmodelinferrer (use the domain model example as the example it is meant to be)

unfortunately this depends on the rest of the grammar but a simple implementation could look like
(asuming you inferr the context of the notification to a class - based on the domain model example)

Feature:
	Property | Operation |Notification;


package org.eclipse.xtext.example.domainmodel.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.example.domainmodel.domainmodel.Entity
import org.eclipse.xtext.example.domainmodel.domainmodel.Notification
import org.eclipse.xtext.example.domainmodel.domainmodel.Operation
import org.eclipse.xtext.example.domainmodel.domainmodel.Property
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1

class DomainmodelJvmModelInferrer extends AbstractModelInferrer {
	
	@Inject extension JvmTypesBuilder
	@Inject extension IQualifiedNameProvider

	def dispatch infer(Entity entity, extension IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
		accept(entity.toClass( entity.fullyQualifiedName )) [
			documentation = entity.documentation
			if (entity.superType != null)
				superTypes += entity.superType.cloneWithProxies
			
			// let's add a default constructor
			members += entity.toConstructor []
			
			// and one which can be called with a lambda for initialization.
			val procedureType = typeRef(Procedure1, typeRef(it)) /* Procedure<MyEntity> */ 
			members += entity.toConstructor [
				parameters += entity.toParameter("initializer", procedureType)
				// here we implement the body using black box Java code.
				body = '''
					initializer.apply(this);
				'''
			]
			
			// now let's go over the features
			for ( f : entity.features ) {
				switch f {
					
					Notification: {
						val notification = f
						members += notification.toMethod("notification_"+notification.name, Void.TYPE.typeRef()) [
							body = '''
							if (_notification_«notification.name»_internal_predicate()) {
								_notification_«notification.name»_internal();
							}
							'''
						]
						members += notification.toMethod("_notification_"+notification.name+"_internal", Void.TYPE.typeRef()) [
							body = notification.code
						]
						members += notification.toMethod("_notification_"+notification.name+"_internal_predicate", Boolean.TYPE.typeRef()) [
							body = notification.predicate
						]
					}
						
			
					// for properties we create a field, a getter and a setter
					Property : {
						val field = f.toField(f.name, f.type)
						members += field
						members += f.toGetter(f.name, f.type)
						members += f.toSetter(f.name, f.type)
					}
			
					// operations are mapped to methods
					Operation : {
						members += f.toMethod(f.name, f.type ?: inferredType) [
							documentation = f.documentation
							for (p : f.params) {
								parameters += p.toParameter(p.name, p.parameterType)
							}
							// here the body is implemented using a user expression.
							// Note that by doing this we set the expression into the context of this method, 
							// The parameters, 'this' and all the members of this method will be visible for the expression. 
							body = f.body
						]
					}
				}
			}
			
			// finally we want to have a nice toString methods.
			members += entity.toToStringMethod(it)
		]
	}
	
}


resulting for an example model

entity X {
	notification y ~ true : {println(true)}
}


in

public class X {
  
  public void notification_y() {
    if (_notification_y_internal_predicate()) {
    	_notification_y_internal();
    }
  }
  
  public void _notification_y_internal() {
    InputOutput.<Boolean>println(Boolean.valueOf(true));
  }
  
  public boolean _notification_y_internal_predicate() {
    return true;
  }
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Deriving from XBase [message #1690576 is a reply to message #1690395] Sun, 29 March 2015 09:29 Go to previous messageGo to next message
Fenris Wolf is currently offline Fenris WolfFriend
Messages: 10
Registered: March 2015
Junior Member
It's not what i was after initially, but it does what i want and it seems to be a trick, which is applicable in most cases.

Thank you, very much!

[Updated on: Sun, 29 March 2015 09:30]

Report message to a moderator

Re: Deriving from XBase [message #1691057 is a reply to message #1690576] Wed, 01 April 2015 22:54 Go to previous messageGo to next message
Fenris Wolf is currently offline Fenris WolfFriend
Messages: 10
Registered: March 2015
Junior Member
Sorry to come up with this again, but it really puzzles me, why the generation does not work properly when using "self-made" XExpressions. Do these objects need some kind of special initialization and/or registration?
The reason why i ask is, that the approach from above is quite hard to realize in some cases and it relies on implicit conversions; e.g. the predicate-expression is turned into a return-statement, only because it is assigned to body. Though this is what i want in this situation, it's not a solid solution and is likely to break sooner or later.

Greetings
Re: Deriving from XBase [message #1691061 is a reply to message #1691057] Thu, 02 April 2015 04:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

first of all: what you are doing is "very advanced" stuff.

then: these are two totally different usecases:

Your Notification is not a custom XExpression, it is a high level construct.
The solution for high level constructs is to map it to basic highlevel java constructs. doing this you get scoping / typesystem for free. otherwise it would be a hell of work to adapt this as well (e.g. XbaseBatchScopeProvider)

for XExpressions you adapt XbaseCompiler and TypeComputer


if you keep that in mind it should always work


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

[Updated on: Thu, 02 April 2015 04:56]

Report message to a moderator

Re: Deriving from XBase [message #1691314 is a reply to message #1691061] Sun, 05 April 2015 16:08 Go to previous messageGo to next message
Fenris Wolf is currently offline Fenris WolfFriend
Messages: 10
Registered: March 2015
Junior Member
Christian Dietrich
Your Notification is not a custom XExpression, it is a high level construct.

Making it an XExpression was not my intention.
The "Notification"-stuff was just an example. My question was meant more generally. I want to use the existing infra-structure, i.e. using and constructing XExpressions from XBase. This approach doesn't seem so unreasoning to me. But i'm willing to go another way if it satisfies my needs. The würgaround from above unfortunatelly does not.

Christian Dietrich
The solution for high level constructs is to map it to basic highlevel java constructs. doing this you get scoping / typesystem for free. otherwise it would be a hell of work to adapt this as well (e.g. XbaseBatchScopeProvider)

OK, so how would that look like?

Christian Dietrich
for XExpressions you adapt XbaseCompiler and TypeComputer

Yes, that already works.
Re: Deriving from XBase [message #1691316 is a reply to message #1691314] Sun, 05 April 2015 17:02 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

Quote:
OK, so how would that look like?


how would what look like? i already showed how to map your dsl concept to existing java concepts


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Generate a static initializer with JvmModelInferrer
Next Topic:XText -&gt; Help Required
Goto Forum:
  


Current Time: Tue Apr 23 13:29:20 GMT 2024

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

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

Back to the top