Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to access index from wizard (without eObject)
How to access index from wizard (without eObject) [message #1800819] Tue, 08 January 2019 12:59 Go to next message
Eclipse UserFriend
Hi,

I would like to implement a new entity wizard that provides semantic help to the user (autocompletion, etc...). In order to access the index I need an EObject, which I don't have at this point - while I have the user's current selection (a file or directory), I just use the selection to compute the Eclipse Project, the user is currently working in.

What is the correct way to access the index in this case? Should I search the file strucuture in order to find a file in the current project that stores an Entity and then load this Entity from the file? What if there is not such file (the project could, e.g., be brand new and empty)?

Is there anything I might be missing?

Thanks in advance!
Re: How to access index from wizard (without eObject) [message #1800820 is a reply to message #1800819] Tue, 08 January 2019 13:00 Go to previous messageGo to next message
Eclipse UserFriend
hi, can you please gice more detauls.
to access index you ususally simply @inject Ibuilderstate

[Updated on: Tue, 08 January 2019 13:01] by Moderator

Re: How to access index from wizard (without eObject) [message #1800822 is a reply to message #1800820] Tue, 08 January 2019 13:25 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

I usually access the index like this:

	@Inject
	var ResourceDescriptionsProvider rdp
	
	@Inject 
	IContainer.Manager cm

	private def getVisibleEObjectDescriptions(EObject o, EClass type) {
		val tmp = o.getVisibleContainers.map [ c |
			c.getExportedObjectsByType(type)
		]
		return tmp.flatten
	}

	private def getVisibleContainers(EObject o) {
		if (o?.eResource === null)
			return emptyList
		val index = rdp.getResourceDescriptions(o.eResource)
		val rd = index.getResourceDescription(o.eResource.URI)
		if (rd === null)
			return Collections.emptyList
		cm.getVisibleContainers(rd, index)
	}


So later I access getVisibleEObjectDescriptions when I an searching whether or not an Entity with a known name exists.

How is the IBuilderState different from a ResourceDescriptions?

Edit: Of course the problem here is that in order to use getVisibleEObjectDescriptions() I need an eObject first.

[Updated on: Tue, 08 January 2019 13:26] by Moderator

Re: How to access index from wizard (without eObject) [message #1800824 is a reply to message #1800822] Tue, 08 January 2019 13:54 Go to previous messageGo to next message
Eclipse UserFriend
OK, now that I look at my code again it becomes apparent that I actually do not need an eObject - a org.eclipse.emf.ecore.resource.Resource would suffice.

However, that still does not help me too much as I don't know how to get a matching ressource. :/



Re: How to access index from wizard (without eObject) [message #1800831 is a reply to message #1800824] Tue, 08 January 2019 15:05 Go to previous messageGo to next message
Eclipse UserFriend
IBuilderState implements IResourceDescriptions.

am not sure what you need containers for if you dont have a project?
and if you have at least one file in that project
(otherwise index would be empty)

[Updated on: Tue, 08 January 2019 15:07] by Moderator

Re: How to access index from wizard (without eObject) [message #1800832 is a reply to message #1800831] Tue, 08 January 2019 15:41 Go to previous messageGo to next message
Eclipse UserFriend
Let me clarify:

1. I *do* have an IProject. What I would like to do is to get the index (= an IResourceDescriptions instance) in order to query whether or not an Entity of a given qualified name exists.
2. I need the containers in order to check for visibility - I use the classpath approach proposed in your book.
3. As far as I know, I need a Resource for achieving 1. and 2., but I do not have one, as my code runs whenever the user runs the "new Entity"-Wizard. He might even do this in an empty project, i.e., there does not have to be a single Xtext file in the project yet.

So, what is the best way to get a Resource / ResourceSet that matches the project's contents? Option a) would be to traverse the project and search for the first Entity-File there is. However, this approach seems unelegant to me and it would not work in case there does not exist any Entity-File in the project yet.
Is there an Option b)? Maybe retrieving the matching ResourceSet / Resource from the Project's URI?

Thanks for your help.
Re: How to access index from wizard (without eObject) [message #1800834 is a reply to message #1800832] Tue, 08 January 2019 16:15 Go to previous messageGo to next message
Eclipse UserFriend
What about simply using the uri of any resource in that project?
Re: How to access index from wizard (without eObject) [message #1800840 is a reply to message #1800834] Tue, 08 January 2019 16:32 Go to previous messageGo to next message
Eclipse UserFriend
You mean any Eclipse Resource (= IFile, IProject, ...) or any Xtext / EMF Resource? Because there might not be any Xtext / EMF Resources in the project *yet*.

Would using the URI of an - say - IProject or IFile work? I mean - if I injected an ResourceSetProvider end created a new ResourceSet and then created a resource for any given URI from the IProject (say a folder or the project itself) - would XText be able to cope with that? After all, probable the created resource would not be an XText resource...
Re: How to access index from wizard (without eObject) [message #1800841 is a reply to message #1800840] Tue, 08 January 2019 16:37 Go to previous messageGo to next message
Eclipse UserFriend
well. the main problem is: there is no proper api for your usecase.
what happens if you call it with a fake uri / resource desciption in the project?

IProject p = file.getProject();
//TODO make this be in a source folder if you have a java projct
URI fakeUri = URI.createURI("platform:/resource/"+p.getName()+"/dummy.mydsl");
ResourceSet resourceSet = rsp.get(p);
Resource fakeResource = resourceSet.createResource(fakeUri);
IContainer container = cmgr.getContainer(rdmgr.getResourceDescription(fakeResource), index);
System.err.println(container);
for (IContainer vc : cmgr.getVisibleContainers(rdmgr.getResourceDescription(fakeResource), index)) {
System.err.println(vc);
}
return null;
}
@Inject
IBuilderState index;

@Inject
IContainer.Manager cmgr;

@Inject
IResourceDescription.Manager rdmgr;

@Inject
IResourceSetProvider rsp;

[Updated on: Tue, 08 January 2019 16:57] by Moderator

Re: How to access index from wizard (without eObject) [message #1800844 is a reply to message #1800841] Tue, 08 January 2019 17:17 Go to previous messageGo to next message
Eclipse UserFriend
This code works if I use an entity-File, but throws an java.lang.reflect.InvocationTargetException if I use an IProject. Any other ideas?

	@Inject
	var Provider<XtextResourceSet> rsp

	def toResource(IResource proj) {
		val rset = rsp.get
		val uri = URI.createPlatformResourceURI(proj.getFullPath().toString(), true)
		val res = rset.getResource(uri, true)
		return res
	}


Edit: Did not see your Update in time. Will try your code tomorrow. :)

[Updated on: Tue, 08 January 2019 17:24] by Moderator

Re: How to access index from wizard (without eObject) [message #1800877 is a reply to message #1800844] Wed, 09 January 2019 06:04 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

thanks a lot, the code below no (kinda) works. There remain two issues though:
1. When trying to access the dummy file, an exception is thrown which will be logged by class DefaultResourceDescription. I would at least like to prevent the error from being logged, as it is expected.
Caused by: org.eclipse.core.internal.resources.ResourceException: Resource '/Demo/dummy.dsl' does not exist.
	at org.eclipse.core.internal.resources.Resource.checkExists(Resource.java:335)
	at org.eclipse.core.internal.resources.Resource.checkAccessible(Resource.java:209)
	at org.eclipse.core.internal.resources.File.getContents(File.java:275)
	at org.eclipse.core.internal.resources.File.getContents(File.java:268)
	at org.eclipse.emf.ecore.resource.impl.PlatformResourceURIHandlerImpl$WorkbenchHelper.createPlatformResourceInputStream(PlatformResourceURIHandlerImpl.java:207)
	... 65 more

2. The exception is thrown, because the Container created is a DescriptionAddingContainer, which delegates to a StateBasedContainer. I think the whole error could be prevented, if I could create a StateBasedContainer directly, i.e., without nesting it in a DescriptionAddingContainer, as the "added" description is a dummy anyway. Any ideas how to achieve this? This would also fix problem 1.
3. Not a problem but a question: You proposed to use a IBuilderState instead of an IResourceDescriptions (I know that IBuilderState *is* an IResourceDescriptions). In your book it is proposed to use IResourceDescriptions - what is the difference? Which one should be used? To retrieve an IResourceDescriptions from a ResourceDescriptionsProvider you need a Resource - why, if an IBuilderState does not require a resource? I am a bit at a loss here.

Thanks for your great support!


Code:
	@Inject
	var Provider<XtextResourceSet> rsp
	
	@Inject
	IBuilderState index;

	@Inject
	IContainer.Manager cmgr;
	
	@Inject
	IResourceDescription.Manager rdmgr;

	def searchIndexByType(IResource ires, EClass type) {
		val rset = rsp.get
		val uri = URI.createURI("platform:/resource/"+ires.project.getName()+"/dummy.dsl");
		val res = rset.createResource(uri)
		
		val IContainer projCont = cmgr.getContainer(rdmgr.getResourceDescription(res), index);
		System.err.println("projCont: " + projCont);
		
		val vcs = cmgr.getVisibleContainers(rdmgr.getResourceDescription(res), index)
		for (IContainer vc : vcs) {
			System.err.println("visible: " + vc);
		}
		val List<IEObjectDescription> result = newArrayList
		for (vc : vcs) {
			try {
				val descrs = vc.getExportedObjectsByType(type).toList
				result.addAll(descrs)
			} catch (Exception e) {
				// do nothing
				println("ERROR")
			}
		}
		
		return result
	}
Re: How to access index from wizard (without eObject) [message #1800878 is a reply to message #1800877] Wed, 09 January 2019 06:10 Go to previous messageGo to next message
Eclipse UserFriend
can you provide complete stacktrace?
Re: How to access index from wizard (without eObject) [message #1800882 is a reply to message #1800878] Wed, 09 January 2019 06:47 Go to previous messageGo to next message
Eclipse UserFriend
hmmm i am rather clueless then :(
as i said before this is currrently not a use case.
can you open an enhancement request at

github.com/eclipse/xtext-core
Re: How to access index from wizard (without eObject) [message #1800885 is a reply to message #1800882] Wed, 09 January 2019 06:52 Go to previous messageGo to next message
Eclipse UserFriend
maybe you can use a mock iresourcedescription instead of a real one
Re: How to access index from wizard (without eObject) [message #1800886 is a reply to message #1800885] Wed, 09 January 2019 07:16 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

please find the full stack trace of the inner exception below.

Mocking the dummy IResourceDescription is a good idea, I will try that.

I will also open an enhancement request.

Do you have an opinion on the IBuilderState question? Should I use this to access the index rather than using a ResourceDescriptionsProvider to retrieve an IResourceDescriptions for a given resource?

Thanks again!

org.eclipse.core.internal.resources.ResourceException: Resource '/Demo/dummy.dsl' does not exist.
	at org.eclipse.core.internal.resources.Resource.checkExists(Resource.java:335)
	at org.eclipse.core.internal.resources.Resource.checkAccessible(Resource.java:209)
	at org.eclipse.core.internal.resources.File.getContents(File.java:275)
	at org.eclipse.core.internal.resources.File.getContents(File.java:268)
	at org.eclipse.emf.ecore.resource.impl.PlatformResourceURIHandlerImpl$WorkbenchHelper.createPlatformResourceInputStream(PlatformResourceURIHandlerImpl.java:207)
	at org.eclipse.emf.ecore.resource.impl.PlatformResourceURIHandlerImpl.createInputStream(PlatformResourceURIHandlerImpl.java:525)
	at org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl.createInputStream(ExtensibleURIConverterImpl.java:360)
	at org.eclipse.xtext.resource.XtextResourceSet$1.createInputStream(XtextResourceSet.java:298)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1269)
	at org.eclipse.xtext.resource.impl.DefaultResourceDescription.computeExportedObjects(DefaultResourceDescription.java:73)
	at org.eclipse.xtext.resource.impl.DefaultResourceDescription$4.get(DefaultResourceDescription.java:179)
	at org.eclipse.xtext.resource.impl.DefaultResourceDescription$4.get(DefaultResourceDescription.java:173)
	at org.eclipse.xtext.util.OnChangeEvictingCache.get(OnChangeEvictingCache.java:76)
	at org.eclipse.xtext.resource.impl.DefaultResourceDescription.getLookUp(DefaultResourceDescription.java:173)
	at org.eclipse.xtext.resource.impl.AbstractResourceDescription.getExportedObjectsByType(AbstractResourceDescription.java:40)
	at org.eclipse.xtext.resource.containers.DescriptionAddingContainer.getExportedObjectsByType(DescriptionAddingContainer.java:73)
	at com.company.myTool.util.IndexUtil.searchIndexByType(IndexUtil.java:191)
	at com.company.myTool.dsl.ui.wizards.NewEntityWizardPage.initialize(NewEntityWizardPage.java:213)
	at com.company.myTool.dsl.ui.wizards.NewEntityWizardPage.createControl(NewEntityWizardPage.java:156)
	at org.eclipse.jface.wizard.Wizard.createPageControls(Wizard.java:176)
	at org.eclipse.jface.wizard.WizardDialog.createPageControls(WizardDialog.java:688)
	at org.eclipse.jface.wizard.WizardDialog.createContents(WizardDialog.java:580)
	at org.eclipse.jface.window.Window.create(Window.java:426)
	at org.eclipse.jface.dialogs.Dialog.create(Dialog.java:1096)
	at org.eclipse.jface.window.Window.open(Window.java:783)
	at com.company.myTool.dsl.ui.handlers.NewEntityHandler.execute(NewEntityHandler.java:31)
	at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:291)
	at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:92)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:305)
	at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:239)
	at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
	at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:152)
	at org.eclipse.core.commands.Command.executeWithChecks(Command.java:494)
	at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:487)
	at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
	at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:431)
	at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.handleWidgetSelection(AbstractContributionItem.java:446)
	at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.lambda$2(AbstractContributionItem.java:472)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1150)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1039)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
	at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:680)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:151)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1472)
