Actor: life is like a play, all depends on acting skills — the design of “three-dimensional” logic programming language (3)


stayLastThis paper introduces the function of logic programming and some concepts in logic programming, including the structure of logic program: facts, rules and problems; The expression of knowledge: predicate calculus, production rules, and the fit relationship between these concepts and three-dimensional (role + scene + time) theory, formally put forward the concept of “three-dimensional logic programming”. In order to better reflect the “three-dimensional” relationship, today we will focus on the role player – actor.

Actually, I’m an actor

The title comes from Stephen Chow’s line in the movie king of comedy. Life is like a play. It all depends on acting. The star says so, why don’t we?

(the picture comes from the Internet, which is infringed and deleted)

In our game of “game life”, Zhang San, a three good man, is a husband at home and a father after having children. He must be a good husband in front of his wife and a good father in front of his children. Similarly, Zhang San must be a good employee when working in the unit. In order to make money and support the family, the boss asked 996 to bear it… Work overtime and go home tired. You have to smile and be a good husband or father. Only in the dead of night can Zhang San be himself. Zhang San wrote a paragraph in a chat group late one night:

Late at night,
I haven't slept yet,
Thinking about writing code for two and a half years,
It's a feeling,
The courage to last,
Very much like 18-year-old himself.
I'm good at cooking, but I still insist,
The crowd is full of great gods I worship,
I don't understand anything,
I still want to be in the group,
Send some emotion
Continue tomorrow!
           -----To junk coders who try to write bugs as hard as I do

 Actor: life is like a play, all depends on acting skills -- the design of Actor: life is like a play, all depends on acting skills -- the design of Actor: life is like a play, all depends on acting skills -- the design of

Actor is the actor

Actor, as the name suggests: actor. In our “three-dimensional” logic program, the word actor is used to represent the native object playing the role. Actor objects need to play a variety of roles, playing different roles in different places (such as at home, in the company) and at different times. Actor is a person in the “social sense”, such as a group driving the will of a person or a group of people, or an organization, company, or a machine. If actor is a person, he will play a role and play many roles in his life. Therefore, the relationship between actor and role is like a person and shadow. As long as he is in the sun, he is always inseparable.

In the current “game life” program, the following example code describes some roles of the two actor objects Zhang San and Diao Chan: Zhang San is an employee and Zhang San is also Diao Chan’s husband; Diao Chan is Zhang San’s wife.

//Create a solid actor object
            Woman Diaochan = new woman () {name = "Diao Chan", birthday = new datetime (1990, 1, 2)};
            Man Zhangsan = new man() {name = "Zhang San", birthday = new datetime (1988, 3, 5)};
            //State the fact: XX is the role of YY
            Worker worker1 = new Worker(zhangsan);
            Wife wife1 = new Wife(diaochan,zhangsan);
            Husband husband1 = new Husband(zhangsan,diaochan);

Note that the above code is only declaring the instance of the actor object and the role fact relationship of the actor object. Here, the constructor of c# object is used to declare a related role for actor object, which will be added to the role set of actor object. But why add a character object with the constructor of the character object instead of the character collection of the actor object? For example, the following code:

zhangsan.Roles.Add(new Worker);

zhangsan.Roles.Add(new Husband(diaochan));

The above code can indeed achieve the effect of actor has a role, but there are several problems:

1. The above code is not as concise as the previous code;

2. The actor object is the main object, and the role object is the subsidiary object of the actor object

3. It is inconvenient to describe the relationship between multiple actor objects and the semantics is not clear.

4. It cannot reflect the predicate relationship in logic programming.

Therefore, to solve these problems, or to understand the characteristics of “three-dimensional” logic programming, we need to deeply understand the meaning of roles and the real subordinate relationship between roles and actors.

Actor’s vest — role predicate

Predicate(Reference definition)It is a word used to describe the nature of individuals and the relationship between individuals. In the current program, the role object defines the properties of objects and the relationship between objects. Therefore, the role here is a predicate, the actor object is the parameter of the predicate object, and a predicate can also represent the relationship between multiple actor object parameters. Therefore, worker, life and husband are predicates and words expressing the appellation of woman / man. In logic programming, the logical relationship between objects is emphasized. This relationship is predicate relationship, and logic programming is predicate calculus. In order to better connect logic programming with “three-dimensional” (role + scene + time) theory, predicates are called role predicates in “three-dimensional” logic programming. The operation of the program depends on the interaction of roles in the system. So what is the role and why is it so important?

