0

i'm working on the following domain :

my domain diagram

i want to express the folowing constraint : " a succession of two actions of Type Rotate is not allowed " i tried this declaration but eclipse is not recognizing indexOf(element ) :

class Choreography
{
    property actions : Action[+|1] { ordered composes };
    attribute name : String[?];

    /*Succession of two actions of Type is not permitted */

    invariant rotate_succ:
    self.actions->asSequence()->forAll(a1:Action,a2:Action
        |

        a1.oclIsTypeOf(Rotate) and (indexOf(a1)=indexOf(a2)+1) implies  
        not a2.oclIsTypeOf(Rotate)
    )


    ;

Does anyone have an idea about how to work with the index of a random element from an ocl colletion ?

Marwa
  • 1

1 Answers1

0

The

OrderedCollection(T):indexOf(obj : OclAny[?]) : Integer[1]

operation requires an OrderedCollection (Sequence/OrderedSet) as its source and an OclAny as its argument. You have not identified a source so OCL will consider first an implicit iterator leading to an

a1.indexOf(a1)
a2.indexOf(a1)

ambiguity which would be an error if an Action had an indexOf operation. Then it considers an implicit self which also fails since there is no Choreography.indexOf() operation.

I presume you meant

self.actions->indexOf(a1)

etc etc, or more readably put self.actions in a let-variable for multi-use.

(Use of oclIsTypeOf is very rarely right. Use oclIsKindOf unless you have a specific intent.)

(self.actions is already an OrderedCollection so there is no need for the asSequence()).

Your use of indexOf will lead to quadratic performance. Better to use indexes - something like:

let theActions = self.actions in
    Sequence{1..theActions->size()-1}->forAll(index |
      theActions->at(index).oclIsKindOf(Rotate)
      implies not theActions->at(index+1).oclIsKindOf(Rotate))
Ed Willink
  • 1,205
  • 7
  • 8
  • Thank you so much ! actually that was my error i should have used the synthax you have mentionned : self.actions->indexOf(a1) now it's working with it ! – Marwa Dec 25 '19 at 12:57