[otj-users] How a role captures context from a base object?

Miguel Pessoa Monteiro mmonteiro at di.fct.unl.pt
Wed Jul 2 21:00:03 CEST 2008


Great! :-)
Thanks for the quick and informed reply. It makes it less likely that I'll have to
clutter the list with lots of basic questions.

mpm


Em Qua, Julho 2, 2008 5:35 pm, Marco Mosconi escreveu:
> Hi Miguel,
>
>> I'm taking my first steps on OT/J, using OT/J 1.2.0M3 on top of eclipse 3.4.0. I
>> hope you can bear with some beginner questions.
> You are welcome to ask whatever questions you have.
> Just a sidenote: OTDT 1.2.0 is now final
> (http://www.objectteams.org/#distrib/otdt_1.2.html).
>
>> To keep things focused, I'll stick to one question per post. This one is
>> straightforward: how does a role obtain state from a base object?
> The primary construct for accessing the features of a base object from a
> role is the "callout" binding (§3). To access state (i.e. fields) of its
> base object directly, a role can use "callout to field" bindings(§3.5).
> So, in your example you could write the following to retrieve the value
> of BaseObject._internalNumber:
>
> public class Role playedBy BaseObject {
>      public void doSomething() {
>          // use getter declared in callout binding below:
>          System.out.println("BlackBox base object: " + getBaseNumber());
>      }
>      void doSomething() <- before void trigger();
>      // callout to field _internalNumber of BaseObject:
>      int getBaseNumber() -> get int _internalNumber;
> }
>
> (Note, that this callout makes use of "decapsulation" to allow access to
> the otherwise private field. Of course, instead of the "get"-callout,
> you could also use a method callout to BaseObject.value() in this case.)
>
> Another, more convenient way to directly access fields of the base is to
> make use of "inferred callouts". Just access base fields (or methods)
> from within a role method as if they were locally defined. The compiler
> implicitly maps this to corresponding callouts. Your example then looks
> like:
>
> public class MyRole playedBy BaseObject {
>      public void doSomething() {
>          // use BaseObject._internalNumber directly:
>          System.out.println("BlackBox base object: " + _internalNumber);
>      }
>      doSomething <- before trigger;
> }
>
> In your original example, you tried to directly reference the base
> object itself (not its fields). Note, that this is normally not
> possible. If it is really needed, it can be achieved through "explicit
> lowering" (§2.2.(d)).
> The keyword "base" is only valid in special locations as described in
> (§2.6).
>
> HTH,
> Marco
>
>> I tried the following simple example. I understand that using keyword 'base' is
>> not
>> the way, but it is not clear to me what is the right way to do it.
>> Note that method BaseObject.trigger() is just a way to "call" the team from main,
>> as
>> I'm also uncertain of a suitable way to do that too. I'll come to that in some
>> other
>> post.
>>
>> I looked into the language definition before writing this post, but I may have
>> missed the relevant section. If there is a section that directly answers it, just
>> point it to me.
>>
>> Thanks,
>> Miguel P. Monteiro
>>
>> ----------------------------------------------------------------------------
>>
>> public class BaseObject {
>>    private int _internalNumber = 23;
>>
>>    public int value() {
>>       return _internalNumber;
>>    }
>>    public void trigger() {
>>       System.out.println("trigger");
>>    }
>> }
>>
>> public team class Team {
>>
>>     public class Role playedBy BaseObject {
>>        public void doSomething() {
>>           //Syntax error on token "base", invalid Expression:
>>           System.out.println("BlackBox base object: " + base);
>>        }
>>        void doSomething() <- before void trigger();
>>     }
>>
>> }
>>
>> public class Main {
>>    public static void main(String[] args) {
>>       Team teamObject = new Team();
>>       teamObject.activate();
>>
>>       BaseObject baseObject = new BaseObject();
>>       baseObject.trigger();
>>    }
>> }
>>
>> ----------------------------------------------------------------------------
>>
>
> --
> Dipl.-Inform. Marco Mosconi
> Technische Universität Berlin
> Fak. IV, Fachg. Softwaretechnik
>
>


-- 
Miguel P. Monteiro          | cell phone +351 96 700 35 45
Departamento de Informatica | Phone +351 21 294 8536 ext. 10708
Faculdade Ciencias e Tecnol.| Fax: +351 21 294 8541
Universidade Nova de Lisboa | URL: http://ctp.di.fct.unl.pt/~mpm
2829-516 Caparica, PORTUGAL | e-mail: mmonteiro [at] di fct unl pt




More information about the otj-users mailing list