Search Engine
The search engine is the core inference mechanism in DS. It manages a knowledge base of rules and facts, and performs logical inference by matching rules with facts.
Overview
The search engine:
- Maintains a collection of rules and facts
- Iteratively applies rules to generate new facts
- Notifies you of each new inference via a callback
- Automatically prevents duplicate inferences
How It Works
The search engine uses a forward-chaining inference approach:
- When you call
execute(), the engine tries to match the first premise of each rule with existing facts
- When a match is found, variables in the rule are substituted and a new rule (with one fewer premise) is created
- If the new rule has no premises, it becomes a new fact
- The callback is invoked for each newly derived rule
- Duplicate rules are automatically filtered out
Creating a Search Engine
Parameters
- limit_size: Maximum size (in bytes) for each stored rule/fact (default: 1000). Rules or facts larger than this are rejected.
- buffer_size: Size of the internal buffer for intermediate operations (default: 10000). Increase this if you work with complex rules.
Adding Rules and Facts
Use the add() method to add rules and facts to the knowledge base.
Executing Search
The execute() method performs one round of inference. It matches all rules against all facts and generates new conclusions.
Callback Function
The callback receives each newly inferred rule and should return:
False (Python) / false (TypeScript/C++): Continue searching
True (Python) / true (TypeScript/C++): Stop searching
Searching for a Target
To search until a specific target is found:
Configuration Methods
Set Limit Size
Controls the maximum size for each stored rule/fact:
Set Buffer Size
Controls the internal buffer size for operations:
Reset
Clears all rules and facts:
- Buffer Size: Larger buffers allow more complex intermediate results but use more memory
- Limit Size: Restricts maximum rule/fact complexity - too small may reject valid rules
- Iterative Execution: Call
execute() in a loop to continue inference until convergence
- Early Termination: Return
true from callback to stop as soon as target is found
- Deduplication: The engine automatically deduplicates facts, avoiding redundant computation
See Also
- Terms - Building blocks for the search
- Rules - How rules are structured and matched