Confusion about two ways to get resourceDescription [message #1777172] |
Sat, 25 November 2017 16:46 |
serio adamo Messages: 24 Registered: September 2017 |
Junior Member |
|
|
Recent days, I'm creating my DSL with xtext.
My DSL has no package mechanism, all element are global visible in one project.
I found that the default NamesAreUniqueValidator only support check duplicate names in one resource, but I need check duplicate names globally.
So I write my own "NamesAreUniqueValidator" (SmlValidator):
class SmlValidator extends AbstractSmlValidator {
@Inject
IResourceServiceProvider.Registry resourceServiceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE;
@Inject
ResourceDescriptionsProvider resourceDescriptionsProvider;
@Inject
IContainer.Manager containerManager;
@Inject
ISmlNamesAreUniqueValidationHelper helper;
@Check
def checkUniqueNamesInProjectOf(SmlModel smlModel) {
println("checkUniqueNamesInProjectOf SmlType " + smlModel + " resource " + smlModel.eResource)
val Resource resource = smlModel.eResource
val IResourceDescriptions resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(resource)
val IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(resource.getURI());
val IResourceDescription.Manager manager = resourceServiceProvider.getResourceDescriptionManager();
// Issue:
// Two ways to get resourceDescription, whats is the difference???
// If use resourceDescription from manager, it works fine.
val IResourceDescription resourceDescription = manager.getResourceDescription(resource);
// If use resourceDescription from resourceDescriptions, exception thrown when found duplicate names.
// val IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(resource.URI)
val ArrayList<IResourceDescription> outsideVisibleResourceDescriptions = new ArrayList;
for (visibleContainer : containerManager.getVisibleContainers(resourceDescription, resourceDescriptions)) {
for (visibleResourceDesc : visibleContainer.resourceDescriptions) {
if(visibleResourceDesc.URI != resourceDescription.URI){
outsideVisibleResourceDescriptions.add(visibleResourceDesc)
}
}
}
for (eod : resourceDescription.exportedObjects) {
for (ovrd : outsideVisibleResourceDescriptions) {
for (oveod : ovrd.exportedObjects) {
if(eod.name == oveod.name){
helper.createError(eod, eod.EClass, this)
}
}
}
}
}
}
interface ISmlNamesAreUniqueValidationHelper {
def void createError(IEObjectDescription description, EClass clusterType, ValidationMessageAcceptor acceptor)
}
class SmlNamesAreUniqueValidationHelper extends NamesAreUniqueValidationHelper implements ISmlNamesAreUniqueValidationHelper {
// protected to public
override createError(IEObjectDescription description, EClass clusterType, ValidationMessageAcceptor acceptor) {
super.createDuplicateNameError(description, clusterType, acceptor)
}
}
class SmlRuntimeModule extends AbstractSmlRuntimeModule {
override configure(Binder binder) {
super.configure(binder)
binder.bind(ISmlNamesAreUniqueValidationHelper).to(SmlNamesAreUniqueValidationHelper);
}
}
The source code above can works.
But my question is:
There are two ways to get resourceDescription, what is different between
val IResourceDescription resourceDescription = manager.getResourceDescription(resource);
and
val IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(resource.URI)
If I comment
val IResourceDescription resourceDescription = manager.getResourceDescription(resource);
and uncomment
val IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(resource.URI)
After run eclipse, the exception occured when there are some duplicate names in two resources:
0 [Worker-3] ERROR org.eclipse.xtext.validation.CompositeEValidator - Error executing EValidator
java.lang.IllegalArgumentException: You can only add issues for EObjects contained in the currently validated resource 'platform:/resource/test.sml/AAA.sml'. But the given EObject was contained in 'null'
at org.eclipse.xtext.validation.AbstractDeclarativeValidator.checkIsFromCurrentlyCheckedResource(AbstractDeclarativeValidator.java:571)
at org.eclipse.xtext.validation.AbstractDeclarativeValidator.acceptError(AbstractDeclarativeValidator.java:554)
I am very confused about this issue.
Very thanks.
[Updated on: Sat, 25 November 2017 17:11] Report message to a moderator
|
|
|
|
Re: Confusion about get resourceDescription from IReso [message #1777174 is a reply to message #1777173] |
Sat, 25 November 2017 16:59 |
serio adamo Messages: 24 Registered: September 2017 |
Junior Member |
|
|
Christian Dietrich wrote on Sat, 25 November 2017 16:49Adding errors and retrieving stuff from the index are two different things.
=> you should only add errors to the stuff you are currently validating
smlModel and it's children.
Thanks for your answer.
Yes, I understand I should ony add errors to the stuff I am currently validating.
But what is difference between
val IResourceDescription resourceDescription = manager.getResourceDescription(resource);
and
val IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(resource.URI)
Only if I use the second one, the exception occur.
Both two resourceDescription point to the same resource which the smlModel(currently validating) in.
[Updated on: Sat, 25 November 2017 17:08] Report message to a moderator
|
|
|
|
Re: Confusion about get resourceDescription from IReso [message #1777176 is a reply to message #1777175] |
Sat, 25 November 2017 17:30 |
serio adamo Messages: 24 Registered: September 2017 |
Junior Member |
|
|
Hi, Christian,
I like xtext, but I am very confused about its resource management (IResourceDescription, IResourceDescriptions, IResourceDescription.Manager, IResourceServiceProvider, ResourceDescriptionsProvider, IContainer.Manager, and so on...)
It seems to be very different from emf.
My original understanding is
IResourceDescription is a symbol table for one resource.
IResourceDescriptions is global symbol table, it should be the singleton.
Am I right?
But what is IResourceDescription.Manager? why do we need to get IResourceDescriptions from manager instead of IResourceDescriptions?
(For instance, Why IResourceDescription in manager and IResourceDescription in IResourceDescriptions are not the same object when it point to the same resource?)
Is there any document, tutorial or video about xtext resource management?
IResourceDescription, IResourceDescriptions, IResourceDescription.Manager, IResourceServiceProvider, ResourceDescriptionsProvider, IContainer.Manager, etc...
(To be frank, the official documents looks some difficult..)
Very thanks.
[Updated on: Sat, 25 November 2017 17:34] Report message to a moderator
|
|
|
|
Re: Confusion about get resourceDescription from IReso [message #1777178 is a reply to message #1777177] |
Sat, 25 November 2017 17:40 |
|
and
IResourceDescription is a symbol table for one resource.
IResourceDescriptions is global symbol table, it should be the singleton.
thats correct.
and the manager is used by xtext to create the index entries. see e.g. org.eclipse.xtext.builder.clustering.ClusteringBuilderState.writeNewResourceDescriptions(BuildData, IResourceDescriptions, CurrentDescriptions, IProgressMonitor)
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Confusion about get resourceDescription from IReso [message #1777181 is a reply to message #1777180] |
Sat, 25 November 2017 17:48 |
|
yes but: the vaidtor checks in the objects you pass are from the curret resource.
but they are not for entries in the index. they even might be proxies.
so you should use the index entries only to find out which are duplicates and which are not
if you call the manager it will simply wrap the current resource this is why its working.
if you are standing on a street you can walk to the houses you see on a map in your google glasses,
but you cant if you use the same map on a browser at home
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Sat, 25 November 2017 17:50] Report message to a moderator
|
|
|
Re: Confusion about get resourceDescription from IReso [message #1777182 is a reply to message #1777179] |
Sat, 25 November 2017 18:06 |
serio adamo Messages: 24 Registered: September 2017 |
Junior Member |
|
|
Christian Dietrich wrote on Sat, 25 November 2017 17:42and no there is no docs. these compontents go very deep into xtext internas
Is there any way to create a simple test bench which integrated IResourceDescription, IResourceDescriptions, IResourceDescription.Manager, IResourceServiceProvider, ResourceDescriptionsProvider, IContainer.Manager, etc?
For instance, the test bench has a clear entry point (use source code instead of decompile class), and we can simply run and debug it.
I want to dig into xtext source code, but I lack eclipse plug-in development knowledge (So it seems to need a lot of effort) .
[Updated on: Sat, 25 November 2017 18:08] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.07794 seconds