Re: How to access index from wizard (without eObject) [message #1800891 is a reply to message #1800886] Wed, 09 January 2019 08:12 Go to previous messageGo to next message
Eclipse UserFriend
well the main problem is: there are way to many usecases.
with open / dirty editors it even becomes worse
Re: How to access index from wizard (without eObject) [message #1800899 is a reply to message #1800891] Wed, 09 January 2019 09:08 Go to previous message
Eclipse UserFriend
OK, thank you. I will keep the IResourceDescriptions version then, and maybe try out IBuilderState later.

I also tried Mocking the ResourceDescription and now it works better (no more ErrorLogs). See below for the code, but its trivial.

Thanks again, you've been tremendously helpful!

		val dDesc = rdmgr.getResourceDescription(res)
		val dummyDesc = new IResourceDescription() {
			override getExportedObjects() { dDesc.getExportedObjects }
			
			override getImportedNames() {
				dDesc.getImportedNames
			}
			
			override getReferenceDescriptions() {
				emptyList
			}
			
			override getURI() {
				dDesc.getURI
			}
			
			override getExportedObjects(EClass type, QualifiedName name, boolean ignoreCase) {
				emptyList
			}
			
			override getExportedObjectsByObject(EObject object) {
				emptyList
			}
			
			override getExportedObjectsByType(EClass type) {
				emptyList
			}
			
			override isEmpty() {
				dDesc.isEmpty
			}
		}
Previous Topic:Delay of Content Assist Autoactivation
Next Topic:is it possible to override TaskMarkerContributor?
Goto Forum:
  


Current Time: Tue Jun 17 04:43:31 EDT 2025

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

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

Back to the top