0

Part of my xText grammar is as follows:

Transition:
   'Transition' from=TransitionTerminal;
TransitionTerminal: StateTerminal|SubStateTerminal;
StateTerminal: 'st' state=[State|ID];
State: 'state' name=ID;

Now, I want to identify Transitions with the same TransitionTerminal as in 'from'. So, in xtend I would write:

var origin = transition.from
//to check 'from' given some other Transition t, I use:
if(origin==t.from) {}

However, the above if statement is never entered. I suppose that additional nesting based on the provided grammar needs to be provided. Any help on how to achieve this is welcome.

  • May this behavior be caused by the usage of == instead of equals? Have you debugged your code and are there really some other 'from' references with the same id? – shillner Jul 22 '14 at 08:27

3 Answers3

1

You may want to try to use EcoreUtil.equals(EObject, EObject) to compare two instances of EObject structurally as in:

if(EcoreUtil.equals(origin, t.from) {}
Sebastian Zarnekow
  • 6,609
  • 20
  • 23
0

You are comparing two instances (transition.from and t.from) that can't be equals because there will always be two different objects generated by Xtext. You have to implements your own comparator.

Here an example of what should be your code:

var origin = transition.from
//to check 'from' given some other Transition t, I use:
if(compare(origin, t.from)) {}

def compare(EObject origin, EObject from) {
    if (origin instanceof State && from instanceof State) {
        return ((State) origin).name == ((State) from).name;
    else if (origin instanceof StateTerminal && from instanceof StateTerminal) {
        ...
    }
    return false;
}

Edit:

Using EcoreUtil.equals(EObject, EObject) as proposed by Sebastian Zarnekow is a better solution.

Fabien Fleureau
  • 693
  • 5
  • 14
0

Wait, I found the problem. There cannot be identical TransitionTerminals in multiple 'from' properties since you are creating new TransitionTerminals for each Transition when writing 'Transition st state ...'.

You will either have to use equality comparison or declare the TransitionTerminal anywhere else and use a real reference to it. Then you should be able to use ==.

shillner
  • 1,806
  • 15
  • 24
  • Please rewrite the above code to reflect your suggestion. Thanks. – user3560285 Jul 22 '14 at 08:44
  • Ok, the equality comparison can easily be done using EcoreUtil.equals(EObject, EObject) as Sebastian Zarnekow stated below. That is one option that doesn'r require a change of your grammar. The second option is to write something like that: Transition: 'Transition' from=[TransitionTerminal]; TransitionTerminal: StateTerminal|SubStateTerminal; StateTerminal: 'st' name=ID 'state' state=[State|ID]; State: 'state' name=ID; With this change you should be able to use the == comparison but option 1 is also valid. It depends on how you want your DSL to look like. – shillner Jul 22 '14 at 09:21