Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » autoimport with Xbase 2.4
autoimport with Xbase 2.4 [message #1057936] Thu, 09 May 2013 21:22 Go to next message
andrea rossi is currently offline andrea rossiFriend
Messages: 23
Registered: April 2013
Junior Member
Hi ,

i have my dsl grammar :
grammar provxbase.Provxbase with org.eclipse.xtext.xbase.Xbase

....

Model:
	elements+=Entity*;
	
Entity:
	'entity' name = ValidID body = XExpressionInsideBlock ;
...


I translate each entity in a java class.For this i extend AbstractModelInferrer.
Now how can i add import automatically in each java class?(without user write import in my dsl)

For example the user write this :
entity First { ... } 


and java class is :
import b.*
import c.*

class First { ..
}



Thank you
Re: autoimport with Xbase 2.4 [message #1058207 is a reply to message #1057936] Mon, 13 May 2013 08:37 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
You can add imports on startup using the ImportedNamespaceAwareScopeProvider. Create a custom subclass of it which adds "b" and "c" to the list of import normalizers for getImports and then change the bind in the runtime module to delegate to your custom class.

This is when you want some common one always available. Search the documentation first but I may be able to paste you some examples later on (I have done this in 2.3 but will be moving to 2.4 shortly so hope it still works there).
Re: autoimport with Xbase 2.4 [message #1058354 is a reply to message #1058207] Mon, 13 May 2013 16:05 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
To be more precise, now I've dug up my bit of code, to add import a.b.c.* just subclass the ImportedNamespaceAwareLocalScopeProvider
public class MyDSLImportedNamespaceAwareLocalScopeProvider extends ImportedNamespaceAwareLocalScopeProvider 
{
    static final ImportNormalizer myImport = new ImportNormalizer(QualifiedName.create("a","b","c"), true, false);

    static List<ImportNormalizer> myImportList = new ArrayList<ImportNormalizer>();
    static 
    {
	    System.out.println("Adding my imports.");
	    myImportList.add(myImport);
    }

    @Override
	  protected List<ImportNormalizer> getImplicitImports(boolean ignoreCase) {
	    return builtinList;
	  }

and insert a line into your MyDSLRuntimeModule.java to get it used as a scoping delegate (of AbstractDeclarativeScopeProvider in this example)
	@Override
	public void configureIScopeProviderDelegate(com.google.inject.Binder binder) {
		System.out.println("Bind in my NS aware scope provider delegate.");
		binder.bind(org.eclipse.xtext.scoping.IScopeProvider.class)    .annotatedWith(Names.named(org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider.NAMED_DELEGATE)) .to(my.dsl.scoping.MyDSLImportedNamespaceAwareLocalScopeProvider.class);
	}


Of course replacing 'mydsl' bits with your actual names.
Re: autoimport with Xbase 2.4 [message #1058751 is a reply to message #1058354] Tue, 14 May 2013 21:44 Go to previous messageGo to next message
andrea rossi is currently offline andrea rossiFriend
Messages: 23
Registered: April 2013
Junior Member
Thank you so much for your help.It seems to work but i have a problem.

I set implicit import jade.core and if i write this :
Entity hello {
	val i = 4
    var AID j = new AID()
}


The relative class hello is :
import jade.core.AID;

@SuppressWarnings("all")
public class hello {
  public void setup() {
    final int i = 4;
    AID _aID = new AID();
    AID j = _aID;
  }
}


but if i don't use any class of package the relative class hello is
@SuppressWarnings("all")
public class hello {
  public void setup() {
    final int i = 4;
  }
}


Now, How can I get import in translate class even if in the my dsl editor i don't use any class of import package?
Re: autoimport with Xbase 2.4 [message #1058764 is a reply to message #1058751] Tue, 14 May 2013 23:49 Go to previous messageGo to next message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
If you don't use any class from the import package then you don't need to specify the import. In your example, not using AID in the DSL code means you don't need AID in the generated Java either.
I'm not sure what the problem is.
Re: autoimport with Xbase 2.4 [message #1058859 is a reply to message #1058764] Wed, 15 May 2013 12:36 Go to previous messageGo to next message
andrea rossi is currently offline andrea rossiFriend
Messages: 23
Registered: April 2013
Junior Member
Hi,

My grammatic is like this :
grammar provxbase.Provxbase with org.eclipse.xtext.xbase.Xbase

....

Model:
	elements+=Entity*;
	
Entity:
	'entity' name = ValidID body = XExpressionInsideBlock ;

XExpressionInsideBlock returns xbase::XExpression:
	XVariableDeclaration | XExpression | SendMessage;

....



If i write this in my dsl editor :
entity hello {
	send message msg {
		content = "hello"
		receivers {
			receiver = "Ping"
		}
	}
}


the relative class is :
public class ciao {
  public void setup() {
    ....
    ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
    msg.setContent("hello");
    msg.addReceiver(new AID ("Ping",AID.ISLOCALNAME));
    myAgent.send(msg);
    
  }
}


In the first file i don't need to use AID class but in the relative translate java class i need to import it.
i thought to solve my problem with auto-import....
Re: autoimport with Xbase 2.4 [message #1058865 is a reply to message #1058859] Wed, 15 May 2013 13:09 Go to previous messageGo to next message
Colonel Panic is currently offline Colonel PanicFriend
Messages: 7
Registered: May 2013
Junior Member
This may or may not be the best approach, but I had a similar problem when I was writing my JvmModelInferrer and wanted to print "prettier" Java by using imports and relative classnames.

My solution was to subclass JvmModelGenerator, and override createAppendable(EObject, ImportManager, GeneratorConfig). This gives access to the ImportManager object, on which you can call addImportFor(JvmType) as many times as needed. Don't forget to call super.createAppendable(...) in your overridden method!

Inject your new JvmModelGenerator in your DSL's RuntimeModel with:

public Class<? extends IGenerator> bindIGenerator() {
    return MyCustomJvmModelGenerator.class;
}


Note that this doesn't make XText any more expressive; you could always have written a.b.c.AID. This just lets you write the pattern "import a.b.c.AID"..."new AID()".

Does anyone have a better approach to this?
Re: autoimport with Xbase 2.4 [message #1058912 is a reply to message #1058865] Wed, 15 May 2013 17:37 Go to previous message
Ian McDevitt is currently offline Ian McDevittFriend
Messages: 70
Registered: December 2012
Location: Belfast
Member
Ah, your generated code needs classes which aren't in your DSL. Then I'd need to see your generating code but I think if your model inferrer code has something like this to append the type separately
body = [
 val typeRef = element.newTypeRef(typeof(jade.core.AID))
 append('''new ''')
 append(typeRef.type)
 append('''("Ping",AID.ISLOCALNAME)''')
]

then the append-with-type should trigger the import manager to add your class to its list. If you just append it all as text then that would not happen.

(Note - this code snippet is from memory so it may not compile exactly)
Re: autoimport with Xbase 2.4 [message #1059253 is a reply to message #1058751] Wed, 15 May 2013 08:36 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 05/14/2013 11:44 PM, andrea rossi wrote:
> Thank you so much for your help.It seems to work but i have a problem.
>
> I set implicit import jade.core and if i write this :
> Entity hello {
> val i = 4
> var AID j = new AID()
> }
>
>
> The relative class hello is :
> import jade.core.AID;
>
> @SuppressWarnings("all")
> public class hello {
> public void setup() {
> final int i = 4;
> AID _aID = new AID();
> AID j = _aID;
> }
> }
>
>
> but if i don't use any class of package the relative class hello is
> @SuppressWarnings("all")
> public class hello {
> public void setup() {
> final int i = 4;
> }
> }
>
>
> Now, How can I get import in translate class even if in the my dsl
> editor i don't use any class of import package?

Hi

why would you want an unused import in the generated Java class? :)
the import would be useless.

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it


Previous Topic:A Rule with a list of rules where all entrys are allowed once
Next Topic:IUnitOfWork and undo
Goto Forum:
  


Current Time: Fri Apr 19 20:25:03 GMT 2024

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

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

Back to the top