A role is a specific relationship

Human sociality is manifested in the sum of various relationships between people. These relationships form a complex network, and role is one of them. This relationship is sometimes unstable and sometimes stable. For example:

  • Employee role: a specific person is currently an employee of a company. The relationship between the employee and the company is unstable and loose. The company can terminate the contract at any time, so the subordinate company object can not be specified when defining the employee role;
  • The role of husband is represented by the family relationship between a man and a woman. This family relationship is stable and strong. Complex legal procedures are required to terminate this relationship (divorce). Therefore, when defining the role of husband, the man object and the woman object must be clearly specified, and the woman object belongs to the South object.
  • The definition of the role of wife is similar to that of husband.

Through these role relationships, our society is organically organized and operates in an orderly manner. In short, a role expresses the relationship between a subject object and one or more dependent objects. If the role relationship is unstable, the dependent object can not be clearly pointed out when it still exists.

A bad name is a bad word

People all over the world pursue fame and wealth. If they want to do things, they must have a name. From marching to war, they need to “start a career and be famous”, to delivering a takeout. Without a suitable job role, they can’t do things honestly and openly. The name that can do something is a role. This name represents an identity, social status and rights.Scientific explanationRole is “the general way of behavior required by a certain social identity and the basis of its internal attitudes and values”. In short, a person must do something in a certain rolePlay a role。 In order to clearly distinguish different roles, we need to give each role a conventional name and an appellation, which is the role predicate.

In the “three-dimensional” logic programming, each role is a unique role class. They all inherit from a role interface, which defines the role name and the role owner. When defining a specific role, you must ensure the uniqueness of the name and the Convention of the name.

interface IRole where T : Actor
        string RoleName { get; }
        T Owner { get; }
        bool MatchRules(string ruleName);

The above interface definition reflects that an interface must have an owner, and the owner must be an actor object. Here, the role owner appears as an attribute of the role object, which also reflects the master-slave relationship between the role and the role owner.

The character is the real master

I said the importance of role. People must play a role to do something, which is the necessity of human sociality. Human society is driven by a complex set of roles. This set of roles is called “courtesy” by Chinese Confucianism. Kings and officials are polite,the three cardinal guides and the five constant virtues as specified in the feudal ethical code, through this system to maintain the stable operation of feudal society. Modern countries have set up a strict hierarchical system. They live and work in this system every two years. For example, there are obvious differences in the social status of people inside and outside the system. Each of us wants to be a civil servant and a big boss. It is these ideal social roles that make each of us look forward to and struggle for them and never stop. Someone calls you, so is it very comfortable for XX president and YY bureau? Therefore, what you call others is not important, what others call you is important, and the role is the real master of each of us.

This paragraph indicates that when describing the relationship between roles and actors, roles should be the main and actors should be the auxiliary. Therefore, in our logic programming, roles are used as predicates, called role predicates, and actor objects are used as parameters of role predicates, expressed in c# as mentioned earlier:

Worker worker1 = new Worker(zhangsan);
            Wife wife1 = new Wife(diaochan,zhangsan);
            Husband husband1 = new Husband(zhangsan,diaochan);

By using the constructor of the role object, it is easier to focus on the construction declaration of the object rather than the construction process. We ignore irrelevant details and pay more attention to the semantics of code expression, which reflects the dependency between role object and actor object.

Although the role is very important for the script to tell stories, no matter how important the role is, it also needs a good actor. The acting skill of an actor is the key to whether he can control the role. Let’s introduce the actor again.

Actor’s acting skills — role use

The talent of an actor

Without actor, the role will be meaningless. As an actor, it is born for the role. Therefore, the actor object needs a built-in character set, which cannot be removed or reset. Only a character can be added or removed from the character set. Each actor object eventually inherits from an actor abstract class, which has a birth time and a name. In this article, it also has a way to play a role, which makes actor naturally have the ability to be an actor.

abstract class Actor
        public Actor()
            Roles = new List>();
            AtTime = DateTime.Now;
        protected internal List> Roles { get; private set; }

        public T ActAs() where T : class,IRole
            foreach (IRole role in Roles)
                if (role is T)
                    return role as T;
            Throw new invalidcastexception ("actor does not have this role:" + typeof (T). Name);

        public DateTime AtTime { get; set; }
        public string Name { get; set; }


