Expert system developed by eresye (for demonstration of toys)

Source: Internet
Author: User

Erlang has a group of "Generals", one of which is eresye, a specialized "Expert System engine ".

Eresye is a light cavalry, well-equipped and armed with high strength.

Built-in functions exported by eresye are about 10. The expert system framework can be built at an extremely fast speed, and the Rete algorithm makes the system run at high speed.

Recently, I learned the power of eresye, and I have to write down my feelings. First, let's make a summary. Second, I want to see if I have understood it. I have always thought that, to be clear, the first is to make a simple explanation of complicated problems, and the second is to use black and white words.

Eresye is an expert system model. Although it is very simple and belongs to the toys demonstration class and has no practical value, it shows the working principle and powerful functions of eresye.

This system simulates copyright experts and provides consultation and judgment on whether the work is copyrighted. The system consists of the inference engine, knowledge base, and inference rules. The inference engine is built-in by eresye and can be used for use. The knowledge base is a set of asserted facts derived from the provisions of China's copyright law. The reasoning rules are used based on fact knowledge and users' answers, draw a conclusion.

There is no academic theory. In fact, there is no need for too much theory to program, unless you want to scare people. For programming, it is best to use the program code directly. The following is the source code of toys.

%

% C_f_2.erl

%

-Module (c_f_2 ).

-Export ([start/0,

Determine_legal/2,

Determine_applicable1/4,

Determine_applicable2/4,

Determine_applicable3/4,

Determine_works1/4,

Determine_works2/4,

Determine_works3/4,

Determine_works4/4,

Determine_works5/4,

Determine_works6/4,

No_copyright/2

]).

Ask_yn (prompt)->

P = prompt ++ "(y/N ):",

[Response | _] = IO: get_line (P ),

Case response

$ Y-> true;

_-> False

End.

% Knowledge (fact) Library

Add_works (ENGINE)->

Eresye: assert (engine, {works, 1, "music, drama, music, dance, acrobatics "}),

Eresye: assert (engine, {works, 2, "Art, architectural works "}),

Eresye: assert (engine, {works, 3, "photography "}),

Eresye: assert (engine, {works, 4, "movie works and works created in a method similar to a video production "}),

Eresye: assert (engine, {works, 5, "engineering design drawing, product design drawing, map, and other graphic works and model works "}),

Eresye: assert (engine, {works, 6, "computer software "}).

Add_not_applicable (ENGINE)->

Eresye: assert (engine, {not_applicable, 1, "laws and regulations, resolutions, decisions, commands, and other files with legislative, administrative, and judicial nature of State organs, and its official translations "}),

Eresye: assert (engine, {not_applicable, 2, ""}),

Eresye: assert (engine, {not_applicable, 3, "calendar, general number table, general table and formula "}).

% Inference rules

Determine_legal (engine, {start, PID})->

Add_not_applicable (engine ),

Add_works (engine ),

Case ask_yn ("/N: Files prohibited from being published or disseminated by law? ")

True->

