Access a reference's feature [message #786638] |
Mon, 30 January 2012 17:07 |
|
Hi,
I hope the following description and code snippets makes my problem clear. I have a ForceDefinition:
ForceDefinition:
keyword=ForceDefKeyword
name=STRING
(
side=[SideDefinition|STRING]
& attributes+=Attribute*
& attributes+=EnemyAttribute*
& attributes+=UnitAttribute*
& attributes+=SatelliteAttribute*
)
=>EndKeyword
;
Which, as you saw above, can reference a SideDefinition:
SideDefinition:
keyword=SideDefKeyword
name=STRING
attributes+=Attribute*
=>EndKeyword
;
I test things out with DSL text that looks like this:
Plan "hierarchy"
SideDef "Good guys"
End
SideDef "Bad guys"
End
ForceDef "Good force"
Side "Good guys"
End
End
I am accessing Xtext's global index in my Java code, and I manage to obtain an EReference from a ForceDefinition instance:
for (IEObjectDescription eObjectDesc :
desc.getExportedObjectsByType(TplXPackage.Literals.FORCE_DEFINITION))
{
// Force's name?
QualifiedName qName = eObjectDesc.getQualifiedName();
String forceName = qName.getLastSegment();
// Does this Force reference a Side?
String sideName = null;
for (EReference eRef : eObjectDesc.getEClass().getEReferences())
{
if (eRef.getName().equals("side"))
// TODO: Wish I could access the name of this side...
}
}
How can I achieve the TODO above? What approach would allow me to determine the name of the SideDefinition being referenced?
|
|
|
|
|
Re: Access a reference's feature [message #787441 is a reply to message #787179] |
Tue, 31 January 2012 15:32 |
|
Meinte,
I expanded your one-line suggestion and found that the call to eGet(...) returns null:
// Does this Force reference a Side?
String sideName = null;
for (EReference eRef : eObjectDesc.getEClass().getEReferences())
{
if (eRef.getName().equals("side"))
{
EObject objOrProxy = eObjectDesc.getEObjectOrProxy();
Object obj = objOrProxy.eGet(eRef); // <-- returns null
SideDefinition sideDef = (SideDefinition)obj;
sideName = sideDef.getName();
}
}
I'm not sure if this is what Sebastian was referring to when he said, "the proxy which has no values set (usually)."
Sebastian,
Thanks for the advice - I obtained an XtextResourceSetProvider the following way:
Class MyClass {
...
@Inject
private XtextResourceSetProvider xtextResourceSetProvider;
...
public MyClass ()
{
Injector injector =
TplXActivator.getInstance().getInjector("com.exoanalytic.seas.tplx.TplX");
injector.injectMembers(this);
}
...
}
And used the following code to obtain the SideDefinition referenced by a ForceDefinition:
ResourceSet xtextResourceSet = xtextResourceSetProvider.get(currentProject);
for (IEObjectDescription eObjectDesc :
desc.getExportedObjectsByType(TplXPackage.Literals.FORCE_DEFINITION))
{
ForceDefinition forceDef = (ForceDefinition)
xtextResourceSet.getEObject(eObjectDesc.getEObjectURI(), true);
SideDefinition referencedSideDef = forceDef.getSide();
String referencedSideName = referencedSideDef.getName();
...
}
Thanks much for the replies!
|
|
|
Powered by
FUDForum. Page generated in 0.03123 seconds