Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Xtext imports
Xtext imports [message #1413972] Fri, 29 August 2014 18:35 Go to next message
Eclipse UserFriend
Does anybody know how to generate some imports or package name in java from dsl but I don't want write imports in my dsl.
For example

from example.mydsl:

Hello Marko!
Hello Michael!

to example.java:

import java.lang.String;
import my.package.*;
import ...

@SuppressWarnings("all")
public class MyGreetings {
public String helloMarko() {
return "Hello Marko";
}

public String helloMichael() {
return "Hello Michael";
}
}


Thanks in advance,

Marko
Re: Xtext imports [message #1414156 is a reply to message #1413972] Sat, 30 August 2014 08:46 Go to previous messageGo to next message
Eclipse UserFriend
Hi,
i am not sure if i understand your question. if you want to do implicit imports in the dsl then
have a look at org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider.getImplicitImports(boolean)

if it is about generating then i do not understand the problem
Re: Xtext imports [message #1417749 is a reply to message #1414156] Fri, 05 September 2014 20:32 Go to previous messageGo to next message
Eclipse UserFriend
Thank you very much Christian,
I use xbase and I tried example from attach but it doesn't work because xtext 2.6 have no LocalVariableScopeContext.
Instead next codes:

@Override
protected IScope createLocalVarScope(IScope parentScope,
LocalVariableScopeContext scopeContext) {
if (scopeContext != null && scopeContext.getContext() != null) {
EObject context = scopeContext.getContext();
if (context instanceof Model) {
Model model = (Model) context;
return Scopes.scopeFor(model.getVarDeclarations());
}
}

return super.createLocalVarScope(parentScope, scopeContext);
}

I wrote:

@Override
protected List<ImportNormalizer> getImplicitImports(boolean ignoreCase) {
List<ImportNormalizer> temp=new ArrayList();
temp.add(new ImportNormalizer(QualifiedName.create("builtin","types","namespace"), true, ignoreCase));
return temp;
}

and

public class HelloXvarsRuntimeModule extends
org.xtext.example.helloxvars.AbstractHelloXvarsRuntimeModule {

@Override
public Class<? extends IScopeProvider> bindIScopeProvider() {
return HelloXvarsScopeProvider.class;
}

}

but it doesn't work. Can I call getImplicitImports(...) maybe in inferr class or something other?
Sorry if my questions are stupid, I'm still a begginer.

Thank you in advance very much,

Marko
  • Attachment: example.txt
    (Size: 0.03KB, Downloaded 312 times)
Re: Xtext imports [message #1417891 is a reply to message #1417749] Sat, 06 September 2014 02:06 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

i think this is about imports and not vars ?!?

first: xbased stuff works totally different from non xbase stuff. xbase uses XImportSectionNamespaceScopeProvider.
there you have a org.eclipse.xtext.xbase.scoping.XImportSectionNamespaceScopeProvider.getImplicitImports(boolean) as well
Re: Xtext imports [message #1418335 is a reply to message #1417891] Sat, 06 September 2014 19:01 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Sory, I only tried example with vars so I can see how scoping works..

I saw org.eclipse.xtext.xbase.scoping.XImportSectionNamespaceScopeProvider.getImplicitImports(boolean)
but I don't know what I need do. I tried some solutions but nothing works.

Best regards,
Marko
Re: Xtext imports [message #1418798 is a reply to message #1418335] Sun, 07 September 2014 13:12 Go to previous messageGo to next message
Eclipse UserFriend
you should test it with something that works with imports from java lang e.g. the domain model example.

i dont know what todo either, but i thought it would work to do

protected List<ImportNormalizer> getImplicitImports(boolean ignoreCase) {
return Lists.<ImportNormalizer>newArrayList(
doCreateImportNormalizer(JAVA_LANG, true, false),
doCreateImportNormalizer(QualifiedName.create("your","package"), true, false),
doCreateImportNormalizer(XBASE_LIB, true, false));
}


but maybe it dont understand your problem in the first place

and it is in deed about generating and not about default imports ?!?
Re: Xtext imports [message #1420429 is a reply to message #1418798] Tue, 09 September 2014 21:15 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

Thank you very much for your answer! I tried it on next way:
in class MyDsl2ScopeProvider.xtend I have:

class MyDsl2ScopeProvider extends org.eclipse.xtext.xbase.scoping.XImportSectionNamespaceScopeProvider
{
override protected List<ImportNormalizer> getImplicitImports(boolean ignoreCase) {
System.out.println("Program is here...###########################")
return Lists.<ImportNormalizer>newArrayList(
doCreateImportNormalizer(JAVA_LANG, true, false),
doCreateImportNormalizer(QualifiedName.create("your","package"), true, false),
doCreateImportNormalizer(XBASE_LIB, true, false));
}
}

In class AbstractMyDsl2RuntimeModule.java instead

public void configureIScopeProviderDelegate(com.google.inject.Binder binder) {
binder.bind(org.eclipse.xtext.scoping.IScopeProvider.class).annotatedWith(Names.named(org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider.NAMED_DELEGATE)). to(org.eclipse.xtext.xbase.scoping.XImportSectionNamespaceScopeProvider.class);
}

I write:
public void configureIScopeProviderDelegate(com.google.inject.Binder binder) {
binder.bind(org.eclipse.xtext.scoping.IScopeProvider.class).annotatedWith(Names.named(org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider.NAMED_DELEGATE)). to(org.xtext.example.mydsl2.scoping.MyDsl2ScopeProvider.class);
}

Program enters in class MyDsl2ScopeProvider in method getImplicitImports (in my console I can see "Program is here...###########################") but in my generated java class have no implicit imports.

Also, I tried and next code in class MyDsl2RuntimeModule.java but unsuccessfully:
public class MyDsl2RuntimeModule extends org.xtext.example.mydsl2.AbstractMyDsl2RuntimeModule {
// @Override
@SuppressWarnings("restriction")
public Class<? extends org.eclipse.xtext.xbase.scoping.XImportSectionNamespaceScopeProvider> getImplicitImports(boolean ignoreCase) {
return MyDsl2ScopeProvider.class;
}
}


Do I need to change my xtext grammar or something else?
Also, importSection doesn't work.

grammar org.xtext.example.mydsl2.MyDsl2 with org.eclipse.xtext.xbase.Xbase

generate myDsl2 "http://www.xtext.org/example/mydsl2/MyDsl2"

Domainmodel:
importSection=XImportSection?
elements+=AbstractElement*;

AbstractElement:
PackageDeclaration | Entity;

PackageDeclaration:
'package' name=QualifiedName '{'
elements+=AbstractElement*
'}';

Entity:
'entity' name=ValidID
('extends' superType=JvmTypeReference)? '{'
features+=Feature*
'}';

Feature:
Property | Operation;

Property:
name=ValidID ':' type=JvmTypeReference;

Operation:
'op' name=ValidID
'('(params+=FullJvmFormalParameter
(',' params+=FullJvmFormalParameter)*)?')'
':' type=JvmTypeReference
body=XBlockExpression;

Sorry for long text,

Thank you in advance,
Marko
Re: Xtext imports [message #1420516 is a reply to message #1420429] Wed, 10 September 2014 00:47 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

i dont understand what you want to do.
the implicit imports makes the stuff available in the dsl. so when you use it, it will be here (if you do the inferrer right)
so you want to hardcode the imports? why do you want to have imports you dont use?!?

so why do you need the import in the generated file????

i asked in the very first place if this is about generation or about imports in the dsl
Re: Xtext imports [message #1420517 is a reply to message #1420516] Wed, 10 September 2014 00:48 Go to previous messageGo to next message
Eclipse UserFriend
P.S. so please explain what you want to do and why (at all) - what is not working if you dont do it. maybe i then can tell how to do it.
Re: Xtext imports [message #1420868 is a reply to message #1420517] Wed, 10 September 2014 11:53 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

Thank you for your answer,
I want to create method onMessage in my generated java file and code for this in infer is:

members += entity.toMethod("onMessage", entity.newTypeRef(void)) [
documentation = entity.documentation
parameters += entity.toParameter("msg", newTypeRef("ACLMessage"))
body = [append('''
if(msg.getPerformative() == Performative.REQUEST){
String s = msg.getContent().toString();
JSONParser parser = new JSONParser();
JSONObject json;
try {
json = (JSONObject) parser.parse(s);
String serviceName = json.get("serviceName").toString();
switch (serviceName){
«FOR sn: entity.services»

case "«sn.service.name»":
...

I want to include next imports that are not include automatically because they are strings..

import xjaf.server.msm.fipa.acl.ACLMessage;
import xjaf.server.msm.fipa.acl.Performative;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

I want to include this imports in generated java file because I don't want to write them every time in dsl.
Sorry, maybe my English is bad but I hope that you will understand this time what is my problem.

Thank you very much,
Marko
Re: Xtext imports [message #1420886 is a reply to message #1420868] Wed, 10 September 2014 12:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

this now shows a very very very different picture. i will have a deeper look this evening oder tomorrow evening.
Re: Xtext imports [message #1420892 is a reply to message #1420886] Wed, 10 September 2014 12:33 Go to previous messageGo to next message
Eclipse UserFriend
the following works for me in the domain model example

			members += entity.toMethod("lala", entity.newTypeRef("java.util.List",entity.newTypeRef("java.lang.String"))) [
				body = '''
				return new «entity.newTypeRef("java.util.ArrayList",entity.newTypeRef("java.lang.String"))»();
				'''
			]
Re: Xtext imports [message #1420903 is a reply to message #1420892] Wed, 10 September 2014 12:59 Go to previous messageGo to next message
Eclipse UserFriend
Am 10.09.14 18:33, schrieb Christian Dietrich:
> the following works for me in the domain model example
>
>
> members += entity.toMethod("lala",
> entity.newTypeRef("java.util.List",entity.newTypeRef("java.lang.String")))
> [
> body = '''
> return new
> «entity.newTypeRef("java.util.ArrayList",entity.newTypeRef("java.lang.String"))»();
>
> '''
> ]
>

With 2.6 and better, you could even use the simple version:

body = '''
return new «ArrayList»<«String»>();
'''

ArrayList and String are class literals in that example (same as
typeof(ArrayList)).

Automatic imports dont work the with lambda body that uses the append
notation, though (as in Markos snippet).

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Xtext imports [message #1421630 is a reply to message #1420903] Thu, 11 September 2014 12:49 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Thank you Christian and Sebastian, I tried your solution and it works for me too but only with java class. When I try with some my class it doesn't work
members += entity.toMethod("onMessage", entity.newTypeRef(void)) [
documentation = entity.documentation
parameters += entity.toParameter("msg", newTypeRef("xjaf.server.msm.fipa.acl.ACLMessage"))
body = [append('''
if(msg.getPerformative() == «entity.newTypeRef("xjaf.server.msm.fipa.acl.Performative")».REQUEST){
String s = msg.getContent().toString();
«entity.newTypeRef("org.json.simple.parser.JSONParser")» parser = new JSONParser();
«entity.newTypeRef("org.json.simple.JSONObject")» json;
...

from this code I get:

public void onMessage(final xjaf.server.msm.fipa.acl.ACLMessage msg) {
if(msg.getPerformative() == JvmParameterizedTypeReference: xjaf.server.msm.fipa.acl.Performative.REQUEST){
String s = msg.getContent().toString();
JvmParameterizedTypeReference: org.json.simple.parser.JSONParser parser = new JSONParser();
JvmParameterizedTypeReference: org.json.simple.JSONObject json;
...

Is there any solution for non java class?


Thank you in advace,

Marko
Re: Xtext imports [message #1421654 is a reply to message #1421630] Thu, 11 September 2014 13:39 Go to previous messageGo to next message
Eclipse UserFriend
hi

that does this make a "non java class"???? is xjaf.server.msm.fipa.acl.Performative inferred from the entity?
if so in the domain model example there is an example how to retrieve the ref then

 it.newTypeRef()
Re: Xtext imports [message #1421754 is a reply to message #1421654] Thu, 11 September 2014 17:22 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

Sorry, it was a misunderstanding, I thought non java class = classes from my java project which I included in build path of xtext project.
I have my java project 'xjaf' and I create xjaf.jar and this xjaf.jar included in build path of xtext project.

xjaf.server.msm.fipa.acl.Performative and AclMessage are classes from my xjaf project.

org.json.simple.JSONObject and org.json.simple.parser.JSONParser are from json-simple-1.1.1.jar and I included it in build path of xtext project too.

I tried some solutions with it.newTypeRef but it doesn't work :/

Best regards,
Marko
Re: Xtext imports [message #1421779 is a reply to message #1421754] Thu, 11 September 2014 18:22 Go to previous messageGo to next message
Eclipse UserFriend
No if it is a real Java class the solution you had first looks ok. But: the class you mention has to be in the classpath Of the project containing the .yourdsl file
Re: Xtext imports [message #1421815 is a reply to message #1421779] Thu, 11 September 2014 19:44 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I already have that classes in .alas file.
If I write
«entity.newTypeRef(org.json.simple.JSONObject)»
and when I create my .alas file I get error.png from attach

for next code
«entity.newTypeRef("org.json.simple.JSONObject")»
I get java code:
JvmParameterizedTypeReference: org.json.simple.JSONObject;

Best regards,
Marko
  • Attachment: Error.png
    (Size: 14.41KB, Downloaded 146 times)
Re: Xtext imports [message #1421971 is a reply to message #1421815] Fri, 12 September 2014 02:01 Go to previous messageGo to next message
Eclipse UserFriend
Hi can you show a screenshot of the project/classpath as well
Re: Xtext imports [message #1422140 is a reply to message #1421630] Fri, 12 September 2014 07:30 Go to previous messageGo to next message
Eclipse UserFriend
Am 11.09.14 18:49, schrieb Marko Markovic:
> body = [append('''
> if(msg.getPerformative() ==
> «entity.newTypeRef("xjaf.server.msm.fipa.acl.Performative")».REQUEST){
> String s = msg.getContent().toString();
>
> «entity.newTypeRef("org.json.simple.parser.JSONParser")» parser = new
> JSONParser();
>
> «entity.newTypeRef("org.json.simple.JSONObject")» json; ..

This doesn't work since you use the explicit tree appendable. There you
have to use a different notation to get imports. I strongly recommend to
use juse the template:

body = '''
if(msg.getPerformative() ==
«entity.newTypeRef("xjaf.server.msm.fipa.acl.Performative")».REQUEST){
String s = msg.getContent().toString();

«entity.newTypeRef("org.json.simple.parser.JSONParser")» parser = new
JSONParser();

«entity.newTypeRef("org.json.simple.JSONObject")» json; ..
'''

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Xtext imports [message #1422193 is a reply to message #1422140] Fri, 12 September 2014 09:17 Go to previous message
Eclipse UserFriend
Hi,

Thank you very very much Sebastian, it works!
Christian, thank you very much too!

Best regards,

Marko
Previous Topic:xtext 2.4.1 in eclipse luna
Next Topic:[2.3.1 to 2.6.2] Migrating TypeProvider and Validator
Goto Forum:
  


Current Time: Wed Jul 23 14:14:01 EDT 2025

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

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

Back to the top