Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Efficient way of loading an EMF resource at start-up?
Efficient way of loading an EMF resource at start-up? [message #1769187] Thu, 27 July 2017 11:23 Go to next message
Wolfgang Schmidt is currently offline Wolfgang SchmidtFriend
Messages: 8
Registered: April 2017
Junior Member
I want to extend the AST of my DSL with dynamically calculated content. This additional code realize some sort of "build-in" library which is created at start-up time and is used during scoping. I have found information about attaching a resource in standalone-applications but the "build-in" lib should also be loaded in the Eclipse plugin. Loading a static DSL source code file is also not an option because I want to create the lib dynamically.

Here is what I have done so far....

class Lib {

    @Inject Provider<XtextResourceSet> resourceSetProvider

    @Inject IResourceScopeCache cache

    def Resource load() {
        val XtextResourceSet resourceSet = resourceSetProvider.get

        val resource = resourceSet.createResource(URI.createURI("dummy:/lib.mydsl"))
        cache.get("load", resource, [
                val InputStream in = new ByteArrayInputStream(" /* 'dynamic' DSL code ('build-in lib') */".getBytes("UTF8"))
                resource.load(in, resourceSet.getLoadOptions())
                return resource
            ]
        )
    }

    // called by scope provider
    def calc() {
        val resource = load()
        cache.get("calc", resource, [ /* calculations based on the loaded resource */ ]
        )
    }
}


- Is this the correct way of loading an EMF resource at runtime?
- The cache is useless. I have measured the time executing calc() with and without cache and there has been no difference. How can I cache the resource loading and the calculation?
Re: Efficient way of loading an EMF resource at start-up? [message #1769189 is a reply to message #1769187] Thu, 27 July 2017 11:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
can you elaborate on when and how this transformation can be done.
does it need any information from the ast or not?

what is the actual performance problem you face?
why not doing in the constructor / static initializer of the scope provider
or have a singleton eager binding in your module

why do you need to "load" at all if you create it programmatically?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Thu, 27 July 2017 11:43]

Report message to a moderator

Re: Efficient way of loading an EMF resource at start-up? [message #1769192 is a reply to message #1769189] Thu, 27 July 2017 12:58 Go to previous messageGo to next message
Wolfgang Schmidt is currently offline Wolfgang SchmidtFriend
Messages: 8
Registered: April 2017
Junior Member
My application creates a library for the DSL user. Due to performance issues in the Eclipse plugin (typing gets terrible slow for huge files) I want to speed-up things by putting some stuff in a build-in library for which I can pre-calculate some things. The build-in library doesn't depend on other resources (no imports) which (as I understand it correctly) makes it a perfect candidate for caching.

why not doing in the constructor / static initializer of the scope provider
or have a singleton eager binding in your module

> Do you have examples for your suggestions? I have my own scope provider and overloaded the getScope() method. If I put things in the constructor I have the problem that resourceSetProvider gets injected and injected members are null in the constructor.

I want to "load" the build-in lib because so I can handle the build-in library like any other DSL source code and I do not need to re-implement algorithms.
Re: Efficient way of loading an EMF resource at start-up? [message #1769194 is a reply to message #1769192] Thu, 27 July 2017 13:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
i think simply annotation a util with @Singleton should do the trick

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Efficient way of loading an EMF resource at start-up? [message #1769196 is a reply to message #1769194] Thu, 27 July 2017 13:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
adding the libary as a jar to the classapth is not an option?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Efficient way of loading an EMF resource at start-up? [message #1769256 is a reply to message #1769196] Fri, 28 July 2017 10:08 Go to previous messageGo to next message
Wolfgang Schmidt is currently offline Wolfgang SchmidtFriend
Messages: 8
Registered: April 2017
Junior Member
Thanks Christian, @Singleton was a good tip... I now try to integrate the lib-loading in the Activator class. I have derived from the generated MyDslActivator class and extend the start() method by calling my load() method to load the build-in resource. I inject the library by
@Inject extension Lib lib
but this is the problem. The injection mechanism seams to run later and lib is null. How can I solve this problem?
Re: Efficient way of loading an EMF resource at start-up? [message #1769260 is a reply to message #1769256] Fri, 28 July 2017 10:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
no that approach would not involve the activator

in the lib you would simply do the loading on first access or on a method annoted with @Inject.

still my question: why not put a jar with lib files on the classpath?




Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 28 July 2017 10:48]

Report message to a moderator

Re: Efficient way of loading an EMF resource at start-up? [message #1769304 is a reply to message #1769260] Fri, 28 July 2017 22:26 Go to previous messageGo to next message
Wolfgang Schmidt is currently offline Wolfgang SchmidtFriend
Messages: 8
Registered: April 2017
Junior Member
OK, I understand. It works if I use the cache and always call load() in my calc() function but it should not be necessary. The load() function should be called once by Guice so I have rewritten my code. The problem is that the "resourceSet.createResource(..)" call triggers the load() method again and again (if the createResource() call is missing load() is executed once as expected). Do you know why is this the case and how I can circumvent this?

Regarding your question... Yes, creating the library as an external resource would be possible but I think it is cleaner to don't go the extra detour around an external file. It is also makes unit testing easier....

@Singleton
class Lib {

    @Inject Provider<XtextResourceSet> resourceSetProvider

    @Inject IResourceScopeCache cache
    
    var Resource resource

    @Inject
    def void load() {
        val XtextResourceSet resourceSet = resourceSetProvider.get

        resource = resourceSet.createResource(URI.createURI("dummy:/lib.mydsl"))
        val InputStream in = new ByteArrayInputStream(" /* 'dynamic' DSL code ('build-in lib') */".getBytes("UTF8"))
        resource.load(in, resourceSet.getLoadOptions())
    }
}
Re: Efficient way of loading an EMF resource at start-up? [message #1769307 is a reply to message #1769304] Sat, 29 July 2017 05:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
Other Utils that do that manage a loaded / is loading state

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Efficient way of loading an EMF resource at start-up? [message #1769308 is a reply to message #1769307] Sat, 29 July 2017 05:17 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
And maybe you should no load on startup but on first use.
And use a loaded / is loading state


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Efficient way of loading an EMF resource at start-up? [message #1769314 is a reply to message #1769308] Sat, 29 July 2017 09:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
just gave it a try and something like this could work

@Singleton
class Lib {

    @Inject Provider<XtextResourceSet> resourceSetProvider

    @Inject IResourceScopeCache cache
    
    var Resource resource

    def void load() {
        val XtextResourceSet resourceSet = resourceSetProvider.get

        resource = resourceSet.createResource(URI.createURI("dummy:/lib.mydsl"))
        val InputStream in = new ByteArrayInputStream("Hello World!".getBytes("UTF8"))
        resource.load(in, resourceSet.getLoadOptions())
    }
    
    def getAllGreetings() {
    	return (resource.contents.head as Model).greetings
    }
}
class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
	static class LibTypeListener implements TypeListener {
		override <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
			typeEncounter.register(([Object i|var Lib m = (i as Lib) m.load()] as InjectionListener<I>))
		}
	}

	def void configurePostConstructListener(Binder binder) {
		binder.bindListener(new AbstractMatcher<TypeLiteral<?>>() {
			override boolean matches(TypeLiteral<?> typeLiteral) {
				return typeof(Lib).isAssignableFrom(typeLiteral.getRawType())
			}
		}, new LibTypeListener())
	}
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Efficient way of loading an EMF resource at start-up? [message #1769328 is a reply to message #1769187] Sat, 29 July 2017 16:06 Go to previous messageGo to next message
Wolfgang Schmidt is currently offline Wolfgang SchmidtFriend
Messages: 8
Registered: April 2017
Junior Member
This does not work... :( The load() method is called mysteriously again and again. Do you have any idea what is happening behind the curtain. Yes, I could manage a state but I would have a better feeling to know what is going on.
Re: Efficient way of loading an EMF resource at start-up? [message #1769333 is a reply to message #1769328] Sat, 29 July 2017 19:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
Can you give a complete example of what you are doing

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Efficient way of loading an EMF resource at start-up? [message #1769612 is a reply to message #1769333] Wed, 02 August 2017 08:17 Go to previous messageGo to next message
Wolfgang Schmidt is currently offline Wolfgang SchmidtFriend
Messages: 8
Registered: April 2017
Junior Member
I have tried your example above but the load() method gets called, which calls the createResource() and this seams to trigger the load() method again...
Re: Efficient way of loading an EMF resource at start-up? [message #1769613 is a reply to message #1769612] Wed, 02 August 2017 08:19 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
as i said i would need a complete example and a step by step guide

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Defining subtypes without unsigned rule calls
Next Topic:Dependency error while upgrading to 2.12
Goto Forum:
  


Current Time: Sat Sep 18 08:44:14 GMT 2021

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

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

Back to the top