Adding implicitly created objects to model [message #693347] |
Wed, 06 July 2011 09:50 |
Olaf Messages: 3 Registered: July 2011 |
Junior Member |
|
|
Hi!
I am currently developing a language for test data generation for my master thesis at university and I have a few problems with Xtext.
There exists a database with some entity types etc. . All the information about entity types, their attributes, the correspoding database tables/ columns and even the relationships is available to me.
Now for the language:
The user can define entity types (similir to SQL DDL) for later use in the language. But I want to offer the possibility to name i.e. two existing entity types and the runtime system of my language automatically imports all entity types on the "shortest path" between the start and end.
When "running" such a script in eclipse I first convert the Ecore-Model in my own language model. There I can search for those implicitly imported entity types and it all works well.
My solution and the problem:
In later parts of the language (i.e. you want to define integrity constraints) I have to refer to existing or imported entity types.
So I created my own ResourceDescription that searches for all those "implicit" entity types on the shortest path and adds them to the exportedEObjects. To avoid the "dangling object" problem I then add those created objects to the resource's content.
Now from time to time a get some errors, that such an entity type cannot be resolved and the Xtext editor proposes to replace the entity type name by the exact same string - for example I typed CARGO (because CARGO is one of the implicitly imported types) and the editor underlines it red and proposes to replace it by CARGO. When I accept the proposal the red underlining is gone (it returns when I make bigger changes in the file).
From time to time I also get the error message that some types a declared multiple times within the file.
Some code to show how I added the objects. It's from my ResourceDescription which extends the DefaultResourceDescription:
@Override
protected List<IEObjectDescription> computeExportedObjects()
{
[...]
while (allProperContents.hasNext())
{
EObject content = allProperContents.next();
// ignore certain objects
if (objectsToIgnore.contains(content))
continue;
// add objects to ignore list because we handle them ourselves
if (content instanceof ExistingEntityType)
{
existingTypeDeclarations.add((ExistingEntityType) content);
Iterator<EObject> it = EcoreUtil.getAllProperContents(content, false);
while (it.hasNext())
objectsToIgnore.add(it.next());
}
// business as usual
if (!strategy.createEObjectDescriptions(content, acceptor))
allProperContents.prune();
}
// find entity types to be exported
[...]
// export them
for (String entityTypeName : toRead)
{
EntityType et = Utils.getEntityType(entityTypeName);
// Wrap it <------ EntityTypeWrapper implements the EObject interface generated by Xtext
// every entity type has a "singleton" wrapper. it always gets the same wrapper when asking getInstance()
EntityTypeWrapper wrapper = EntityTypeWrapper.getInstance(typeToParent.get(entityTypeName), et);
// Dangling Object prevention
if (!getResource().getContents().contains(wrapper))
getResource().getContents().add(wrapper);
exportedEObjects.add(new EObjectDescription(QualifiedName.create(entityTypeName), wrapper, null));
for (EntityTypeAttribute eta : wrapper.getAttributes())
exportedEObjects.add(new EObjectDescription(QualifiedName.create(et.name, eta.getName()), eta, null));
}
return exportedEObjects;
}
And my question now is:
Is this the right way to do this? Is there any other possibility? because creating my own EObjects to fool Xtext that those entity types exist... well it seems quite strange.
Thanks for your help!
Olaf
|
|
|
|
Re: Adding implicitly created objects to model [message #697126 is a reply to message #696964] |
Fri, 15 July 2011 15:24 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
I am also doing similar things, and the pattern where something is not
valid, and at the same time proposed as a solution seems to be related
to build order.
Probably if you revalidate the resource the error will go away. This
because at that time, the global index is fully populated.
I had a similar case, and the problem was that I had not made Xtext
aware of what was imported and thus, the build order was sometimes
wrong. (I am not using the normal linking).
So - in my case, for the IDE, I use Eclipse projects as containers, and
I sync their dependencies with meta data in the resources using a
builder. This way I get the correct visibility/build order and all the
nice dirty/saved etc. management performed by the out of the box
builder/global state management in Xtext. For headless, I have a
different container manager that solves visibility based on meta data in
resources in a single resource set.
Hope that is of some help to you...
Regards
- henrik
On 7/15/11 10:20 AM, Mirko Raner wrote:
> Hm, looks like a possible solution, though I'm not 100% sure that you
> really need the custom ResourceDescription. Maybe Sven or Sebastian can
> chime in on this...
> At my company we are dealing with a lot of very similar problems (i.e.,
> implicit external models that need to be incorporated into an Xtext
> language). In most cases we just add the external model Resource to the
> ResourceSet in the ScopeProvider for the grammar productions that refer
> to the external model. Xtext pretty much does all the rest that's
> necessary. Would that work for you?
>
> HTH,
>
> Mirko
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.01449 seconds