Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Adding a Fake Resource (like Object in Java)(and a default reference for inheritence)
Adding a Fake Resource (like Object in Java) [message #1105940] Tue, 10 September 2013 13:18 Go to next message
kamo cuvao is currently offline kamo cuvaoFriend
Messages: 11
Registered: July 2013
Junior Member
Hello again!

Recently I managed to add inheritance to my language, but I still have a problem:

I need a "default" class from which all classes, which don't inherit other classes, are inherited (basically an Object class like in Java).

I think I need two Parts to make this work:
(1) Create a resource (with the Object class) and add it to a ResourceSet?
(2) I need a "default reference" to the Object in any class, where no "extends" is present.


To achieve (1) I tried to enhance my XtextResourceSet, but that didn't work:
public class MyResourceSet extends XtextResourceSet {
    public MyResourceSet(){
        Resource resource = this.createResource(URI.createURI("fakepath://Object.tts"));
        Content cont = MyDslFactory.eINSTANCE.createContent();
        cont.setName("Object");
        resource.getContents().add(cont);
        this.getResources().add(resource);
    }
}


with
    public Class<? extends XtextResourceSet> bindXtextResourceSet() {
        return MyResourceSet.class;
    }


To solve (2) I really have no clue how to do this.

Thanks for your answers,
Martin
Re: Adding a Fake Resource (like Object in Java) [message #1106104 is a reply to message #1105940] Tue, 10 September 2013 17:53 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 10/09/2013 15:18, kamo cuvao wrote:
> Hello again!
>
> Recently I managed to add inheritance to my language, but I still have a
> problem:
>
> I need a "default" class from which all classes, which don't inherit
> other classes, are inherited (basically an Object class like in Java).
>
> I think I need two Parts to make this work:
> (1) Create a resource (with the Object class) and add it to a ResourceSet?
> (2) I need a "default reference" to the Object in any class, where no
> "extends" is present.
>
>
> To achieve (1) I tried to enhance my XtextResourceSet, but that didn't
> work:
>
> public class MyResourceSet extends XtextResourceSet {
> public MyResourceSet(){
> Resource resource =
> this.createResource(URI.createURI("fakepath://Object.tts"));
> Content cont = MyDslFactory.eINSTANCE.createContent();
> cont.setName("Object");
> resource.getContents().add(cont);
> this.getResources().add(resource);
> }
> }
>
>
> with
>
> public Class<? extends XtextResourceSet> bindXtextResourceSet() {
> return MyResourceSet.class;
> }
>
>
> To solve (2) I really have no clue how to do this.
>
> Thanks for your answers,
> Martin
>

Hi

I used to do that in one of my first Xtext experiments (FJ,
http://fj-eclipse.sourceforge.net/ ), you may want to see the sources
(though they were developed for Xtext 2.0).

In general though, it is not a good idea to do like that; it is much
better to provide a library for your DSL
(http://zarnekow.blogspot.de/2012/11/xtext-corner-8-libraries-are-key.html),
e.g., including Object.

In one of the examples (SmallJava) of my new book I deal with
inheritance and a library including Object (and how to consider a class
without any 'extends' as an implicit subclass of Object); the code of
the examples is available at

https://github.com/LorenzoBettini/packtpub-xtext-book-examples

hope this helps
cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: Adding a Fake Resource (like Object in Java) [message #1106655 is a reply to message #1106104] Wed, 11 September 2013 11:21 Go to previous messageGo to next message
kamo cuvao is currently offline kamo cuvaoFriend
Messages: 11
Registered: July 2013
Junior Member
Hello Lorenzo!

I looked through your examples and found them very informative (I'm thinking on getting my hands/eyes on your book ^^). Yes you are right that it's better to have a library, but I have to rebuild an existing language, and this language has no libraries.

So for my purpose I found the FJ-Solution better. I managed to implement the LinkingResource, the LinkingService and the ResourceFactory in my own language and its working pretty well. I can create my Object and I can inherit from it (solved Problem (1)), but I still can't solve my problem (2).

If I write
class A { }

it should implicitly extend from Object like
class A extends Object { }


PS: It would also be awesome to create a library which doesn't need to be imported. In your SmallJava example I have to create a (SmallJava)Project to get access to the library.

cheers
Re: Adding a Fake Resource (like Object in Java) [message #1106686 is a reply to message #1106655] Wed, 11 September 2013 12:01 Go to previous message
kamo cuvao is currently offline kamo cuvaoFriend
Messages: 11
Registered: July 2013
Junior Member
Ok nevermind, I just found the implicity in your scope provider... (silly me)

Thanks again!
Previous Topic:Disable project build before xtext generator is run
Next Topic:Best way to reuse xtext grammar in GMF editor
Goto Forum:
  


Current Time: Fri Apr 26 14:16:26 GMT 2024

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

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

Back to the top