Using the index with an hierarchically directory tree [message #902781] |
Mon, 20 August 2012 12:09 |
Tobias Mayer Messages: 8 Registered: August 2012 |
Junior Member |
|
|
Hello everybody,
I have some questions about the index in xtext and how to use it correctly.
In my case, an element is referable if it is on the same level or above in the directory tree (not beside / not under).
Here is a little example to better describe my concern:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "..."
Model:
greetings+=element*;
element:
Greeting
| RefGreetings
;
Greeting:
'Hello' name=ID '!';
RefGreetings:
'Ref' el = [Greeting];
The directory tree is as follows:
root
|---subdir---file2.mydsl
|
|---file1.mydsl
The file file1.mydsl contains the following code:
The file file2.mydsl contains the following code:
It shall be possible now to extend the file file2.mydsl to:
But it is not allowed to extend the file file1.mydsl to:
To get this behavior, I have extended the classes ResourceDescriptionsBasedContainer and SimpleResourceDescriptionsBasedContainerManager (the sourcecode is at the end of this topic). An IEObjectDescription and an IResourceDescription contain an URI. The code beneath uses a method "visible(URI candidate, URI base)" to determine whether an IEObjectDescription or an IResourceDescription is visible to the current regarded IResourceDescription.
I'm pretty sure that something is wrong with my solution because I will get the following message when testing the code:
An internal error occurred during: "Xtext validation".
java.lang.UnsupportedOperationException
My questions are:
Is this the right way to get the desired behavior?
Does anyone know how to do it correctly?
Best Regards,
Tobias
public class MySimpleResourceDescriptionsBasedContainerManager extends
SimpleResourceDescriptionsBasedContainerManager {
@Override
public IContainer getContainer(IResourceDescription desc, IResourceDescriptions resourceDescriptions) {
MyResourceDescriptionsBasedContainer result = new MyResourceDescriptionsBasedContainer(desc, resourceDescriptions);
result.setUriToDescriptionCacheEnabled(false);
return result;
}
@Override
public List<IContainer> getVisibleContainers(IResourceDescription desc, IResourceDescriptions resourceDescriptions) {
return Collections.singletonList(this.getContainer(desc, resourceDescriptions));
}
}
public class MyResourceDescriptionsBasedContainer extends ResourceDescriptionsBasedContainer {
protected IResourceDescription desc;
@Override
public Iterable<IEObjectDescription> getExportedObjects() {
List<IEObjectDescription> filtered = Collections.emptyList();
for (IEObjectDescription od : super.getExportedObjects())
{
if (this.visible(od.getEObjectURI(), desc.getURI()))
{
filtered.add(od);
}
}
return filtered;
}
@Override
public Iterable<IEObjectDescription> getExportedObjectsByType(EClass type) {
List<IEObjectDescription> filtered = Collections.emptyList();
for (IEObjectDescription od : super.getExportedObjectsByType(type))
{
if (this.visible(od.getEObjectURI(), desc.getURI()))
{
filtered.add(od);
}
}
return filtered;
}
@Override
public Iterable<IEObjectDescription> getExportedObjectsByObject(EObject object) {
List<IEObjectDescription> filtered = Collections.emptyList();
for (IEObjectDescription od : super.getExportedObjectsByObject(object))
{
if (this.visible(od.getEObjectURI(), desc.getURI()))
{
filtered.add(od);
}
}
return filtered;
}
@Override
public Iterable<IEObjectDescription> getExportedObjects(EClass type, QualifiedName qualifiedName, boolean ignoreCase) {
List<IEObjectDescription> filtered = Collections.emptyList();
for (IEObjectDescription od : super.getExportedObjects(type, qualifiedName, ignoreCase))
{
if (this.visible(od.getEObjectURI(), desc.getURI()))
{
filtered.add(od);
}
}
return filtered;
}
public MyResourceDescriptionsBasedContainer(IResourceDescription desc, IResourceDescriptions descriptions) {
super(descriptions);
this.desc = desc;
}
@Override
public Iterable<IResourceDescription> getResourceDescriptions() {
List<IResourceDescription> result = Collections.emptyList();
for (IResourceDescription element : getUriToDescription().values())
{
if (visible(element.getURI(), this.desc.getURI()))
{
result.add(element);
}
}
return result;
}
@Override
public int getResourceDescriptionCount() {
int counter = 0;
for (IResourceDescription element : getUriToDescription().values())
{
if (visible(element.getURI(), this.desc.getURI()))
counter++;
}
return counter;
}
@Override
public boolean hasResourceDescription(URI uri) {
return visible(uri, this.desc.getURI());
}
@Override
public IResourceDescription getResourceDescription(URI uri) {
if (hasResourceDescription(uri)){
return getUriToDescription().get(uri);
}
}
/* Common Methods */
protected boolean visible(URI candidate, URI base)
{
boolean result = false;
List<String> uriBase = getMyDirectory(base);
List<String> uriCandidate = getMyDirectory(candidate);
if (uriCandidate.size() <= uriBase.size()) {
int limit = uriCandidate.size();
boolean equal = true;
int i = 0;
for (i = 0; i < limit && equal; i++) {
String str1 = uriBase.get(i);
String str2 = uriCandidate.get(i);
equal = str1.equals(str2);
}
result = equal;
}
return result;
}
protected List<String> getMyDirectory(URI paramUri) {
List<String> pathStr = paramUri.segmentsList().subList(0,
paramUri.segmentsList().size() - 1);
return pathStr;
}
}
[Updated on: Mon, 20 August 2012 12:19] Report message to a moderator
|
|
|
Re: Using the index with an hierarchically directory tree [message #903082 is a reply to message #902781] |
Tue, 21 August 2012 20:47 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi Tobias,
I'd try to bind a custom IContainer.Manager to achieve the described
visibility rules, too. You're probably hit by
https://bugs.eclipse.org/bugs/show_bug.cgi?id=372041
Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Am 20.08.12 14:09, schrieb Tobias Mayer:
> Hello everybody,
>
> I have some questions about the index in xtext and how to use it correctly.
> In my case, an element is referable if it is on the same level or above
> in the directory tree (not beside / not under).
>
> Here is a little example to better describe my concern:
>
>
> grammar org.xtext.example.mydsl.MyDsl with
> org.eclipse.xtext.common.Terminals
>
> generate myDsl "..."
>
> Model:
> greetings+=element*;
>
> element:
> Greeting
> | RefGreetings
>
> ;
>
> Greeting:
> 'Hello' name=ID '!';
>
> RefGreetings:
> 'Ref' el = [Greeting];
>
>
> root
> |---subdir---file2.mydsl
> |
> |---file1.mydsl
>
>
> The file file1.mydsl contains the following code:
> Hello test1!
>
> The file file2.mydsl contains the following code:
> Hello test2!
>
> It shall be possible now to extend the file file2.mydsl to:
>
> Hello test2!
> Ref test1
>
> But it is not allowed to extend the file file1.mydsl to:
>
> Hello test1!
> Ref test2
>
> To get this behavior, I have extended the classes
> ResourceDescriptionsBasedContainer and
> SimpleResourceDescriptionsBasedContainerManager (the sourcecode is at
> the end of this topic). An IEObjectDescription and an
> IResourceDescription contain an URI. The code beneath uses a method
> "visible(URI candidate, URI base)" to determine whether an
> IEObjectDescription or an IResourceDescription is visible to the current
> regarded IResourceDescription.
>
> I'm pretty sure that something is wrong with my solution because I will
> get the following message when testing the code:
>
> An internal error occurred during: "Xtext validation".
> java.lang.UnsupportedOperationException
>
>
> My questions are:
>
> Is this the right way to get the desired behavior?
> Does anyone know how to do it correctly?
>
> Best Regards,
>
> Tobias
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05525 seconds