Документ взят из кэша поисковой машины. Адрес оригинального документа : http://www.cplire.ru/Lab144/start/e_concur.html
Дата изменения: Mon Sep 24 15:02:27 2007
Дата индексирования: Tue Oct 2 03:37:43 2012
Кодировка:

Поисковые слова: storm
Actor Prolog User Guide. Concurrent Processes.

Concurrent Processes

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:

  1. Concurrent programming is simplifying significantly. Here one may notice the similarity with the graphical user interfaces (GUI) development history. Event-oriented programming appearing has made it possible to simplify GUI development radically and brought this field of programming on a new quality level. Logical actors are the one more step higher means than event-oriented programming.
  2. As a result some new areas where logic programming may be used correctly from the mathematical point of view have appeared. For example an Actor Prolog program logic semantics does not depend on if it is executed on a single computer (where it is possible to provide finite process expecting time) or on Internet where an information may never cover the way from one node to another.

Of course, the higher level programming means causes additional spending. In particular:

  1. Processes synchronization absence and repeated proving of actors cause an increasing of total amount of calculations and processor (processors) load.
  2. The usage of asynchronous processes requires some change of programmer thinking. Again the analogy with the GUI development is appropriated here. Some time required for people to agree that unpredictability of messages reception in event-oriented programming is a blessing but not a problem and that it is necessary to turn attention from the operations with the screen objects to the objects themselves.
  3. Computing model based on the asynchronous processes and repeated proving generally does not conform to the low level programming means. Some time ago the absence of free access to the computer ports from the applications working in a graphical shell appeared unpleasant surprise for many programmers. Stream input/output and modal dialogs do not harmonize with the ideology of Actor Prolog in exactly the same way. Nevertheless text windows, file management, dialog boxes, and other low level means are implemented in the predefined classes of the language. All these means may be used for software development though the programmer is to understand that they violating the language logical semantics. That is programmer is to take on himself a part of responsibility for correctness of program work results. For example, if the program is to output a list of numbers in a text window, do not forget to clear the window before. It is quite possible that logical actor proof that prints the text will be fulfilled once more. Hence one cannot be sure that nothing has been printed in the window before. Let us note that modeless dialogs in the Actor Prolog ideology play the role of low level input/output means that corresponds to the logical semantics of the language.

The processes and messages in Actor Prolog are designated very simply. Let us begin from the processes designation.

Processes designation

Process 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:

  1. The "proved" state means that all the process actors are proved successfully and there are no any logical contradictions between them.
  2. The "failed" state means that several process actors proof failed or even never took place.

Process state is changed in the result of processing the messages that were sent to it.

Flow messages

The 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:

  1. The flow messages are the only mechanism of information transmission "from one process to many" and even "from many processes to many". A common variable may connect any number of processes, and the flow message will be transmitted to all the processes connected with this variable.
  2. The information transfer via common variable cancels the preceding value of this variable. So, the flow message fulfills the cancellation of the flow message sent earlier in the case when receiving process has not processed it yet. Common variables are the channels of information transfer without buffering.

The second type of messages is direct ones. They are used in Actor Prolog for implementation of the information transfer with buffering.

Direct messages

Direct 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:

  1. Direct messages are transmitted "from one to one processes".
  2. Direct messages always reach receiving process. So, direct messages are data transmission method with buffering.

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:

  1. The switching direct messages switch receiving process to the "failed" state if repeated proving of actors called by them resulted with a failure.
  2. Informational direct messages never switches the process to the "failed" state. If the repeated proof of actors resulted with failure, then informational direct message is simply cancelled. And the process remains in its former state.

The second important difference between the switching direct messages and the informational direct messages is the following one:

  1. Processing of switching direct messages takes place independently of the current state of receiving process, whether it is "proved" or "failed" one.
  2. Processing of informational direct messages is delayed if receiving process is in "failed" state.

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 "<-" infix is used in Actor Prolog. And for informational direct messages sending the "<<" infix is used.

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