# Rails Expert System

The article is devoted to the creation of an expert system. At the beginning of the article - a flowchart from a book from the list of references, then a description of the database and algorithm. Next comes the “certificate on how to do this project,” which describes the algorithm for creating this project. At the end of the article - a list of references. It also has a couple of screenshots.

The block diagram of the

database algorithm contains three tables - images, questions and outcomes. The first of them, the main one in “classification and identification,” contains a sign of successful recognition (the flag field) and the number of “object pairs, the answer” in the “training set” (field n). The second one contains this sample (the value field), as well as the names of “properties and attributes” (the name field) and the rule field used in the recognition algorithm. Finally, the third table contains possible answers (rain / no rain, etc.).

Action show

Algorithm works as follows. At the current iteration, the sum of the rule * value does not exceed the rule * n (since value has one of two values: 0 or 1). At the next iteration, the single rule value exceeds this amount, that is, rule = rule * n + 1.

The number of the inertia with the unit subtracted from it (the answer is unknown at the first iteration with a zero sum) is the answer number (for K. Neylor, the positive sum of the values ​​of the rules * value signified the first answer, the negative one - the second one).

Obviously, on the first iteration (zero values ​​rule by default) the sum of the values ​​will also be zero. At the second iteration, it will be in the range [1, n] (see the formula for the rule above), on the third - in the range [n + 1, (n + 1) * n]; on the fourth range will be: [(n + 1) * n + 1, ((n + 1) * n + 1) * n] and so on.

Thus, the right border of the range is calculated by the formula sum = (sum + 1) * n. When all possible answers are checked, reset the rule.

What it looks like in a web browser

## Help on how to do this project

Almost all project files can be created using the commands

rails generate scaffold Image name:string flag:boolean n:integer
rails generate scaffold Question name:string value:integer rule:integer image:references
rails generate scaffold Outcome name:string image:references

In the file db / migrate / yyyyMMddhhmmss_create_images.rb instead of the string t.boolean: flag we write t.boolean: flag, default: false. We also add null default values ​​for the images.n, questions.value, and questions.ru fields. And do rake db: migrate.

Next, in the config / routes.rb file, we will make the questions and outcomes with resources subordinate to the images resource. Also in the file app / models / image.rb we add has_many: questions and has_many: outcomes. It remains to change the paths in the controllers and views accordingly: replace the questions_path by image_questions_path (params [: image_id]), etc.

Create _question.html.erb and _outcome.html.erb partials to render them later in images / show.html.erb view. You can learn more about this technique by watching a video tour of Rails 5, which is on the main page of the official site of this web framework.

As a result of all these actions, the new.html.erb and edit.html.erb views for both subordinate resources are broken. Together with the single _form.html.erb partial, they get the same path for the action of this form, which is wrong. We fix.

Algorithm with recalculation of rule field values ​​(if necessary), see above. It starts when the web page displayed by the action of the ImageController controller is updated.

Finally, automatic testing. In connection with the change of routes, tests and stands (fixtures) also require appropriate changes.

## Literature

K. Naylor. How to build your own expert system - Energoatomizdat Publishing House, 1991