Pyke logic programming (15): "rule repository" in the Knowledge Base"

Source: Internet
Author: User
Tags name database
Rule Repository

A rule repository is a set of rules. To create a rule repository, you can use your favorite text editor to compile the. krb rule file.

A rule repository can contain both forward and reverse inference rules.

When a rule repository is activated to assert new facts, the forward inference rules automatically run. It can be seen that forward inference rules are not directly used to determine whether specific facts are true or not.

Reverse inference rules are directly used to determine the authenticity of a specific rule. Therefore, if the name of a fact statement includes the knowledge base name (more specifically, the type name of the rule repository), it is the reverse inference rule in the rule repository.

Note

Pyke runs forward rules first, and then reverse rules.
Fact statements in a forward inference rule do not use the rule repository name as the name of the knowledge base because they disrupt the operation sequence of the two rules.

Reasons for coexistence of multiple rule Libraries

There are two reasons for coexistence of multiple rule repository (. krb) files:

    • Divides a large number of rules into units that are easy to manage manually.

      Therefore, Pyke is required to combine all rule libraries.

      For example, some rules control the automatic generation of SQL statements, and some rules control the generation of HTML documents. To facilitate management of these Rules, place them in different rule libraries.

    • To make your pythonProgram, Can be made between different rule repository, suitable for different needs.

      For example, some rules that control the automatic generation of SQL statements may be very dependent on the running database (such as MySQL, PostgreSQL, and Oracle ).
      In this case, you can ask Pyke to select a rule repository based on the database to be run. In the same time period, you only need one rule repository. Therefore, these rules repository are independent and mutually exclusive.

The implementation of the above requirements relies on the following three functions:

    • Rule repository category
    • Rule repository inheritance
    • Rule repository Activation
Rule repository category

A rule repository is composed of categories. Each type of rule repository can only have one rule repository activated.

To activate some rule libraries at the same time, they should be put into different rule repository categories, and these rule libraries are mutually exclusive. For example, you can add a rule repository related to MySQL, PostgreSQL, and Oracle to the same rule repository category.

Each rule repository category has a unique name. In the preceding example, you need two rule repository categories: Database generation and HTML document generation.

The rule repository category name is used as the rule repository name for fact statements in the rule. These rules are in a rule repository category, but you must reference another rule repository category. For example, in the rule library of class html_generation, You need to reference the rule in class database with the name database. insert.

Inheritance of rule repository

Similar rule libraries share relevant rules through inheritance of Rule libraries.

A rule repository inherits the rules of another rule repository. It is unidirectional inheritance. One-way inheritance can reach any depth. Both forward and reverse inference rules can be inherited.

This allows mutually exclusive rule libraries to share rules of higher-level rule libraries, thus avoiding copying them.

The following is the definition of a rule repository category:

    • A rule repository at the bottom of an inheritance relationship defines a unique rule repository category. Rules generated from this rule repository category are directly or indirectly inherited and belong to the same rule repository category.
    • The name of the rule repository category is the name of the bottommost rule repository in the inheritance relationship.

The database and html_generation In the example look like this:

Rule repository category

The underlying rule repository is called database. The rule repository derived from it is called MySQL, PostgreSQL, and Oracle. There is also an underlying rule repository named html_generation, which derives from Firefox and internet_explorer.

The two underlying rule libraries define two rule repository categories:

    • Database, including:

      • Database
      • MySQL
      • PostgreSQL
      • Oracle
    • Html_generation, including:
      • Html_generation
      • Firefox
      • Internet_explorer
Note

The. krb file of the rule repository can be stored anywhere in your Pyke source file directory. That is to say, the file directory does not have to match the inheritance relationship of the rule repository.

At any time, only one rule repository is activated in the rule repository category.

In a rule repository, if the name of the knowledge base is omitted in the fact statement of a reverse reasoning rule, the default name is the rule repository category name, not the rule repository name. For example, in PostgreSQL, the fact insert (...), database. insert (...); in Firefox, make_tag (...) html_generation.make_tag (...).

Important

No matter whether the rule repository category is referenced directly or indirectly, the reference always points to the active rule repository. It does not point to the underlying rule repository, nor implicitly uses the rule repository category. For example, in the derived rule library PostgreSQL, if the rule library oracle is activated, the fact statement insert (...) will end its reference to the insert rule in Oracle.

If the referenced rule belongs to an external rule repository category, you must directly reference the rule repository category name. For example, if you reference the insert rule of databases in the rule repository category html_generation, you must write database. insert (...).

In this way, all the rule repository categories that are active can be used.

Rule inheritance

In the Pyke rule repository, there is an important difference between the Inheritance Method of reverse reasoning rules and the method Inheritance Method in Python classes:

    • In a python derived class, if the method name is the same as its parent class, only the method in the derived class will be called.
    • In contrast. In a rule repository derived from Pyke, if a reverse inference rule is defined for a certain target and such a rule is also available in the higher-level rule repository, the derived rule will be extended to the upper-level rule, to prove this goal. The derived rule first tries to prove the target. If all of them fail, then tries the parent rule.

      If you do not need a higher-level rule for a specific target, the name of the target must be listed in the extension clause without at the beginning of the derived rule library.

Note

The forward inference rules in the higher-level rule repository must all be included in the derived rule repository. The without clause is only applicable to reverse inference rules.

Activate rule repository

Load the rule repository to the memory. You can only make it available, but it is not activated for running. To activate it, you must use your python program directly. It can activate different rule libraries according to different situations.

In addition, you can activate multiple rule libraries based on your reasoning needs. However, if a rule repository has been activated as a rule repository category, only its sub-databases can be activated. Because the sub-database inherits the activated parent database, new rules are obtained without reducing the number of rules. When a rule repository is activated or is running in a forward direction, it is not blocked by a later activated rule repository.

In our database example, your program will activate the underlying rule repository database to enable its forward inference rules. Determine which rule repository is activated based on the running database.

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.