Generics Support

Hello,

i'm relatively new to MPS and i'm trying to write a generator that uses a property of a concept as a parameter in my generic java class.

Thats the code of the generator root template:

public class $Converter extends  AbstractConverter<$COPY_SRC$<FROM>, $COPY_SRC$<TO>>  implements <none> {

...

}

Thats the DSL:

converter MyConverter : transferobject To1 -> transferobject To2

Thats the text generated:

public class MyConverter extends  AbstractConverter<, > {
}

Thats what should be generated:

public class MyConverter extends  AbstractConverter<To1, To2> {
}

Hope someone can help me on this!

Felix

6 comments
Comment actions Permalink

Hi,

I suppose that a wrong thing (or maybe nothing?) is specified inside those $COPY_SRC$ macroses. You should specify what node should be inserted as a result of the macro. In your case, you need a ClassifierType to be returned as a result of $COPY_SRC$. You are likely to copy the source of "transferobject from" - it's a node of what concept (or, in other words, how "transferobjects" are described in your language's structure?)


Maybe it would be better if you could attach the source code of your project. I could say exactly what went wrong then and propose a way to write what you need.

Regards,

Mihail

0
Comment actions Permalink

Hi,

i think the problem is that i want the 'Converter'-generator to insert the name of a child node ('TransferObject') of the 'Converter' concept.

I've attached the project. Thank you for your help!

Felix



Attachment(s):
mps-project.zip
0
Comment actions Permalink

I understood your mistake. You think of MPS as of text editor, and of code generation as of a text transformation. That's wrong.

So, the design of your language should be changed - instead of referencing class by name, you should reference it directly:

Screen shot 2010-07-14 at 6.39.40 PM.png

Editor for that concept should look like:
Screen shot 2010-07-14 at 6.56.22 PM.png

So that you edit the reference by editing the name of referenced object


Generator (I've replaced your class with Object, because I don't have a jar containing that class):

Screen shot 2010-07-14 at 6.58.26 PM.png

You create a node to use here, not just provide some name (what does this name mean for MPS?).

Corrected project in attachment. Generated code:

-------------------

package architektura.sandbox.sandbox;

/*Generated by MPS */


public class MyConverter extends Object<ClassFrom, ClassTo> {
}

---------------------

Regards,

Mihail



Attachment(s):
Screen shot 2010-07-14 at 6.18.40 PM.png
Screen shot 2010-07-14 at 6.39.40 PM.png
Screen shot 2010-07-14 at 6.56.22 PM.png
Screen shot 2010-07-14 at 6.58.26 PM.png
architektura.zip
0
Comment actions Permalink

Maybe you'll need to regenerate the project I've posted because I was editing it in a newer MPS version.

BTW, why do you extend Statement in all your concept? This seems to be unnecessary.

And, I've forgot to mention, you can look at the first copy_src macro. I've used our "quotations" language there (a language for quick node creation). The code means absolutely the same as the code in the second copy_src, but is much more compact

Mihail

0
Comment actions Permalink

Hello Mihail,

your corrections and hints really helped me improve my understanding of MPS. The result are now as expected. Thank you!

Unfortunately if i do some unrelevant changes like changing a name and then generate the language again - the language in the sandbox can't resolve types like 'ClassFrom' anymore.

The DSL in the sandbox changes to the following:

converter MyConverter : transferobject ?object? -> transferobject ?object?

Do you have an idea what's going on here and how to fix it?

Felix



Attachment(s):
screenshot.png
0
Comment actions Permalink

Felix,

What name do you change?

If you change the name of the class, a new name will be shown in test's editor as the reference to class is the same.

If you change a name of a reference in your language's structure - yes, it's a problem - this situation requires refactoring to be performed on your code because references in your model should be changed. For such cases (minor language structure change), there are special refactorings in MPS. In the case of changing a name, the "rename" refactoring should be executed (shift-f6) on a reference, which will change all the references in your models correctly.

BTW, if I understand the screenshot right, you've changed the name of a reference to "class". Though no error is shown in this case, you'd better change the name of a reference. The reference with name="class" will result in a getClass() method generated from it, which will override Java's getClass() method.

If you have any more questions, you are welcome to ask.

Sorry for late reply - I was on vacations.


Good luck,

Mihail

0

Please sign in to leave a comment.