Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [XTEXT] Referencing Model Objects from Memory (no Resource associated)
[XTEXT] Referencing Model Objects from Memory (no Resource associated) [message #760007] Wed, 30 November 2011 21:46 Go to next message
Michael Ernst is currently offline Michael ErnstFriend
Messages: 49
Registered: July 2010
Member
Hi,

I want to provide global available functions which should be configured separately. I started as mentioned in the documentation with inheriting from DefaultGlobalScopeProvider and overriding the getScope method (see the snippet below).

In the Editor the auto completion works fine with this setup, but than I get an Error Marker with the following message:
Quote:
The feature 'feature' of '...FeatureCallImpl@ a238d1{platform:/resource/test/test.test#// @ childElements.1/@ assignments.1/@ rightOperand/@ ruleExpression}' contains a dangling reference '...FunctionImpl@ 6a22a{#//}'


Any hints how to fix this.

Thanks in advance!

Kind regards
Michael

   public IScope getScope( Resource resource, EReference reference, Predicate<IEObjectDescription> filter )
   {
      EClass referenceType = getEReferenceType( resource, reference );
      if( EcoreUtil2.isAssignableFrom( CommonPackage.Literals.IDENTIFIABLE_ELEMENT, referenceType ) )
      {
         return new SimpleScope( IScope.NULLSCOPE, FunctionsHolder.DESCRIPTIONS );
      }
      return super.getScope( resource, reference, filter );
   }


The DESCRIPTIONS field is filled using this pattern:
      Function myFirstFunction = globalFactory.createFunction();
      myFirstFunction.setName( "helloWorld" );
      myFirstFunction.setType( commonFactory.createStringType() );
      ...
      DESCRIPTIONS.add( EObjectDescription.create( "helloWorld", myFirstFunction ) );
Re: [XTEXT] Referencing Model Objects from Memory (no Resource associated) [message #760062 is a reply to message #760007] Thu, 01 December 2011 07:06 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

have you considered the "library approach"? See here. If the function can be defined within your language, simply ship it with your dsl plugin.

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: [XTEXT] Referencing Model Objects from Memory (no Resource associated) [message #760067 is a reply to message #760062] Thu, 01 December 2011 07:47 Go to previous messageGo to next message
Michael Ernst is currently offline Michael ErnstFriend
Messages: 49
Registered: July 2010
Member
Thanks for your response. Currently the language does not allow to define functions. Is it also possible to define them in a different language and importing them? Both languages would use the same base ecore classes to be compatible.

Currently the xtext documentation delegates for configuration based global available language elements to the class I inherited from. The documentation doesn't provide a hint how to provide a resource for them so they can be referenced without the mentioned error marker.

Kind regards
Michael
Re: [XTEXT] Referencing Model Objects from Memory (no Resource associated) [message #760068 is a reply to message #760067] Thu, 01 December 2011 07:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13661
Registered: July 2009
Senior Member
Hi,

yes it will of course work cross language

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [XTEXT] Referencing Model Objects from Memory (no Resource associated) [message #760479 is a reply to message #760068] Fri, 02 December 2011 14:05 Go to previous messageGo to next message
Michael Ernst is currently offline Michael ErnstFriend
Messages: 49
Registered: July 2010
Member
Hi,

thanks for your reply. I just created the DSL for defining functions which works fine (autocompletion in the editor and so on).

Because I want to realize functions which have the same name (ID) but different parameters (helloWorld(String), helloWorld(String,String)) I implemented a qualified name provider for the Function DSL. Declaring this functions works fine (no error markers and so on) but when I try to use them in the other DSL I get no autocompletation and error markers that no reference could be resolved. Any ideas are welcome.

public class GlobalQualifiedNameProvider extends DefaultDeclarativeQualifiedNameProvider
{
   @Inject
   private IQualifiedNameConverter converter = new IQualifiedNameConverter.DefaultImpl();

   public QualifiedName qualifiedName( Function function )
   {
      StringBuilder builder = new StringBuilder();
      builder.append( function.getName() );
      builder.append( '.' );
      Iterator<Parameter> iterator = function.getParameters().iterator();
      while( iterator.hasNext() )
      {
         Parameter next = iterator.next();
         builder.append( getType( next.getType() ) );
         if( next.isMultiple() )
         {
            builder.append( "[]" );
         }
      }
      String result = builder.toString();
      QualifiedName qualifiedName = converter.toQualifiedName( result );
      return qualifiedName;
   }

   private String getType( DataType dataType )
   {
      if( dataType instanceof BoolType )
      {
         return "boolean";
      }...


Kind regards
Michael
Re: [XTEXT] Referencing Model Objects from Memory (no Resource associated) [message #760701 is a reply to message #760479] Sun, 04 December 2011 12:14 Go to previous message
Michael Ernst is currently offline Michael ErnstFriend
Messages: 49
Registered: July 2010
Member
Ok, I've got it!
For all who are intrested in, here my solution.
I had to implement my own IQualifiedNameProvider.
I had to override the ID method of DefaultTerminalConverters and my own Linking Service. Here are the missing snippets:
public class MyValueConverterService extends DefaultTerminalConverters
{
   @Inject
   private AbstractIDValueConverter idValueConverter;

   public class FeatureCallValueConverter implements IValueConverter<String>
   {
      @Override
      public String toValue( String string, INode node ) throws ValueConverterException
      {
         return idValueConverter.toValue( string, node );
      }

      @Override
      public String toString( String value ) throws ValueConverterException
      {
         if( value.contains( "." ) )
         {
            String[] split = value.split( "\\." );
            return split[0] + "()";
         }
         return value;
      }
   }

   private FeatureCallValueConverter valueConverter = new FeatureCallValueConverter();

   @Override
   public IValueConverter<String> ID()
   {
      return valueConverter;
   }
}

public class MyLinkingService extends DefaultLinkingService
{
   @Override
   public List<EObject> getLinkedObjects( EObject context, EReference ref, INode node ) throws IllegalNodeException
   {
      List<EObject> linkedObjects = super.getLinkedObjects( context, ref, node );
      if( linkedObjects.isEmpty() && context instanceof FeatureCall
         && ( (FeatureCall) context ).isExplicitFunctionCall() )
      {
         IScope scope = getScope( context, ref );
         Iterable<IEObjectDescription> allElements = scope.getAllElements();
         // find the best matching function based on name and parameters and the parameters type
         EObject result = bestMatch( (FeatureCall) context, allElements, node );
         if( result != null )
         {
            return Lists.newArrayList( result );
         }
      }
      return linkedObjects;
   }
...


Thanks for all replies.

Kind regards
Michael
Previous Topic:importURI relative to reference in resource
Next Topic:API doc for xtext
Goto Forum:
  


Current Time: Fri Nov 27 15:03:10 GMT 2020

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

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

Back to the top