Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.cplire.ru/Lab144/start/e_actors.html
Дата изменения: Mon Sep 24 15:02:27 2007 Дата индексирования: Tue Oct 2 03:37:27 2012 Кодировка: Поисковые слова: trees |
The best way to explain what the logical actors are, is to consider an example. Let us consider the Actors.A program that I commonly show in the beginning of lectures on Actor Prolog. This program creates several logical actors, each of that writes messages in a separate text window. All the windows have different colors. The actors are linked by two common variables i and j. Any change of the values of these variables causes the repeated proving of the actors and some new output into the text windows. Moreover, the variables i and j are connected to the edit controls of the dialog box. So, a user can directly interact with the actors by changing the content of the edit controls.
Please load the program and press the |
Fig.1. An example of logical actors (the Actors.A program).
Please enter any text into the edit controls I and J. The program will react to each change of these variables. Note that one does not need the programming of event handling for the implementation of such a behavior of an Actor Prolog program, in contrast to other languages of a lower level. All the updates of the variables i and j are transferred directly to the logical actors and cause a repeated proving of them. Let us consider the source text of the program now and investigate the principles of its work. Example. The idea of logical actors.------------------------------------------- -- An example of Actor Prolog program. -- -- (c) 2002, Alexei A. Morozov, IRE RAS. -- -- The principle of logical actors. -- ------------------------------------------- project: (('Main')) ------------------------------------------- class 'Main' specializing 'Dialog': -- identifier = "input"; x = 27; y = 8; -- i; j; -- w1 = ('TextPage', value_1=i, value_2=j, text_color='White', background_color='Red', x=9, y=4); The 'Main' class of the program is a descendant of the 'Dialog' predefined class. The 'Dialog' class is intended for the control of dialog boxes. There are some special slots defined in the 'Main' class. The identifier, x, and y slots define the format and the coordinates of the dialog box on the screen (we will consider a special file containing the definition of the format of this dialog box later). The i and j slots are common variables linking the actors of the program with the environment. The w1 - w7 slots (see some of them below) contain instances of the 'TextPage' class, where logical actors "live". Note that each constructor of an instance of the 'TextPage' class has different colors and screen coordinates. The values i and j are transmitted into the worlds w1 - w7, but the value j is not transmitted into some of them. Thus, the value_2 slots of the worlds w2, w4, and w6 are unbound variables, disconnected with the j slot of the world 'Main'. w2 = ('TextPage', value_1=i, text_color='White', background_color='Orange', x=27, y=1); w3 = ('TextPage', value_1=i, value_2=j, text_color='Black', background_color='Yellow', x=45, y=4); w4 = ('TextPage', value_1=i, text_color='Black', background_color='Green', x=47, y=9); w5 = ('TextPage', value_1=i, value_2=j, text_color='Black', background_color='Cyan', x=41, y=12); w6 = ('TextPage', value_1=i, text_color='White', background_color='Blue', x=13, y=12); w7 = ('TextPage', value_1=i, value_2=j, text_color='White', background_color='Violet', x=7, y=9); -- [ The goal predicate calls the show method of the 'Dialog' predefined class that creates a dialog box on the screen. goal:- show. --
The action predicate is an ordinary event handler. The "stop" event occurs if one press the action("stop"):- hide, w1 ? hide, w2 ? hide, w3 ? hide, w4 ? hide, w5 ? hide, w6 ? hide, w7 ? hide. ] -------------------------------------------
The 'TextPage' class is a descendant of the 'Report' predefined class implementing a control of text windows. There are several output predicates, namely 'write', 'writeln', etc., in the 'Report' class (press the class 'TextPage' specializing 'Report': -- width = 27; height = 10; -- value_1; value_2; -- [ goal:- write_value("i",value_1), write_value("j",value_2). -- write_value(_,#):-!. write_value(Name,V):- writeln(Name,"= ",V). ] -------------------------------------------
Now it is the time to explain how the actors appear in the instances of the 'TextPage' class? In accordance with the semantics of the language, each automatic call of the goal predicate (in the course of building of a new instance of a class) automatically creates a new actor. Thus, each instance of any class has its own goal actor that checks given logical conditions and reacts to any change of given common variables. In the example under consideration the goal actor simply writes values of the value_1 and value_2 slots into corresponding text window. Note that the program does not write unbound values of the variables (which could be unified with the An update of the variable i causes a reaction of the actors corresponding to all the worlds w1 - w7, while the update of the variable j causes only the repeated proving of the actors that belong to the separate worlds w1, w3, w5, and w7. The reason of this behavior is the disconnection of the variable j from the worlds w2, w4, and w6. Thus, the control strategy of Actor Prolog provides repeated proving of separate actors linked with updated common variables only. At the same time it keeps the results of the proving of all other actors. Actor Prolog enables a use of habitual event-driven programming (as one could see in the case of handling the dialog button event). However the language supports some different, high level principles of programming of man-machine interface as well. The fundamental difference between the repeated proving of actors and the handling of events one could demonstrate by executing the example under consideration on some very slow computer. In this case the actors will not have enough time to write all intermediate values of the common variables if user types characters quickly enough. So, only the final value and some intermediate values will be written in the windows. This operational semantics of the program is in absolute conformity with the declarative ones. The work of Actor Prolog is to keep a logical proof in the conformity with recent updates of the environment. Therefore Actor Prolog can omit processing of events that are not necessary for getting the end results. Let us consider the definition of the format of the dialog box now (see the Actors.DLG file). Of course, the syntax of this file bears no relation to the syntax of Actor Prolog because it is a special feature of given implementation of the language only. Now the player automatically loads a file containing definitions of dialogs, if the file has the same name as a source file of a program but with the extension DLG and it is placed in the same directory. The file containing the definition of the dialog box.grid(80,25) window_font("Arial",17,[bold]) dialog_font("Arial",12,[]) dialog "input" ( "Title",default,default,default, centered,centered,default) vbox(center) table row column "I= " end_of_column column edittext(i,10,1,"") end_of_column end_of_row row column "J= " end_of_column column edittext(j,10,1,"") end_of_column end_of_row end_of_table button("stop","&Exit") end_of_vbox end_of_dialog Here is an explanation for the definitions:
|
![]() |
Table of content |
![]() |