Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.cplire.ru/Lab144/start/e_concur.html
Дата изменения: Mon Sep 24 15:02:27 2007 Дата индексирования: Tue Oct 2 03:37:43 2012 Кодировка: |
An Actor Prolog program may consist of concurrent processes. They interact between themselves with the help of messages. Unlike the classical object-oriented computing model there are two types of messages in the Actor Prolog language. The processes in an Actor Prolog program are strictly asynchronous; in other words they never interrupt to provide information exchange. Processes synchronization absence providing by the language results in an inconvenient programming paradigm. Its main idea may be described in the following way. In the case when necessary data are not ready, a process interrupts according to traditional models of concurrent programming. But an Actor Prolog program process does not wait all required data appearing. Instead of this it fulfills calculations with the data that is in its disposal to current moment. For example, if the process is to output a sum of a list of numbers that are not obtained yet, the result is the sum of empty list or the sum of an uncompleted list. Every time later when the process will receive an updated list it correspondently changes the sum value on the screen. In other words every time after receiving new information the process will repeat the proving of several actors in order to modify the logical inference and make it correspondent to the new outer conditions. The advantages of the Actor Prolog programming paradigm described above are the following:
Of course, the higher level programming means causes additional spending. In particular:
The processes and messages in Actor Prolog are designated very simply. Let us begin from the processes designation. Processes designationProcess is a class instance whose clauses are executed concurrently with clauses of other processes. If one is going to declare the process s/he needs simply to write double brackets instead of single ones in the corresponding class instance constructor. Let us look at the P1.A example that is situated in the Concur directory. Example 1. Processes designation.------------------------------------------- -- An example of Actor Prolog program. -- -- (c) 2002, Alexei A. Morozov, IRE RAS. -- -- A definition of one process. -- ------------------------------------------- project: (('MyClass')) ------------------------------------------- class 'MyClass': -- con = ('Report'); -- [ Message:- con ? show, con ? writeln( "I have received " "a message:"), con ? set_color('Blue'), con ? writeln(Message), con ? set_color('Black'). ] ------------------------------------------- As a matter of fact we have already considered all the elements of this program above. Goal statement of the program declares the process that is the 'MyClass' class instance. The 'MyClass' class has the slot con, the value of that is an instance of the 'Report' predefined class that implements the text windows management. The clause of the class is written with using of language meta-construct which we have not described yet. This construct means that every predicate called in the world 'MyClass' can be unified with the variable Message in the clause header. Hence the clause will be called while reception of any beforehand unknown message and will print the message received in the text window with blue color. For example, during the process creation the goal predicate that is called automatically will be printed in the window. |
Fig. 1. Printing of messages in the text window.
We shall use this process as an elementary brick during explanation of information transmission mechanisms between processes in Actor Prolog. There are distinguished several states of processes. They depend of their actors and several other things that we shall describe later. There are two main states of a process:
Process state is changed in the result of processing the messages that were sent to it.
Flow messagesThe flow messages are the most simple ones from two message types used in Actor Prolog. The flow messages are the data being transmitted via processes common variables. During this the processes common variables simply play the role of logical actors' common variables that belong to different processes. That is common variable change made by some process causes repeated proving of several actors in the processes linked to this variable. Let us look at the Flow1.A example in the Concur directory. Example 2. Transmission of flow message.------------------------------------------- -- An example of Actor Prolog program. -- -- (c) 2002, Alexei A. Morozov, IRE RAS. -- -- Transmission of flow message. -- ------------------------------------------- project: (('Flow1')) ------------------------------------------- The 'Flow1' class instance contains two processes: p1 and p2. The first of them is the 'Receiver' class instance and the second is the 'Sender' class instance. The processes are connected with the variable V. The flow message will be transmitted with the help of it. Note that the protecting keyword is used in the definition of the process that sends the message. This keyword means that the value of the port output of the sending process is to be protected from change by other processes the ports of which are not declared protecting one. So, the data will be transmitted strictly in one direction form the process p2 to the process p1. class 'Flow1': -- p1 = (('Receiver', input=V)); p2 = (('Sender', protecting: output=V)); -- [ goal. ] ------------------------------------------- The 'Receiver' class instance has also a simple structure. The actor corresponding to the goal predicate of this class prints the value of the slot input in the text window. Hence any change of this slot value causes a repeated proving of the actor and new output in the text window. class 'Receiver': -- input; -- con = ('Report'); -- [ goal:- con ? write("Common variable= "), con ? set_color('Blue'), con ? writeln(input), con ? set_color('Black'). ] ------------------------------------------- The 'Sender' class instance fulfills the only action. The goal predicate that is called automatically during this class instance initialization assigns the "Message" text string to the slot output. class 'Sender': -- output; -- [ goal:- output== "Message". ] ------------------------------------------- The processes and the common variable that connects them can be depicted graphically in the following way: |
Fig. 2. Graphical depiction of flow messages.
Now let us execute the program and observe the results on the display. |
Fig. 3. Flow message transmission.
Note that the process p1 runs not waiting for data accepting from the process p2. That is why first of all it printed the # value. The # constant is used in Actor Prolog as an initial value of processes common variables and for other auxiliary purposes. After the common variable V change performed by the process p2, the process p1 fulfilled the repeated proving of its actor that printed the new value of the common variable. Completing the description of flow messages let us note that this type of messages has the following properties by their nature:
The second type of messages is direct ones. They are used in Actor Prolog for implementation of the information transfer with buffering. Direct messagesDirect messages is an asynchronous analogue of the ordinary method call of the classical object-oriented computing model. Direct message is a call of one process clause from another one:
One can outline two types of direct messages, depending of processing rules of received message: the switching direct messages and the informational direct messages. They differ in the following:
The second important difference between the switching direct messages and the informational direct messages is the following one:
Let us look at the Direct1.A example of the direct message transmission between the processes that is situated in the Concur directory. Example 3. Transmission of direct message.------------------------------------------- -- An example of Actor Prolog program. -- -- (c) 2002, Alexei A. Morozov, IRE RAS. -- -- Transmission of direct message. -- ------------------------------------------- project: (('Direct1')) ------------------------------------------- The 'Direct1' class instance contains two processes: p1 and p2. As in the previous example, the first of them is the 'Receiver' class instance and the second one is the 'Sender' class instance. The world p1 is passed as a parameter to the constructor of the world p2 in order to make the process p2 sending it direct messages. class 'Direct1': -- p1 = (('Receiver')); p2 = (('Sender', target=p1)); -- [ goal. ] ------------------------------------------- The 'Receiver' class accepts any direct messages and prints them in the text window. class 'Receiver': -- con = ('Report'); -- [ Message:- con ? show, con ? writeln( "I have received " "a message:"), con ? set_color('Blue'), con ? writeln(Message), con ? set_color('Black'). ] -------------------------------------------
The 'Sender' class sends two direct messages. For sending switching direct messages the " class 'Sender': -- target; -- [ goal:- target << p(1,2,3), target <- q(7,8,9). ] ------------------------------------------- For graphical depiction of switching direct messages and informational direct messages we recommend to use the following symbols: |
Fig. 4. Graphical depiction of direct messages.
The program will show on the display the following: |
Fig. 5. Direct messages transmission.
Note that the switching message was processed by the receiving process earlier than the direct one. The switching direct messages have higher priority than the informational direct messages in Actor Prolog. It is necessary to note also that the flow messages have higher priority than the direct messages. Now we are finishing the description of the processes and the messages in Actor Prolog. We shall describe several another language means linked with concurrency and data transfer between processes in the next chapters. |
![]() |
Table of content |
![]() |