IO: Format ("~ N conclusion: illegal work, no copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

False->

Eresye: assert (engine, {legal, OK })

End.

Determine_applicable1 (engine, {start, PID}, {legal, OK}, {not_applicable, 1, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has no copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_applicable2 (engine, {start, PID}, {legal, OK}, {not_applicable, 2, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has no copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_applicable3 (engine, {start, PID}, {legal, OK}, {not_applicable, 3, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has no copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_works1 (engine, {start, PID}, {legal, OK}, {works, 1, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_works2 (engine, {start, PID}, {legal, OK}, {works, 2, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_works3 (engine, {start, PID}, {legal, OK}, {works, 3, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_works4 (engine, {start, PID}, {legal, OK}, {works, 4, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_works5 (engine, {start, PID}, {legal, OK}, {works, 5, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

Determine_works6 (engine, {start, PID}, {legal, OK}, {works, 6, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

No_copyright (engine, {start, PID})->

Eresye: retract (engine, {start, PID }),

IO: Format ("~ N conclusion: the work has no copyright ~ N "),

PID! OK.

% Program start and stop

Start ()->

IO: format ("% % ~ N "),

IO: Format ("% ~ N "),

IO: Format ("% determine whether the file has copyright %% ~ N "),

IO: Format ("% ~ N "),

IO: format ("% % ~ N ~ N "),

Eresye: Start (c_f_2 ),

[Eresye: add_rule (c_f_2 ,{? Module, x}, 10) |

X <-[

Determine_legal,

Determine_applicable1,

Determine_applicable2,

Determine_applicable3

],

[Eresye: add_rule (c_f_2 ,{? Module, x}, 0) |

X <-[

Determine_works1,

Determine_works2,

Determine_works3,

Determine_works4,

Determine_works5,

Determine_works6

],

Eresye: add_rule (c_f_2 ,{? Module, no_copyright},-10 ),

Eresye: assert (c_f_2, {start, self ()}),

Receive

_-> OK

End,

IO: Format ("~ N ~ N ...... Consultation Ended ......~ N "),

Eresye: Stop (c_f_2 ).

Next, let's make some explanations.

1. The module name c_f_2 refers: Copyright_forward_chaing_version_2 indicates that the copyright expert system uses the forward inference method version 2nd.

Ii. Export Module:-Export ([start/0,…]). The full inference rule function must be written; otherwise, it will not be used during compilation.

Iii. First parameter of knowledge base and inference rule FunctionIt must be a variable that represents the name of the inference engine. Otherwise, an error occurs during running, for example:

Add_not_applicable (ENGINE)->

Eresye: assert (engine, {not_applicable, 1, "laws and regulations, resolutions, decisions, commands, and other files with legislative, administrative, and judicial nature of State organs, and its official translations "}),

......

4. Start and Stop the system

1. eresye: Start (c_f_2). The process is enabled. The parameter is the module name;

2. eresye: add_rule (......), Add Rules to the inference engine;

3. eresye: assert (......), Add knowledge (FACTS) to the inference engine;

4. Receive

_-> OK

End, which is equivalent to a terminable loop. The termination condition is that the process sends a signal;

5. eresye: Stop (c_f_2). process stopped. The parameter is the module name;

V. inference engine operation mechanism

The operation task of the expert system is carried out in the rule body. To run a rule, you must first activate it. The activation condition is that all the modes of the Rule header match. For example:

Determine_works6 (engine, {start, PID}, {legal, OK}, {works, 6, a})->

Case ask_yn (A)

True->

IO: Format ("~ N conclusion: the work has copyright ~ N "),

Eresye: retract (engine, {start, PID }),

PID! OK;

_-> OK

End.

To activate this rule and execute it, you must first give three fact assertions {start, PID}, {legal, OK}, {works, 6, }.

That is to say, when the program eresye: assert has these three facts, they enter the knowledge base and the inference engine automatically activates this rule.

Eresye: assert (......) To activate the rule.

The first condition for activating the system rule is eresye: assert (c_f_2, {start, self ()}) in the START () function. After this asserted, the inference engine starts to run, the first activation rule is determine_legal (engine, {start, PID }).

There are new fact assertions during the execution of the activated rule, so other rules will be activated for execution.

This type of fact data (premise)-oriented reasoning is called forward chain reasoning and forward reasoning.

6. Control the rule activation sequence

If fact assertions activate multiple rules at the same time and the rule execution must be sequential, you can set the priority of rule execution for control. For example, in the START () function:

Eresye: add_rule (c_f_2 ,{? Module, x}, 10). The priority of rule X is 10. A rule with a higher priority is first activated for execution.

VII. eresye: retract (......) Important Role

The system stops running. You need to execute the statement PID in the rule! OK

In this example, after this is done, the program is not terminated normally, and an exception occurs when some rules continue to be activated. This problem is not caused by errors in writing this routine, but by the characteristics of eresye.

In this example, eresye: retract (engine, {start, PID}) is used to eliminate the first condition required for activation of each rule and solve the problem.

8. How to make practical changes

In my opinion, the positive reasoning method is not as good as reverse reasoning. The latter is built in Prolog, and its practicability, control, scalability, application field, and code scale are superior to the former.

In fact, the first Expert System Model I implemented on eresye is reverse reasoning. It is a simulation of the Prolog Inference Engine.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.