FactEngine is a straightforward fact storage and querying system that enables users to store facts and then query or match against them. This guide will walk you through the basic usage of the FactEngine.
- Navigate to your project directory in your terminal.
- Start an IEx session with your project using:
iex -S mix
To start the FactEngine, execute:
FactEngine.start()
To input facts into the engine, use:
FactEngine.input("fact_name(value1, value2)")
For example:
FactEngine.input("likes(alex, sam)")
FactEngine.input("likes(sam, sam)")
FactEngine.input("dislikes(sam, alex)")
FactEngine.input("is_a_cat(biscuit)")
FactEngine.input("is_a_cat(beetlejuice)")
You can query facts using variables (X, Y). For example, if you have inputted the fact likes(alex, sam), you can query:
FactEngine.query_facts("likes(X, sam)")
Expected output:
[["alex"], ["sam"]]
if you have inputted the fact likes(sam, sam), you can query:
FactEngine.query_facts("likes(Y, Y)")
Expected output:
[["sam"]]
you can also query the like facts likes(X, Y) Expected output:
FactEngine.query_facts("likes(X, Y)")
[[%{"X" => "alex", "Y" => "sam"}], [%{"X" => "sam", "Y" => "sam"}]]
if you have inputted the fact is_a_cat(x), you can query:
FactEngine.query_facts("is_a_cat(x)")
[["biscuit"], ["beetlejuice"]]
You can also process a .txt file that contains a list of inputs and queries. The format of the file should be:
INPUT fact_name1(value1, value2)
INPUT fact_name2(value1, value2)
...
QUERY your_query
...
For example, fact_file.txt might contain:
INPUT likes(alex, sam)
INPUT likes (sam, sam)
QUERY likes(X, sam)
To process the file, use an IEx session with your project using:
iex -S mix
FactEngine.process_file("path_to_your_file.txt")
The results of the queries will be printed to the console.
When you process a file or make a query in an IEx session, the results will be printed to the console. For file processing, the output for each query in the file will be displayed sequentially.
The expected output for fact_file.txt with data:
INPUT likes(alex, sam)
INPUT likes (sam, sam)
QUERY likes(X, sam)
Expected output:
Result of query likes (X, sam): [["alex"], ["sam"]]