No matter how good your acting skills are, you also need roles

The above design of actor abstract class gives actor the ability to play a role. We know that role playing is to implement the method of role definition. Because the role of actor is not stable, the role is available when needed and will be lost at a specific time. Therefore, in design, actor cannot inherit the role interface, and the method of interface should not be directly defined in actor object. So can the actor object directly execute the methods of the character object? That is to say, let the object dynamically add a method / function. This function is not a problem in dynamic type languages (such as JavaScript, python, lisp, etc.), but it is very difficult for static type languages (such as C + +, Java, c#, go, etc.).

The following is an example of JavaScript dynamically adding properties and methods:

var object = new Object(); = "name";
object.age = 19; = function(){
  Console.log ("this is a dynamically added method")
};// Call the new method

However, it is difficult to adopt the dynamic addition method of dynamic type languageExpressed semanticallyWhat is included in calling this methodRole semanticsMore importantly, calling this dynamic method is not guaranteedType safetyFor example, the wrong thing happens when an employee (role) object calls the “pay” method of the boss role. The “three-dimensional” logic programming puts forward the concept of role predicate, and clearly requires the actor object to play the role in the design, and the method of safely executing the role through the role object, which not only reflects the role semantics in the code, but also ensures the type safety, so that the specified role can do the specified things without overstepping, and everyone has clear responsibilities, The system is orderly.

Actor realizes different functions by switching roles. This can switch the role of the current actor object through the actas generic method of the actor object. The actas generic method traverses all the roles in the actor’s current role set. If it is found, it will return this role type. If it is not found, an exception will be thrown. For the detailed code, see [actor’s talent] above.

Recall the example code example in the previous program where the actor object switches the current role to perform the role function through the actas generic method. Since the actor object can be used to switch to the owned role object, there is no need to declare the variables of the role object, which can further show the powerful power of the role predicate:

//Use role predicates to represent the fact relationship new worker (Zhangsan); new Wife(diaochan,zhangsan);             new Husband(zhangsan,diaochan);             // Scene participants begin to play roles
            zhangsan.ActAs().Money += zhangsan.ActAs().Work();

The above code switches roles through the actor object to implement the methods only available to the role object, such as the child-bearing function of the story hero and heroine as a husband and wife function, and the function of the man as an employee role to work and make money. In fact, this is also true. The whole process is very natural to understand.

Good actors need better scripts

Of course, a good actor is an actor who plays a role incisively and vividly. However, to play such a role, an actor has to meet a very good script in addition to his own acting skills. After all, everyone has the opportunity to play the same role. For example, I also want to play the role of boss, but are you a canteen boss or the CEO of the world’s top 500 companies, It depends on the script given to you by the director. There is a way to play a small boss, and there must be a way to play a CEO of a large company. The difference here is the role rules set in the script. For example, the value set for the role of a small boss cannot be greater than 1 million, while the value set for the role of a CEO exceeds 100 million. The CEO can hire 10000 employees, while the small boss has only one employee. Even for the same small boss, some scripts are that the small boss has been running all the way for three or five years to become the CEO of a large company, and some scripts are that the small boss went bankrupt and laid off in less than a year. What determines the fate of the characters in these scripts is the rules set by the screenwriter / Director for the characters.

Therefore, our “game life” script needs to tell a good story, introduce the actors and characters, and focus on itRules for roles, this is the content to be talked about in the next article, and it is also the key content of the “three-dimensional” logic programming language. This part is《Sod framework “enterprise class” application data architecture practice》I have made corresponding introductions. Friends with this book in their hands can have a look at it first.



————–Dividing line————————-

It took more than 2 years to conceive the whole series. I wrote this article for 3 days and deleted it repeatedly. However, limited to my shallow knowledge and ability of programming language, there must be many fallacies. The contents of the previous articles were also criticized by the leaders in the PL field. I would like to thank these leaders for their criticism and suggestions, but compared with the level of the leaders, the leaders are in the sky and I am on the ground, not at an intersection, so it is inevitable that this article and the whole series are criticized by the leaders. I just hope the boss can show mercy. Before clicking on the article [objection], please reply and leave a message detailing your objection. Thank you for your support!

If you only object and don’t reply to the message, I have to treat you as air. Your efforts are meaningless and will be skipped.