📜 ⬆️ ⬇️

Paradigm of situationally oriented programming

As you know, there are three types of algorithms: linear, branched and cyclic:

image

The basis of all that has been done in programming methodology, including object programming, was the structured programming proposed by Edsger Dijkstra in the 1970s. One of the main ideas was the introduction of block branch operators (IF, THEN, ELSE) and cyclicity (WHILE, FOR, DO, UNTIL, etc.) instead of the problematic GOTO operator, which resulted in an intricate, unreadable “spaghetti code”.
')
For use in intelligent systems, structural programming has a serious drawback. Any intellectual system implies the existence of a learning process — changing one’s behavior through training with a teacher, or as experience accumulates on the basis of one’s own observations. Such a change in behavior should be performed by special metaprogramming tools and ultimately leads to changes in the source code of an intelligent program. The disadvantage of the block paradigm of structured programming lies in the possibility of unlimited nesting of one block into another. Roughly correcting the code of one block can lead to incorrect operation of nested blocks, and sometimes even blocks higher. Such a change in the source code requires a specially trained person (software developer) who, before making changes, is obliged to study not only the code block intended for correction, but also the nested, as well as adjacent blocks.

A special difficulty is created by the “string dependence” of the structured approach to programming. In other words, a change in the value of a certain variable must be made in a certain line of code, since the value of this variable may be related to the values ​​of other variables. Thus, the wrong choice of the string can lead to critical errors, which, moreover, can be non-systemic and difficult to detect.

All this creates certain difficulties for making changes to the source code of the program by an untrained person and makes it almost impossible for the program to change itself.

In the paradigm of situationally oriented programming, an attempt was made to avoid using branching algorithms and loops directly in the source code. Instead, the second and third types of algorithms are put into a separate mechanism for describing situations. The source code, therefore, is only a linear algorithm. The procedure for developing software code is reduced to declaring a set of situations and assigning a handler for each of them.

A situation is an analogue of a conditional expression from a branch or loop algorithm. The situation uniquely describes the state of the values ​​of the program variables. As soon as all the variables described in the situation take the specified values, the situation handler receives control and its source code is executed. A situation handler is a linear algorithm with a very small number of instructions.

This approach allows you to change the handler of a particular situation, regardless of the rest of the code. You do not need to search for a suitable place to change a variable. The system itself takes care of this with the help of a special implementation priorities mechanism. This means that of the two handlers, the one in which the value of the variable changes is first called, after which the handler in which the value of this variable is used is called.

The difference between the slational-oriented approach and the block approach can be seen on an example. Suppose there is a C program that removes all occurrences of the character “C” from the source string “ABCABCABC” and prints the result. The source code of such a program will look something like this:

image

The result of this program will be the line:
ABABAB


- :

image

. i , i S2, i, s[i]!=c. s[0]=='A', s[i]!=c . S2 :

s[j]=s[i];
j=j+1;

S2 S1, i. S1 i :
i=i+1;

i S2 S1. s. , s[i] «C», S2 , s[i]!=c .

, s '\0', S3. S3 , s[i]=='\0' . S3 :
  S1;
s[j]='\0';
printf ("%s\n", s);

, S3 S1. S1 , . . , , , «ABABAB» .

, , , 'C' s. «ABCABAB».

k — '' s, , , k==1:

image

, . -, if (s[i]==c) , FOR. , . FOR, . -, FOR , . -,

k++;
if (k==1) s[j++]=s[i];


. . , .

, , , :

  1. , «C», 'C' s.
  2. , 'C' , .

, , . - :

image

, - :

Source: https://habr.com/ru/post/266829/


All Articles